I have a keyboard emulator which also uses a combination of momentary switches and rotary encoders all works fine. But I cannot figure out how to add the following keys: keyboard arrow keys, Escape key and Enter key. When I try to change the char keys in the first section, say I remove '5' or 'g' and want up arrow nothing happens.
Is there anyone that can help with this code
#include <Keypad.h>
#include <Keyboard.h>
#define ENABLE_PULLUPS
#define NUMROTARIES 5
#define NUMBUTTONS 24
#define NUMROWS 5
#define NUMCOLS 5
char keys[NUMROWS][NUMCOLS] = {
{'5','2','v','4','5'},
{'6','7','8','9','0'},
{'p','b','u','p','e'},
{'t','g','c','q','j'},
{'f','a','m','s','o'},
};
struct rotarykeysdef {
byte pin1;
byte pin2;
char ccwchar;
char cwchar;
volatile unsigned char state;
};
rotarykeysdef rotarykeys[NUMROTARIES] {
{0,1,'i','d',0},
{2,3,'r','n',0},
{4,5,'h','l',0},
{6,7,'3','1',0},
{8,9,'x','z',0},
};
#define DIR_CCW 0x10
#define DIR_CW 0x20
#define R_START 0x0
#ifdef HALF_STEP
#define R_CCW_BEGIN 0x1
#define R_CW_BEGIN 0x2
#define R_START_M 0x3
#define R_CW_BEGIN_M 0x4
#define R_CCW_BEGIN_M 0x5
const unsigned char ttable[6][4] = {
// R_START (00)
{R_START_M, R_CW_BEGIN, R_CCW_BEGIN, R_START},
// R_CCW_BEGIN
{R_START_M | DIR_CCW, R_START, R_CCW_BEGIN, R_START},
// R_CW_BEGIN
{R_START_M | DIR_CW, R_CW_BEGIN, R_START, R_START},
// R_START_M (11)
{R_START_M, R_CCW_BEGIN_M, R_CW_BEGIN_M, R_START},
// R_CW_BEGIN_M
{R_START_M, R_START_M, R_CW_BEGIN_M, R_START | DIR_CW},
// R_CCW_BEGIN_M
{R_START_M, R_CCW_BEGIN_M, R_START_M, R_START | DIR_CCW},
};
#else
#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},
};
#endif
byte rowPins[NUMROWS] = {21,20,19,18,15};
byte colPins[NUMCOLS] = {14,16,10};
Keypad buttbx = Keypad( makeKeymap(keys), rowPins, colPins, NUMROWS, NUMCOLS);
void setup() {
rotary_init();
}
void loop() {
CheckAllEncoders();
CheckAllButtons();
}
void CheckAllButtons(void) {
if (buttbx.getKeys())
{
for (int i=0; i<LIST_MAX; i++)
{
if ( buttbx.key[i].stateChanged )
{
switch (buttbx.key[i].kstate) {
case PRESSED:
Keyboard.press(buttbx.key[i].kchar);
break;
case HOLD:
break;
case RELEASED:
Keyboard.release(buttbx.key[i].kchar);
break;
case IDLE:
break;
}
}
}
}
}
void rotary_init() {
for (int i=0;i<NUMROTARIES;i++) {
pinMode(rotarykeys[i].pin1, INPUT);
pinMode(rotarykeys[i].pin2, INPUT);
#ifdef ENABLE_PULLUPS
digitalWrite(rotarykeys[i].pin1, HIGH);
digitalWrite(rotarykeys[i].pin2, HIGH);
#endif
}
}
unsigned char rotary_process(int _i) {
unsigned char pinstate = (digitalRead(rotarykeys[_i].pin2) << 1) | digitalRead(rotarykeys[_i].pin1);
rotarykeys[_i].state = ttable[rotarykeys[_i].state & 0xf][pinstate];
return (rotarykeys[_i].state & 0x30);
}
void CheckAllEncoders(void) {
for (int i=0;i<NUMROTARIES;i++) {
unsigned char result = rotary_process(i);
if (result == DIR_CCW) {
Keyboard.press(rotarykeys[i].ccwchar); delay(50); Keyboard.release(rotarykeys[i].ccwchar);
};
if (result == DIR_CW) {
Keyboard.press(rotarykeys[i].cwchar); delay(50); Keyboard.release(rotarykeys[i].cwchar);
};
}
}