stc_rpncalc/src/main.c

390 lines
9.3 KiB
C
Raw Normal View History

2019-03-20 05:34:51 +01:00
//
// STC15 RPN calculator
//
#include "stc15.h"
#include <stdint.h>
#include "lcd.h"
2019-03-20 08:21:11 +01:00
#include "key.h"
2019-04-01 01:20:28 +02:00
#include "decn/decn.h"
#include "calc.h"
2019-03-20 05:34:51 +01:00
#include "utils.h"
2019-03-22 02:03:35 +01:00
#define FOSC 11583000
2019-03-20 05:34:51 +01:00
2019-03-22 02:01:17 +01:00
static const char KEY_MAP[20] = {
'c', '<', 'r', 'm',
'/', '9', '8', '7',
'*', '6', '5', '4',
'-', '3', '2', '1',
'+', '=', '.', '0'
};
uint32_t NewKeyBuf[4];
volatile uint8_t new_key_write_i;
volatile uint8_t new_key_read_i;
volatile uint8_t NewKeyEmpty;
2019-03-22 02:01:17 +01:00
#define INCR_NEW_KEY_I(i) i = (i + 1) & 3
volatile uint8_t SecCount;
//#define TRACK_TIME
void timer0_isr() SDCC_ISR(1,1)
2019-03-20 05:34:51 +01:00
{
#ifdef TRACK_TIME
2019-03-22 02:01:17 +01:00
static uint8_t count = 0;
static uint8_t min_count = 0, hour_count = 0;
#endif
2019-03-22 02:01:17 +01:00
uint32_t new_keys;
//scan keyboard
KeyScan();
new_keys = GetNewKeys();
if (new_keys != 0){
if (!NewKeyEmpty && (new_key_write_i == new_key_read_i)){
2019-03-22 02:01:17 +01:00
//do not overwrite keymap currently being processed
INCR_NEW_KEY_I(new_key_write_i);
}
NewKeyBuf[new_key_write_i] = new_keys;
INCR_NEW_KEY_I(new_key_write_i);
NewKeyEmpty = 0;
2019-03-22 02:01:17 +01:00
}
2019-03-22 02:03:35 +01:00
//track time
#ifdef TRACK_TIME
2019-03-22 02:03:35 +01:00
count++;
if (count == 200){
count = 0;
SecCount++;
if (SecCount == 60){
SecCount = 0;
min_count++;
if (min_count == 60){
min_count = 0;
hour_count++;
}
}
}
#endif
2019-03-20 05:34:51 +01:00
}
#ifdef DESKTOP
void Timer0Init(void) { }
static void latch_on(void){ }
#else
2019-03-22 02:03:35 +01:00
// Call timer0_isr() 200/sec: 5 ms period
// Initialize the timer count so that it overflows after 0.01 sec
// THTL = 0x10000 - FOSC / 200 = 0x10000 - 115830 = 7621 = 0x1DC5
void Timer0Init(void)
2019-03-20 05:34:51 +01:00
{
2019-03-22 02:03:35 +01:00
// TMOD = 0; // default: 16-bit auto-reload
AUXR |= 0x80; // use undivided SYSclk for timer0
2019-03-20 05:34:51 +01:00
// Initial values of TL0 and TH0 are stored in hidden reload registers: RL_TL0 and RL_TH0
2019-03-22 02:03:35 +01:00
TL0 = 0xC5; // Initial timer value
TH0 = 0x1D; // Initial timer value
2019-03-20 05:34:51 +01:00
TF0 = 0; // Clear overflow flag
TR0 = 1; // Timer0 start run
ET0 = 1; // Enable timer0 interrupt
EA = 1; // Enable global interrupt
}
#endif //!DESKTOP
2019-03-20 05:34:51 +01:00
2019-04-01 01:20:28 +02:00
char Buf[DECN_BUF_SIZE];
__xdata char EntryBuf[MAX_CHARS_PER_LINE + 1];
__xdata uint8_t ExpBuf[2] = {0, 0};
//#define DEBUG_UPTIME
2019-03-20 05:34:51 +01:00
/*********************************************/
int main()
{
2019-04-01 01:20:28 +02:00
enum {
ENTERING_DONE,
ENTERING_SIGNIF_NOLIFT,
ENTERING_SIGNIF,
ENTERING_FRAC,
ENTERING_EXP,
ENTERING_EXP_NEG
};
uint8_t entry_i = 0;
uint8_t entering_exp = ENTERING_DONE;
uint8_t no_lift = 0;
uint8_t exp_i = 0;
#ifdef DEBUG_KEYS
uint8_t j = 0;
2019-03-22 02:01:17 +01:00
const uint8_t* keys;
2019-03-20 08:21:11 +01:00
uint8_t key_i;
#endif
#ifdef DEBUG_UPTIME
uint32_t i;
#endif
latch_on();
2019-03-20 05:34:51 +01:00
Timer0Init(); // display refresh & switch read
LCD_Open();
2019-03-20 08:21:11 +01:00
KeyInit();
2019-03-20 05:34:51 +01:00
P3_4 = 0; //turn on led backlight
2019-03-22 02:03:49 +01:00
//set (P3_2) as push pull output
P3_2 = 1; //latch on
P3M1 &= ~(0x4);
P3M0 |= (0x4);
BACKLIGHT_ON(); //turn on led backlight
2019-03-22 02:03:49 +01:00
#ifdef DEBUG_UPTIME
2019-03-20 05:34:51 +01:00
i = 0;
#endif
2019-03-20 05:34:51 +01:00
// LOOP
while (1)
{
//turn off?
if (Keys[0] == 8 && Keys[4] == 8){
TURN_OFF();
}
2019-03-20 08:21:11 +01:00
LCD_GoTo(0,0);
#ifdef DEBUG_UPTIME
u32str(i++, Buf, 10);
LCD_OutString(Buf, MAX_CHARS_PER_LINE);
#else
2019-04-01 01:20:28 +02:00
//display y register on first line
if (entering_exp == ENTERING_DONE){
dec80_to_str(Buf, get_y());
} else {
//display x on 1st line, entered number on 2nd line
dec80_to_str(Buf, get_x());
}
LCD_OutString(Buf, MAX_CHARS_PER_LINE);
#endif //DEBUG_UPTIME
#ifdef DEBUG_KEYS
2019-03-22 02:01:17 +01:00
//keyboard debug
keys = DebugGetKeys();
2019-03-20 08:21:11 +01:00
for (key_i = 0; key_i < 5; key_i++){
LCD_OutNibble(keys[key_i]);
}
2019-03-22 02:01:17 +01:00
TERMIO_PutChar(',');
2019-03-20 08:21:11 +01:00
//counter
2019-03-22 02:01:17 +01:00
if (SecCount == 0){
LCD_OutString(" ");
} else if (SecCount < 10){
TERMIO_PutChar(' ');
LCD_OutString(u32str(SecCount, Buf, 10));
2019-03-22 02:01:17 +01:00
} else {
LCD_OutString(u32str(SecCount, Buf, 10));
2019-03-22 02:01:17 +01:00
}
#endif //DEBUG_KEYS
2019-03-20 05:34:51 +01:00
///get new keys
if (!NewKeyEmpty){
2019-03-22 02:01:17 +01:00
uint8_t i_key;
uint32_t new_keys = NewKeyBuf[new_key_read_i];
INCR_NEW_KEY_I(new_key_read_i);
if (new_key_read_i == new_key_write_i){
NewKeyEmpty = 1;
2019-03-22 02:01:17 +01:00
}
2019-04-01 01:20:28 +02:00
#ifdef DEBUG_KEYS
2019-03-22 02:01:17 +01:00
LCD_GoTo(1,j);
2019-04-01 01:20:28 +02:00
#endif
//find first key pressed in array (if any)
2019-03-22 02:01:17 +01:00
for (i_key = 0; i_key < 20; i_key++){
if (new_keys & ((uint32_t) 1 << i_key)){
2019-04-01 01:20:28 +02:00
#ifdef DEBUG_KEYS
2019-03-22 02:01:17 +01:00
TERMIO_PutChar(KEY_MAP[i_key]);
j++;
j &= 0x0f;
2019-04-01 01:20:28 +02:00
#endif
//process key
switch(KEY_MAP[i_key]){
//////////
case '0': {
if (entering_exp >= ENTERING_EXP){
ExpBuf[exp_i] = 0;
exp_i = (exp_i + 1) & 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++;
}
} 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){
ExpBuf[exp_i] = KEY_MAP[i_key] - '0';
exp_i = (exp_i + 1) & 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++;
}
} 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])
2019-03-22 02:01:17 +01:00
break;
2019-04-01 01:20:28 +02:00
} //if found new key pressed
} //for new key array
} //if (!NewKeyEmpty)
//print X
LCD_ClearToEnd(0); //go to 2nd row
if (entering_exp == ENTERING_DONE){
dec80_to_str(Buf, get_x());
LCD_OutString(Buf, MAX_CHARS_PER_LINE);
} else if (entry_i == 0){
TERMIO_PutChar('0');
} else if (entering_exp < ENTERING_EXP){
uint8_t idx;
for (idx = 0; idx < entry_i && idx < MAX_CHARS_PER_LINE; idx++){
TERMIO_PutChar(EntryBuf[idx]);
}
} else {
uint8_t idx;
//print significand
for (idx = 0; idx < entry_i && idx < MAX_CHARS_PER_LINE - 3; idx++){
TERMIO_PutChar(EntryBuf[idx]);
}
//go to exponent
if (idx < MAX_CHARS_PER_LINE - 3){
//clear until exponent
for ( ; idx < MAX_CHARS_PER_LINE - 3; idx++){
TERMIO_PutChar(' ');
2019-03-22 02:01:17 +01:00
}
2019-04-01 01:20:28 +02:00
} else {
LCD_GoTo(1, MAX_CHARS_PER_LINE - 3);
2019-03-22 02:01:17 +01:00
}
2019-04-01 01:20:28 +02:00
//print exponent sign
if (entering_exp == ENTERING_EXP_NEG){
TERMIO_PutChar('-');
} else {
TERMIO_PutChar(' ');
}
//print exp
TERMIO_PutChar(ExpBuf[1] + '0');
TERMIO_PutChar(ExpBuf[0] + '0');
2019-03-22 02:01:17 +01:00
}
2019-04-01 01:20:28 +02:00
LCD_ClearToEnd(1);
//turn backlight back on
BACKLIGHT_ON();
2019-04-01 01:20:28 +02:00
} //while (1)
2019-03-20 05:34:51 +01:00
}
/* ------------------------------------------------------------------------- */