-
Notifications
You must be signed in to change notification settings - Fork 0
/
darter.ino
106 lines (84 loc) · 2.81 KB
/
darter.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
#include "definitions.h";
#include "neopixel.h";
#include "MusicPlayer.h";
#include "LightshowController.h";
#include "ImpactSensor.h";
#include "MQTTClient.h";
#include "MQTT.h";
#include "ArduinoJson.h";
#include "papertrail.h";
ImpactSensor *impactSensor;
MusicPlayer *player;
LightshowController *lightshowController;
Adafruit_NeoPixel *strip;
MQTT *mqttConnection;
MQTTClient *mqttClient;
byte serverIP[] = {MQTT_BROKER_HOST_B1, MQTT_BROKER_HOST_B2, MQTT_BROKER_HOST_B3, MQTT_BROKER_HOST_B4};
PapertrailLogHandler papertailHandler(SYSLOG_HOST, SYSLOG_PORT, "ballistic", "ballistic-client-" + System.deviceID(), LOG_LEVEL_INFO);
SerialLogHandler serialLogger(LOG_LEVEL_INFO);
void endHit();
void nullCallback() {}
void mqttCallback(char* topic, byte* payload, unsigned int length);
void hitDetected(int reading);
void setup() {
pinMode(SPEAKER_PIN, OUTPUT);
pinMode(IMPACT_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
impactSensor = new ImpactSensor(IMPACT_PIN, LED_PIN, IMPACT_THRESHOLD, HIT_DELAY_MS, &hitDetected);
player = new MusicPlayer();
strip = new Adafruit_NeoPixel(NEOPIXEL_COUNT, NEOPIXEL_PIN, NEOPIXEL_TYPE);
strip->begin();
strip->setBrightness(NEOPIXEL_BRIGHTNESS);
lightshowController = new LightshowController(strip);
lightshowController->playShow("idle", true, &nullCallback);
digitalWrite(LED_PIN, LOW);
Log.info("Initial MQTT setup");
mqttConnection = new MQTT(serverIP, MQTT_BROKER_PORT, mqttCallback);
mqttClient = new MQTTClient(mqttConnection, System.deviceID());
mqttClient->connect();
Log.info("Setup Complete");
Particle.publish("Setup Complete");
}
void ticks() {
impactSensor->tick();
player->tick();
lightshowController->tick();
mqttClient->tick();
}
void loop() {
ticks();
}
// Callback for the ImpactSensor
void hitDetected(int reading) {
Log.info("Hit: %d", reading);
mqttClient->publishHit();
}
void endShow() {
Log.trace("Ending show. Back to idle.");
lightshowController->playShow("idle", true, &nullCallback);
}
void playShow(String showId) {
Log.info("Playing show: %s", showId.c_str());
if(showId.equals("win") || showId.equals("lose")) {
lightshowController->playShow(showId, false, &endShow);
} else if(showId.equals("live")) {
lightshowController->playShow(showId, true, &endShow);
}
player->playTune(showId);
}
void mqttCallback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
String message(p);
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(p);
Log.trace("MQTT message received: %s", topic);
String topicStr(topic);
if(topicStr.endsWith("playShow")) {
const char* showId = root["showId"];
playShow(String(showId));
} else if(topicStr.endsWith("requestIntroduction")) {
mqttClient->publishIntroduction();
}
}