made new functions accessible to the calculator

This commit is contained in:
Mirko Scholz 2020-09-10 16:37:23 +02:00
parent e4ad37623b
commit 0b5e011017
5 changed files with 91 additions and 39 deletions

View File

@ -34,7 +34,8 @@ __xdata dec80 LastX;
#define STACK_T 3 #define STACK_T 3
uint8_t NoLift = 0; uint8_t NoLift = 0;
__bit IsShifted = 0; __bit IsShiftedUp = 0;
__bit IsShiftedDown = 0;
//stack "grows" towards 0 //stack "grows" towards 0
__xdata dec80 Stack[STACK_SIZE]; //0->x, 1->y, 2->z, 3->t initially __xdata dec80 Stack[STACK_SIZE]; //0->x, 1->y, 2->z, 3->t initially
@ -88,10 +89,6 @@ static void do_unary_op(void (*f_ptr)(void)){
} }
} }
static void toggle_shifted(void){
IsShifted ^= 1;
}
void process_cmd(char cmd){ void process_cmd(char cmd){
//turn off backlight before start of processing //turn off backlight before start of processing
backlight_off(); backlight_off();
@ -99,12 +96,12 @@ void process_cmd(char cmd){
switch(cmd){ switch(cmd){
////////// //////////
case '+':{ case '+':{
if (IsShifted){ // LastX if (IsShiftedUp){ // LastX
if (NoLift != 1){ if (NoLift != 1){
StackPtr--; StackPtr--;
} }
copy_decn(&stack(STACK_X), &LastX); copy_decn(&stack(STACK_X), &LastX);
IsShifted = 0; IsShiftedUp = 0;
} else { // + } else { // +
do_binary_op(add_decn); do_binary_op(add_decn);
} }
@ -115,22 +112,36 @@ void process_cmd(char cmd){
} break; } break;
////////// //////////
case '-':{ case '-':{
negate_decn(&stack(STACK_X)); if (IsShiftedUp) {
do_binary_op(add_decn); do_unary_op(to_radian_decn);
negate_decn(&LastX); //stored LastX was after negation of X IsShiftedUp = 0;
} else if (IsShiftedDown) {
do_unary_op(to_degree_decn);
IsShiftedDown = 0;
} else {
negate_decn(&stack(STACK_X));
do_binary_op(add_decn);
negate_decn(&LastX); //stored LastX was after negation of X
}
} break; } break;
////////// //////////
case '/':{ case '/':{
do_binary_op(div_decn); if (IsShiftedUp){
StackPtr--;
pi_decn();
copy_decn(&stack(STACK_X), &AccDecn);
} else if (IsShiftedDown) {
} else {
do_binary_op(div_decn);
}
} break; } break;
////////// //////////
case '=':{ case '=':{
if (IsShifted){ //RCL if (IsShiftedUp){ //RCL
if (NoLift != 1){ if (NoLift != 1){
StackPtr--; StackPtr--;
} }
copy_decn(&stack(STACK_X), &StoredDecn); copy_decn(&stack(STACK_X), &StoredDecn);
IsShifted = 0;
} else { //Enter } else { //Enter
if (!decn_is_nan(&stack(STACK_X))){ if (!decn_is_nan(&stack(STACK_X))){
StackPtr--; StackPtr--;
@ -140,9 +151,8 @@ void process_cmd(char cmd){
} break; } break;
////////// //////////
case '.':{ case '.':{
if (IsShifted){ //STO if (IsShiftedUp){ //STO
copy_decn(&StoredDecn, &stack(STACK_X)); copy_decn(&StoredDecn, &stack(STACK_X));
IsShifted = 0;
} }
} break; } break;
////////// //////////
@ -151,8 +161,8 @@ void process_cmd(char cmd){
} break; } break;
////////// //////////
case '<':{ //use as +/- and sqrt case '<':{ //use as +/- and sqrt
if (IsShifted){ //take sqrt if (IsShiftedUp){ //take sqrt
IsShifted = 0; IsShiftedUp = 0;
if (decn_is_zero(&stack(STACK_X))){ if (decn_is_zero(&stack(STACK_X))){
//sqrt(0) = 0 //sqrt(0) = 0
} else if (!decn_is_nan(&stack(STACK_X))){ } else if (!decn_is_nan(&stack(STACK_X))){
@ -176,8 +186,7 @@ void process_cmd(char cmd){
} break; } break;
////////// //////////
case 'r':{ //use as swap and 1/x case 'r':{ //use as swap and 1/x
if (IsShifted){ //take 1/x if (IsShiftedUp){ //take 1/x
IsShifted = 0;
do_unary_op(recip_decn); do_unary_op(recip_decn);
} else { // swap } else { // swap
if (!decn_is_nan(&stack(STACK_X))){ if (!decn_is_nan(&stack(STACK_X))){
@ -190,41 +199,70 @@ void process_cmd(char cmd){
} break; } break;
////////// //////////
case 'm':{ //use as shift case 'm':{ //use as shift
toggle_shifted(); if (IsShiftedUp) {
IsShiftedUp = 0;
IsShiftedDown = 1;
} else if (IsShiftedDown) {
IsShiftedUp = 0;
IsShiftedDown = 0;
} else {
IsShiftedUp = 1;
IsShiftedDown = 0;
}
return;
} break;
//////////
case '1':{
if (IsShiftedUp){
do_unary_op(sin_decn);
} else if (IsShiftedDown){
// do_unary_op(arcsin_decn);
}
} break;
//////////
case '2':{
if (IsShiftedUp){
do_unary_op(cos_decn);
} else if (IsShiftedDown){
// do_unary_op(arccos_decn);
}
} break;
//////////
case '3':{
if (IsShiftedUp){
do_unary_op(tan_decn);
} else if (IsShiftedDown){
do_unary_op(arctan_decn);
}
} break; } break;
////////// //////////
case '4':{ case '4':{
if (IsShifted){ //roll down if (IsShiftedUp){ //roll down
StackPtr++; StackPtr++;
IsShifted = 0;
} }
} break; } break;
////////// //////////
case '5':{ case '5':{
if (IsShifted){ //e^x if (IsShiftedUp){ //e^x
do_unary_op(exp_decn); do_unary_op(exp_decn);
IsShifted = 0;
} }
} break; } break;
////////// //////////
case '6':{ case '6':{
if (IsShifted){ //10^x if (IsShiftedUp){ //10^x
do_unary_op(exp10_decn); do_unary_op(exp10_decn);
IsShifted = 0;
} }
} break; } break;
////////// //////////
case '9':{ case '9':{
if (IsShifted){ //log10(x) if (IsShiftedUp){ //log10(x)
do_unary_op(log10_decn); do_unary_op(log10_decn);
IsShifted = 0;
} }
} break; } break;
////////// //////////
case '8':{ case '8':{
if (IsShifted){ //ln(x) if (IsShiftedUp){ //ln(x)
do_unary_op(ln_decn); do_unary_op(ln_decn);
IsShifted = 0;
} }
} break; } break;
////////// //////////
@ -239,10 +277,11 @@ void process_cmd(char cmd){
copy_decn(&stack(STACK_Y), &AccDecn); copy_decn(&stack(STACK_Y), &AccDecn);
} }
pop(); pop();
IsShifted = 0;
} break; } break;
////////// //////////
} //switch(cmd) } //switch(cmd)
IsShiftedUp = 0;
IsShiftedDown = 0;
} }

View File

@ -32,7 +32,8 @@ void process_cmd(char cmd);
//push_decn is equivalent to "set_x()" if no_lift is true //push_decn is equivalent to "set_x()" if no_lift is true
void push_decn(__xdata const char* signif_str, __xdata exp_t exponent); void push_decn(__xdata const char* signif_str, __xdata exp_t exponent);
extern uint8_t NoLift; extern uint8_t NoLift;
extern __bit IsShifted; extern __bit IsShiftedUp;
extern __bit IsShiftedDown;
void clear_x(void); void clear_x(void);
__xdata dec80* get_x(void); __xdata dec80* get_x(void);

View File

@ -1393,6 +1393,13 @@ void to_radian_decn(void) {
div_decn(); div_decn();
} }
void pi_decn(void) {
set_dec80_zero(&BDecn);
BDecn.lsu[0] = 5; // 0.5 00 ..
copy_decn(&AccDecn, &DECN_2PI);
mult_decn();
}
static void set_str_error(void){ static void set_str_error(void){
Buf[0] = 'E'; Buf[0] = 'E';

View File

@ -92,6 +92,7 @@ void tan_decn(void);
void arctan_decn(void); void arctan_decn(void);
void to_degree_decn(void); void to_degree_decn(void);
void to_radian_decn(void); void to_radian_decn(void);
void pi_decn(void);
//Buf should hold at least 18 + 4 + 5 + 1 = 28 //Buf should hold at least 18 + 4 + 5 + 1 = 28
#define DECN_BUF_SIZE 28 #define DECN_BUF_SIZE 28

View File

@ -307,7 +307,7 @@ int main()
switch(KEY_MAP[I_Key]){ switch(KEY_MAP[I_Key]){
////////// //////////
case '0': { case '0': {
if (IsShifted){ if (IsShiftedUp){
//off //off
TURN_OFF(); TURN_OFF();
} else { } else {
@ -343,7 +343,7 @@ int main()
case '7': //fallthrough case '7': //fallthrough
case '8': //fallthrough case '8': //fallthrough
case '9': { case '9': {
if (IsShifted){ if (IsShiftedUp || IsShiftedDown){
finish_process_entry(); finish_process_entry();
} else if ( EnteringExp >= ENTERING_EXP){ } else if ( EnteringExp >= ENTERING_EXP){
if ( Exp_i == 0){ if ( Exp_i == 0){
@ -368,7 +368,7 @@ int main()
} break; } break;
////////// //////////
case '.': { case '.': {
if (IsShifted){ if (IsShiftedUp || IsShiftedDown){
//STO //STO
finish_process_entry(); finish_process_entry();
} else { } else {
@ -391,7 +391,7 @@ int main()
} break; } break;
////////// //////////
case '=': { case '=': {
if (IsShifted){ //RCL if (IsShiftedUp || IsShiftedDown){ //RCL
finish_process_entry(); finish_process_entry();
} else { //Enter } else { //Enter
//track stack lift //track stack lift
@ -401,9 +401,10 @@ int main()
} break; } break;
////////// //////////
case 'c': { case 'c': {
if (IsShifted || is_entering_done()){ if (IsShiftedUp || IsShiftedDown || is_entering_done()){
//clear //clear
IsShifted = 0; IsShiftedUp = 0;
IsShiftedDown = 0;
NoLift = 1; NoLift = 1;
entering_done(); entering_done();
EnteringExp = ENTERING_DONE_CLEARED; EnteringExp = ENTERING_DONE_CLEARED;
@ -521,12 +522,15 @@ int main()
LCD_ClearToEnd(1); LCD_ClearToEnd(1);
//print shifted status //print shifted status
if (IsShifted){ if (IsShiftedUp){
TERMIO_PutChar('^'); TERMIO_PutChar('^');
#if defined(STACK_DEBUG) && defined(SHOW_STACK) #if defined(STACK_DEBUG) && defined(SHOW_STACK)
TERMIO_PutChar(' '); TERMIO_PutChar(' ');
TERMIO_PrintU8(stack_max); TERMIO_PrintU8(stack_max);
TERMIO_PutChar(' ');
#endif #endif
} else if (IsShiftedDown){
TERMIO_PutChar('v');
} }
#ifdef DESKTOP #ifdef DESKTOP