-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ino
116 lines (101 loc) · 2.89 KB
/
main.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
void setup() {
// set pin modes
pinMode(LED, OUTPUT);
pinMode(SloraRX, INPUT);
pinMode(SloraTX, OUTPUT);
// enable serial consoles
Serial.begin(9600);
Slora.begin(9600);
// store address of LoRa peer
peerAddress = getPeerAddress();
// define LCD columns and rows (and clear it)
lcd.begin(16, 2);
lcd.clear();
}
void loop() {
// always listen for incoming LoRa messages
if (Slora.available()) {
// store received message
String incoming = parseMessage(Slora.readString());
// check if message is a reply
if (incoming.equals("%OK")) {
// reply received; clear messagBuffer and LCD
messageBuffer[0] = '\0';
lcd.clear();
cursor = 1;
// forward reply to GUI
Serial.println("%OK");
} else {
// not a reply
if (!incoming.equals(incomingPrev)) {
// flash notification LED
blinkLED(1, 1000);
// send reply to peer
sendToPeer("%OK\0");
// echo message over hardware serial for GUI
Serial.println(incoming);
// print message to screen
if (!docked) {
writeToLCD(incoming.c_str(), true);
writeToLCD(messageBuffer, false);
}
// store message
incomingPrev = incoming;
} else {
// send reply to peer (even if duplicate)
sendToPeer("%OK\0");
}
}
// clear Slora buffer to avoid overloading Slora serial
clearSloraBuffer();
}
// always listen for requests from GUI (to forward over Slora)
if (Serial.available()) {
String incoming = Serial.readString();
if (incoming.equals("%DOCK")) {
// toggle docked mode
docked = !docked;
if (docked == true) {
blinkLED(2, 100);
} else {
blinkLED(3, 100);
}
} else {
sendToPeer(incoming.c_str());
}
clearSerialBuffer(); // avoid over-loading serial interface
}
// always listen for keypresses
key = keypad.getKey();
// actions to perform when a key is pressed
if (key) {
typeKey = false;
determineMultipress(key);
if (typeKey) {
MPkey = calcMP(numeric);
multipress[1] = 0; // reset press count
if (capitalShift) {
// disable capitalShift for next iteration
capitalShift = false;
if (MPkey == ' ') {
// print a backspace and set the cursor to the appropriate position
backspace();
return;
} else if (isAlpha(MPkey)) {
// capitalize MPkey if necessary
MPkey = toUpperCase(MPkey);
}
}
// print MPkey at cursor location and add to message buffer; increment cursor position tracker (only if cursor is not already at max position)
if (cursor <= 32) {
if (cursor == 17) {
lcd.setCursor(0, 1);
}
lcd.print(MPkey);
messageBuffer[cursor - 1] = MPkey;
messageBuffer[cursor] = '\0';
cursor++;
}
}
}
}