-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator_parser.ino
More file actions
53 lines (41 loc) · 1001 Bytes
/
Copy pathcalculator_parser.ino
File metadata and controls
53 lines (41 loc) · 1001 Bytes
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
#include <LiquidCrystal.h>
#include "parser.h"
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
double answer;
// input buffer
char inputBuf[64];
uint8_t idx = 0;
void setup() {
lcd.begin(16, 2);
Serial.begin(9600);
lcd.clear();
}
void loop() {
while (Serial.available() > 0) {
lcd.setCursor(0, 0);
char c = Serial.read();
lcd.write(c);
// newline = evaluate expression
if (c == '\n' || c == '\r') {
if (idx == 0) return; // ignore empty lines
inputBuf[idx] = '\0'; // terminate C-string
double result = parser(inputBuf);
lcd.clear();
lcd.setCursor(0, 1);
if (isnan(result)) {
lcd.write("ERROR");
} else {
char out[16];
dtostrf(result, 0, 6, out); // adjust width/precision as needed
lcd.write(out);
}
idx = 0; // reset buffer
return;
}
// normal character: show + store
if (idx < sizeof(inputBuf) - 1) {
lcd.write(c);
inputBuf[idx++] = c;
}
}
}