-
Notifications
You must be signed in to change notification settings - Fork 8
/
iv_1_0.ino
2969 lines (2744 loc) · 103 KB
/
iv_1_0.ino
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
____________________
Welcome to Ivee (IV) - A Powerful Programable RPN Calculator based on FORTH
Version 1.0, (c) 2020 by deetee/zooxo
This software is covered by the 3-clause BSD license.
See also: https://github.com/zooxo/iv and https://youtu.be/m1aFRhqvuLM
____________________
____________________
PREAMBLE
____________________
Ivee is a powerful programable RPN calculator based on the programming
language FORTH. This innovative and powerful machine benefits of a brilliant
symbiosis, as both - the RPN calculator and FORTH - are stack based systems.
The name Ivee or IV stands for the roman number 4, which was also a basis for
naming Forth (4th generation of programming languages).
Ivee complements a viable set of Forth words (26 commands) with a set of
system oriented commands and additional built-in Forth words. In addition
the user can define/program new words based on existing words and commands.
The hardware is simple:
- Arduino Pro Micro
- OLED display (128x64 pixel) with SSD1306- or SSD1309-controller
- 16 keys (push buttons)
optional: - LIPO battery
- LIPO battery charger (TP4056)
- ON/OFF-switch
Allthough Ivee is operated by 16 keys only it offers a wide range of functions
and possibilities:
- 120 intrinsic functions based on FORTH
- Programming: Handle up to 40 user definable programs
(Edit, New, Rename, Move, Delete, Send, Load)
- Dictionary of all commands (sorted), words and programs
- Fast user definable menu (longpressed F-key)
- Calculations with complex numbers for nearly every function
- Calculus (f(x), plot, solve, integrate)
- Basic math operations in number systems with selectable BASE
- Business calculator mode (accuracy of 18 digits)
- Save up to 10 numbers/constants permanently
- Statistics and line best fit
- Many unit conversions
- Clock and Torch function
- Basic system functions (Battery voltage, Brightness, Screensaver)
Have fun!
deetee
____________________
KEYBOARD, SHORTCUTS
____________________
F(G)[OFF]{MENU} 7(S+)[SCLR] 8(PRG)[BASE] 9(/)[MOD]
E(SWAP)[ROT] 4(DICT)[USR] 5(FX)[FPLOT] 6(*)[LIT]
N(OVER)[PICK] 1(RCL)[STO] 2(SOLVE)[FINT] 3(-)[LIT-]
X(CLR)[TOFF] 0(CPLX)[BATT] .(R<>P)[CLK] #(+)[LIT+]
PRG Shortcuts: BASE/HEX Shortcuts:
5 OUT 6 UP 4 E 5 F
1 NEW 2 IN 3 DOWN 1 B 2 C 3 D
0 REN . DEL D EDIT 0 A . A~F
MENU/DICT/REN Navigation: SUM/STAT/L.R. Storage Registers:
Q1 Q2 Q3 Q4 7 SN 8 SX 9 SY
UP E 4 5 6 5 SXX 6 SXY
DOWN N 1 2 3
PREV0 .NEXT
____________________
DISPLAY, INDICATORS
____________________
> ... Empty stack, ready to calculate
> ... Indicates selected program in program selection mode
< ... Indicates selected program step in program editing mode
a~z ... Stack level
_ ... Indicates number input
i, angle ... Imaginary part, imaginary angle
B ... BASE mode indicator
n BASE ... Numeric base of BASE mode
! ... Indicates float of data stack (data loss in lowest stack level)
^, v ... F-key, G-key
____________________
LIMITS
____________________
As a microprocessor is primarily not made to do such complex things like
performing a powerful calculator there are some limits in performance and
resources.
Most obviously is the limited precision of the intrinsic float format (IEEE
754, 32 bit). As four bytes only are used to represent a float respective
double number the decimal digits of precision is limited to 6...7. For
example calculating sqrt(-1) yields in a display of "7.5E-8 + i" (instead of
"0 + i").
In addition the resources of a microcontroller are limited like the FLASH
memory (holds the executable program code), the RAM memory (holds variables
and data while running) and the EEPROM (holds permanent data like settings or
user programs).
However Ivee tries to offer a maximum of features, comfort and performance
with a minimum of required resources.
LIMITS:
26 ... Maximal data stack size (a...z)
7 ... Maximum number of displayed significant digits of a number
36 ... Maximum number of decimal exponent digits (1E-37 < X < 1E37)
12 ... Maximal size of text display (see EMIT, CTX)
10 ... Maximal amount of (complex) numbers saved permanently (0...9)
3 ... Maximal number of characters for naming an user program
40 ... Maximal number of user programs
128 ... Maximal size of an user program (steps)
900 ... Maximal size (steps) of all user programs
64 ... Maximal size of address stack ("deep of nesting")
1E-4 ... X-range of solver (Newton) to determine slope of function
10 ... Calculation "stripes" for integrating (Simpson)
32 ... Maximal definable command slots of user menu
____________________
BROWSING MENUS
____________________
To navigate through the menu of some functions (MENU, DICT, USR, renamePRG)
all selectable items are divided into four sections. Every section has its
own up and down key (section I: E/N, section II: 4/1, section III: 5/2 and
section IV: 6/3).
To move to the previous or next entry (regardless which section is selected)
the keys "0" or "." can be used. To select one of the four presented items
use the appropriate function key (F/7/8/9) or escape the menu with "X".
____________________
PROGRAMMING
____________________
Ivee is able to deal with up to 40 user programs with a total number of
900 steps/commands. To deal with programs enter the program selector mode
with PRG.
While in the program selection mode on the left side of the display the
available programs are numbered and listed. On the right side of the display
the number of programs, the memory used and the memory available are shown.
To navigate between programs use the cursor keys E (up) and N (down).
Edit a selected program with # or press 1 to enter a new program.
To delete a program press "." - to rename it use the key 0.
You can move a program with 6 up or with 3 down. Note that the top program
has special calculus features (f(x), plot, solve, integrate).
To send a program (does not include the program name) via USB to another
computer press "5". Note that corresponding ascii characters will be sent
(see chapter DICTIONARY, second column). In a similar way programs can be
send from another computer to Ivee (press key 2) and will be stored as new
program.
With the 3 powerful commands @, ! and EXE (see chapter DICTIONARY) it is
even possible to manipulate the program memory and execute code.
A program is structured into the three parts NAME, COMMANDS and EOP-marker.
Please note that the length of the program name is restricted to 3
characters and the maximum number of program steps is restricted to 128.
____________________
BASE, BUSINESS MODE
____________________
To calculate with integer values with other numeric base enter the base and
select the BASE command (ie 16 BASE to calculate with hexadecimal numbers).
To return to the scientific mode press BASE again. When changing the mode
the whole stack will be converted (as far as appropriate) to the new base.
Numbers will be displayed in groups of four digits in two lines with a
B-indicator (BASE) on the left side and the base itself on the right side of
the first line. Pressing "E" enters four zeros.
Note that only basic operations (/*-+) and some stack operations are
supported. To enter digits bigger than 9 (ie F) press "." followed by a key
(ie 5 to enter F).
The BASE state will be stored permanently.
Entering the BASE mode with 10 activates the business mode to deal with big
numbers and high accuracy, like trillions of dollars with cent accuracy.
Numbers are grouped into triples and two digits after the decimal dot
(cents) are supported.
____________________
CALCULUS
____________________
Ivee is able to analyze the first user defined program. Note that you can
move the desired user program to the first position using the keys 6 or 3
in program mode (PRG).
To calculate the value of a function use F(X).
To find the root of a function enter a start value (X) and press FSOLVE.
Enter the x-range (from Y to X) before plotting a function (FPLOT) or
calculating an integral (FINT).
____________________
COMMANDS
____________________
MEMORY
functions mem[] EEPROM
|<--intrinsic-->|<--builtin-->|<--user-->|
0 76 120 160
MAXCMDI^ MAXCMDB^ MAXCMDU^
OVERVIEW
Intrinsic
Forth DUP DROP NEGATE / * - + MOD DICT SWAP ROT PICK
< = <> > KEY EMIT BASE BEGIN UNTIL IF ELSE THEN @ !
System 0~9 . E STO RCL CLR USR NAND INTEGER PI INV SIN EXP LN
COMPLEX REC<>POL F(X) FPLOT FSOLVE FINTEGRATE
PRG T+ PSE EXE BREAK CLRTEXT ISREAL
SUM+ SUM- SUMCLR COMB PERM STAT LR
BATT LIT LIT+ LIT- CLOCK OFF TOFF
Builtin
Forth OVER HEX AND NOT OR ABS
Other $ SQRT POWER POWER10 LOG LN! PV ND QE HMS>H H>HMS CLOCK
COS TAN ASIN ACOS ATAN SINH COSH TANH ASINH ACOSH ATANH
DEG>RAD RAD>DEG C>F F>C KM>MI MI>KM M>FT FT>M CM>IN IN>CM
DICTIONARY (in order of "appearence")
000 0 ... Numbers
001 ! 1
002 " 2
003 # 3
004 $ 4
005 % 5
006 & 6
007 ' 7
008 ( 8
009 ) 9
010 * . ... Dot key
011 + DUP ... Duplicate, end number input
012 , DROP ... Drop X, clear entry
013 - N ... NEG, change sign (negate X)
014 . E ... Enter 10th exponent (actually Y*10^X)
015 / F ... Function key (Shift)
016 0 CPLX ... Enter complex number and vice versa (see R<>P)
017 1 RCL ... Recall memory (number = X) from permanent memory. Note that
the memories 5...9 are used as statistic registers.
018 2 FSOLV ... Find the root of the first user defined program/function
(PRG) using X as start value
019 3 - ... Substract (X = Y-X)
020 4 DICT ... Catalog all functions (see section BROWSING MENUS)
021 5 F(X) ... Calculate first user defined program/function (PRG)
022 6 * ... Multiply (X = Y*X)
023 7 S+ ... Add values of X- and Y-register to sums (see STAT/LR)
024 8 PRG ... Enter program mode (see section PROGRAMMING)
025 9 / ... Divide (X = Y/X)
026 : R<>P ... Toggle rectangular or polar mode (see CPLX)
027 ; + ... Addition (X = Y+X)
028 < CLR ... Clear stack and alpha register
029 = OVER ... Push/copy Y to stack (YX -> YXY)
030 > SWAP ... Swap last two stack contents (XY -> XY)
031 ? G ... Second function key (Shift-Shift)
032 @ BATT ... Show battery voltage (push to stack)
033 A STO ... Store Y memory (number = X). Note that the memories 5...9
are used as statistic registers (Sxx, Sxy, n, Sx, Sy).
034 B FINT ... Integrate the first user defined program (PRG) using
the x-range from Y to X
035 C B- ... Lower the brightness of the display (5 levels). Note that
the display has to be rebooted (needs two seconds).
036 D USR ... Set function key of (custom) user MENU
037 E FPLOT ... Plot the first user defined program (PRG) using the the
x-range from Y to X. Escape with key "X".
038 F LIT ... Illuminate the whole display
039 G SCLR ... Clear all sum register. Note that statistic functions are
using the permanent memories 5...9.
040 H BASE ... Set number base for integer calculations (i.e. 2, 8, 16).
Note that setting the base to 10 enters the business
calculator mode (see chapter BASE, BUSINESS MODE).
041 I MOD ... Modulo, division remainder (X = Y%X)
042 J CLOCK ... Simple clock. To set the time use hh.mmss.
043 K LIT+ ... Rise the brightness of the display (5 levels). Note that
the display has to be rebooted (needs two seconds).
044 L TOFF ... Set the screen off time (x 10 s) (>=3)
045 M PICK ... Copy n-th number of stack to stack
046 N ROT ... Rotate 3 numbers of stack (ZYX -> YXZ)
047 O DARK ... Enter screen off manually (triple press F)
048 P < ... Condition less than (returns true, if Y<X)
049 Q = ... Condition equal (returns true, if Y=X)
050 R <> ... Condition not equal (returns true, if Y<>X)
051 S GT ... Condition greater than (returns true, if Y>X)
052 T NAND ... Logical NAND (used for other logical functions)
053 U T+ ... Adds duration in ms to stack (since last call)
054 V PSE ... Pause program execution for X*250 ms and print screen
055 W INT ... Calculate integer value of X
056 X @ ... Fetch the value from address (X) (see also ! and EXE)
057 Y ! ... Store Y at X-address (see also @ and EXE)
058 Z BEGIN ... Begin a BEGIN-UNTIL-Loop
059 [ UNTIL ... Continue executing a BEGIN-UNTIL-Loop until X is true
060 \ EXE ... Execute code at X-address (see also @ and !)
061 ] IF ... Execute the following code if true
062 ^ ELSE ... Execute the following code if IF failed
063 _ THEN ... Terminate an IF(-ELSE) condition
064 ` BREAK ... Pause code execution for input (key "X" resumes)
065 a KEY ... Wait for keypress and push the keycode to stack
066 b CTX ... Clear text area (written with EMIT)
067 c EMIT ... Emit ascii character of X
068 d RE? ... Return true, if X is a real value
069 e nCr ... Calculate the combination (of yCx)
070 f nPr ... Calculate the permutation (of yPx)
071 g PI ... Push the value of PI to stack
072 h INV ... Push the reciprocal value of X to stack
073 i SIN ... Push the sine of X to stack
074 j EXP ... Push the exponential value of X to stack
075 k LN ... Push the natural logarithm of X to stack
076 l $ ... Activate the business calculator mode (10 BASE)
077 m HEX ... Activate HEX mode (16 BASE)
078 n AND ... Logical AND
079 o NOT ... Logical NOT
080 p OR ... Logical OR
081 q OVER ... Push/copy Y to stack (YX -> YXY)
082 r ABS ... Push the absolute value of X to stack
083 s SQRT ... Push the quare root of X to stack
084 t COS ... Push the cosine of X to stack
085 u TAN ... Push the tangent of X to stack
086 v POW ... Push Y raised to the power of X to stack (X = Y^X)
087 w 10^ ... Push 10 raised to the power of X to stack (X = 10^X)
088 x LOG ... Push the 10 based logarithm of X to stack
089 y ASIN ... Push the arcus (inverse) sinus of X to stack
090 z ACOS ... Push the arcus (inverse) cosine of X to stack
091 { ATAN ... Push the arcus (inverse) tangent of X to stack
092 | SINH ... Push the hyperbolic sine of X to stack
093 } COSH ... Push the hyperbolic cosine of X to stack
094 ~ TANH ... Push the hyperbolic tangent of X to stack
095 ? ASINH ... Push the area (inverse) hyperbolic sine of X to stack
096 ? ACOSH ... Push the area (inverse) hyperbolic cosine of X to stack
097 ? ATANH ... Push the area (inverse) hyperbolic tangent of X to stack
098 ? LN! ... Push the natural logarithm of gamma of X to stack
099 ? >h ... Convert hh.mmss to h (HMS->H)
100 ? h> ... Convert h to hh.mmss (H->HMS)
101 ? PV ... Present value of given interest rate and periods
102 ? ND ... PDF (X) and CDF (Y) of standard normal distribution
103 ? QE ... Quadratic equation xx+Yx+X=0 X|Y=-Y/2+-sqrt(Y*Y/4-X)
104 ? CLOCK ... Simple clock (set with hh.mmss)
105 ? SCLR ... Clear statistic registers (permanent memories 5...9)
106 ? S+ ... Add values of X- and Y-register to sums (see STAT/LR)
107 ? S- ... Substract values of X- and Y-register to sums (see STAT/LR)
108 ? STAT ... Mean value (X) and standard deviation (Y). Note that the
permanent memories 5...9 (see RCL/STO) are used as statistic
registers (Sxx, Sxy, n, Sx, Sy).
109 ? LR ... Line best fit (y = X * x + Y)
110 ? % ... Percent (X/Y*100%)
111 ? %CHG ... Percent change (X-Y)/Y*100%
112 ? FRAC ... Fractional part
113 ? ><d ... Convert degrees to radians (and vice versa)
114 ? ><C ... Convert Celsius to Fahrenheit (and vice versa)
115 ? ><km ... Convert kilometer to miles (and vice versa)
116 ? ><m ... Convert meter to feet (and vice versa)
117 ? ><cm ... Convert centimeter to inches (and vice versa)
118 ? ><kg ... Convert kilogram to lbs (and vice versa)
119 ? ><l ... Convert liter to gallons (and vice versa)
____________________
EEPROM
____________________
ADDRESS BYTES PURPOSE
0 ... 0 1 Brightness
1 ... 1 1 Screen off time (*250 ms)
2 ... 2 1 Number base (BASE)
3 ... 82 80 Memory area for RCL/STO (real and imaginary numbers)
83 ... 90 8 Memory area for RCL/STO (integer number in BASE mode)
91 ... 122 32 Memory area for user selectable MENU
123 ... 1023 901 User program memory area
____________________
MEMORY, STACKS
____________________
| sizeof(mem)
_ _ _ _ _ _ _ _ _ _ _ v
mem[] |_|_|_|_|_ _ _ _ _|_|_|_|
|<--builtinprograms-->|
| ^ | | sizeof(mem) +
0|1 mp | | sou=sizeofusr()
v |_ _ _ _ _ _ _ _ _ _ v _ _ _ _ _ _ _ _
EEPROM[] |_|_|_|_ _ _ _ _|_|_|_|_|_|_|_|_|_|_|_|
|<---user-programs--->|<-- @ ! EXE -->|
|^EEUSTART EEUEND^|
|<------- sou ------->| |
|<------------ 901 bytes ------------>|
DATASTACK ADDRESSSTACK CONDITIONAL
(2 x double, int64) (int) POINTER
| | | | | |
|______|______|_______| |_____| <cp (byte)
|______|______|_______|<dp (byte) |_____|
|______|______|_______| |_____|<ap (int)
|__re__|__im__|_int64_| |_____|
ds[] as[]
____________________
CIRCUIT DIAGRAM
____________________
_________________________________
| OLED-DISPLAY 128x64 SPI SSD1309 |
|____GND_VCC_SCL_SDA_RES_DC_CS____|
| | | | | | |
_________|___|___|___|___|___|___|________
| GND VCC 15 16 A1 A2 GND |
| 19 20 |
| PF6 PF5 |
| |
| ARDUINO PRO MICRO |
| |
|__GND__2___8___3___0___1___5___7___6___9__|
| | | | | | | | | |
| | | | | | | | | |
| F | 7---8---9---+ | | |
|___| | | | | | | | 4x4 Keyboard
E---4---5---6-------+ | | (F-key on interuptable
| | | | | | pin to enable sleep mode)
N---1---2---3-----------+ |
| | | | |
X---0---.---#---------------+
(with LIPO-Battery):
_______/ _______
| OnOff-Switch |
| __________ | __
| | | | |
+----+--|BATT+ IN+|--+---VCC-|
_|_ | | | Arduino
= | | | Pro Micro
+-------|BATT- IN-|------GND-|
|__________| |__
LIPO LIPO Battery
Battery Charger (TP4056)
____________________
ASCII TABLE:
____________________
DEC | 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
HEX | 0 1 2 3 4 5 6 7 8 9 a b c d e f
------------------------------------------
032 20 | ! " # $ % & ' ( ) * + , - . /
048 30 | 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
064 40 | @ A B C D E F G H I J K L M N O
080 50 | P Q R S T U V W X Y Z [ \ ] ^ _
096 60 | ` a b c d e f g h i j k l m n o
112 70 | p q r s t u v w x y z { | } ~
____________________
PHYSICAL CONSTANTS
____________________
Constant ID Name
365.2425 A Gregorian year
5.291772E-11 Ao Bohr radius
384.4E6 Am Semi-major axis of the Moon's orbit
1.495979E11 Ae Semi-major axis of the Earth's orbit
2.997942E8 c Speed of light
3.741772E-16 C1 First radiation constant
0.01438777 C2 Second radiation constant
1.602177E-19 e Electron charge
96485.34 F Faraday constant
2.502908 Fa Feigenbaum's alpha
4.669202 Fd Feigenbaum's delta
9.80665 g Standard earth accelleration
6.674083E-11 G Newtonian constant of gravitation
7.748092E-5 Go Conductance quantum
0.9159656 Gc Catalan's constant
-2.002232 Ge Lande's electron g-factor
6.626069E-34 h Planck constant
1.38065E-23 k Boltzmann constant
4.835979E14 Kj Josephson constant
1.616199 lP Planck length
9.109383E-31 me Electron mass
7.349E22 mM Mass of the Moon
1.674927E-27 mn Neutron mass
1.672622E-27 mp Proton mass
2.17651E-8 mP Planck mass
1.660539E-27 mu Atomic mass unit
1.492417E-10 Muc Energy equivalent of atomic mass unit
1.883541E-28 mm Muon mass
1.9891E30 mS Mass of the Sun
5.9736E24 mE Mass of the Earth
6.022141E23 NA Avogadro's number
101325 po Standard atmospheric pressure
1.875546E-18 qP Planck charge
8.314472 R Molar gas constant
2.81794E-15 re Electron radius
25812.81 RK Von Klitzing constant
1.73753E6 RM Mean radius of the Moon
1.097373E7 Ryd Rydberg constant
6.96E8 rS Mean radius of the Sun
6.37101E6 rE Mean radius of the Earth
273.15 To Standard temperature
5.39106E-44 tP Planck time
1.416833E-32 TP- Planck temperature
0.02241397 Vm Molar volume of an ideal gas
376.7303 Zo Impedance of vacuum
7.297353E-3 _A Fine structure constant
0.5772157 _GE Euler-Mascheroni constant
2.675222E8 _GP Proton gyromagnetic ratio
8.854188E-12 _Eo Electric constant or vacuum permitivity
2.42631E-12 _Le Compton wavelength of the electron
1.319591E-15 _Ln Compton wavelength of the neutron
1.32141E-15 _Lp Compton wavelength of the proton
1.256673E-6 _mo Magnetic constant or vacuum permeability
9.274009E-24 _mB Bohr's magneton
-9.284764E-24 _me Electron magnetic moment
-9.662364E-27 _mn Neutron magnetic moment
1.410607E-26 _mp Proton magnetic moment
5.050783E-27 _mu Nuclear magneton
-4.490448E-26 _mm Muon magnetic moment
5.670373E-8 _SB Stefan-Boltzmann constant
1.618034 _P Golden ratio
2.067834E-15 _Po Magnetic flux quantum
*/
// ***** I N C L U D E S
#include <avr/power.h> // Needed for power management
#include <avr/sleep.h> // Needed for sleeping
#include <EEPROM.h> // For saving data to EEPROM
// ***** F O N T S
#define FONT4 4
//#define FONT5 5
#define FONT6 6
// BIGNUMBERFONT 6x8
#define FONTBIGWIDTH 6
#define FONTOFFSETBIG '-'
const byte fontbig [] PROGMEM = {
0x00, 0x08, 0x08, 0x08, 0x08, 0x00, // -
0x60, 0x60, 0x00, 0x00, 0x00, 0x00, // .
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // / space
0x7f, 0x7f, 0x41, 0x41, 0x7f, 0x7f, // 0
0x40, 0x42, 0x7f, 0x7f, 0x40, 0x40, // 1
0x79, 0x79, 0x49, 0x49, 0x4f, 0x4f, // 2
0x41, 0x49, 0x49, 0x49, 0x7f, 0x7f, // 3
0x0f, 0x0f, 0x08, 0x08, 0x7f, 0x7f, // 4
0x4f, 0x4f, 0x49, 0x49, 0x79, 0x79, // 5
0x7f, 0x7f, 0x49, 0x49, 0x79, 0x78, // 6
0x03, 0x03, 0x01, 0x01, 0x7f, 0x7f, // 7
0x7f, 0x7f, 0x49, 0x49, 0x7f, 0x7f, // 8
0x0f, 0x4f, 0x49, 0x49, 0x7f, 0x7f, // 9
0x00, 0x00, 0x36, 0x36, 0x00, 0x00, // :
};
// NORMAL FONT 4x8
#define FONTWIDTH 4
#define FONTOFFSET ' '
const byte font4 [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, // space
0x00, 0x5f, 0x00, 0x00, // !
0x0b, 0x07, 0x0b, 0x07, // "
//0x3e, 0x14, 0x14, 0x3e, // #
0x6d, 0x11, 0x11, 0x6d, // ; # mean value
0x2e, 0x6a, 0x2b, 0x3a, // $
0x26, 0x12, 0x48, 0x64, // %
0x76, 0x49, 0x76, 0x60, // &
//0x00, 0x0b, 0x07, 0x00, // '
0x22, 0x72, 0x27, 0x22, // ' leftright
0x7f, 0x41, 0x41, 0x00, // (
0x00, 0x41, 0x41, 0x7f, // )
0x24, 0x18, 0x18, 0x24, // *
0x08, 0x1c, 0x08, 0x00, // +
//0x00, 0x58, 0x38, 0x00, // ,
0x10, 0x7f, 0x01, 0x01, // , squareroot
0x08, 0x08, 0x08, 0x08, // -
0x00, 0x60, 0x60, 0x00, // .
0x20, 0x10, 0x08, 0x04, // /
0x7f, 0x41, 0x41, 0x7f, // 0
0x00, 0x02, 0x7f, 0x00, // 1
0x79, 0x49, 0x49, 0x4f, // 2
0x41, 0x49, 0x49, 0x7f, // 3
0x0f, 0x08, 0x08, 0x7f, // 4
0x4f, 0x49, 0x49, 0x79, // 5
0x7f, 0x49, 0x49, 0x79, // 6
0x03, 0x01, 0x01, 0x7f, // 7
0x7f, 0x49, 0x49, 0x7f, // 8
0x4f, 0x49, 0x49, 0x7f, // 9
0x00, 0x36, 0x36, 0x00, // :
0x00, 0x5b, 0x3b, 0x00, // ;
0x08, 0x14, 0x22, 0x41, // <
0x14, 0x14, 0x14, 0x14, // =
0x41, 0x22, 0x14, 0x08, // >
0x03, 0x59, 0x09, 0x0f, // ?
0x3e, 0x41, 0x5d, 0x5e, // @
0x7f, 0x09, 0x09, 0x7f, // A
0x7f, 0x49, 0x4f, 0x78, // B
0x7f, 0x41, 0x41, 0x40, // C
0x41, 0x7F, 0x41, 0x7F, // D
0x7F, 0x49, 0x49, 0x41, // E
0x7F, 0x09, 0x09, 0x01, // F
0x7f, 0x41, 0x49, 0x79, // G
0x7F, 0x08, 0x08, 0x7F, // H
0x41, 0x7f, 0x41, 0x40, // I
0x60, 0x40, 0x40, 0x7f, // J
0x7F, 0x08, 0x0f, 0x78, // K
0x7F, 0x40, 0x40, 0x40, // L
0x7F, 0x07, 0x07, 0x7F, // M
0x7F, 0x06, 0x0c, 0x7F, // N
0x7f, 0x41, 0x41, 0x7f, // O
0x7F, 0x09, 0x09, 0x0f, // P
0x7f, 0x41, 0x61, 0x7f, // Q
0x7F, 0x09, 0x79, 0x4f, // R
0x4f, 0x49, 0x49, 0x78, // S
0x01, 0x7f, 0x01, 0x01, // T
0x7F, 0x40, 0x40, 0x7F, // U
0x1F, 0x70, 0x70, 0x1F, // V
0x7F, 0x70, 0x70, 0x7F, // W
0x77, 0x08, 0x08, 0x77, // X
0x4f, 0x48, 0x48, 0x7f, // Y
0x71, 0x49, 0x49, 0x47, // Z
//0x7f, 0x41, 0x41, 0x00, // [
//0x7f, 0x47, 0x47, 0x7f, // [ upper
0x02, 0x7f, 0x7f, 0x02, // [ arrow up
//0x04, 0x08, 0x10, 0x20, // backslash
0x2a, 0x1c, 0x1c, 0x2a, // backslash sun
//0x00, 0x41, 0x41, 0x7f, // ]
//0x7f, 0x71, 0x71, 0x7f, // ] lower
0x20, 0x7f, 0x7f, 0x20, // ] arrow down
0x02, 0x01, 0x01, 0x02, // ^
0x40, 0x40, 0x40, 0x40, // _
//0x00, 0x07, 0x0b, 0x00, // `
0x3c, 0x20, 0x28, 0x20, // ` angle
0x70, 0x54, 0x54, 0x7c, // a
0x7F, 0x44, 0x44, 0x7c, // b
0x7c, 0x44, 0x44, 0x44, // c
0x7c, 0x44, 0x44, 0x7F, // d
0x7c, 0x54, 0x54, 0x5c, // e
0x04, 0x7f, 0x05, 0x01, // f
0x5c, 0x54, 0x54, 0x7c, // g
0x7F, 0x04, 0x04, 0x7c, // h
0x40, 0x44, 0x7d, 0x40, // i
0x40, 0x40, 0x44, 0x7d, // j
0x7f, 0x10, 0x1c, 0x70, // k
0x01, 0x7f, 0x40, 0x40, // l
0x7C, 0x0c, 0x0c, 0x7c, // m
0x7C, 0x04, 0x04, 0x7c, // n
0x7c, 0x44, 0x44, 0x7c, // o
0x7c, 0x14, 0x14, 0x1c, // p
0x1c, 0x14, 0x14, 0x7c, // q
0x7C, 0x04, 0x04, 0x04, // r
0x5c, 0x54, 0x54, 0x74, // s
0x04, 0x7F, 0x44, 0x40, // t
0x7C, 0x40, 0x40, 0x7C, // u
0x1c, 0x60, 0x60, 0x1c, // v
0x7C, 0x60, 0x60, 0x7C, // w
0x6c, 0x10, 0x10, 0x6c, // x
0x5c, 0x50, 0x50, 0x7c, // y
0x74, 0x54, 0x54, 0x5c, // z
0x08, 0x1c, 0x3e, 0x7f, // { left arrow
0x00, 0x7f, 0x00, 0x00, // |
0x7f, 0x3e, 0x1c, 0x08, // } right arrow
//0x04, 0x02, 0x04, 0x02, // ~
0x77, 0x5d, 0x49, 0x63, // ~ sum
0x55, 0x2a, 0x55, 0x2a // del grey rectangle
};
// ***** D I S P L A Y
// DEFINES
#define SCREENBYTES 256 // Number of bytes to address SCREEN (SCREENWIDTH x SCREENHEIGHT)/8
#define EEBRIGHTNESS 0 // EEPROM address for brightness
// PINS, PORTS
// Display
#define CS_PORT PORTD // CS port
#define CS_BIT PORTD6 // CS physical bit number
#define PIN_DC 20 // DC pin number
#define DC_PORT PORTF // DC port
#define DC_BIT PORTF5 // DC physical bit number
#define PIN_RST 19 // RST pin number
#define RST_PORT PORTF // RST port
#define RST_BIT PORTF6 // RST physical bit number
#define SPI_MISO_PORT PORTB
#define SPI_MISO_BIT PORTB3
#define SPI_MOSI_PORT PORTB
#define SPI_MOSI_BIT PORTB2
#define SPI_SCK_PORT PORTB
#define SPI_SCK_BIT PORTB1
#define SPI_SS_PORT PORTB
#define SPI_SS_BIT PORTB0
// VARIABLES
static byte dbuf[SCREENBYTES]; // Buffer for virtual screen (costs 256 bytes of dynamic memory)
static byte eachframemillis, thisframestart, lastframedurationms; // Framing times
static boolean justrendered; // True if frame was just rendered
static boolean isscreensave = false; // True if screensaver is active
static long powertimestamp = 0; // Needed for timing of power manangement
static byte brightness = EEPROM[EEBRIGHTNESS]; // Brightness
// SUBPROGRAMS
void SPItransfer(byte data) { // Write to the SPI bus (MOSI pin)
SPDR = data;
asm volatile("nop"); // Tiny delay before wait
while (!(SPSR & _BV(SPIF))) {} // Wait for byte to be sent
}
static void ScreenCommandMode(void) { // Set screen to command mode
bitClear(DC_PORT, DC_BIT);
}
static void ScreenDataMode(void) { // Set screen to data mode
bitSet(DC_PORT, DC_BIT);
}
static void bootpins(void) { // Declare and boot port pins
DDRB |= _BV(SPI_MOSI_BIT) | _BV(SPI_SCK_BIT) | _BV(SPI_SS_BIT);
PORTD |= _BV(CS_BIT); // Port D
PORTD &= ~(_BV(RST_BIT));
DDRD |= _BV(RST_BIT) | _BV(CS_BIT) | _BV(DC_BIT);
}
static void bootSPI(void) { // Initialize the SPI interface for the display
SPCR = _BV(SPE) | _BV(MSTR); // master, mode 0, MSB first, CPU clock / 2 (8MHz)
SPSR = _BV(SPI2X);
}
const byte PROGMEM ScreenBootProgram[] = { // SSD1306 boot sequence
// 0xAE, // Display Off
0xD5, 0xF0, // Set Display Clock Divisor v = 0xF0 - default is 0x80
// 0xA8, 0x3F, // Set Multiplex Ratio v = 0x3F
// 0xD3, 0x00, // Set Display Offset v = 0
// 0x40, // Set Start Line (0)
0x8D, 0x14, // Charge Pump Setting v = enable (0x14) - default is disabled
0xA1, // Set Segment Re-map (A0) | (b0001) - default is (b0000)
0xC8, // Set COM Output Scan Direction
// 0xDA, 0x12, // Set COM Pins v
//0x81, 0xCF, // Set Contrast v = 0xCF
//0x81, 0x00, // Set Contrast v = 0x00
0xD9, 0xF1, // Set Precharge = 0xF1
// 0xDB, 0x40, // Set VCom Detect
// 0xA4, // Entire Display ON
// 0xA6, // Set normal/inverse display
0xAF, // Display On
0x20, 0x00, // Set display mode = horizontal addressing mode (0x00)
0x21, 0x00, 0x7f, // Set col address range ... needed for SSD1309 controller
0x22, 0x00, 0x07 // Set page address range ... needed for SSD1309 controller
};
static void bootscreen(void) { // Boot screen - reset the display
delayshort(5); // Reset pin should be low here - let it stay low a while
bitSet(RST_PORT, RST_BIT); // Set reset pin high to come out of reset
delayshort(5); // Wait
bitClear(CS_PORT, CS_BIT); // Select the display (permanently, since nothing else is using SPI)
ScreenCommandMode(); // Run customized boot-up command sequence
for (byte i = 0; i < sizeof(ScreenBootProgram); i++)
SPItransfer(pgm_read_byte(ScreenBootProgram + i));
SPItransfer(0x81); SPItransfer(EEPROM[EEBRIGHTNESS]); // Set contrast
ScreenDataMode();
}
static void screenoff(void) { // Shut down the display
ScreenCommandMode();
SPItransfer(0xAE); // Display off
SPItransfer(0x8D); // Disable charge pump
SPItransfer(0x10);
delayshort(100);
bitClear(RST_PORT, RST_BIT); // Set RST to low (reset state)
}
static void screenon(void) { // Restart display after screenoff
bootscreen();
}
// ***** K E Y B O A R D
// PINS
#define KEYBOARDCOL1 10 // IV BIG
#define KEYBOARDCOL2 3
#define KEYBOARDCOL3 0
#define KEYBOARDCOL4 1
#define KEYBOARDROW1 5
#define KEYBOARDROW2 7
#define KEYBOARDROW3 8
#define KEYBOARDROW4 9
/*/
#define KEYBOARDCOL1 8 // IV TINY
#define KEYBOARDCOL2 3
#define KEYBOARDCOL3 0
#define KEYBOARDCOL4 1
#define KEYBOARDROW1 5
#define KEYBOARDROW2 7
#define KEYBOARDROW3 6
#define KEYBOARDROW4 9//*/
// DEFINES
#define MAGICKEYPIN 2 // Pin of magic key
#define KEY1 '?' // SHIFT 1-? 2-7 3-8 4-9
#define KEY2 '7' // 5-> 6-4 7-5 8-6
#define KEY3 '8' // 9-= 10-1 11-2 12-3
#define KEY4 '9' // 13-< 14-0 15-: 16-;
#define KEY5 '>' // EE
#define KEY6 '4'
#define KEY7 '5'
#define KEY8 '6'
#define KEY9 '=' // CHS
#define KEY10 '1'
#define KEY11 '2'
#define KEY12 '3'
#define KEY13 '<' // CLX
#define KEY14 '0'
#define KEY15 ':' // DOT
#define KEY16 ';' // #
#define PRINTNOKEY 254 // Only evaluate keys smaller
#define NOPRINTNOKEY 255 // Evaluate keys smaller
// VARIABLES
static byte key = PRINTNOKEY; // Holds entered key
static byte oldkey = PRINTNOKEY; // Holds oldkey - to prevent keyrepeat
// SUBPROGRAMS
static byte getkey() {
pinMode(MAGICKEYPIN, INPUT_PULLUP);
if (!digitalRead(MAGICKEYPIN)) return (KEY1); // F-key pressed
byte pinscol[4] = {KEYBOARDCOL1, KEYBOARDCOL2, KEYBOARDCOL3, KEYBOARDCOL4}; // Pins
byte pinsrow[4] = {KEYBOARDROW1, KEYBOARDROW2, KEYBOARDROW3, KEYBOARDROW4};
char k[4][4] = {
{NULL, KEY2, KEY3, KEY4},
{KEY5, KEY6, KEY7, KEY8},
{KEY9, KEY10, KEY11, KEY12},
{KEY13, KEY14, KEY15, KEY16},
};
byte kee = NOPRINTNOKEY;
for (byte r = 0; r < 4; r++) pinMode(pinsrow[r], INPUT_PULLUP); // Set rows to Vcc
for (byte c = 0; c < 4; c++) {
pinMode(pinscol[c], OUTPUT); // Set cols as OUTPUT and to GND
digitalWrite(pinscol[c], LOW);
for (byte r = 0; r < 4; r++) if (!digitalRead(pinsrow[r])) kee = (k[r][c]); //Assign key
digitalWrite(pinscol[c], HIGH); // Reset cols to Vcc and INPUT
}
return (kee);
}
// ***** S Y S T E M
// DEFINES
#define SCREENWIDTH 64 // Virtual screen width
#define SCREENHEIGHT 32 // Virtual screen height
#define MAXLIN 4 // Maximum of lines
#define FRAMERATE 15 // Maximal number of screen refreshes per second (>3)
#define SIZES 0x01 // Printing size
#define SIZEM 0x02 // Printing size
#define SIZEL 0x04 // Printing size
#define EECONTRAST 0 // EEPROM address to save brightness (1 byte)
// SUBPROGRAMS
static double _abs(double f) { // Absolute value
return (f < 0.0 ? -f : f);
}
static void dbuffill(byte n) { // Fill display buffer with pattern
memset(dbuf, n, SCREENBYTES);
}
static void display(void) { // Print display buffer (64x32) to real screen (128x64)
for (byte l = 0; l < MAXLIN; l++) { // Four lines
for (byte k = 0; k < 2; k++) { // Two nibbles (double height)
for (byte j = 0; j < SCREENWIDTH; j++) {
byte tmp = expand4bit((dbuf[j + l * SCREENWIDTH] >> (k * 4)) & 0x0f); // Expand 0000abcd
SPItransfer(tmp); SPItransfer(tmp); // Double width
}
}
}
}
static void setframerate(byte rate) { // Calculate frameduration
eachframemillis = 1000 / rate;
}
static void setscreencontrast(byte c) { // Set screen contrast
ScreenCommandMode();
SPItransfer(0x81);
SPItransfer(c);
ScreenDataMode();
EEPROM.write(EECONTRAST, c);
}
static void idle(void) { // Idle, while waiting for next frame
SMCR = _BV(SE); // Select idle mode and enable sleeping
sleep_cpu();
SMCR = 0; // Disable sleeping
}
static bool nextFrame(void) { // Wait (idle) for next frame
byte now = (byte) millis(), framedurationms = now - thisframestart;
if (justrendered) {
lastframedurationms = framedurationms;
justrendered = false;
return false;
}
else if (framedurationms < eachframemillis) {
if (++framedurationms < eachframemillis) idle();
return false;
}
justrendered = true;
thisframestart = now;
return true;
}
static byte expand4bit(byte b) { // 0000abcd Expand 4 bits (lower nibble)
b = (b | (b << 2)) & 0x33; // 00ab00cd
b = (b | (b << 1)) & 0x55; // 0a0b0c0d
return (b | (b << 1)); // aabbccdd
}
static void printcat(byte c, byte fnt, boolean bitshift, byte w, byte h, byte x, byte y) {
// Print char c (one bit shifted down) with width (1, 2, 4), size (1, 2) at position (x, y)
byte printbitshift = bitshift ? 1 : 0;
byte fwidth = FONTWIDTH;
byte foffset = FONTOFFSET;
if (fnt != FONT4) {
fwidth = FONTBIGWIDTH;
foffset = FONTOFFSETBIG;
if (c == '.') fwidth = 2; // Dot
}
for (byte k = 0; k < h; k++) {
for (int i = 0; i < fwidth; i++) {
byte tmp;
if (fnt == FONT4) tmp = pgm_read_byte(font4 + FONTWIDTH * (c - foffset) + i) << printbitshift;
else tmp = pgm_read_byte(fontbig + FONTBIGWIDTH * (c - foffset) + i) << printbitshift;
if (h == 2) tmp = expand4bit(tmp >> (4 * k) & 0x0f);
for (byte j = 0; j < w; j++) dbuf[x + (w * i + j) + (y + k) * SCREENWIDTH] = tmp;
}
}
}
static void printsat(char * s, boolean bitshift, byte w, byte h, byte x, byte y) { // Print sized string s at (x|y)
byte i = 0;
while (s[i]) printcat(s[i++], FONT4, bitshift, w, h, x + i * (FONTWIDTH + 1) * w , y );
}
static void printpixel(byte x, byte y) { // Print pixel at x,y
dbuf[x + (y / 8)*SCREENWIDTH] |= 1 << (y % 8);
}
static void printhline(byte y) { // Print horizontal line
for (byte i = 0; i < SCREENWIDTH; i++) printpixel(i, y);
}
static void printvline(byte x) { // Print horizontal line
for (byte i = 0; i < SCREENHEIGHT; i++) printpixel(x, i);
}
static void delayshort(byte ms) { // Delay (with timer) in ms with 8 bit duration
long t = millis();
while ((byte)(millis() - t) < ms) ;
}
static void delaylong(byte nr) { // Delay nr quarters of a second
for (byte i = 0; i < nr; i++) delayshort(250);
}
static double pow10(int8_t e) { // Calculates 10 raised to the power of e
double f = 1.0;
if (e > 0) while (e--) f *= 10.0;
else while (e++) f /= 10.0;
return (f);
}
static void wakeupnow() {} // Dummy wakeup code
static void sleepnow(void) { // Power down
screenoff(); // Display off only if screensaver didn't
pinMode(MAGICKEYPIN, INPUT_PULLUP);
sleep_enable();
attachInterrupt(MAGICKEYPIN, wakeupnow, CHANGE); // LOW, HIGH, CHANGE, FALLING, RISING
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_mode();
// SLEEP ... till SHIFT is pressed //
sleep_disable();
detachInterrupt(MAGICKEYPIN);
bootscreen(); // Display on
powertimestamp = millis();// Keep awake when active
delayshort(200);
}
//#define MX0 0