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
uint8_t NoLift = 0;
__bit IsShifted = 0;
__bit IsShiftedUp = 0;
__bit IsShiftedDown = 0;
//stack "grows" towards 0
__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){
//turn off backlight before start of processing
backlight_off();
@ -99,12 +96,12 @@ void process_cmd(char cmd){
switch(cmd){
//////////
case '+':{
if (IsShifted){ // LastX
if (IsShiftedUp){ // LastX
if (NoLift != 1){
StackPtr--;
}
copy_decn(&stack(STACK_X), &LastX);
IsShifted = 0;
IsShiftedUp = 0;
} else { // +
do_binary_op(add_decn);
}
@ -115,22 +112,36 @@ void process_cmd(char cmd){
} break;
//////////
case '-':{
negate_decn(&stack(STACK_X));
do_binary_op(add_decn);
negate_decn(&LastX); //stored LastX was after negation of X
if (IsShiftedUp) {
do_unary_op(to_radian_decn);
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;
//////////
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;
//////////
case '=':{
if (IsShifted){ //RCL
if (IsShiftedUp){ //RCL
if (NoLift != 1){
StackPtr--;
}
copy_decn(&stack(STACK_X), &StoredDecn);
IsShifted = 0;
} else { //Enter
if (!decn_is_nan(&stack(STACK_X))){
StackPtr--;
@ -140,9 +151,8 @@ void process_cmd(char cmd){
} break;
//////////
case '.':{
if (IsShifted){ //STO
if (IsShiftedUp){ //STO
copy_decn(&StoredDecn, &stack(STACK_X));
IsShifted = 0;
}
} break;
//////////
@ -151,8 +161,8 @@ void process_cmd(char cmd){
} break;
//////////
case '<':{ //use as +/- and sqrt
if (IsShifted){ //take sqrt
IsShifted = 0;
if (IsShiftedUp){ //take sqrt
IsShiftedUp = 0;
if (decn_is_zero(&stack(STACK_X))){
//sqrt(0) = 0
} else if (!decn_is_nan(&stack(STACK_X))){
@ -176,8 +186,7 @@ void process_cmd(char cmd){
} break;
//////////
case 'r':{ //use as swap and 1/x
if (IsShifted){ //take 1/x
IsShifted = 0;
if (IsShiftedUp){ //take 1/x
do_unary_op(recip_decn);
} else { // swap
if (!decn_is_nan(&stack(STACK_X))){
@ -190,41 +199,70 @@ void process_cmd(char cmd){
} break;
//////////
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;
//////////
case '4':{
if (IsShifted){ //roll down
if (IsShiftedUp){ //roll down
StackPtr++;
IsShifted = 0;
}
} break;
//////////
case '5':{
if (IsShifted){ //e^x
if (IsShiftedUp){ //e^x
do_unary_op(exp_decn);
IsShifted = 0;
}
} break;
//////////
case '6':{
if (IsShifted){ //10^x
if (IsShiftedUp){ //10^x
do_unary_op(exp10_decn);
IsShifted = 0;
}
} break;
//////////
case '9':{
if (IsShifted){ //log10(x)
if (IsShiftedUp){ //log10(x)
do_unary_op(log10_decn);
IsShifted = 0;
}
} break;
//////////
case '8':{
if (IsShifted){ //ln(x)
if (IsShiftedUp){ //ln(x)
do_unary_op(ln_decn);
IsShifted = 0;
}
} break;
//////////
@ -239,10 +277,11 @@ void process_cmd(char cmd){
copy_decn(&stack(STACK_Y), &AccDecn);
}
pop();
IsShifted = 0;
} break;
//////////
} //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
void push_decn(__xdata const char* signif_str, __xdata exp_t exponent);
extern uint8_t NoLift;
extern __bit IsShifted;
extern __bit IsShiftedUp;
extern __bit IsShiftedDown;
void clear_x(void);
__xdata dec80* get_x(void);

View File

@ -1393,6 +1393,13 @@ void to_radian_decn(void) {
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){
Buf[0] = 'E';

View File

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

View File

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