bugfix for keys

- initialize count, so that there is no initial keypress at powerup
- use int8_t instead of uint32_t to pass new key, since only 1 new key
used
This commit is contained in:
Jeff Wang 2019-04-01 01:55:47 -04:00
parent 8aec2ae134
commit f5d8d601a2
3 changed files with 212 additions and 198 deletions

View File

@ -9,7 +9,15 @@
#include "utils.h" #include "utils.h"
#include "key.h" #include "key.h"
#ifndef DESKTOP #ifdef DESKTOP
#include <stdio.h>
const char state_names[4][32] = {
"STEADY_LOW" ,
"TRANS_LOW_HIGH",
"STEADY_HIGH" ,
"TRANS_HIGH_LOW"
};
#else
#include "stc15.h" #include "stc15.h"
#endif #endif
@ -17,7 +25,7 @@
#define M_COLS 4 #define M_COLS 4
uint8_t Keys[TOTAL_ROWS]; //only bottom nibbles get set uint8_t Keys[TOTAL_ROWS]; //only bottom nibbles get set
static uint32_t new_keys_pressed; //bottom 20 bits get set int8_t NewKeyPressed; //row*M_COLS + col, see KEY_MAP[] in main.c
static uint8_t last_state[TOTAL_ROWS]; //all used, 2 bits per key static uint8_t last_state[TOTAL_ROWS]; //all used, 2 bits per key
static int8_t last_count[TOTAL_ROWS][M_COLS]; //all used static int8_t last_count[TOTAL_ROWS][M_COLS]; //all used
static uint8_t unexpected_count; //count of unexpected transitions static uint8_t unexpected_count; //count of unexpected transitions
@ -35,7 +43,14 @@ static const int8_t THRESH_TRANS = 2;
#ifdef DESKTOP #ifdef DESKTOP
//test functions //test functions
void KeyInit(void){} void KeyInit(void){
//initialize counts
for (uint8_t i = 0; i < TOTAL_ROWS; i++){
for (uint8_t j = 0; j < M_COLS; j++){
last_count[i][j] = COUNT_LIM_LOW;
}
}
}
static void raw_scan(void){ static void raw_scan(void){
static uint8_t i = 0; static uint8_t i = 0;
@ -49,7 +64,7 @@ static void raw_scan(void){
0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,0,0,0,0,0,0
}; };
keys[1] = data[i]; Keys[1] = data[i];
i++; i++;
if (i >= (sizeof(data)/sizeof(data[0]))){ if (i >= (sizeof(data)/sizeof(data[0]))){
i = 0; i = 0;
@ -57,6 +72,8 @@ static void raw_scan(void){
} }
#else #else
void KeyInit(void){ void KeyInit(void){
uint8_t i, j;
//set column drivers bits 7:4 to quasi-bidirectional w/ pullup M[1:0]=b00 //set column drivers bits 7:4 to quasi-bidirectional w/ pullup M[1:0]=b00
//and row inputs bits 3:0 to quasi-bidirectional w/ pullup M[1:0]=b00 //and row inputs bits 3:0 to quasi-bidirectional w/ pullup M[1:0]=b00
P1M1 = 0x00; P1M1 = 0x00;
@ -73,6 +90,13 @@ void KeyInit(void){
P5M1 &= ~(0x30); P5M1 &= ~(0x30);
P5M0 &= ~(0x30); P5M0 &= ~(0x30);
P5 |= 0x30; //pull up P5 |= 0x30; //pull up
//initialize counts
for (i = 0; i < TOTAL_ROWS; i++){
for (j = 0; j < M_COLS; j++){
last_count[i][j] = COUNT_LIM_LOW;
}
}
} }
static void raw_scan(void){ static void raw_scan(void){
@ -112,7 +136,8 @@ static void raw_scan(void){
//https://summivox.wordpress.com/2016/06/03/keyboard-matrix-scanning-and-debouncing/ //https://summivox.wordpress.com/2016/06/03/keyboard-matrix-scanning-and-debouncing/
static void debounce(void){ static void debounce(void){
uint8_t i, j; uint8_t i, j;
new_keys_pressed = 0; //initially NewKeyPressed = -1; //initially
// new_keys_pressed = 0; //initially
for (i = 0; i < TOTAL_ROWS; i++){ for (i = 0; i < TOTAL_ROWS; i++){
for (j = 0; j < M_COLS; j++){ for (j = 0; j < M_COLS; j++){
int8_t new_count; int8_t new_count;
@ -180,9 +205,14 @@ static void debounce(void){
new_count = COUNT_LIM_LOW; new_count = COUNT_LIM_LOW;
unexpected_count++; unexpected_count++;
} //switch(curr_state) } //switch(curr_state)
//track new keys down //track new key down
if (curr_state != TRANS_LOW_HIGH && new_state == TRANS_LOW_HIGH){ if (curr_state != TRANS_LOW_HIGH && new_state == TRANS_LOW_HIGH){
new_keys_pressed |= ((uint32_t) 1 << (i*M_COLS + j)); NewKeyPressed = (i*M_COLS + j); //can only track 1 for now
#ifdef DESKTOP
printf("new key (%d, %d) pressed\n", i, j);
// printf("curr_state: %d, new_state: %d\n", curr_state, new_state);
#endif
// new_keys_pressed |= ((uint32_t) 1 << (i*M_COLS + j));
} }
//write back new count //write back new count
last_count[i][j] = new_count; last_count[i][j] = new_count;
@ -204,33 +234,29 @@ const uint8_t* DebugGetKeys(void){
} }
#endif #endif
const uint32_t GetNewKeys(void){
return new_keys_pressed;
}
#ifdef DESKTOP #ifdef DESKTOP
#include <stdio.h>
const char state_names[4][32] = {
"STEADY_LOW" ,
"TRANS_LOW_HIGH",
"STEADY_HIGH" ,
"TRANS_HIGH_LOW"
};
int main(void){ int main(void){
KeyInit();
for (int iii = 0; iii < 100; iii++){ for (int iii = 0; iii < 100; iii++){
KeyScan(); //look at '9' key at row 1, col 1 (0 indexed)
const uint8_t* keys = DebugGetKeys(); //the corresponding new key code is 5, see KEY_MAP[] in main.c
static const uint8_t i = 1, j = 1; static const uint8_t i = 1, j = 1;
uint8_t curr_count = last_count[i][j/2] & (0xf << ((j&1)*4)); int8_t curr_count = last_count[i][j/2] & (0xf << ((j&1)*4));
uint8_t curr_state = last_state[i] & (0x3 << (j*2)); uint8_t curr_state = last_state[i] & (0x3 << (j*2));
curr_count >>= (j&1)*4; curr_count >>= (j&1)*4;
curr_state >>= j*2; curr_state >>= j*2;
printf("%3d: %x, %14s, %2u, %2lx\n", if (iii == 0){
iii, keys[1], state_names[curr_state], curr_count, (unsigned long) GetNewKeys()); printf("%3d: %x, %14s, %2d, %2d\n",
iii, Keys[1], state_names[curr_state], curr_count, NewKeyPressed);
}
KeyScan();
printf("%3d: %x, %14s, %2d, %2d\n",
iii, Keys[1], state_names[curr_state], curr_count, NewKeyPressed);
} }
return 0; return 0;

View File

@ -21,6 +21,7 @@ const uint8_t* DebugGetKeys(void);
//prefer using GetNewKeys(); //prefer using GetNewKeys();
#define TOTAL_ROWS 5 #define TOTAL_ROWS 5
extern uint8_t Keys[TOTAL_ROWS]; //only bottom nibbles get set extern uint8_t Keys[TOTAL_ROWS]; //only bottom nibbles get set
extern int8_t NewKeyPressed;
#endif /* SRC_KEY_H_ */ #endif /* SRC_KEY_H_ */

View File

@ -22,7 +22,7 @@ static const char KEY_MAP[20] = {
}; };
uint32_t NewKeyBuf[4]; int8_t NewKeyBuf[4];
volatile uint8_t new_key_write_i; volatile uint8_t new_key_write_i;
volatile uint8_t new_key_read_i; volatile uint8_t new_key_read_i;
volatile uint8_t NewKeyEmpty; volatile uint8_t NewKeyEmpty;
@ -37,17 +37,14 @@ void timer0_isr() SDCC_ISR(1,1)
static uint8_t min_count = 0, hour_count = 0; static uint8_t min_count = 0, hour_count = 0;
#endif #endif
uint32_t new_keys;
//scan keyboard //scan keyboard
KeyScan(); KeyScan();
new_keys = GetNewKeys(); if (NewKeyPressed != -1){
if (new_keys != 0){
if (!NewKeyEmpty && (new_key_write_i == new_key_read_i)){ if (!NewKeyEmpty && (new_key_write_i == new_key_read_i)){
//do not overwrite keymap currently being processed //do not overwrite keymap currently being processed
INCR_NEW_KEY_I(new_key_write_i); INCR_NEW_KEY_I(new_key_write_i);
} }
NewKeyBuf[new_key_write_i] = new_keys; NewKeyBuf[new_key_write_i] = NewKeyPressed;
INCR_NEW_KEY_I(new_key_write_i); INCR_NEW_KEY_I(new_key_write_i);
NewKeyEmpty = 0; NewKeyEmpty = 0;
} }
@ -200,21 +197,15 @@ int main()
#endif //DEBUG_KEYS #endif //DEBUG_KEYS
///get new keys ///get new key
if (!NewKeyEmpty){ if (!NewKeyEmpty){
uint8_t i_key; int8_t i_key = NewKeyBuf[new_key_read_i];
uint32_t new_keys = NewKeyBuf[new_key_read_i];
INCR_NEW_KEY_I(new_key_read_i); INCR_NEW_KEY_I(new_key_read_i);
if (new_key_read_i == new_key_write_i){ if (new_key_read_i == new_key_write_i){
NewKeyEmpty = 1; NewKeyEmpty = 1;
} }
#ifdef DEBUG_KEYS #ifdef DEBUG_KEYS
LCD_GoTo(1,j); LCD_GoTo(1,j);
#endif
//find first key pressed in array (if any)
for (i_key = 0; i_key < 20; i_key++){
if (new_keys & ((uint32_t) 1 << i_key)){
#ifdef DEBUG_KEYS
TERMIO_PutChar(KEY_MAP[i_key]); TERMIO_PutChar(KEY_MAP[i_key]);
j++; j++;
j &= 0x0f; j &= 0x0f;
@ -374,11 +365,7 @@ int main()
default: process_cmd(KEY_MAP[i_key]); default: process_cmd(KEY_MAP[i_key]);
////////// //////////
} //switch(KEY_MAP[i_key]) } //switch(KEY_MAP[i_key])
break;
} //if found new key pressed } //if found new key pressed
} //for new key array
} //if (!NewKeyEmpty)
//print X //print X
LCD_ClearToEnd(0); //go to 2nd row LCD_ClearToEnd(0); //go to 2nd row