/
sploder.cpp
280 lines (235 loc) · 7.37 KB
/
sploder.cpp
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
#include <Arduino.h>
/*
SPLODER - a connected button for blowing stuff up. Or whatever else you want to do with it.
Check the README.md for info on the Sploder circuit.
Copyright (C) 2015 George White <stonehippo@gmail.com>. All rights reserved.
See https://raw.githubusercontent.com/stonehippo/sploder/master/LICENSE.txt for license details.
*/
// Override enabling of logging and debug output
#define DEBUG false
#include <FiniteStateMachine.h>
#include <SPI.h>
#include "Adafruit_BLE_UART.h"
#include "LogHelpers.h"
#include "TimingHelpers.h"
#include "sploder.h"
// Hold the state of the arming switch
boolean armed = false;
// Pins configs for the buttons and switches
const byte ARMING_SWITCH = 8;
const byte TRIGGER_BUTTON = 3;
const byte POWERED_LED = 7;
const byte FIRING_LED = 5;
// current brightness of the firing LED (to enable pulsing); see the helpers for a little more detail
const float START_RADIAN = 4.712; // Start at the mid-point of the sine wave (all the way off)
const float MAX_RADIAN = 10.995;
const float RADIAN_STEP = 0.00045; // how many radians do we step the sine wave per loop? optimized for 3.3v, 8mHz set to 0.000225 for a 16mHz Arduino
const float SIN_OFFSET = 127.5; // the offset to map the sin values back to 0-255 for analogWrite
float firingLEDBrightness = START_RADIAN;
// Pin configs for the Adafruit nrf8001 breakout board
const byte BLE_REQUEST = 10;
const byte BLE_READY = 2;
const byte BLE_RESET = 9;
// Create a Bluetooth LE UART instance to set up the basic connection
Adafruit_BLE_UART BluetoothLESerial = Adafruit_BLE_UART(BLE_REQUEST, BLE_READY, BLE_RESET);
// Global to monitor the state of the nrf8001 Bluetooth board
aci_evt_opcode_t bleLastStatus = ACI_EVT_DISCONNECTED;
// Managing debounce of the button used to FIRE!
long lastDebounceTime = 0;
long debounceDelay = 1000; // This needs to be fairly large for the big dome button; that large spring is pretty bouncy
// We'll hold timing information about certain states here
long timerStartupState = 0;
long timerFiringState = 0;
// FSM states
State startupState = State(enterStartupState,updateStartupState,leaveStartupState);
State readyState = State(enterReadyState,updateReadyState,leaveReadyState);
State armedState = State(enterArmedState,updateArmedState,leaveArmedState);
State firingState = State(enterFiringState,updateFiringState,leaveFiringState);
// Kicking off the FSM
FSM stateMachine = FSM(startupState);
// ******************* BASIC ARDUINO SETUP & LOOP *******************
void setup() {
pinMode(TRIGGER_BUTTON, INPUT_PULLUP);
pinMode(ARMING_SWITCH, INPUT_PULLUP);
pinMode(POWERED_LED, OUTPUT);
pinMode(FIRING_LED, OUTPUT);
attachInterrupt(1,fireEvent,HIGH);
startLog();
BluetoothLESerial.begin();
BluetoothLESerial.setDeviceName("Sploder");
}
void loop() {
// What's up, Bluetooth?
BluetoothLESerial.pollACI();
//check the status of the Bluetooth LE connection
aci_evt_opcode_t bleStatus = BluetoothLESerial.getState();
if (bleStatus != bleLastStatus) {
if (bleStatus == ACI_EVT_DEVICE_STARTED) {
info("BLE advertising started");
}
if (bleStatus == ACI_EVT_CONNECTED) {
info("BLE connected");
// Immediately send the current state of the FSM
// Have to use the library print() helper, as the current status has not been updated yet
if (stateMachine.isInState(readyState)) {
BluetoothLESerial.print("ready");
}
if (stateMachine.isInState(armedState)) {
BluetoothLESerial.print("armed");
}
if (stateMachine.isInState(firingState)) {
BluetoothLESerial.print("firing");
}
}
if (bleStatus == ACI_EVT_DISCONNECTED) {
info("BLE disconnected");
}
bleLastStatus = bleStatus;
}
// Time to handle the state machine
stateMachine.update();
}
// ******************* INTERRUPT EVENTS *******************
void fireEvent() {
if ((millis() - lastDebounceTime) > debounceDelay) {
if (stateMachine.isInState(armedState)) {
stateMachine.transitionTo(firingState);
}
lastDebounceTime = millis();
}
}
// ******************* FSM state callback methods *******************
// -------------- Startup State ---------------
void enterStartupState() {
digitalWrite(POWERED_LED, HIGH);
note("starting up");
startTimer(timerStartupState);
}
void updateStartupState() {
if(isTimerExpired(timerStartupState, 1000)) {
stateMachine.transitionTo(readyState);
}
}
void leaveStartupState() {
note("startup complete");
clearTimer(timerStartupState);
}
// -------------- Ready State ---------------
void enterReadyState() {
blePrint("ready");
}
void updateReadyState() {
armedStatus();
if (armed) {
stateMachine.transitionTo(armedState);
}
}
void leaveReadyState() {}
// -------------- Armed State ---------------
void enterArmedState() {
armedStatus();
if (armed) {
blePrint("armed");
beginPulsingFiringLED();
}
}
void updateArmedState() {
armedStatus();
if (armed) {
pulseFiringLED();
}
if (!armed) {
blePrint("disarmed");
stateMachine.transitionTo(readyState);
}
}
void leaveArmedState() {
endPulsingFiringLED();
}
// -------------- Firing State ---------------
void enterFiringState () {
blePrint("firing");
firingLEDOn();
startTimer(timerFiringState);
}
void updateFiringState() {
delay(100);
if (isFiringLEDOn()) {
firingLEDOff();
} else {
firingLEDOn();
}
if(isTimerExpired(timerFiringState, 5000)) {
stateMachine.transitionTo(armedState);
}
}
void leaveFiringState() {
blePrint("fired");
firingLEDOff();
clearTimer(timerFiringState);
}
// ******************* HELPERS *******************
// Switch state helpers
void armedStatus() {
if (digitalRead(ARMING_SWITCH) == HIGH) {
armed = false;
} else {
armed = true;
}
}
// Firing LED helpers
void firingLEDOff() {
digitalWrite(FIRING_LED, LOW);
}
void firingLEDOn() {
digitalWrite(FIRING_LED, HIGH);
}
// Firing LED animation helpers
void beginPulsingFiringLED() {
firingLEDBrightness = START_RADIAN;
}
boolean isFiringLEDOn() {
if (digitalRead(FIRING_LED) == HIGH) {
return true;
}
return false;
}
// We use a sine wave to get a nice, clean pulse for the LED
// Rather than using an explicit loop, we use the existing Arduino loop, the LED gets updated
// at the speed of the microcontroller in a minimally-blocking manner
// See https://www.sparkfun.com/tutorials/329 for more
void pulseFiringLED() {
if (firingLEDBrightness < MAX_RADIAN) {
firingLEDBrightness = firingLEDBrightness + RADIAN_STEP;
} else {
beginPulsingFiringLED();
}
float currentBrightness = sin(firingLEDBrightness) * SIN_OFFSET + SIN_OFFSET;
analogWrite(FIRING_LED, currentBrightness);
}
void endPulsingFiringLED() {
firingLEDOff();
}
// Bluetooth UART helpers
boolean bleIsConnected() {
if (bleLastStatus == ACI_EVT_CONNECTED) {
return true;
}
return false;
}
/*
The nrf8001 breakout has a maximum send buffer of 20 bytes, so we need to
convert message strings into a byte array for buffering.
I'm also using my own helper so I can check the status of the modem easily
to prevent hanging when there's no connection.
*/
void blePrint(String message) {
if (bleIsConnected()) {
uint8_t sendBuffer[20];
message.getBytes(sendBuffer, 20);
char sendBufferSize = min(20, message.length());
BluetoothLESerial.write(sendBuffer, sendBufferSize);
}
// echo out the message to the serial console, regardless of BLE status
note(message);
}