-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbannerControl.cpp
More file actions
372 lines (311 loc) · 7.95 KB
/
Copy pathbannerControl.cpp
File metadata and controls
372 lines (311 loc) · 7.95 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include <Wire.h>
#include <SPI.h>
#include <LoRa.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// TTGO LoRa32 banner controller port
// Commands over LoRa:
// - DRO: start banner drop
// - PUL: start banner pull
// - STP: stop all motion
// - TST: short relay test
// - RST: reset hall counter
// - GET: publish current state
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define I2C_SDA 21
#define I2C_SCL 22
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define CONFIG_MOSI 27
#define CONFIG_MISO 19
#define CONFIG_CLK 5
#define CONFIG_NSS 18
#define CONFIG_RST 23
#define CONFIG_DIO0 26
static const int RELAY_PINS[4] = {2, 15, 13, 12};
static const int HALL_SENSOR_PIN = 32;
static const int STOP_BTN_PIN = 33;
static const int LED_PIN = 25;
static const int BAT_ADC_PIN = 35;
static const int TARGET_COUNT = 60;
static const unsigned long DISPLAY_IDLE_MS = 120000UL;
static const unsigned long BAT_SAMPLE_MS = 2000UL;
static const unsigned long COUNT_PUBLISH_MS = 250UL;
static const unsigned long LED_PULSE_MS = 120UL;
enum BannerAction { ACTION_IDLE, ACTION_DROP, ACTION_PULL, ACTION_TEST };
volatile int turnCount = 0;
volatile bool hallTick = false;
bool relayState[4] = {false, false, false, false};
BannerAction action = ACTION_IDLE;
String lastMsg = "READY";
bool ledOn = false;
unsigned long ledUntil = 0;
unsigned long lastUiAt = 0;
unsigned long lastDisplayActivityAt = 0;
unsigned long nextBatSampleAt = 0;
unsigned long nextCountPublishAt = 0;
float batteryVoltage = 0.0f;
bool haveBattery = false;
int lastPublishedCount = -1;
bool displayReady = false;
void hallSensorISR() {
turnCount++;
hallTick = true;
}
void setRelay(int relay, bool on) {
if (relay < 1 || relay > 4) return;
relayState[relay - 1] = on;
digitalWrite(RELAY_PINS[relay - 1], on ? LOW : HIGH);
}
void allRelaysOff() {
for (int i = 0; i < 4; i++) {
relayState[i] = false;
digitalWrite(RELAY_PINS[i], HIGH);
}
}
void pulseLed() {
digitalWrite(LED_PIN, HIGH);
ledOn = true;
ledUntil = millis() + LED_PULSE_MS;
}
void ledService() {
if (ledOn && (long)(millis() - ledUntil) >= 0) {
digitalWrite(LED_PIN, LOW);
ledOn = false;
}
}
void noteActivity() {
lastDisplayActivityAt = millis();
}
const char *actionName(BannerAction a) {
switch (a) {
case ACTION_DROP: return "DROP";
case ACTION_PULL: return "PULL";
case ACTION_TEST: return "TEST";
default: return "IDLE";
}
}
void sendPacket(const String &msg) {
LoRa.beginPacket();
LoRa.print(msg);
LoRa.endPacket();
}
void publishState() {
String sta = "STA ";
for (int i = 0; i < 4; i++) sta += (relayState[i] ? '1' : '0');
sendPacket(sta);
sendPacket("CNT " + String(turnCount));
sendPacket("ACT " + String(actionName(action)));
}
void readBattery() {
analogSetPinAttenuation(BAT_ADC_PIN, ADC_11db);
uint32_t sum = 0;
const int samples = 8;
for (int i = 0; i < samples; i++) {
sum += analogRead(BAT_ADC_PIN);
delay(2);
}
float raw = (float)sum / (float)samples;
float vAdc = (raw / 4095.0f) * 3.3f;
float vBat = vAdc * 2.0f;
if (vBat > 0.5f && vBat < 6.0f) {
batteryVoltage = vBat;
haveBattery = true;
}
}
void drawUI() {
if (!displayReady) return;
display.clearDisplay();
display.setTextColor(1);
display.setCursor(0, 12);
display.print("BANNER");
display.setCursor(0, 24);
display.print("ACT:");
display.print(actionName(action));
display.setCursor(0, 34);
display.print("CNT:");
display.print(turnCount);
display.print("/");
display.print(TARGET_COUNT);
display.setCursor(0, 44);
display.print("MSG:");
display.print(lastMsg);
display.setCursor(0, 56);
display.print("BAT:");
if (haveBattery) {
display.print(batteryVoltage, 2);
display.print("V");
} else {
display.print("N/A");
}
display.setCursor(72, 24);
display.print(relayState[1] ? "HALL:ON" : "HALL:OFF");
display.setCursor(72, 34);
display.print(relayState[2] ? "UP:ON" : "UP:OFF");
display.setCursor(72, 44);
display.print(relayState[3] ? "DN:ON" : "DN:OFF");
display.display();
lastUiAt = millis();
}
void startMotion(BannerAction nextAction) {
allRelaysOff();
turnCount = 0;
lastPublishedCount = -1;
action = nextAction;
if (nextAction == ACTION_TEST) {
setRelay(4, true);
delay(400);
setRelay(4, false);
delay(150);
setRelay(3, true);
delay(400);
setRelay(3, false);
setRelay(2, false);
action = ACTION_IDLE;
lastMsg = "TEST DONE";
noteActivity();
pulseLed();
drawUI();
publishState();
return;
}
setRelay(2, true);
delay(250);
if (nextAction == ACTION_DROP) {
setRelay(4, true);
lastMsg = "DROP";
} else if (nextAction == ACTION_PULL) {
setRelay(3, true);
lastMsg = "PULL";
}
attachInterrupt(digitalPinToInterrupt(HALL_SENSOR_PIN), hallSensorISR, CHANGE);
noteActivity();
pulseLed();
drawUI();
publishState();
}
void stopMotion(const String &reason) {
detachInterrupt(digitalPinToInterrupt(HALL_SENSOR_PIN));
allRelaysOff();
action = ACTION_IDLE;
lastMsg = reason;
noteActivity();
pulseLed();
drawUI();
publishState();
}
void resetCounter() {
turnCount = 0;
lastPublishedCount = -1;
lastMsg = "COUNT RESET";
noteActivity();
drawUI();
publishState();
}
void serviceMotion() {
if (action == ACTION_IDLE) return;
if (digitalRead(STOP_BTN_PIN) == LOW) {
stopMotion("STOP BTN");
return;
}
int countSnapshot;
noInterrupts();
countSnapshot = turnCount;
interrupts();
if (countSnapshot >= TARGET_COUNT && action != ACTION_TEST) {
stopMotion("DONE");
return;
}
if (millis() >= nextCountPublishAt && countSnapshot != lastPublishedCount) {
lastPublishedCount = countSnapshot;
nextCountPublishAt = millis() + COUNT_PUBLISH_MS;
sendPacket("CNT " + String(countSnapshot));
}
}
void handleCommand(String msg) {
msg.trim();
msg.toUpperCase();
if (msg == "DRO" || msg == "DROP") {
startMotion(ACTION_DROP);
return;
}
if (msg == "PUL" || msg == "PULL") {
startMotion(ACTION_PULL);
return;
}
if (msg == "STP" || msg == "STOP") {
stopMotion("STOP");
return;
}
if (msg == "TST" || msg == "TEST") {
startMotion(ACTION_TEST);
return;
}
if (msg == "RST" || msg == "RESET" || msg == "RESETCOUNTER") {
resetCounter();
return;
}
if (msg.startsWith("GET")) {
publishState();
return;
}
if (msg == "ACK") return;
lastMsg = msg;
noteActivity();
drawUI();
}
void readLora() {
int packetSize = LoRa.parsePacket();
if (!packetSize) return;
String msg;
while (LoRa.available()) msg += (char)LoRa.read();
msg.trim();
lastMsg = msg;
noteActivity();
pulseLed();
handleCommand(msg);
}
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
pinMode(HALL_SENSOR_PIN, INPUT_PULLUP);
pinMode(STOP_BTN_PIN, INPUT_PULLUP);
for (int i = 0; i < 4; i++) {
pinMode(RELAY_PINS[i], OUTPUT);
digitalWrite(RELAY_PINS[i], HIGH);
}
Wire.begin(I2C_SDA, I2C_SCL);
Wire.setClock(400000);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) while (1) {}
display.display();
displayReady = true;
analogReadResolution(12);
nextBatSampleAt = millis() + 200;
nextCountPublishAt = millis() + COUNT_PUBLISH_MS;
SPI.begin(CONFIG_CLK, CONFIG_MISO, CONFIG_MOSI, CONFIG_NSS);
LoRa.setPins(CONFIG_NSS, CONFIG_RST, CONFIG_DIO0);
if (!LoRa.begin(868E6)) while (1) {}
LoRa.setSpreadingFactor(8);
LoRa.setSignalBandwidth(125E3);
LoRa.setCodingRate4(5);
LoRa.setSyncWord(120);
LoRa.setTxPower(20, PA_OUTPUT_PA_BOOST_PIN);
publishState();
drawUI();
}
void loop() {
ledService();
readLora();
serviceMotion();
if (millis() >= nextBatSampleAt) {
nextBatSampleAt = millis() + BAT_SAMPLE_MS;
readBattery();
drawUI();
}
if ((long)(millis() - lastDisplayActivityAt) >= (long)DISPLAY_IDLE_MS) {
// keep display on for this controller; a future revision can blank it
lastDisplayActivityAt = millis();
}
}