-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrpn.cpp
398 lines (382 loc) · 7.65 KB
/
rpn.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#include "ec_conf.h"
#include "rpn.h"
extern bool sleep_req;
extern bool stop_req;
rpn::rpn(void)
{
altFn = alt_Norm;
lastx=0;
//push_en=true;
deg_en=true;
hex_en=false;
stack_index=0;
sci_en = false;
power_en = true;
bri=100;
}
void rpn::begin(display &dev)
{
PrDev=&dev;
todeg=f64(90)/pio2;//this doesn't work in the constructor!
show_stack();
}
void rpn::key_input(char key,char ch)
{
if(altFn&alt_Bri){
altFn = alt_Norm;
bri=((key-'0')%10*27+12);
analogWrite(BACKLIGHT_PIN, bri);
show_stack();
}
else if(altFn&(alt_Sto|alt_Rcl)){
if(ch!='.'){
if(altFn&(alt_Sto))sto(ch);
else rcl(ch);
}
altFn&=~(alt_Sto|alt_Rcl);
show_stack();
}
else if((altFn&alt_Edit))key_edit(key);
else{
PrDev->noCursor();
PrDev->noBlink();
if(altFn&alt_Shift)key_shift(key);
else key_norm(key);
}
}
void rpn::key_norm(char key)
{
f64 undo_lx=lastx;
const f64 log10 = f64(0x40026bb1,0xbbb55515); //log_e(10)
if(key!='?' && !(altFn&alt_Sto)){
busy();
lastx = stackx;
}
switch(key){
case 'E'://exp
case '.':
case '0' ... '9':
if(!(altFn&alt_noPush) && !(altFn&alt_Fix))stack_push(); //some things disable edit push
PrDev->lcdprint(stacky.toString(),1);
PrDev->lcdclear(0);
key_edit(key);
return;
case '+':
stackx+=stack_pull();
break;
case '-':
stackx-=stack_pull();
break;
case '*':
stackx*=stack_pull();
break;
case '/':
stackx/=stack_pull();
break;
case 'a':
stackx=f64(1)/stackx;
break;
case 'b':
stackx=sqrt64(stackx);
break;
case 'c':
if(!stackx.isZero() && stacky.isZero()){
stack_pull();
stackx=0;
}
else stackx=exp64(stack_pull() * log64(stacky));
break;
case 'd':
stackx=log64(stackx);
break;
case 'e':
stackx=log64(stackx)/log10;//log64(f64(10));
break;
case 'f':
stack_pull();
break;
case 'g':
stack_swapxy();
break;
case 'h':
//altFn&=~alt_Hyp;
//stackx=altFn&alt_Hyp?sinh64(stackx):sin64(stackx/todeg);
stackx=sin64(stackx/todeg);
break;
case 'i':
//altFn&=~alt_Hyp;
//stackx=altFn&alt_Hyp?cosh64(stackx):cos64(stackx/todeg);
stackx=cos64(stackx/todeg);
break;
case 'j':
//altFn&=~alt_Hyp;
//stackx=altFn&alt_Hyp?tanh64(stackx):tan64(stackx/todeg);
stackx=tan64(stackx/todeg);
break;
case '_':
stackx=-stackx;
break;
case '\177': //pc raw backspace
case '\010': //backspace
case '\\':
stackx = 0;
altFn|=alt_noPush; //backspace disables push on next edit or rcl
break;
case '?':
altFn ^= alt_Shift;
lastx = undo_lx;
break;
case 'S':
PrDev->lcdprint("STO");
altFn |= alt_Sto;
return;
case 'R':
PrDev->lcdprint("RCL");
altFn |= alt_Rcl;
return;
case '!':
sleep_req^=1;
if(!sleep_req){
PrDev->command(LCD_DISPLAYCONTROL|LCD_DISPLAYON);
analogWrite(10,bri);
break;
}
/*
power_en ^= 1;
if(power_en){
}
else{
}
*/
return;
break;
case CR:
stack_push();
break;
case 'X':
lastx = undo_lx;
hex_en ^= 1;
lastx.setBase(hex_en?16:10);
break;
default:
break;
}
show_stack();
}
void rpn::key_shift(char key)
{
f64 undo_lx = lastx;;
if(key!='a' && key!='?' && !(altFn&alt_Sto)){
busy();
lastx = stackx;
}
switch(key){
case 'a':
stack_push();
stackx = lastx;
break;
case 'b':
stackx = stackx*stackx;
break;
case 'c':
stackx = exp64((f64(1)/stack_pull()) * log64(stacky));
break;
case 'd':
stackx=exp64(stackx);
break;
case 'e':
stackx=exp64(stack_pull()*log64(10));
break;
case 'f':
stack_push();
stackx=f64( 0x3FF921FB, 0x54442D18)*f64(2);
case '_':
stackx=stackx.fabs();
break;
case 'E':
stackx=stackx.intval();
break;
case '+':
PrDev->lcdprint("Fix:");
altFn = alt_Fix;
key_edit(0);
return;
case '!':
PrDev->lcdprint("BRI:");
altFn |= alt_Bri;
return;
#ifdef EXTRA_FN
case 'g':
lastx = undo_lx;
altFn ^= alt_Hyp;
break;
case '/':
//stackx%=stack_pull();
stackx=stacky.ipart()%(long)(stack_pull()); //32-bit version saves ram
break;
case 'h':
stackx=altFn&alt_Hyp?asinh64(stackx):asin64(stackx)*todeg;
break;
case 'i':
stackx=altFn&alt_Hyp?acosh64(stackx):acos64(stackx)*todeg;
break;
case 'j':
stackx=altFn&alt_Hyp?atanh64(stackx):atan64(stackx)*todeg;
break;
case '7':
stackx=stack_pull()==stackx;
break;
case '8':
stackx=stack_pull()<stackx;
break;
case '9':
stackx=stack_pull()<=stackx;
break;
case '4':
//->R
break;
case '5':
//->P
break;
case '6':
//random
break;
case '*':
stackx=stackx==0;
break;
case '1':
//->HMS
break;
case '2':
//HMS->
break;
case '3':
//seed
break;
case '-':
stackx=(stackx<0);
break;
case '+':
stackx=(stackx<=0);
break;
case 'B':
Serial.print(stackx.bits(1),16);
Serial.println(stackx.bits(0),16);
Serial.println();
break;
#else
case 'g': // deg/rad
lastx = undo_lx;
deg_en=!deg_en;
if(deg_en)todeg=f64(90)/f64( 0x3FF921FB, 0x54442D18);
else todeg=1;
break;
#ifndef TEST_SMALL
case 'h':
stackx=asin64(stackx)*todeg; //no hyp functions
//stackx=altFn&alt_Hyp?asinh64(stackx):asin64(stackx)*todeg;
break;
case 'i':
stackx=acos64(stackx)*todeg;
//stackx=altFn&alt_Hyp?acosh64(stackx):acos64(stackx)*todeg;
break;
case 'j':
stackx=atan64(stackx)*todeg;
//stackx=altFn&alt_Hyp?atanh64(stackx):atan64(stackx)*todeg;
break;
#endif
case '*': //ENG
eng_en ^= 1;
sci_en = 0;
goto expo;
case '-': //SCI
sci_en ^= 1;
eng_en = 0;
expo:
lastx.setExpMax(sci_en|eng_en?0:14,eng_en);
break;
case CR:
case '0' ... '9':
case '?':
key_norm(key);
return;
case '/':
stackx=int32_t(stacky.ipart()%stack_pull().ipart()); //integer version saves ram
break;
case '.':
stop_req=true;
break;
/*
case 'B':
Serial.print(stackx.bits(1),16);
Serial.println(stackx.bits(0),16);
Serial.println();
break;
//*/
#endif
default:
lastx=stackx;
break;
}
altFn = alt_Norm;
show_stack();
}
void rpn::show_flags(void)
{
PrDev->home();
PrDev->print(hex_en?'x':deg_en?' ':'r');
PrDev->setCursor(0,1);
PrDev->print(altFn&alt_Shift?'^':' ');
}
void rpn::show_stack(void)
{
int i,j;
if(altFn&alt_Edit)return;
PrDev->noCursor();
PrDev->noBlink();
show_flags();
PrDev->lcdprint(stacky.toString(),1);
PrDev->lcdprint(stackx.toString());
#if !defined(ARDUINO)
Serial.println();
Serial.println("----------------");
for(i=0,j=stack_index+STACK_TOP;i<STACK_DEPTH;i++,j--){
Serial.println(stack[j%STACK_DEPTH]);
}
#endif
}
void rpn::stack_push(void)
{
stack_index+=STACK_TOP;
stack_index%=STACK_DEPTH;
stackx = stacky;
}
f64 rpn::stack_pull(void)
{
stack_index++;
stack_index%=STACK_DEPTH;
//stackt = stacks; //duplicate the stack top
stackt = 0; //zero stack top
return lastx;
}
void rpn::stack_swapxy(void)
{
f64 z=stacky;
stacky=stackx;
stackx=z;
}
void rpn::busy(void)
{
PrDev->clear();
PrDev->lcdprint(" ...",1);
}
void rpn::sto(char key)
{
stovars[key-'a'] = stackx;
}
void rpn::rcl(char key)
{
if(!(altFn&alt_noPush))stack_push();
altFn=alt_Norm;
stackx = stovars[key-'a'];
}