fix digit entry before taking log

separate out finishing entry/processing command into common function
This commit is contained in:
Jeff Wang 2019-10-02 23:29:13 -04:00
parent 5c194c68e4
commit 0f17fbdcc7

View File

@ -127,9 +127,40 @@ __xdata char EntryBuf[MAX_CHARS_PER_LINE + 1];
__xdata uint8_t ExpBuf[2]; __xdata uint8_t ExpBuf[2];
__xdata const char VER_STR[32+1] = "STC RPN Calculator v1.04"; __xdata const char VER_STR[32+1] = "STC RPN Calculator v1.04";
enum {
ENTERING_DONE,
ENTERING_SIGNIF,
ENTERING_FRAC,
ENTERING_EXP,
ENTERING_EXP_NEG
};
static uint8_t Entry_i = 0;
static uint8_t EnteringExp = ENTERING_DONE;
static uint8_t Exp_i = 0;
static int8_t I_Key;
static inline void finish_process_entry (void){
//finish entry
int8_t exponent; //exponent is only 2 digits
exponent = 10*ExpBuf[1] + ExpBuf[0];
if ( EnteringExp == ENTERING_EXP_NEG){
exponent = -exponent;
}
EntryBuf[Entry_i] = '\0';
push_decn(EntryBuf, exponent);
process_cmd(KEY_MAP[I_Key]);
//reset state as initial ENTERING_DONE state
EnteringExp = ENTERING_DONE;
Entry_i = 0;
Exp_i = 0;
ExpBuf[0] = 0;
ExpBuf[1] = 0;
}
#ifdef DESKTOP #ifdef DESKTOP
static void print_entry_bufs(int entering_exp){ static void print_entry_bufs(void){
printf("EntryBuf:~%s~ (%d)\n", EntryBuf, entering_exp); printf("EntryBuf:~%s~ (%d)\n", EntryBuf, EnteringExp);
printf("ExpBuf:%c%c\n", '0'+ExpBuf[1], '0'+ExpBuf[0]); printf("ExpBuf:%c%c\n", '0'+ExpBuf[1], '0'+ExpBuf[0]);
} }
#endif #endif
@ -143,16 +174,6 @@ int calc_main()
int main() int main()
#endif #endif
{ {
enum {
ENTERING_DONE,
ENTERING_SIGNIF,
ENTERING_FRAC,
ENTERING_EXP,
ENTERING_EXP_NEG
};
uint8_t entry_i = 0;
uint8_t entering_exp = ENTERING_DONE;
uint8_t exp_i = 0;
int8_t disp_exponent; int8_t disp_exponent;
NewKeyEmpty = 1; //initially empty NewKeyEmpty = 1; //initially empty
#ifdef DEBUG_KEYS #ifdef DEBUG_KEYS
@ -225,15 +246,15 @@ int main()
KeysAvailable.acquire(); KeysAvailable.acquire();
#endif #endif
if (!NewKeyEmpty){ if (!NewKeyEmpty){
int8_t i_key = NewKeyBuf[new_key_read_i]; I_Key = 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 DESKTOP #ifdef DESKTOP
printf("\nprocessing key %c (r=%d, w=%d, e=%d)\n", printf("\nprocessing key %c (r=%d, w=%d, e=%d)\n",
KEY_MAP[i_key], new_key_read_i, new_key_write_i, NewKeyEmpty); KEY_MAP[I_Key], new_key_read_i, new_key_write_i, NewKeyEmpty);
printf("entry_i=%d,exp_i=%d\n", entry_i, exp_i); printf("entry_i=%d,exp_i=%d\n", Entry_i, Exp_i );
#endif #endif
#ifdef DEBUG_KEYS #ifdef DEBUG_KEYS
LCD_GoTo(1,j); LCD_GoTo(1,j);
@ -242,28 +263,28 @@ int main()
j &= 0x0f; j &= 0x0f;
#endif #endif
//process key //process key
switch(KEY_MAP[i_key]){ switch(KEY_MAP[I_Key]){
////////// //////////
case '0': { case '0': {
if (entering_exp >= ENTERING_EXP){ if ( EnteringExp >= ENTERING_EXP){
if (exp_i == 0){ if ( Exp_i == 0){
ExpBuf[0] = 0; ExpBuf[0] = 0;
exp_i = 1; Exp_i = 1;
} else { } else {
ExpBuf[1] = ExpBuf[0]; ExpBuf[1] = ExpBuf[0];
ExpBuf[0] = 0; ExpBuf[0] = 0;
exp_i++; Exp_i++;
if (exp_i > 2){ if ( Exp_i > 2){
exp_i = 1; Exp_i = 1;
} }
} }
} else if (entering_exp == ENTERING_DONE){ } else if ( EnteringExp == ENTERING_DONE){
entering_exp = ENTERING_SIGNIF; EnteringExp = ENTERING_SIGNIF;
EntryBuf[entry_i] = KEY_MAP[i_key]; EntryBuf[Entry_i] = KEY_MAP[I_Key];
//do not increment entry_i from 0, until first non-0 entry //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){ } else if ( Entry_i != 0 && Entry_i < MAX_CHARS_PER_LINE - 1 + 1){
EntryBuf[entry_i] = KEY_MAP[i_key]; EntryBuf[Entry_i] = KEY_MAP[I_Key];
entry_i++; Entry_i++;
} }
} break; } break;
////////// //////////
@ -277,91 +298,83 @@ int main()
case '8': //fallthrough case '8': //fallthrough
case '9': { case '9': {
if (IsShifted){ if (IsShifted){
process_cmd(KEY_MAP[i_key]); if ( EnteringExp != ENTERING_DONE){
} else if (entering_exp >= ENTERING_EXP){ finish_process_entry();
if (exp_i == 0){ } else {
ExpBuf[0] = KEY_MAP[i_key] - '0'; //process key
exp_i = 1; process_cmd(KEY_MAP[I_Key]);
}
NoLift = 0;
} else if ( EnteringExp >= ENTERING_EXP){
if ( Exp_i == 0){
ExpBuf[0] = KEY_MAP[I_Key] - '0';
Exp_i = 1;
} else { } else {
ExpBuf[1] = ExpBuf[0]; ExpBuf[1] = ExpBuf[0];
ExpBuf[0] = KEY_MAP[i_key] - '0'; ExpBuf[0] = KEY_MAP[I_Key] - '0';
exp_i++; Exp_i++;
if (exp_i > 2){ if ( Exp_i > 2){
exp_i = 1; Exp_i = 1;
} }
} }
} else if (entering_exp == ENTERING_DONE){ } else if ( EnteringExp == ENTERING_DONE){
entering_exp = ENTERING_SIGNIF; EnteringExp = ENTERING_SIGNIF;
EntryBuf[entry_i] = KEY_MAP[i_key]; EntryBuf[Entry_i] = KEY_MAP[I_Key];
entry_i++; Entry_i++;
} else if (entry_i < MAX_CHARS_PER_LINE - 1 + 1){ } else if ( Entry_i < MAX_CHARS_PER_LINE - 1 + 1){
EntryBuf[entry_i] = KEY_MAP[i_key]; EntryBuf[Entry_i] = KEY_MAP[I_Key];
entry_i++; Entry_i++;
} }
} break; } break;
////////// //////////
case '.': { case '.': {
if (entering_exp == ENTERING_DONE){ if ( EnteringExp == ENTERING_DONE){
EntryBuf[entry_i++] = '0'; EntryBuf[Entry_i++] = '0';
EntryBuf[entry_i++] = '.'; EntryBuf[Entry_i++] = '.';
entering_exp = ENTERING_FRAC; EnteringExp = ENTERING_FRAC;
} else if (entering_exp == ENTERING_SIGNIF){ } else if ( EnteringExp == ENTERING_SIGNIF){
if (entry_i == 0){ if ( Entry_i == 0){
EntryBuf[entry_i++] = '0'; EntryBuf[Entry_i++] = '0';
} }
EntryBuf[entry_i++] = '.'; EntryBuf[Entry_i++] = '.';
entering_exp = ENTERING_FRAC; EnteringExp = ENTERING_FRAC;
} else if (entering_exp <= ENTERING_EXP) { } else if ( EnteringExp <= ENTERING_EXP) {
entering_exp++; EnteringExp++;
} else { //entering_exp == ENTERING_EXP_NEG } else { //entering_exp == ENTERING_EXP_NEG
entering_exp = ENTERING_EXP; EnteringExp = ENTERING_EXP;
} }
} break; } break;
////////// //////////
case '=': { case '=': {
//track stack lift //track stack lift
if (entering_exp != ENTERING_DONE){ if ( EnteringExp != ENTERING_DONE){
//finish entry finish_process_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);
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 { } else {
//dup //dup
process_cmd(KEY_MAP[i_key]); process_cmd(KEY_MAP[I_Key]);
} }
NoLift = 1; NoLift = 1;
} break; } break;
////////// //////////
case 'c': { case 'c': {
if (IsShifted || entering_exp == ENTERING_DONE){ if (IsShifted || EnteringExp == ENTERING_DONE){
//clear //clear
clear_x(); clear_x();
NoLift = 1; NoLift = 1;
entering_exp = ENTERING_SIGNIF; EnteringExp = ENTERING_SIGNIF;
EntryBuf[entry_i] = '0'; EntryBuf[Entry_i] = '0';
//do not increment entry_i from 0, until first non-0 entry //do not increment entry_i from 0, until first non-0 entry
} else if (entering_exp >= ENTERING_EXP){ } else if ( EnteringExp >= ENTERING_EXP){
//go back to digit entry //go back to digit entry
entering_exp--; EnteringExp--;
exp_i = 0; Exp_i = 0;
ExpBuf[0] = 0; ExpBuf[0] = 0;
ExpBuf[1] = 0; ExpBuf[1] = 0;
} else if (entry_i > 0){ } else if ( Entry_i > 0){
//backspace //backspace
entry_i--; Entry_i--;
if (EntryBuf[entry_i] == '.'){ if (EntryBuf[Entry_i] == '.'){
entering_exp = ENTERING_SIGNIF; EnteringExp = ENTERING_SIGNIF;
} }
} }
} break; } break;
@ -372,30 +385,16 @@ int main()
case '/': //fallthrough case '/': //fallthrough
case '<': //fallthrough //use as +/- case '<': //fallthrough //use as +/-
case 'r': { //use as swap case 'r': { //use as swap
if (entering_exp != ENTERING_DONE){ if ( EnteringExp != ENTERING_DONE){
//finish entry finish_process_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);
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 { } else {
//process key //process key
process_cmd(KEY_MAP[i_key]); process_cmd(KEY_MAP[I_Key]);
} }
NoLift = 0; NoLift = 0;
} break; } break;
////////// //////////
default: process_cmd(KEY_MAP[i_key]); default: process_cmd(KEY_MAP[I_Key]);
////////// //////////
} //switch(KEY_MAP[i_key]) } //switch(KEY_MAP[i_key])
} else { //else for (if found new key pressed) } else { //else for (if found new key pressed)
@ -406,7 +405,7 @@ int main()
LCD_GoTo(0,0); LCD_GoTo(0,0);
//display y register on first line //display y register on first line
if (entering_exp == ENTERING_DONE){ if ( EnteringExp == ENTERING_DONE){
disp_exponent = decn_to_str(get_y()); disp_exponent = decn_to_str(get_y());
} else { } else {
//display x on 1st line, entered number on 2nd line //display x on 1st line, entered number on 2nd line
@ -430,10 +429,10 @@ int main()
LCD_ClearToEnd(0); //go to 2nd row LCD_ClearToEnd(0); //go to 2nd row
#ifdef DESKTOP #ifdef DESKTOP
print_lcd(); print_lcd();
printf("entry_i=%d,exp_i=%d\n", entry_i, exp_i); printf("entry_i=%d,exp_i=%d\n", Entry_i, Exp_i );
print_entry_bufs(entering_exp); print_entry_bufs();
#endif #endif
if (entering_exp == ENTERING_DONE){ if ( EnteringExp == ENTERING_DONE){
disp_exponent = decn_to_str(get_x()); disp_exponent = decn_to_str(get_x());
if (disp_exponent == 0){ if (disp_exponent == 0){
LCD_OutString(Buf, MAX_CHARS_PER_LINE); LCD_OutString(Buf, MAX_CHARS_PER_LINE);
@ -448,17 +447,17 @@ int main()
TERMIO_PutChar((disp_exponent / 10) + '0'); TERMIO_PutChar((disp_exponent / 10) + '0');
TERMIO_PutChar((disp_exponent % 10) + '0'); TERMIO_PutChar((disp_exponent % 10) + '0');
} }
} else if (entry_i == 0){ } else if ( Entry_i == 0){
TERMIO_PutChar('0'); TERMIO_PutChar('0');
} else if (entering_exp < ENTERING_EXP){ } else if ( EnteringExp < ENTERING_EXP){
uint8_t idx; uint8_t idx;
for (idx = 0; idx < entry_i && idx < MAX_CHARS_PER_LINE; idx++){ for (idx = 0; idx < Entry_i && idx < MAX_CHARS_PER_LINE; idx++){
TERMIO_PutChar(EntryBuf[idx]); TERMIO_PutChar(EntryBuf[idx]);
} }
} else { } else {
uint8_t idx; uint8_t idx;
//print significand //print significand
for (idx = 0; idx < entry_i && idx < MAX_CHARS_PER_LINE - 3; idx++){ for (idx = 0; idx < Entry_i && idx < MAX_CHARS_PER_LINE - 3; idx++){
TERMIO_PutChar(EntryBuf[idx]); TERMIO_PutChar(EntryBuf[idx]);
} }
//go to exponent //go to exponent
@ -471,7 +470,7 @@ int main()
LCD_GoTo(1, MAX_CHARS_PER_LINE - 3); LCD_GoTo(1, MAX_CHARS_PER_LINE - 3);
} }
//print exponent sign //print exponent sign
if (entering_exp == ENTERING_EXP_NEG){ if ( EnteringExp == ENTERING_EXP_NEG){
TERMIO_PutChar(CGRAM_EXP_NEG); TERMIO_PutChar(CGRAM_EXP_NEG);
} else { } else {
TERMIO_PutChar(CGRAM_EXP); TERMIO_PutChar(CGRAM_EXP);
@ -489,8 +488,8 @@ int main()
#ifdef DESKTOP #ifdef DESKTOP
print_lcd(); print_lcd();
printf("entry_i=%d,exp_i=%d\n", entry_i, exp_i); printf("entry_i=%d,exp_i=%d\n", Entry_i, Exp_i );
print_entry_bufs(entering_exp); print_entry_bufs();
LcdAvailable.release(); LcdAvailable.release();
#endif #endif
//turn backlight back on //turn backlight back on