-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallbacks.cpp
More file actions
285 lines (256 loc) · 7.51 KB
/
callbacks.cpp
File metadata and controls
285 lines (256 loc) · 7.51 KB
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
/*
* (C) Copyright 2022 Larry Milne (https://www.larrycloud.ca)
*
* This code is distributed on "AS IS" BASIS,
* WITHOUT WARRANTINES OR CONDITIONS OF ANY KIND.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author: qawse3dr a.k.a Larry Milne
*/
#include "callbacks.h"
#include "math/binary_operator.h"
#include "math/bracket.h"
#include "math/function.h"
#include "math/literals.h"
#include "math/literals_piece.h"
#include "math/math_util.h"
#include "math/unary_operator.h"
#include "pico/bootrom.h"
using picolator::math::BinaryOperator;
using picolator::math::Bracket;
using picolator::math::DomainError;
using picolator::math::ExprTree;
using picolator::math::Letter;
using picolator::math::Literals;
using picolator::math::LiteralsPiece;
using picolator::math::NotImplementedError;
using picolator::math::SyntaxError;
using picolator::math::TypeError;
using picolator::math::UnaryOperator;
using LP = ExprTree::LetterPtr;
int cursorIndexToLcdIndex(const CalculatorState& state) {
int lcd_cursor = 0;
int count = 0;
for (auto& e : state.equation) {
if (count++ < state.cursor) {
lcd_cursor += e->getSymbol().length();
} else {
break;
}
}
return lcd_cursor;
}
void redrawEquation(CalculatorState& state) {
state.lcd.clear(0);
state.lcd.setCursor(0, 0);
for (const auto& l : state.equation) {
state.lcd.put(l->getSymbol());
}
state.lcd.setCursor(0, cursorIndexToLcdIndex(state));
state.lcd.update();
}
void copyExprVec(const ExprTree::ExprVec& src, ExprTree::ExprVec& dst) {
dst.clear();
for (const auto& l : src) {
dst.emplace_back(l);
}
}
void insertEquation(CalculatorState& state,
const ExprTree::LetterPtr& mapping) {
if (state.cursor == state.equation.size()) {
state.equation.emplace_back(mapping);
state.lcd.put(mapping->getSymbol(), true);
state.cursor += 1;
} else {
if (state.insert_mode) {
state.equation.insert(state.equation.begin() + state.cursor, mapping);
} else {
state.equation[state.cursor] = mapping;
}
state.cursor += 1;
}
redrawEquation(state);
}
void reflash_cb(CalculatorState& state) {
state.lcd.clear();
state.lcd.setCursor(0, 0);
state.lcd.put("re-flash mode");
state.lcd.setCursor(1, 0);
state.lcd.put("unplug battery!");
state.lcd.update();
reset_usb_boot(0, 0);
}
void moveLeft_cb(CalculatorState& state) {
if (state.cursor > 0) {
state.cursor -= 1;
state.lcd.setCursor(0, cursorIndexToLcdIndex(state));
state.lcd.update();
}
}
void moveRight_cb(CalculatorState& state) {
if (state.cursor < state.equation.size()) {
state.cursor += 1;
} else if (state.cleared) { // replace equation
copyExprVec(state.history.back(), state.equation);
// state.equation = state.history.back();
state.cursor = 1;
}
redrawEquation(state);
}
void moveUp_cb(CalculatorState& state) {
printf("moveUp pressed\n");
if (state.history_cursor < state.history.size()) {
copyExprVec(state.history[state.history.size() - ++state.history_cursor],
state.equation);
redrawEquation(state);
}
}
void moveDown_cb(CalculatorState& state) {
printf("moveDown pressed\n");
if (state.history_cursor > 1) {
copyExprVec(state.history[state.history.size() - --state.history_cursor],
state.equation);
redrawEquation(state);
}
}
// Calculates the result and prints it on
// the screen. or display error in case of error
void calculate_cb(CalculatorState& state) {
state.lcd.clear(1);
Literals::getAnswer() = state.ans->getLiteral();
if (state.equation.empty()) {
if (state.history.empty()) return;
state.equation = state.history.back();
redrawEquation(state);
}
ExprTree::LiteralPtr value = 0;
try {
auto tree = ExprTree(state.equation);
value = tree.getValue();
} catch (const DomainError& e) {
state.lcd.setCursor(1, 0);
state.lcd.put(e.what());
state.lcd.setView(0, 0);
state.lcd.setCursor(0, 0);
state.lcd.update();
return;
} catch (const SyntaxError& e) {
state.lcd.setCursor(1, 0);
state.lcd.put(e.what());
state.lcd.setView(0, 0);
state.lcd.setCursor(0, cursorIndexToLcdIndex(state));
state.lcd.update();
return;
} catch (const TypeError& e) {
state.lcd.setCursor(1, 0);
state.lcd.put(e.what());
state.lcd.setView(0, 0);
state.lcd.setCursor(0, 0);
state.lcd.update();
return;
} catch (const NotImplementedError& e) {
state.lcd.setCursor(1, 0);
state.lcd.put(e.what());
state.lcd.setView(0, 0);
state.lcd.setCursor(0, 0);
state.lcd.update();
} catch (std::exception& e) {
state.lcd.setCursor(1, 0);
state.lcd.put("caught error");
state.lcd.setView(0, 0);
state.lcd.setCursor(0, 0);
state.lcd.update();
return;
}
state.clear = true;
state.lcd.setCursor(1, 0);
state.lcd.put("\x7E" + value->toString());
state.lcd.setCursor(0, 0);
state.lcd.setView(0, 0);
state.lcd.update();
state.cursor = 0;
state.history_cursor = 0;
// Save the equation in the history
state.history.push_back(state.equation);
state.equation.clear();
state.ans = value;
}
// Clears the screen and the result
void clear_cb(CalculatorState& state) {
state.equation.clear();
state.cursor = 0;
state.lcd.clear();
state.lcd.update();
}
void backspace_cb(CalculatorState& state) {
if (state.equation.size() < 1) {
clear_cb(state);
return;
}
state.equation.erase(state.equation.begin() + --state.cursor);
state.lcd.setCursor(0, 0);
redrawEquation(state);
}
void convertDouble_cb(CalculatorState& state) {
if (!state.ans) state.ans = 0;
state.lcd.clear();
state.lcd.setCursor(0, 0);
state.lcd.put("ANS \x7E DOUBLE");
state.lcd.setCursor(1, 0);
state.lcd.put("\x7E" + std::to_string(state.ans->getValue()));
state.lcd.update();
}
static char selectVar(CalculatorState& state) {
int cursor = 0;
state.lcd.clear();
state.lcd.setCursor(0, 0);
state.lcd.put("A B C D E F");
state.lcd.setCursor(1, 0);
state.lcd.put("\x7E" + Literals::getVariable('A').getSymbol());
state.lcd.setCursor(0, 0);
state.lcd.update();
while (1) {
sleep_ms(10);
auto but = state.buttons.getPressed(true);
if (!but) continue;
if (but->second == 8 && but->first == 4) break;
if (but->second == 1 && but->first == 2 && cursor < 6) {
cursor++;
state.lcd.setCursor(1, 0);
state.lcd.clear(1);
state.lcd.put("\x7E" + Literals::getVariable('A' + cursor).getSymbol());
state.lcd.setCursor(0, cursor * 2);
state.lcd.update();
}
if (but->second == 1 && but->first == 1 && cursor > 0) {
cursor--;
state.lcd.setCursor(1, 0);
state.lcd.clear(1);
state.lcd.put("\x7E" + Literals::getVariable('A' + cursor).getSymbol());
state.lcd.setCursor(0, cursor * 2);
state.lcd.update();
}
}
return 'A' + cursor;
}
void saveVar_cb(CalculatorState& state) {
if (!state.ans) state.ans = 0;
char var = selectVar(state);
Literals::getVariable(var) = state.ans->getLiteral();
state.lcd.clear();
state.lcd.setCursor(0, 0);
state.lcd.put("ANS \x7E " + std::string(1, var));
state.lcd.setCursor(1, 0);
state.lcd.put("\x7E" + Literals::getVariable(var).toString());
state.lcd.setCursor(0, 0);
state.lcd.update();
}
void getVar_cb(CalculatorState& state) {
if (!state.ans) state.ans = 0;
char var = selectVar(state);
insertEquation(state, LP(new Literals(var)));
redrawEquation(state);
}
void noOp(CalculatorState&){};
void layer2_cb(CalculatorState& state) { state.layer2 = true; }