Hallo,
Ich baue grad eine Button Box für AssettoCorsaCompetitione PS5. Ich habe ein Arduino Micro Pro mit 4 Encodern und 20 Buttons. Den Code hab ich im Netz gefunden und für mich angepaßt. Ich kann jedem Encoder und Button einen Buchstaben zu weisen. Und da liegt das Problem: Ich brauche Tasten-Kombinationen (zB für die '5' brauche ich Shift+i)
In der Defention der Matrix am Anfang kann ich nur einen Wert eingeben. An der Stelle kann ich das also nicht einfügen!?
Ich habe in anderen Codes gesehen, daß case'1', case'2' defeniert werden können. An welcher Stelle kann ich so etwas in meine Code einbauen?
#include <Keypad.h>
#include <Keyboard.h>
#define ENABLE_PULLUPS
#define NUMROTARIES 4
#define NUMBUTTONS 20
#define NUMROWS 4
#define NUMCOLS 5
char buttons[NUMROWS][NUMCOLS] = {
{'1','2','3','4','5'},
{'6','7','8','9','0'},
{'a','b','c','d','e'},
{'f','g','h','i','j'},
};
struct rotariesdef {
byte pin1;
byte pin2;
int ccwchar;
int cwchar;
volatile unsigned char state;
};
rotariesdef rotaries[NUMROTARIES] {
{0,1,'n','m',0},
{2,3,'o','t',p},
{4,5,'q','r',0},
{6,7,'s','t',0},
};
#define DIR_CCW 0x10
#define DIR_CW 0x20
#define R_START 0x0
#define R_CW_FINAL 0x1
#define R_CW_BEGIN 0x2
#define R_CW_NEXT 0x3
#define R_CCW_BEGIN 0x4
#define R_CCW_FINAL 0x5
#define R_CCW_NEXT 0x6
const unsigned char ttable[7][4] = {
// R_START
{R_START, R_CW_BEGIN, R_CCW_BEGIN, R_START},
// R_CW_FINAL
{R_CW_NEXT, R_START, R_CW_FINAL, R_START | DIR_CW},
// R_CW_BEGIN
{R_CW_NEXT, R_CW_BEGIN, R_START, R_START},
// R_CW_NEXT
{R_CW_NEXT, R_CW_BEGIN, R_CW_FINAL, R_START},
// R_CCW_BEGIN
{R_CCW_NEXT, R_START, R_CCW_BEGIN, R_START},
// R_CCW_FINAL
{R_CCW_NEXT, R_CCW_FINAL, R_START, R_START | DIR_CCW},
// R_CCW_NEXT
{R_CCW_NEXT, R_CCW_FINAL, R_CCW_BEGIN, R_START},
};
byte rowPins[NUMROWS] = {21,20,19,18}; //connect to the row pinouts of the keypad
byte colPins[NUMCOLS] = {15,14,16,10,9}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad buttbx = Keypad( makeKeymap(buttons), rowPins, colPins, NUMROWS, NUMCOLS);
void setup() {
Keyboard.begin();
rotary_init();
}
void loop() {
CheckAllEncoders();
CheckAllButtons();
}
void CheckAllButtons(void) {
char key = buttbx.getKey();
if (key != NO_KEY) {
Keyboard.press(key);
delay(150);
Keyboard.releaseAll();
}
}
/* Call this once in setup(). */
void rotary_init() {
for (int i=0;i<NUMROTARIES;i++) {
pinMode(rotaries[i].pin1, INPUT);
pinMode(rotaries[i].pin2, INPUT);
#ifdef ENABLE_PULLUPS
digitalWrite(rotaries[i].pin1, HIGH);
digitalWrite(rotaries[i].pin2, HIGH);
#endif
}
}
/* Read input pins and process for events. Call this either from a
* loop or an interrupt (eg pin change or timer).
*
* Returns 0 on no event, otherwise 0x80 or 0x40 depending on the direction.
*/
unsigned char rotary_process(int _i) {
unsigned char pinstate = (digitalRead(rotaries[_i].pin2) << 1) | digitalRead(rotaries[_i].pin1);
rotaries[_i].state = ttable[rotaries[_i].state & 0xf][pinstate];
return (rotaries[_i].state & 0x30);
}
void CheckAllEncoders(void) {
for (int i=0;i<NUMROTARIES;i++) {
unsigned char result = rotary_process(i);
if (result) {
Keyboard.write(result == DIR_CCW ? rotaries[i].ccwchar : rotaries[i].cwchar );
}
}
}