-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheartware.ino
282 lines (228 loc) · 5.82 KB
/
heartware.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
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
// build settings change between featherboard and arduino nano
#define __BUILD_FEATHER__ // to switch to Arduino #undef this line...
#undef __BUILD_NANO__ // ... and #define this one
#ifdef __BUILD_NANO__
#include <Arduino.h>
#endif
#ifdef __BUILD_FEATHER__
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
// this library is needed: https://github.com/CNMAT/OSC
#include <OSCMessage.h>
#endif
//#include <Plaquette.h>
//#include <PqExtra.h>
//
#include "coils.h"
#include "animation.h"
//#define INPUT_PIN A0 // change as needed
//#define STDDEV_THRESHOLD 0.9 //.0
//
//AnalogIn in(INPUT_PIN);
//AdaptiveNormalizer normalizer(0, 1);
//
//float lastreading, thisreading;
//float IBI = 0;
//unsigned long lastbeat = 0;
//unsigned long now = 0;
// TESTED OK
#ifdef __BUILD_FEATHER__
#include "wifisettings.h"
#endif
bool flapUp = false;
unsigned long lastFlapUp = 0;
int coil = 0;
const int beatTime = 20; // in ms
int beats[3] = {20, 80, 60};
int beatidx = 0;
int heartware_id = -1;
Animation *current = NULL;
// INIT /////////////////////////////////////////////////////////////
/*
void init_flappy_board() {
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
flapUp = false;
lastFlapUp = millis();
}
*/
void init_animator() {
// needed for animator
flapUp = false;
lastFlapUp = millis();
}
#ifdef __BUILD_FEATHER__
void init_wifi() {
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
thisip = WiFi.localIP();
Serial.println( thisip );
// sensor ID is the last byte in the IP quad
heartware_id = thisip[3];
Serial.println("Starting UDP");
Udp.begin(localPort);
Serial.print("Local port: ");
Serial.println(Udp.localPort());
}
#endif
void setup() {
Serial.begin(115200);
while( !Serial ) {} // wait for serial init
// lastreading = thisreading = 0;
init_flappy_board();
init_animator();
#ifdef __BUILD_FEATHER__
init_wifi();
#endif
Serial.println("Initializing...");
current = new Flutter(coils, 1000); //new LeftRight(coils, 120); //Heartbeat(coils, 120);
}
// ANIMATION ////////////////////////////////////////////////////////////
void animation_change(Animation *anim) {
current = anim;
}
void animation_loop() {
if( (current != NULL)
&& (current->ready()) ) {
current->update();
draw_coils();
}
if(current->finished()) {
Serial.println("FINISHED!");
}
}
/*
void sensor_loop() {
lastreading = thisreading;
// send to normalizer
in >> normalizer;
thisreading = normalizer;
Serial.print(thisreading);
Serial.print(",");
Serial.print(STDDEV_THRESHOLD);
Serial.print(",");
if ( (lastreading < STDDEV_THRESHOLD)
&& (thisreading >= STDDEV_THRESHOLD) ) { //(norm > STDDEV_THRESHOLD) { // oldvalue<threshold && newvalue>=threshold){
Serial.print(3.0);
now = millis();
IBI = abs(now - lastbeat); //2 + (abs(now - lastbeat) / 1000); // IBI in seconds
lastbeat = now;
// if(current) {
// current->reset();
// }
#ifdef __BUILD_FEATHER__
OSCMessage out("/beat");
out.add(IBI);
Udp.beginPacket(outIp, outPort);
out.send(Udp);
Udp.endPacket();
#endif
} else {
Serial.print(.0);
// tip (just for fun): lines 17-20 can be replaced by this: (normalizer > STDDEV_THRESHOLD) >> led;
}
// Serial.print(",");
// Serial.print(IBI);
Serial.println();
delay(20);
}
*/
// MESSAGE HANDLERS /////////////////////////////////////////////////////
#ifdef __BUILD_FEATHER__
void on_sleep(OSCMessage &msg, int addrOffset) {
Serial.println("going to sleep to save battery...");
ESP.deepSleep(10 * 1000000);
}
void on_scene_1(OSCMessage &msg, int addrOffset) {
}
void on_scene_2(OSCMessage &msg, int addrOffset) {
}
void on_scene_3(OSCMessage &msg, int addrOffset) {
}
void on_scene_4(OSCMessage &msg, int addrOffset) {
}
void on_beat_single(OSCMessage &msg, int addrOffset) {
}
void on_coil(OSCMessage &msg, int addrOffset) {
if( msg.isInt(0) ) {
coil = msg.getInt(0);
Serial.print("beating flap #");
Serial.println(coil);
if( (coil > 0) && (coil < COILS) ) {
Serial.println("kick-out");
coil_write(coil, 1);
delay(10);
Serial.println("kick-in");
coil_write(coil, 2);
delay(10);
Serial.println("rest");
coil_write(coil, 0);
delay(10);
} else {
Serial.println("flap number out of range");
}
} // if
}
// //////////////////////////////////////////////////////////////
void osc_message_pump() {
OSCMessage in;
int size;
if( (size = Udp.parsePacket()) > 0)
{
// parse incoming OSC message
while(size--) {
in.fill( Udp.read() );
}
if(!in.hasError()) {
in.route("/wearable/sleep", on_sleep);
in.route("/wearable/beat", on_beat_single);
in.route("/wearable/coil", on_coil);
in.route("/wearable/scene/1", on_scene_1);
in.route("/wearable/scene/2", on_scene_2);
in.route("/wearable/scene/3", on_scene_3);
in.route("/wearable/scene/4", on_scene_4);
}
} // if
}
#endif
void loop() {
// SEND
// OSCMessage out("/test");
// out.add(123); //"hello, osc.");
//
// Udp.beginPacket(outIp, outPort);
// out.send(Udp);
// Udp.endPacket();
//
// out.empty();
//delay(5);
// if( (flapUp == true)
// && ((millis() - lastFlapUp) > beatTime) ) {
// coil_write(coil, INACTIVE);
// flapUp = false;
// }
// update animation sequence
animation_loop();
// update physical coils
update_coils();
// sensor_loop();
if( current->finished() ) {
// delay(2000);
current->reset();
}
#ifdef __BUILD_FEATHER__
// run message pump
osc_message_pump();
#endif
} // loop()