Dann hast jetzt was zu tun.
Alter. Der Code war eine reine Wüste.
So Schei... bekommt den vielleicht dieses Chatgdingens hin...
Ich hab mir was gedacht, obs passt, weiss ich nicht.
Du wirst eine Menge neuer Dinge lernen müssen
Und ich kann Dir sagen, das der auf 1/3 runtergekürzt ist und auch genauso wenig Speicher braucht.
Auf Anhieb wird der nicht laufen, aber den musst Du dann debuggen mit vielen zusätzlichen serielen Ausgaben versehen.
Bedenke: Ich habe keine Hardware - ich kann Dir nur sagen, das der fehlerfrei kompiliert.
Na denne...
#include <Arduino.h>
#include <SPI.h>
#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_SPI.h"
#include "Adafruit_BluefruitLE_UART.h"
#ifdef SOFTWARE_SERIAL_AVAILABLE
#include <SoftwareSerial.h>
#endif
// #if USB_HID_KEYS
// #include <usb_hid_keys.h>
// #endif
/**
Modifier masks - used for the first byte in the HID report.
NOTE: The second byte in the report is reserved, 0x00
*/
#define KEY_MOD_LCTRL 0x01
#define KEY_MOD_LSHIFT 0x02
#define KEY_MOD_LALT 0x04
#define KEY_MOD_LMETA 0x08
#define KEY_MOD_RCTRL 0x10
#define KEY_MOD_RSHIFT 0x20
#define KEY_MOD_RALT 0x40
#define KEY_MOD_RMETA 0x80
Adafruit_BluefruitLE_SPI ble(8, 7, 4);
//Factory reset to 0 for no reset, or 1 to factory reset when scketch starts
constexpr byte FACTORYRESET_ENABLE {0};
constexpr byte BUFSIZE {128}; // Size of the read buffer for incoming data
//APP LED SEQUENCE OF COLOUR. SWOP THE VALUE OF YOUR COLCOURS ARE WRONG WAY AROUND
constexpr byte ledGREEN {A1};
constexpr byte ledRED {A0};
//PUSH BUTTONS STATE FOR NORMALLY OPEN AND NORMALLY CLOSED
//FOR Normally open buttons keen the state LOW.
//FOR Normally closed buttons change the state to HIGH
constexpr byte pushBtn[] {12, 11, 10, 9, 5};
constexpr byte pushBtnSize {sizeof(pushBtn) / sizeof(pushBtn[0])};
//TOGGLE SWITCH BUTTONS
constexpr byte toggleBtn[] {A2, A3};
constexpr byte toggleBtnSize {sizeof(toggleBtn) / sizeof(toggleBtn[0])};
constexpr byte joy[] {A4, A5};
constexpr byte joySize {sizeof(joy) / sizeof(joy[0])};
constexpr uint16_t deadZone {100};
// Joystick: variables for calibration
uint16_t neutralPos[joySize];
// Joystick: analog readings for positions (l,r,u,d)
uint16_t joyPos[joySize];
//APP SWITCH BUTTON
constexpr byte modeSelectPin {6}; // Mode-Switch-Button on PIN-6
byte modeSelectState = 0; // Testing to set Mode-Select via "Taster-Knopf"
//int softApp; //Keep softapp default 0 when starting up
byte softApp ; //preset softapp to app 3 when starting up
constexpr uint32_t SHORT_PRESS_TIME {1000}; // milliseconds
constexpr uint32_t debounce = 40;
// Variables will change:
bool mb_lastState = LOW; // the previous state from the input pin
uint32_t mb_pressedTime;
// NEU!
char startCode[] = {"AT+BLEKEYBOARDCODE="};
constexpr char endCode[] {"AT+BLEKEYBOARDCODE=00-00"};
char *myCode;;
//
// COUNTER FOR LOCUS BUTTON ITERATO7
byte locusIter;
// For Button-6
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change:
const long interval = 250; // interval at which an App can be selected (milliseconds)
int mouseClickFlag = 0;
//BleMouse bleMouse;
//BUTTON & TOGGLE FIXED SCROLL SPEED
const int buttonWait = 250;
const int toggleWait = 150;
const int arrowButtonWait = 250;
void setup()
{
//Serial.begin(115200);
Serial.begin(9600);
Serial.println(F("Starting up ..."));
//Push buttons
for (byte b = 0; b < pushBtnSize; b++)
{ pinMode(pushBtn[b], INPUT_PULLUP); }
//Toggle switch buttons
for (byte b = 0; b < toggleBtnSize; b++)
{ pinMode(toggleBtn[b], INPUT_PULLUP); } // For MouseClick
//Mode LEDs
pinMode(ledGREEN, OUTPUT);
pinMode(ledRED, OUTPUT);
//App rocker switch
pinMode(modeSelectPin, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
bleINIT();
// Joystick: set neutral positions
setJoyNeutral();
//ble Mouse Starter
//bleMouse.begin();
}
//
void loop()
{
// Serial.print("SoftApp # at startup is: ");
// Serial.println(softApp);
//Start of select softapp 1
if (!digitalRead(modeSelectPin)) // Taste geddrückt?
{
digitalWrite(ledRED, HIGH); //
digitalWrite(ledGREEN, LOW);
//Serial.print(" modeSelect = LOW :: "); Serial.println(modeSelect);
for (byte b = 0; b < pushBtnSize; b++) // frage die tasten ab
{
if (!digitalRead(pushBtn[b])) // Taste gedrückt?
{
softApp = b; // Tastendruck merken
setApp(); // Voreinstellung setzen
}
}
}
else // Taste nicht gedrückt
{
digitalWrite(ledRED, LOW);
digitalWrite(ledGREEN, HIGH);
//Serial.print(" modeSelect = HIGH :: "); Serial.println(modeSelect);
//PUSH BUTTONS
getToggleBtn();
// JOYSTICK MOVEMENTS
getJoyPosition();
controlJoyPosition();
checkUpBtn();
if (softApp == 0)
{
if (locusIter >= 10)
{
locusIter = 0;
}
}
// ******************************************************** START of GLOBAL PART *****************************************************
// START of global button6
unsigned long currentMillis = millis();
if (!digitalRead(toggleBtn[0])) // Side-Button to switch the App and Mode
{
Serial.print(F("Pressed: Button global 6"));
// check to see if it's time to blink the LED; that is, if the difference
// between the current time and last time you blinked the LED is bigger than
// the interval at which you want to blink the LED.
// INFO: LOW = App-Select-Mode / HIGH = App-Normal-Mode !!!
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
modeSelectState = !modeSelectState;
Serial.print(F(" :: modeSelectState = "));
Serial.print(modeSelectState);
Serial.print(F(" :: currentMillis = "));
Serial.print(currentMillis);
}
Serial.println();
digitalWrite(ledGREEN, LOW);
delay(buttonWait);
digitalWrite(ledGREEN, HIGH);
}
else if ((modeSelectState == HIGH) &&
(currentMillis - previousMillis >= 50) &&
(currentMillis - previousMillis <= interval + 1500)) // If normal-app-mode and WITH-IN +2 sec after press, do....
{
Serial.println(currentMillis);
delay(toggleWait);
for (byte b = 0; b < pushBtnSize; b++)
{
if (digitalRead(pushBtn[b]))
{
Serial.print(F("App-Set Button-"));
Serial.print(b);
Serial.println(F(" pressed"));
softApp = b;
setApp();
}
}
}
if ((modeSelectState == HIGH) &&
(currentMillis - previousMillis >= interval + 1500)) // If normal-app-mode set and modeSelectState=LOW and Time-Out for fake App-Select-Mode, then set modeSelectState back to HIGH
{
modeSelectState = LOW;
Serial.println(modeSelectState);
}
// END of global button6
// START :: JOY STICK CONTROLS
// if (digitalRead(button7) == button7state) {
// ble.sendCommandCheckOK(F("AT+BleHidMouseButton=L"));
// delay(buttonWait);
// ble.sendCommandCheckOK(F("AT+BleHidMouseButton=0"));
// Serial.print("Mouse-Click Global-7 : Pressed ");
// Serial.print(" :mouseClickFlag: ");
// Serial.println(mouseClickFlag);
// digitalWrite(ledGREEN, LOW);
// delay(arrowButtonWait);
// digitalWrite(ledGREEN, HIGH);
// }
} //End of HIGH state button loo
} //End of code
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////// Functions ////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setApp()
{
ble.print("AT+BleKeyboard=SoftApp ");
ble.println(softApp + 1);
Serial.print(F("While in Mode Select you have selected softApp "));
Serial.println(softApp + 1);
digitalWrite(ledRED, LOW);
// Joystick: set neutral positions
setJoyNeutral();
for (byte b = 1; b <= softApp; b++)
{
digitalWrite(ledGREEN, !digitalRead(ledGREEN));
delay(300);
}
digitalWrite(ledRED, HIGH);
// *INDENT-OFF*
switch (softApp)
{
case 0:
// Send LOCUS Keystrokes to start Locus using KeyMapper-App
//ble.sendCommandCheckOK("AT+BLEKEYBOARDCODE=00-00-0F-12-06-18-16"); // L+O+C+U+S Keycodes
strcpy(myCode,"00-00-0F-12-06-18-16");
ble.sendCommandCheckOK(strcat(startCode,myCode)); // L+O+C+U+S Keycodes
ble.sendCommandCheckOK(endCode);
delay(arrowButtonWait);
strcpy(myCode,"00-00-29-00-00-00-00");
ble.sendCommandCheckOK(strcat(startCode,myCode)); // 0x29 Keyboard ESCAPE
break;
// NOTE: Sending only DM2, because KeyMapper Keypress at same time cannot recognize 2x Ds!
case 1: strcpy(myCode,"00-00-07-10-1F-00-00"); ble.sendCommandCheckOK(strcat(startCode,myCode)); break;// 0x07 Keyboard d and D, 0x10 Keyboard m and M, 0x1F Keyboard 2 and @
case 2: strcpy(myCode,"00-00-0A-10-04-13-16"); ble.sendCommandCheckOK(strcat(startCode,myCode)); break;// 0x0A Keyboard g and G, 0x10 Keyboard m and M, 0x04 Keyboard a and A, 0x13 Keyboard p and P, 0x16 Keyboard s and S
case 3: strcpy(myCode,"00-00-13-0C-16-17-08"); ble.sendCommandCheckOK(strcat(startCode,myCode)); break;// 0x13 Keyboard p and P, <0x0C Keyboard i and I, 0x16 Keyboard s and S, 0x17 Keyboard t and T, 0x08 Keyboard e and E
case 4: strcpy(myCode,"00-00-10-18-16-0C-06"); ble.sendCommandCheckOK(strcat(startCode,myCode)); break;// 0x10 Keyboard m and M, 0x18 Keyboard u and U, 0x16 Keyboard s and S, 0x0C Keyboard i and I, 0x06 Keyboard c and C
ble.sendCommandCheckOK(endCode);
}
// *INDENT-ON*
//ble.sendCommandCheckOK("AT+BLEKEYBOARDCODE=00-00");
ble.sendCommandCheckOK(endCode);
digitalWrite(ledGREEN, LOW);
delay(arrowButtonWait);
digitalWrite(ledGREEN, HIGH);
}
//
void checkUpBtn()
{
for (byte btn = 0; btn < pushBtnSize; btn++)
{
if (!digitalRead(pushBtn[btn]))
{
digitalWrite(ledGREEN, LOW);
delay(buttonWait);
digitalWrite(ledGREEN, HIGH);
Serial.print(F("Button "));
Serial.print(softApp);
Serial.print('-');
Serial.println(btn);
switch (softApp)
{
// *INDENT-OFF* //
case 0:
switch (btn)
{
case 0: ble.sendCommandCheckOK("AT+BleHidControlKey=VOLUME+"); break;
case 1: ble.sendCommandCheckOK("AT+BleHidControlKey=VOLUME-"); break;
case 2: strcpy(myCode,"00-00-15-00-00-00-00");ble.sendCommandCheckOK(strcat(startCode,myCode)); break;
case 3: strcpy(myCode,"00-00-06-00-00-00-00");ble.sendCommandCheckOK(strcat(startCode,myCode)); break;
case 4: strcpy(myCode,"00-00-07-00-00-00-00");ble.sendCommandCheckOK(strcat(startCode,myCode)); break;
}
if (btn > 1)
//{ ble.sendCommandCheckOK("AT+BLEKEYBOARDCODE=00-00"); }
ble.sendCommandCheckOK(endCode);
break;
case 1:
switch (btn)
{
case 0: ble.sendCommandCheckOK("AT+BleHidControlKey=VOLUME+"); break;
case 1: ble.sendCommandCheckOK("AT+BleHidControlKey=VOLUME-"); break;
case 2: strcpy(myCode,"00-00-15-00-00-00-00");ble.sendCommandCheckOK(strcat(startCode,myCode)); break; //0x15 Keyboard r and R --- should be 0x11 Keyboard n and N, but somehow not working.
case 3: strcpy(myCode,"00-00-06-00-00-00-00");ble.sendCommandCheckOK(strcat(startCode,myCode)); break; //0x06 Keyboard c and C
case 4: strcpy(myCode,"00-00-07-00-00-00-00");ble.sendCommandCheckOK(strcat(startCode,myCode)); break; //0x07 Keyboard d and D
}
if (btn > 1)
//{ ble.sendCommandCheckOK("AT+BLEKEYBOARDCODE=00-00"); }
ble.sendCommandCheckOK(endCode);
break;
case 2:
switch (btn)
{
case 0: strcpy(myCode,"00-00-1A-00-00-00-00");ble.sendCommandCheckOK(strcat(startCode,myCode)); break; //0x1A Keyboard w and W
case 1: strcpy(myCode,"00-00-1B-00-00-00-00");ble.sendCommandCheckOK(strcat(startCode,myCode)); break; //0x1B Keyboard x and X
case 2: strcpy(myCode,"00-00-06-00-00-00-00");ble.sendCommandCheckOK(strcat(startCode,myCode)); break; //0x06 c and C for CENTER-MAP
case 3: strcpy(myCode,"00-00-07-00-00-00-00");ble.sendCommandCheckOK(strcat(startCode,myCode)); break; //0x07 Keyboard d and D
case 4: strcpy(myCode,"00-00-14-00-00-00-00");ble.sendCommandCheckOK(strcat(startCode,myCode)); break; //0x14 Keyboard q and Q
}
//ble.sendCommandCheckOK("AT+BLEKEYBOARDCODE=00-00");
ble.sendCommandCheckOK(endCode);
break;
case 3:
switch (btn)
{
case 0: strcpy(myCode,"00-00-1A-00-00-00-00");ble.sendCommandCheckOK(strcat(startCode,myCode)); break; //0x1A Keyboard w and W
case 1: strcpy(myCode,"00-00-1B-00-00-00-00");ble.sendCommandCheckOK(strcat(startCode,myCode)); break; //0x1B Keyboard x and X
case 2: strcpy(myCode,"00-00-04-00-00-00-00");ble.sendCommandCheckOK(strcat(startCode,myCode)); break; //0x04 Keyboard a and A
case 3: strcpy(myCode,"00-00-07-00-00-00-00");ble.sendCommandCheckOK(strcat(startCode,myCode)); break; //0x07 Keyboard d and D
case 4: strcpy(myCode,"00-00-14-00-00-00-00");ble.sendCommandCheckOK(strcat(startCode,myCode)); break; //0x14 Keyboard q and Q
}
//ble.sendCommandCheckOK("AT+BLEKEYBOARDCODE=00-00");
ble.sendCommandCheckOK(endCode);
break;
case 4:
switch (btn)
{
case 0: ble.sendCommandCheckOK("AT+BleHidControlKey=VOLUME+"); break;
case 1: ble.sendCommandCheckOK("AT+BleHidControlKey=VOLUME-"); break;
case 2: ble.sendCommandCheckOK("AT+BleHidControlKey=MEDIANEXT"); break; // Media Next
case 3: ble.sendCommandCheckOK("AT+BleHidControlKey=MEDIAPREVIOUS"); break;// Media Previous
case 4: ble.sendCommandCheckOK("AT+BleHidControlKey=PLAYPAUSE"); break; // MUTE
}
break;
// *INDENT-ON* //
}
}
}
}
//
void getToggleBtn()
{
//read the state of the switch/button:
uint32_t myMillis = millis();
if (!digitalRead(toggleBtn[1])) // button is pressed
{
if (mb_lastState == HIGH)
{
mb_pressedTime = myMillis;
mb_lastState = LOW;
}
}
else if (myMillis - mb_pressedTime > debounce) // button is releasedW)
{
mb_lastState = LOW;
unsigned long mb_pressDuration = myMillis - mb_pressedTime;
if (mb_pressDuration < SHORT_PRESS_TIME)
{
Serial.println(F("A short press is detected"));
Serial.println(mb_pressDuration);
}
// else if (mb_releasedTime - mb_pressedTime > SHORT_PRESS_TIME) {
// Serial.println("The other case");
// }
}
}
//
uint16_t joyRead(const byte analogPin)
{
constexpr byte anzahl {8};
uint16_t reading[anzahl + 2] = {0};
for (byte b = 0; b < anzahl; b++)
{
reading[b] = analogRead(analogPin);
}
uint16_t minValue = 1024;
uint16_t maxValue = 0;
uint32_t myValue = 0;
for (byte b = 0; b < anzahl; b++)
{
if (reading[b] < minValue)
{ minValue = reading[b]; }
if (reading[b] > maxValue)
{ maxValue = reading[b]; }
myValue += reading[b];
}
myValue -= minValue;
myValue -= maxValue;
return myValue / (anzahl - 2);
}
//
void setJoyNeutral()
{
for (byte b = 0; b < joySize; b++)
{ neutralPos[b] = joyRead(b); }
}
//
void getJoyPosition()
{
for (byte b = 0; b < joySize; b++)
{ joyPos[b] = joyRead(b); }
}
//
void controlJoyPosition()
{
constexpr char aCode[2][21]
{{"00-00-4f-00-00-00-00"}, {"00-00-52-00-00-00-00"}};
constexpr char bCode[2][21]
{{"00-00-50-00-00-00-00"}, {"00-00-51-00-00-00-00"}};
for (byte b = 0; b < joySize; b++)
{
Serial.print(joyPos[0]);
if (joyPos[b] >= neutralPos[b] + deadZone)
{
Serial.println(b == 0 ? (F("-RIGHT-")) : (F(" -UP- ")));
ble.sendCommandCheckOK(strcat(startCode, aCode[b]));
ble.sendCommandCheckOK(endCode);
}
else if (joyPos[b] <= neutralPos[b] - deadZone)
{
Serial.println((b == 0) ? (F("-LEFT-")) : (F(" -DOWN- ")));
ble.sendCommandCheckOK(strcat(startCode, bCode[b]));
ble.sendCommandCheckOK(endCode);
}
}
delay(toggleWait);
// END :: JOY STICK CONTROLS
}
//
void bleINIT()
{
/////////////////////////////////////////
////////SETUP BLUETOOTH
/////////////////////////////////////////
//Factory reset if enabled above
if (FACTORYRESET_ENABLE)
{
/* Perform a factory reset to make sure everything is in a known state */
Serial.println(F("Performing a factory reset: "));
if (!ble.factoryReset())
{ Serial.println(F("Couldn't factory reset")); }
}
//Setup BT
ble.begin(false);
//ble.sendCommandCheckOK("AT+GAPDEVNAME=Rampage_Controller_1.7.3");
ble.sendCommandCheckOK("AT+GAPDEVNAME=myController");
ble.sendCommandCheckOK("AT+BLEHIDEN=On");
ble.sendCommandCheckOK("AT+BleKeyboardEn=On");
digitalWrite(LED_BUILTIN, LOW);
//Double reset to see if it improves auto connect on Androidn devices
ble.reset();
for (byte b = 0; b < 20; b++)
{
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
delay(100);
}
ble.reset();
digitalWrite(LED_BUILTIN, HIGH);
Serial.println(F("Bluetooth Started OK!"));
/* Disable command echo from Bluefruit */
ble.echo(false);
/* Print Bluefruit information */
Serial.println(F("Requesting Bluefruit info:"));
ble.info();
}
//
// ble.sendCommandCheckOK("AT+BLEKEYBOARDCODE=00-00-52-00-00-00-00"); // UP
// ble.sendCommandCheckOK("AT+BLEKEYBOARDCODE=00-00-13-00-00-00-00"); // 0x13 Keyboard p and P FOR MAP-ZOOM-IN
// ble.sendCommandCheckOK("AT+BLEKEYBOARDCODE=00-00-51-00-00-00-00"); // Down
// ble.sendCommandCheckOK("AT+BLEKEYBOARDCODE=00-00-10-00-00-00-00"); // 0x10 Keyboard m and M FOR MAP-ZOOM-OUT
// ble.sendCommandCheckOK("AT+BLEKEYBOARDCODE=00-00-04-00-00-00-00"); // 0x04 Keyboard a and A
// ble.sendCommandCheckOK("AT+BLEKEYBOARDCODE=00-00-50-00-00-00-00"); // Left
// ble.sendCommandCheckOK("AT+BLEKEYBOARDCODE=00-00-11-00-00-00-00"); // 0x11 Keyboard n and N
// ble.sendCommandCheckOK("AT+BLEKEYBOARDCODE=00-00-4F-00-00-00-00"); // Right
// ble.sendCommandCheckOK("AT+BLEKEYBOARDCODE=00-00-16-00-00-00-00"); // 0x16 Keyboard s and S
// ble.sendCommandCheckOK("AT+BLEKEYBOARDCODE=00-00-81-00-00-00-00"); // 0x81 Keyboard Volume Down
// ble.sendCommandCheckOK("AT+BLEKEYBOARDCODE=00-00-80-00-00-00-00"); // 0x80 Keyboard Volume Up
// ble.sendCommandCheckOK("AT+BLEKEYBOARDCODE=03-00-04-00-00-00-00"); // Left-ALT + Keyboard a and A
// ble.sendCommandCheckOK("AT+BLEKEYBOARDCODE=00-00-07-10-07-1F-00"); // 0x07 Keyboard d and D, 0x10 Keyboard m and M, 0x07 Keyboard d and D, 0x1F Keyboard 2 and @
// 0x7F Keyboard Mute
// 0x80 Keyboard Volume Up
// 0x81 Keyboard Volume Down
Deine BlinkenDingensens machen wir später - erstmal muss das mit den Keyboardcodes und dem BLE funktionieren.