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:
parent
8aec2ae134
commit
f5d8d601a2
72
src/key.c
72
src/key.c
@ -9,7 +9,15 @@
|
||||
#include "utils.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"
|
||||
#endif
|
||||
|
||||
@ -17,7 +25,7 @@
|
||||
#define M_COLS 4
|
||||
|
||||
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 int8_t last_count[TOTAL_ROWS][M_COLS]; //all used
|
||||
static uint8_t unexpected_count; //count of unexpected transitions
|
||||
@ -35,7 +43,14 @@ static const int8_t THRESH_TRANS = 2;
|
||||
|
||||
#ifdef DESKTOP
|
||||
//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 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
|
||||
};
|
||||
|
||||
keys[1] = data[i];
|
||||
Keys[1] = data[i];
|
||||
i++;
|
||||
if (i >= (sizeof(data)/sizeof(data[0]))){
|
||||
i = 0;
|
||||
@ -57,6 +72,8 @@ static void raw_scan(void){
|
||||
}
|
||||
#else
|
||||
void KeyInit(void){
|
||||
uint8_t i, j;
|
||||
|
||||
//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
|
||||
P1M1 = 0x00;
|
||||
@ -73,6 +90,13 @@ void KeyInit(void){
|
||||
P5M1 &= ~(0x30);
|
||||
P5M0 &= ~(0x30);
|
||||
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){
|
||||
@ -112,7 +136,8 @@ static void raw_scan(void){
|
||||
//https://summivox.wordpress.com/2016/06/03/keyboard-matrix-scanning-and-debouncing/
|
||||
static void debounce(void){
|
||||
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 (j = 0; j < M_COLS; j++){
|
||||
int8_t new_count;
|
||||
@ -180,9 +205,14 @@ static void debounce(void){
|
||||
new_count = COUNT_LIM_LOW;
|
||||
unexpected_count++;
|
||||
} //switch(curr_state)
|
||||
//track new keys down
|
||||
//track new key down
|
||||
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
|
||||
last_count[i][j] = new_count;
|
||||
@ -204,33 +234,29 @@ const uint8_t* DebugGetKeys(void){
|
||||
}
|
||||
#endif
|
||||
|
||||
const uint32_t GetNewKeys(void){
|
||||
return new_keys_pressed;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef DESKTOP
|
||||
#include <stdio.h>
|
||||
|
||||
const char state_names[4][32] = {
|
||||
"STEADY_LOW" ,
|
||||
"TRANS_LOW_HIGH",
|
||||
"STEADY_HIGH" ,
|
||||
"TRANS_HIGH_LOW"
|
||||
};
|
||||
|
||||
int main(void){
|
||||
KeyInit();
|
||||
|
||||
for (int iii = 0; iii < 100; iii++){
|
||||
KeyScan();
|
||||
const uint8_t* keys = DebugGetKeys();
|
||||
//look at '9' key at row 1, col 1 (0 indexed)
|
||||
//the corresponding new key code is 5, see KEY_MAP[] in main.c
|
||||
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));
|
||||
curr_count >>= (j&1)*4;
|
||||
curr_state >>= j*2;
|
||||
printf("%3d: %x, %14s, %2u, %2lx\n",
|
||||
iii, keys[1], state_names[curr_state], curr_count, (unsigned long) GetNewKeys());
|
||||
if (iii == 0){
|
||||
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;
|
||||
|
@ -21,6 +21,7 @@ const uint8_t* DebugGetKeys(void);
|
||||
//prefer using GetNewKeys();
|
||||
#define TOTAL_ROWS 5
|
||||
extern uint8_t Keys[TOTAL_ROWS]; //only bottom nibbles get set
|
||||
extern int8_t NewKeyPressed;
|
||||
|
||||
|
||||
#endif /* SRC_KEY_H_ */
|
||||
|
337
src/main.c
337
src/main.c
@ -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_read_i;
|
||||
volatile uint8_t NewKeyEmpty;
|
||||
@ -37,17 +37,14 @@ void timer0_isr() SDCC_ISR(1,1)
|
||||
static uint8_t min_count = 0, hour_count = 0;
|
||||
#endif
|
||||
|
||||
uint32_t new_keys;
|
||||
|
||||
//scan keyboard
|
||||
KeyScan();
|
||||
new_keys = GetNewKeys();
|
||||
if (new_keys != 0){
|
||||
if (NewKeyPressed != -1){
|
||||
if (!NewKeyEmpty && (new_key_write_i == new_key_read_i)){
|
||||
//do not overwrite keymap currently being processed
|
||||
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);
|
||||
NewKeyEmpty = 0;
|
||||
}
|
||||
@ -200,185 +197,175 @@ int main()
|
||||
#endif //DEBUG_KEYS
|
||||
|
||||
|
||||
///get new keys
|
||||
///get new key
|
||||
if (!NewKeyEmpty){
|
||||
uint8_t i_key;
|
||||
uint32_t new_keys = NewKeyBuf[new_key_read_i];
|
||||
int8_t i_key = NewKeyBuf[new_key_read_i];
|
||||
INCR_NEW_KEY_I(new_key_read_i);
|
||||
if (new_key_read_i == new_key_write_i){
|
||||
NewKeyEmpty = 1;
|
||||
}
|
||||
#ifdef DEBUG_KEYS
|
||||
LCD_GoTo(1,j);
|
||||
TERMIO_PutChar(KEY_MAP[i_key]);
|
||||
j++;
|
||||
j &= 0x0f;
|
||||
#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]);
|
||||
j++;
|
||||
j &= 0x0f;
|
||||
#endif
|
||||
//process key
|
||||
switch(KEY_MAP[i_key]){
|
||||
//////////
|
||||
case '0': {
|
||||
if (entering_exp >= ENTERING_EXP){
|
||||
if (exp_i == 0){
|
||||
ExpBuf[0] = 0;
|
||||
exp_i = 1;
|
||||
} else {
|
||||
ExpBuf[1] = ExpBuf[0];
|
||||
ExpBuf[0] = 0;
|
||||
exp_i++;
|
||||
if (exp_i > 2){
|
||||
exp_i = 1;
|
||||
}
|
||||
}
|
||||
} else if (entering_exp == ENTERING_DONE){
|
||||
entering_exp = ENTERING_SIGNIF;
|
||||
EntryBuf[entry_i] = KEY_MAP[i_key];
|
||||
//do not increment entry_i from 0, until first non-0 entry
|
||||
} else if (entry_i != 0 && entry_i < MAX_CHARS_PER_LINE - 1 + 1){
|
||||
EntryBuf[entry_i] = KEY_MAP[i_key];
|
||||
entry_i++;
|
||||
//process key
|
||||
switch(KEY_MAP[i_key]){
|
||||
//////////
|
||||
case '0': {
|
||||
if (entering_exp >= ENTERING_EXP){
|
||||
if (exp_i == 0){
|
||||
ExpBuf[0] = 0;
|
||||
exp_i = 1;
|
||||
} else {
|
||||
ExpBuf[1] = ExpBuf[0];
|
||||
ExpBuf[0] = 0;
|
||||
exp_i++;
|
||||
if (exp_i > 2){
|
||||
exp_i = 1;
|
||||
}
|
||||
} break;
|
||||
//////////
|
||||
case '1': //fallthrough
|
||||
case '2': //fallthrough
|
||||
case '3': //fallthrough
|
||||
case '4': //fallthrough
|
||||
case '5': //fallthrough
|
||||
case '6': //fallthrough
|
||||
case '7': //fallthrough
|
||||
case '8': //fallthrough
|
||||
case '9': {
|
||||
if (entering_exp >= ENTERING_EXP){
|
||||
if (exp_i == 0){
|
||||
ExpBuf[0] = KEY_MAP[i_key] - '0';
|
||||
exp_i = 1;
|
||||
} else {
|
||||
ExpBuf[1] = ExpBuf[0];
|
||||
ExpBuf[0] = KEY_MAP[i_key] - '0';
|
||||
exp_i++;
|
||||
if (exp_i > 2){
|
||||
exp_i = 1;
|
||||
}
|
||||
}
|
||||
} else if (entering_exp == ENTERING_DONE){
|
||||
entering_exp = ENTERING_SIGNIF;
|
||||
EntryBuf[entry_i] = KEY_MAP[i_key];
|
||||
entry_i++;
|
||||
} else if (entry_i < MAX_CHARS_PER_LINE - 1 + 1){
|
||||
EntryBuf[entry_i] = KEY_MAP[i_key];
|
||||
entry_i++;
|
||||
}
|
||||
} else if (entering_exp == ENTERING_DONE){
|
||||
entering_exp = ENTERING_SIGNIF;
|
||||
EntryBuf[entry_i] = KEY_MAP[i_key];
|
||||
//do not increment entry_i from 0, until first non-0 entry
|
||||
} else if (entry_i != 0 && entry_i < MAX_CHARS_PER_LINE - 1 + 1){
|
||||
EntryBuf[entry_i] = KEY_MAP[i_key];
|
||||
entry_i++;
|
||||
}
|
||||
} break;
|
||||
//////////
|
||||
case '1': //fallthrough
|
||||
case '2': //fallthrough
|
||||
case '3': //fallthrough
|
||||
case '4': //fallthrough
|
||||
case '5': //fallthrough
|
||||
case '6': //fallthrough
|
||||
case '7': //fallthrough
|
||||
case '8': //fallthrough
|
||||
case '9': {
|
||||
if (entering_exp >= ENTERING_EXP){
|
||||
if (exp_i == 0){
|
||||
ExpBuf[0] = KEY_MAP[i_key] - '0';
|
||||
exp_i = 1;
|
||||
} else {
|
||||
ExpBuf[1] = ExpBuf[0];
|
||||
ExpBuf[0] = KEY_MAP[i_key] - '0';
|
||||
exp_i++;
|
||||
if (exp_i > 2){
|
||||
exp_i = 1;
|
||||
}
|
||||
} break;
|
||||
//////////
|
||||
case '.': {
|
||||
if (entering_exp == ENTERING_DONE){
|
||||
EntryBuf[entry_i++] = '0';
|
||||
EntryBuf[entry_i++] = '.';
|
||||
entering_exp = ENTERING_FRAC;
|
||||
} else if (entering_exp == ENTERING_SIGNIF){
|
||||
if (entry_i == 0){
|
||||
EntryBuf[entry_i++] = '0';
|
||||
}
|
||||
EntryBuf[entry_i++] = '.';
|
||||
entering_exp = ENTERING_FRAC;
|
||||
} else if (entering_exp <= ENTERING_EXP) {
|
||||
entering_exp++;
|
||||
} else { //entering_exp == ENTERING_EXP_NEG
|
||||
entering_exp = ENTERING_EXP;
|
||||
}
|
||||
} break;
|
||||
//////////
|
||||
case '=': {
|
||||
//track stack lift
|
||||
if (entering_exp != ENTERING_DONE){
|
||||
//finish entry
|
||||
int8_t exponent; //exponent is only 2 digits
|
||||
exponent = 10*ExpBuf[1] + ExpBuf[0];
|
||||
if (entering_exp == ENTERING_EXP_NEG){
|
||||
exponent = -exponent;
|
||||
}
|
||||
EntryBuf[entry_i] = '\0';
|
||||
push_decn(EntryBuf, exponent, no_lift);
|
||||
process_cmd(KEY_MAP[i_key]);
|
||||
//reset state as initial ENTERING_DONE state
|
||||
entering_exp = ENTERING_DONE;
|
||||
entry_i = 0;
|
||||
exp_i = 0;
|
||||
ExpBuf[0] = 0;
|
||||
ExpBuf[1] = 0;
|
||||
} else {
|
||||
//dup
|
||||
process_cmd(KEY_MAP[i_key]);
|
||||
}
|
||||
no_lift = 1;
|
||||
} break;
|
||||
//////////
|
||||
case 'c': {
|
||||
if (entering_exp == ENTERING_DONE){
|
||||
//clear
|
||||
clear_x();
|
||||
no_lift = 1;
|
||||
entering_exp = ENTERING_SIGNIF;
|
||||
EntryBuf[entry_i] = '0';
|
||||
//do not increment entry_i from 0, until first non-0 entry
|
||||
} else if (entering_exp >= ENTERING_EXP){
|
||||
//go back to digit entry
|
||||
entering_exp--;
|
||||
exp_i = 0;
|
||||
ExpBuf[0] = 0;
|
||||
ExpBuf[1] = 0;
|
||||
} else if (entry_i > 0){
|
||||
//backspace
|
||||
if (EntryBuf[entry_i] == '.'){
|
||||
entering_exp--;
|
||||
}
|
||||
entry_i--;
|
||||
}
|
||||
} break;
|
||||
//////////
|
||||
case '+': //fallthrough
|
||||
case '*': //fallthrough
|
||||
case '-': //fallthrough
|
||||
case '/': //fallthrough
|
||||
case '<': //fallthrough //use as +/-
|
||||
case 'r': { //use as swap
|
||||
if (entering_exp != ENTERING_DONE){
|
||||
//finish entry
|
||||
int8_t exponent; //exponent is only 2 digits
|
||||
exponent = 10*ExpBuf[1] + ExpBuf[0];
|
||||
if (entering_exp == ENTERING_EXP_NEG){
|
||||
exponent = -exponent;
|
||||
}
|
||||
EntryBuf[entry_i] = '\0';
|
||||
push_decn(EntryBuf, exponent, no_lift);
|
||||
process_cmd(KEY_MAP[i_key]);
|
||||
//reset state as initial ENTERING_DONE state
|
||||
entering_exp = ENTERING_DONE;
|
||||
entry_i = 0;
|
||||
exp_i = 0;
|
||||
ExpBuf[0] = 0;
|
||||
ExpBuf[1] = 0;
|
||||
} else {
|
||||
//process key
|
||||
process_cmd(KEY_MAP[i_key]);
|
||||
}
|
||||
no_lift = 0;
|
||||
} break;
|
||||
//////////
|
||||
default: process_cmd(KEY_MAP[i_key]);
|
||||
//////////
|
||||
} //switch(KEY_MAP[i_key])
|
||||
|
||||
break;
|
||||
} //if found new key pressed
|
||||
} //for new key array
|
||||
} //if (!NewKeyEmpty)
|
||||
}
|
||||
} else if (entering_exp == ENTERING_DONE){
|
||||
entering_exp = ENTERING_SIGNIF;
|
||||
EntryBuf[entry_i] = KEY_MAP[i_key];
|
||||
entry_i++;
|
||||
} else if (entry_i < MAX_CHARS_PER_LINE - 1 + 1){
|
||||
EntryBuf[entry_i] = KEY_MAP[i_key];
|
||||
entry_i++;
|
||||
}
|
||||
} break;
|
||||
//////////
|
||||
case '.': {
|
||||
if (entering_exp == ENTERING_DONE){
|
||||
EntryBuf[entry_i++] = '0';
|
||||
EntryBuf[entry_i++] = '.';
|
||||
entering_exp = ENTERING_FRAC;
|
||||
} else if (entering_exp == ENTERING_SIGNIF){
|
||||
if (entry_i == 0){
|
||||
EntryBuf[entry_i++] = '0';
|
||||
}
|
||||
EntryBuf[entry_i++] = '.';
|
||||
entering_exp = ENTERING_FRAC;
|
||||
} else if (entering_exp <= ENTERING_EXP) {
|
||||
entering_exp++;
|
||||
} else { //entering_exp == ENTERING_EXP_NEG
|
||||
entering_exp = ENTERING_EXP;
|
||||
}
|
||||
} break;
|
||||
//////////
|
||||
case '=': {
|
||||
//track stack lift
|
||||
if (entering_exp != ENTERING_DONE){
|
||||
//finish entry
|
||||
int8_t exponent; //exponent is only 2 digits
|
||||
exponent = 10*ExpBuf[1] + ExpBuf[0];
|
||||
if (entering_exp == ENTERING_EXP_NEG){
|
||||
exponent = -exponent;
|
||||
}
|
||||
EntryBuf[entry_i] = '\0';
|
||||
push_decn(EntryBuf, exponent, no_lift);
|
||||
process_cmd(KEY_MAP[i_key]);
|
||||
//reset state as initial ENTERING_DONE state
|
||||
entering_exp = ENTERING_DONE;
|
||||
entry_i = 0;
|
||||
exp_i = 0;
|
||||
ExpBuf[0] = 0;
|
||||
ExpBuf[1] = 0;
|
||||
} else {
|
||||
//dup
|
||||
process_cmd(KEY_MAP[i_key]);
|
||||
}
|
||||
no_lift = 1;
|
||||
} break;
|
||||
//////////
|
||||
case 'c': {
|
||||
if (entering_exp == ENTERING_DONE){
|
||||
//clear
|
||||
clear_x();
|
||||
no_lift = 1;
|
||||
entering_exp = ENTERING_SIGNIF;
|
||||
EntryBuf[entry_i] = '0';
|
||||
//do not increment entry_i from 0, until first non-0 entry
|
||||
} else if (entering_exp >= ENTERING_EXP){
|
||||
//go back to digit entry
|
||||
entering_exp--;
|
||||
exp_i = 0;
|
||||
ExpBuf[0] = 0;
|
||||
ExpBuf[1] = 0;
|
||||
} else if (entry_i > 0){
|
||||
//backspace
|
||||
if (EntryBuf[entry_i] == '.'){
|
||||
entering_exp--;
|
||||
}
|
||||
entry_i--;
|
||||
}
|
||||
} break;
|
||||
//////////
|
||||
case '+': //fallthrough
|
||||
case '*': //fallthrough
|
||||
case '-': //fallthrough
|
||||
case '/': //fallthrough
|
||||
case '<': //fallthrough //use as +/-
|
||||
case 'r': { //use as swap
|
||||
if (entering_exp != ENTERING_DONE){
|
||||
//finish entry
|
||||
int8_t exponent; //exponent is only 2 digits
|
||||
exponent = 10*ExpBuf[1] + ExpBuf[0];
|
||||
if (entering_exp == ENTERING_EXP_NEG){
|
||||
exponent = -exponent;
|
||||
}
|
||||
EntryBuf[entry_i] = '\0';
|
||||
push_decn(EntryBuf, exponent, no_lift);
|
||||
process_cmd(KEY_MAP[i_key]);
|
||||
//reset state as initial ENTERING_DONE state
|
||||
entering_exp = ENTERING_DONE;
|
||||
entry_i = 0;
|
||||
exp_i = 0;
|
||||
ExpBuf[0] = 0;
|
||||
ExpBuf[1] = 0;
|
||||
} else {
|
||||
//process key
|
||||
process_cmd(KEY_MAP[i_key]);
|
||||
}
|
||||
no_lift = 0;
|
||||
} break;
|
||||
//////////
|
||||
default: process_cmd(KEY_MAP[i_key]);
|
||||
//////////
|
||||
} //switch(KEY_MAP[i_key])
|
||||
} //if found new key pressed
|
||||
|
||||
//print X
|
||||
LCD_ClearToEnd(0); //go to 2nd row
|
||||
|
Loading…
Reference in New Issue
Block a user