• ### Calculator Gets New Keyboard

The final keyboard version has been completed. The only remaining task is to design and laser-cut the top cover.

• ### A Ton of Functions

06/15/2020 at 10:19 1 comment

Over 70 application functions have been implemented so far.

The full list is here.

For instance, if you want to compute an average, it is easier than on HP-35s!

## Common Experiment Processing

Given: In the dance class of 10 students, the heights were measured at 147, 152, 165, 163, 168, 163, 175, 166, 170, and 167 cm.

Find: The mean height and its standard deviation in the class.

1. Type `clear sum`, `<Enter>` (a shortcut is `<F1>`, `<A>`, which displays `clear `)
2. Type `sum 147`, `<Enter>`
3. Type `sum 152`, `<Enter>`
4. Continue for the other 8 numbers. Instead of typing sum, you also can type a number and press `<F1>`, `<S>`
5. After each number the calculator displays number of samples, the mean and standard deviation.

After entering all 10 numbers, the display shows:

N____________________10

StDev________ 8.30261003

Mean______________163.6

Sum updated____________

>_

Answer: the average height of the class student is 164 cm (68% of samples are in the interval of 164±9 cm).

## Normal Distribution and Probability

Given: In the dance class as above, assume normal distribution and fair representation of the school population.

Find: (1) Probability the next class participant is taller than 175 cm. (2) Interval of heights for 95% of the school population.

1. Type `prob(175)`, `<Enter>`
2. The calculator displays: 0.91513344. This is the probability the next participant is under 175 cm.
3. To find the answer, type `1`, `<Enter>`, `<Arrow Down>`, `<->`. The calculator displays: 8.48665622e-002.
4. Type `1`, `<Enter>`, `0.95`, `<->`, `2`, `/`, `probit`,`<Enter>`.
5. The calculator displays: 147.3(...) This is the lower value.
6. Type `probit( 0.975)`, `<Enter>`.
7. The calculator displays: 179.9(...) This is the upper value.

Answer: (1) the probability to find a student taller than 175 cm is 8.5% . (2) In the school, 95% of the students are in the interval between 147.3 and 179.9 (or 163.6±16.3) cm in height.

## Variables' implementation

In the calculator, variable names may start with a letter or an underscore (`_`) and can further contain letters and numbers, such as: `My_variable`, `_my_other_variable`, `_my_other_variable2`, `Catch_22`.

As in the standard BASIC, the variable type is defined by name ending.

• The name with no ending is a 64-bit double-precision Real: `The_Ultimate_Answer = 42`.
• The name ending with `%`  is a 64-bit sighed Integer: `The_Ultimate_Answer% = 42`.
• The name ending with `\$` is a string: `The_Ultimate_Answer\$ = "42"`.
• The above three definitions are treated as different variables and co-exist in the memory.
• Some versions of BASIC also support variables of different precision, e.g. `a!` designates a double-precision real. For the Calculator BASIC, these additional types are not yet implemented, and probably will not be - most of the time engineering calculations require double precision anyway.

BASIC supports 4 variable arrangements:

• Real or Integer numbers (scalars), e.g. `pi = 3.1415926`.
• Real or Integer vectors, e.g. `Fibonacci%[0] = 0 : Fibonacci%[1] = 1 : Fibonacci%[2] = 1`.
• Real or Integer matrices, e.g. `ROT[0,0] = cos(a) : ROT[1,1] = cos(a) : ROT[1,0] = -sin(a) : ROT[0,1] = sin(a)`.
• Strings, e.g. `The_Ultimate_Answer\$ != "42"` evaluates to FALSE, `The_Ultimate_Answer\$[0]` evaluates to "4".

## Standard variables and Instant On

The following variables are always available (in the final version will be more):

• stack[i] - the RPN stack. The index i can be from 0 to 19; 0-X, 1-Y, 2-Z. The default value is 0.
• prev - the RPN previous value. The default value is 0.
• amode - the angle mode - 0 - degrees, 1 - radians, 2 - gradians. The default value is 0.
• current_dir\$ - the current directory. The default value is "/" (root).
• scrMessage\$ - the current message displayed in the box. The default value is "RPN Ready".
• rpnLabelX\$ - the current message displayed above the X-register value in the RPN interface. The default value is "X".
• rpnLabelY\$ - the current message displayed above the Y-register value in the RPN interface. The default value is "Y".
• rpnLabelZ\$ - the current message displayed above the Z-register value in the RPN interface. The default value is "Z".

If the SD card is inserted, during a power-down all standard and user-defined variables are stored on the card in the file named /_RPN_SaveStatus.bin Upon the power-up, the calculator is automatically restored to the state just before the power-down. If the card is not present during power-down, all user-defined variables and other settings are lost. Upon the power-up, only the standard variables will be present and will be initialized with defaults.

This allows keeping separate working environments on different SD cards. Or, in some cases, it is important to have no stored environments on the calculator - for example, for math exams.

## Implementation details

The current variable space is defined in Variables.hpp as VARIABLE_SPACE = 32000 bytes.

Variables are placed in the variable space as following:

• byte 0 - variable type (0-undefined, 1-number, 2-vector, 3-matrix, 4-string)
• byte 1 - variable name length (N). The name cannot be longer than 254 bytes.
• bytes 2 to N+2 - variable name (null-terminated)

Then:

• byte N+3 - either an 8-byte double or an 8-byte integer

Or:

• byte N+3 - total vector/string length as a 2-byte unsigned integer (L)
• bytes N+5 and above - either a null-terminated string or a vector of 8-byte numbers

Or:

• byte N+3 - total matrix length as a 2-byte unsigned integer
• byte N+5 - row length as a 2-byte unsigned integer
• byte N+7 and above - a matrix of numbers composed by row
• ### Short Video Update

Quick-and-dirty video for  the current project status:

• ### Keyboard final, Serial connectivity, Wiki docs

04/19/2020 at 08:19 1 comment

Finally the keyboard PCBs have arrived from China.

For the keyboard, it is the final design. The size is 60x100 mm, and the intended position is to the right from the screen:

(The screen itself is still in the mock up stage). The keyboard has 4 LEDs and 60 switches - the one on the bottom left is a hardware power switch. All LEDs and keys can be accessed independently - in theory all 59 switches can be closed at the same time and will register.

The keyboard driver board sits below. A header for Arduino Micro Pro is provided, in case the keyboard is used as a standalone unit for PC.

Currently the driver board and the keyboard are connected by a header; the production version will be soldered.

The RPN calc can now impersonate a keyboard and a 3-button mouse, injecting keystrokes into a PC. Mouse pointer  movements and mouse wheel from  the calculator to the PC are also supported. With a dumb terminal, the RPN understands both the RPN and an Algebraic inputs:

The documentation is started at the project Wiki: https://github.com/ptravnik/Retro_CALC/wiki

Next: programming a File Manager!

• ### Hardware Mock-up Programmed

I have continued debugging the hardware mock-up. The fundamental target was to program a simple RPN calculator UI with 6 operations plus a number of scientific functions. The integration with BASIC will come later, after all the hardware components are finalized.

New Pro Micro clones with 3.3V processors have arrived, so the logic level conversion is no longer necessary. See the new wiring diagram in the file section.

Files are uploaded to https://github.com/ptravnik/Retro_CALC (its a proof-of-concept code, use with caution!)

Two problems have been hit so far. Firstly, the real-time clock of ESP32 is not very precise in the sleep mode; thus an external RT clock module is required in the future. Secondly, the sleep current of this particular Dev board is 15 mA, while in running (no WiFi) - 55 mA. Need to troubleshoot it a bit further.

Currently the mockup looks a bit neater and can run from standalone power. Can you take it to the math exam?

The current functionality fully operational and tested:

(1) Hardware keyboard (a mock-up so far), with CP1251 code page support;
(2) LCD power manager, with PWM control and sleep capability;
(3) LCD driver with fancy LCD font, resembling Electronika MK-90;
(4) Sleep mode and wake-up by a hardware button;
(5) RPN calculator UI.

The font looks like this:

Or in Russian:

The current functionality implemented, but not fully tested:

(6) Serial connectivity to Arduino Micro Pro;

The deferred / not yet implemented functionality:

(8) Beeper;
(9) LiPo charger / battery;
(10) Independent RT clock.

The hardware is running a simple RPN calculator interface. The operation is as following:

o The calculator can be operated by either the hardware keyboard or a serial port terminal.

o The display is composed of 2 parts: the upper depicts the RPN stack, the lower is for entering commands.

o Enter numbers as normal, e.g. 123, 123.456, 123.456e89, 123.456e-89 etc. "E7" or "e7" stand for "10 power 7". Buttons <DEL>, <Backspace>, <Home>, <End>, <Left>, and <Right> should work as expected.
o Besides the entry as above, the following is allowed: 123k, 123M, 123c, etc. The letter is an "engineering multiplier": f="e-15", p="e-12", n="e-9", u="e-6", m="e-3", c="e-2", d="e-1", k="e+3", M="e+6", G="e+9", T="e+12", and P="e+15".

o Upon entering a number, press <Enter>. If the number in the entry line is valid, it is processed into the X-register, if not - ignored. Upon entering the number in stack, all the values shift by one position. The current stack is 20 numbers.

o Alternatively, press an operation button <+>, <->, <*>, </>, <^>, or <+/->. If the number in the entry line is valid, it is processed into the X-register, if not - ignored. Upon this, an RPN operation on stack is performed. Division by 0 causes an error, which can be cleared by pressing <ESC>.

o The available math functions:   sin, cos, tan,
sqrt - square root,
sq - square,
exp - e power x
sign,
inv - computes 1/x
hex - converts x to hexadecimal (rounds to integer first)
o Stack manipulation functions:
prev - previous X (shortcut: PGUP),
swap (shortcut: DOWN),
push, pop,
roll (shortcut: PGDN)
previous entry (shortcut: UP)
#scr status - updates status label
#scr labelx - updates label X
#scr labely - updates label Y
#scr labelz - updates label Z
o Screen power and brightness:
#scr off - puts screen to sleep
#scr on - wakes up screen
#scr+ - increase brightness
#scr- - decrease brightness

• ### Matrix operations with double precision

Can your next DIY programmable calculator do the same? :) This is Arduino DUE, hope ESP32 and even ESP8266 will be ready soon.

• ### ESP32 Mockup Completed

Finally got the working mockup running. Currently the hardware includes:

• ESP32 dev module
• Arduino Micro Pro
• BW LCD with a software LED control
• Hardware keyboard (a working mockup so far; the Chinese PCB manufacturer entered wrong delivery address, so the parcel is back to China)

The LCD part can be easily removed from the primary board - it is held attached by a header.

Power consumption in this configuration:

• Input at 4.96V (WiFi, BT off in all tests)
• The LCD screen LED is fully on (75 Ohm resistor) 103 mA
• The LCD screen LED is fully off (but the LCD is running) 98 mA
• The LCD screen is off (ESP32 and Micro Pro running) 94 mA
• The LCD screen is off, Micro Pro off (ESP32 running) 82 mA
• The total power is wherefore 500 mW, and two 5000 mAh batteries will last 2*5*3.7/0.5 = over 3 days of continuous operation.

I have also compared this to the power consumption of Arduino Mega 2560 with a TFT 480x320 color screen and an SD card (see the video in the Project Log 01). Two different Mega we used: "Classic" and "OSEPP" (in brackets).

• Input at 4.96 V (BASIC is running)
• With TFT connected: 108 (118) mA, 550 mW - this is comparable to the BW LCD system, but no keyboard injection capability.
• With TFT removed : 22(32) mA, 125 mW.

I could not find an elegant way of disconnecting/managing the TFT power and the TFT backlight as it is mounted on a shield.

Obviously, Mega with a BW LCD would make a decent calculator with about 30-50 mA consumption, which equates to one month of a continuous running! Unfortunately, this system has only 4-byte arithmetic, no real-time clock, no WiFi, etc.

The calculator connects to the PC in three ways:

• Via ESP32 USB - for programming ESP32 and direct operation of Terminal BASIC via VT100
• Via Arduino Micro USB - for programming Arduino and impersonating a wired keyboard (still writing code for this - should be an INJECT keyword in BASIC)
• Via BT or WIFI of ESP32 - have not tested software yet, but the hardware is OK - BASIC now has a SET WIFI_NAME, SET WIFI_PASSWORD, NETTIME() keywords, and so you can set time from the network or make a table clock in BASIC.

The Terminal BASIC on ESP32 is working... kind of. I have replaced the capitalized keywords with a lower-case.

The SD card code is not compiling properly as the ESP32 and the Arduino libraries are different - still working on it. The hardware is fully operational, and I am able to run independent tests:

The Terminal BASIC is seemingly having a hard time working with double-precision numbers on ESP32. This program calculates a factorial in integers:

Now replacing r% with r - for 4-byte floats:

Finally, we replace "r" with "r!" ("!" stands for long reals) results in busted stack!

Next: debugging Terminal BASIC on ESP32!

• ### Specific System Requiremens

Overall design notes.

## Hardware:

The main processor is ESP32 WROOM 32U. For the final version can possibly source a board with an in-built LiPo charger, such as GeeekNet https://www.seeedstudio.com/GeeekNET-ESP32-Development-Board-p-2945.html For now, the testing board is more conventional DevKitC.

ESP32 has been selected for two reasons. Firstly, it has excellent low-power features, allowing to save the battery. Secondly, the processor provides double-precision FP computations, which is essential for an engineering calculator. ESP32 can communicate to the PC via its USB port, WiFi and Bluetooth, so BASIC can be used from a PC terminal emulator - for, example, the user may want to enter and debug a complex routine by using the conventional keyboard and the big screen. The same USB port is connected to the LiPo charger / power supply (or will be using different ESP32 boards in the future - have not decided yet).

The main processor is connected to 4 peripheral devices via its primary SPI bus: the SD card reader, the 128x64 LCD display (may consider a different display although), the keyboard and the power controller. The latter allows the ESP32 to manipulate the LCD back-light, thus reducing the power consumption. It also controls the power supplied to the Arduino Micro Pro. The keyboard provides a hardware Reset button, which is needed to load programs into the ESP32.

The primary use of the Arduino Micro Pro is to impersonate a conventional keyboard/mouse for a PC (for example, for a text injection). The processor is connected to the ESP32 via Software Serial. Via the same interface, the ESP32 can send commands for a single-tone sound generation, such as beeps and simple melodies. As an alternative design, the LCD display back-light LED may be driven via the Arduino Micro Pro PWM output.

## Software:

1. BCL interface. In true tradition of retro calculators, this should be a comprehensive BASIC-system. All file system manipulations and other functions should be accessible from the BASIC command line (BCL). For example, to change the calculator mode from Radians to Degrees, the user switches into the BCL and types something like SET ANMODE DEGREES. To change the calculator precision and output pattern to scientific with 4 decimal places, one types, for example, SET NUMODE SCI4  The arithmetic expressions can be evaluated directly from the BCL, for example, ? 1e13/SIN(45) immediately evaluates into 1.175e+13. PRINT HUMODE returns the numeric code for the SCI4 constant: 14 (or rather 1.4e+01 as we requested a scientific format). Naturally, all standard BASIC commands should work in BCL as expected, e.g. 10 PRINT 1e13/SIN(45) returns OK and inputs the program line into the memory. The user should be able to use arrows to scroll the screen up and down beyond the 8-line boundary. At least 40 lines should be supported. Text selection and Copy/Paste function should be supported.

To make the system more practical, several additions to BCL are essential.

2. RPN interface. The screen displays three positions of stack: X, Y, Z. The total stack capacity should be about 20, plus an additional slot for the previous X value. The buttons should work as expected for an RPN calculator, e.g. pressing <1>, <9>, <.>, <7> results in X displaying "19.7". Then, pressing <Enter>, <2>, results in X displaying "1.970e+01" (remember, we are in sci-4 mode) and Y displaying "2". Pressing </> results in X displaying "9.850e+00". The previous X slot contains 2.000e+00. The RPN interface should be accessible from the BASIC programs. For example, the user wants to implement a program for calculating roots of a quadratic equation:

```010 SET RPNLABELS "C (input)", "B (input)", "A (input)"
020 HALT RPN
030 A = RPNSTACK(2)
040 B = RPNSTACK(1)
050 C = RPNSTACK(0)
060 D = B*B - 4*A*C
070 SET RPNSTACK 2, D
080 IF D < 0 GOTO 130
090 SET RPNSTACK 0, -(B+SQRT(D))/A/2
100 SET RPNSTACK 1, -(B-SQRT(D))/A/2
110 SET RPNLABELS "Root 1", "Root 2", "Discriminant"
120 GOTO 160
130 SET RPNSTACK 0, -B/A/2
140 SET RPNSTACK 1, SQRT(-D)/A/2
150 SET RPNLABELS "Real part:", "+/- Imaginary part", "Discriminant"
160 HALT RPN
170 GOTO 10```

To solve quadratic equations, the user goes to BCL and types CHAIN QUAD <Enter> (assuming QUAD.BAS is the program name). The system presents the user with the RPN screen, there instead of labels "X", "Y", "Z" are labels "C (input)", "B (input)", "A (input)". The user operates the RPN to fill the data and presses <Run> button. The system displays the answer in "Root 1", "Root 2", "Discriminant" form. To proceed with the next computation, the user presses RUN, and gets labels "C (input)", "B (input)", "A (input)", etc. To terminate the program, the user presses <Halt> button, and the labels change back to "X", "Y", "Z". The program remains in the memory and could be restarted by pressing <Run> button or by going to BCL and typing RUN <Enter>.

3. EDT interface. In this mode the system allows a direct edit of the program (note that LIST command on a 8-line screen is very inefficient, and even on a large screen is limited). The expected user interaction is similar, but not identical to Windows Notepad editor. NEW, LOAD and SAVE functions are available in BCL and should not be duplicated here. Find, Replace, Copy/Cut/Paste, Move Line Up/Down should be available in the EDT interface. A text "wrap" and "no wrap" modes should be supported. Besides the BASIC files, the same editor may be used to view data files, make quick notes, etc.

4. DIR interface. In this mode the user can traverse the calculator file system. Note that FILES command on a 8-line screen is extremely inefficient. The typical use in BCL would be as following. The user wants to save the file in a certain folder. In the BCL, the user types SAVE <Space>, then presses <Dir> button, navigates to the folder and selects the file name. Pressing <Enter> on a file name, for example, QUAD.BAS, results in the system returning into BCL mode and SAVE QUAD.BAS appearing instead of SAVE. At this point, the user may edit the name, for example, to SAVE QUAD_2.BAS and press <Enter> to complete the command.

• ### Hardware Mock-up Completed

Full hardware mock-up is now operational (yes, it is damn ugly).

The current mock-up version is based upon:

(1) Arduino MEGA 2560

(2) Arduino MEGA SRAM shield (my PCB is modified for 56k RAM instead of 32 in the original version) https://hackaday.io/project/21561-arduino-mega-2560-32kb-ram-shield

(3) Arduino SD card reader; this version is for Uno, but runs on Mega with pins 10-13 using Soft SPI (library mods by David Mellis). Note that the standard SD library will not work if the classic Uno shield is transplanted to MEGA:

(4) Piezo speaker on line 9

(5) Hardware keyboard on lines A0-A4. The driver is here (check the Morse code example!):

(6) LCD screen 128x64 pixels on SPI. The driver is U8g2 from Oli Kraus (it writes Cyrillic and other character sets):

The keyboard PCBs have been ordered from https://www.elecrow.com/ Apparently they have been already manufactured and are in the mail. Hopefully I will have something less ugly in a couple of weeks. The order files are here: