Skip to content

Commit

Permalink
Hopefully fixed #259 by fixing indiscriminate key detection behavior.
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobly0 committed Jun 30, 2018
1 parent 304c241 commit 6cbb192
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions core/keypad.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@ void keypad_intrpt_check() {
intrpt_set(INT_KEYPAD, (keypad.status & keypad.enable) | (keypad.gpioStatus & keypad.gpioEnable));
}

static void keypad_any_check(void) {
uint8_t any = 0;
unsigned int row;
if (keypad.mode != 1) {
return;
}
for (row = 0; row < keypad.rows && row < sizeof(keypad.data) / sizeof(keypad.data[0]); row++) {
any |= keypad.keyMap[row] | keypad.delay[row];
keypad.delay[row] = 0;
}
any &= (1 << keypad.cols) - 1;
for (row = 0; row < keypad.rows && row < sizeof(keypad.data) / sizeof(keypad.data[0]); row++) {
keypad.data[row] = any;
}
if (any) {
keypad.status |= 4;
keypad_intrpt_check();
}
}

void EMSCRIPTEN_KEEPALIVE keypad_key_event(unsigned int row, unsigned int col, bool press) {
if (row == 2 && col == 0) {
intrpt_set(INT_ON, press);
Expand All @@ -29,14 +49,11 @@ void EMSCRIPTEN_KEEPALIVE keypad_key_event(unsigned int row, unsigned int col, b
if (press) {
keypad.keyMap[row] |= 1 << col;
keypad.delay[row] |= 1 << col;
if (keypad.mode == 1) {
keypad.status |= 4;
keypad_intrpt_check();
}
} else {
keypad.keyMap[row] &= ~(1 << col);
keypad_intrpt_check();
}
keypad_any_check();
}
}

Expand Down Expand Up @@ -82,9 +99,8 @@ static void keypad_scan_event(enum sched_item_id id) {
uint8_t row = keypad.row++;
if (row < keypad.rows && row < sizeof(keypad.data) / sizeof(keypad.data[0])) {
/* scan each data row */
uint16_t data = keypad.keyMap[row] | keypad.delay[row];
uint16_t data = (keypad.keyMap[row] | keypad.delay[row]) & ((1 << keypad.cols) - 1);
keypad.delay[row] = 0;
data &= (1 << keypad.cols) - 1;

/* if mode 3 or 2, generate data change interrupt */
if (keypad.data[row] != data) {
Expand All @@ -107,42 +123,27 @@ static void keypad_scan_event(enum sched_item_id id) {
keypad_intrpt_check();
}

static bool keypad_any_key_pressed(void) {
unsigned int row;
for (row = 0; row < keypad.rows && row < sizeof(keypad.data) / sizeof(keypad.data[0]); row++) {
if ((keypad.keyMap[row] | keypad.delay[row]) & ((1 << keypad.cols) - 1)) {
return true;
}
}
return false;
}

static void keypad_write(const uint16_t pio, const uint8_t byte, bool poke) {
uint16_t index = (pio >> 2) & 0x7F;
uint8_t bit_offset = (pio & 3) << 3;

switch (index) {
case 0x00:
write8(keypad.control,bit_offset,byte);
write8(keypad.control, bit_offset, byte);
if (keypad.mode & 2) {
keypad.row = 0;
sched_set(SCHED_KEYPAD, keypad.rowWait);
} else {
sched_clear(SCHED_KEYPAD);
if (keypad.mode == 1 && keypad_any_key_pressed()) {
keypad.status |= 4;
keypad_intrpt_check();
}
keypad_any_check();
}
break;
case 0x01:
write8(keypad.size, bit_offset, byte);
break;
case 0x02:
write8(keypad.status, bit_offset, keypad.status >> bit_offset & ~byte);
if (keypad.mode == 1 && keypad_any_key_pressed()) {
keypad.status |= 4;
}
keypad_any_check();
keypad_intrpt_check();
break;
case 0x03:
Expand Down

0 comments on commit 6cbb192

Please sign in to comment.