-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretro-calculator.ino
231 lines (197 loc) · 6.06 KB
/
retro-calculator.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
// Retro calculator using NSA 1166 Bubble Display and ICM7218 LED driver
// Note that the NSA1166 display only has 6 digits and a single fixed
// decimal point.
//
// https://github.com/Andy4495/retro-calculator
//
// 1.0.0 04/12/2019 A.T. Original
// 1.1.0 12/06/2019 A.T. Use debounce routines from updated keypad library.
#include <ICM7218.h>
#include <Keypad4495.h>
// Create a keypad object
#define NUM_ROWS 4
#define NUM_COLS 4
byte rowPins[NUM_ROWS] = {15, 14, 8, 7};
byte colPins[NUM_COLS] = {19, 18, 13, 12};
char keymap[NUM_COLS * NUM_ROWS] = { '1', '2', '3', '+', '4', '5', '6', '-', '7', '8', '9', '*', 'C', '0', '=', '/' };
Keypad4495 myKeypad(keymap, rowPins, colPins, NUM_ROWS, NUM_COLS, true);
// Configure the 10 OUTPUT pins used to interface with ICM7218: D0-D7, mode, write
ICM7218 myLED(19, 18, 13, 12, 11, 10, 9, 6, 2, 5);
// NSA1166 is a 6 digit display
#define MAXCHARS 6
// Debounce time in ms. Normally 20 ms is sufficient for most types of buttons
#define DEBOUNCE_TIME 20
enum STATE {ENTRY1, ENTRY2, EQUALS, ERROR};
enum OPERATOR {NONE, PLUS, MINUS, MULT, DIV};
char convertBuffer[MAXCHARS + 1];
char entryString[MAXCHARS + 1];
char c;
int i = 0;
long op1, op2;
STATE state = ENTRY1;
OPERATOR op = NONE;
int pos = 5; // Position of current digit in entryString, right justify, leave a space for terminator
void setup() {
// Use DIRECT mode since we will display some special characters
myLED.setMode(ICM7218::DIRECT);
memset(entryString, 0, MAXCHARS + 1); // clear the entry string
displayCalc();
}
void loop() {
long answer;
// Debounce: Waits for button to be released before returning
c = myKeypad.getKeyWithDebounce();
switch (c) {
case Keypad4495::NO_KEY:
// Do nothing and loop around and check again
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
// Add to char array to max of 6 digits
// Drop MSB as more digits entered
if (i >= MAXCHARS) {
for (i = 1; i < MAXCHARS; i++) {
entryString[i - 1] = entryString[i];
}
i = 5;
}
entryString[i] = c;
i++;
convertToDisplayFormat(entryString);
myLED.print(convertBuffer);
break;
case '+':
// Set operation as plus
// Set state as entry2
// Save first value string to number
// Clear out entry string
state = ENTRY2;
op = PLUS;
op1 = atol(entryString);
memset(entryString, 0, MAXCHARS + 1); // clear the entry string
i = 0;
sprintf(convertBuffer, " Plus");
myLED.convertToSegments(convertBuffer);
myLED.print(convertBuffer);
break;
case '-':
// Set operation as "subtraction"
// Save first value string to number
// Set state as entry2
// Clear out entry string
state = ENTRY2;
op = MINUS;
op1 = atol(entryString);
memset(entryString, 0, MAXCHARS + 1); // clear the entry string
i = 0;
sprintf(convertBuffer, " Subtr");
myLED.convertToSegments(convertBuffer);
myLED.print(convertBuffer);
break;
case '*':
// Set operation as "mult"
// Save first value string to number
// Set state as entry2
// Clear out entry string
state = ENTRY2;
op = MULT;
op1 = atol(entryString);
memset(entryString, 0, MAXCHARS + 1); // clear the entry string
i = 0;
sprintf(convertBuffer, " Prod");
myLED.convertToSegments(convertBuffer);
myLED.print(convertBuffer);
break;
case '/':
// Set operation as "div"
// Save first value string to number
// Set state as entry2
// Clear out entry string
state = ENTRY2;
op = DIV;
op1 = atol(entryString);
memset(entryString, 0, MAXCHARS + 1); // clear the entry string
i = 0;
sprintf(convertBuffer, " Div");
myLED.convertToSegments(convertBuffer);
myLED.print(convertBuffer);
break;
case 'C':
// Clear entry string and display zero
memset(entryString, 0, 7); // clear the entry string
i = 0;
convertBuffer[MAXCHARS - 1] = c;
memset(convertBuffer, ' ', MAXCHARS - 2);
convertBuffer[MAXCHARS] = 0;
myLED.convertToSegments(convertBuffer);
myLED.print(convertBuffer);
break;
case '=':
// Check for overflow, display error if needed
//
state = ENTRY1;
op2 = atol(entryString);
i = 0;
switch (op) {
case PLUS:
answer = op1 + op2;
if ((answer > 999999) || (answer < -99999)) state = ERROR;
break;
case MINUS:
answer = op1 - op2;
if ((answer > 999999) || (answer < -99999)) state = ERROR;
break;
case MULT:
answer = op1 * op2;
if ((answer > 999999) || (answer < -99999)) state = ERROR;
break;
case DIV:
if (op2 == 0) state = ERROR;
else answer = op1 / op2;
if ((answer > 999999) || (answer < -99999)) state = ERROR;
break;
default:
state = ERROR;
break;
}
if (state != ERROR) {
snprintf(entryString, MAXCHARS + 1, "%6ld", answer);
convertToDisplayFormat(entryString);
myLED.print(convertBuffer);
} else {
displayError();
state = ENTRY1;
}
memset(entryString, 0, MAXCHARS + 1); // clear the entry string
break;
default:
displayError();
memset(entryString, 0, MAXCHARS + 1); // clear the entry string
state = ENTRY1;
break;
}
}
void displayError() {
snprintf(convertBuffer, MAXCHARS + 1, "Error");
myLED.convertToSegments(convertBuffer);
myLED.print(convertBuffer);
}
void displayCalc() {
snprintf(convertBuffer, MAXCHARS + 1, " CALC ");
myLED.convertToSegments(convertBuffer);
myLED.print(convertBuffer);
}
void convertToDisplayFormat(char* rawString) {
int j;
sprintf(convertBuffer, "%6ld", atol(rawString));
convertBuffer[MAXCHARS] = 0; // Add NULL terminator
myLED.convertToSegments(convertBuffer);
}