-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWaveTooEasy.ino
More file actions
437 lines (356 loc) · 9.13 KB
/
WaveTooEasy.ino
File metadata and controls
437 lines (356 loc) · 9.13 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/***************************************************************************
* Artekit Wavetooeasy
* https://www.artekit.eu/products/devboards/wavetooeasy
*
Written by Ivan Meleca
* Copyright (c) 2021 Artekit Labs
* https://www.artekit.eu
### WaveTooEasy.ino
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
***************************************************************************/
#include <Arduino.h>
#include "Debug.h"
#include "IoPin.h"
#include "PropConfig.h"
#include "lm49450.h"
#include "Led.h"
#include "SerialProtocol.h"
#include "Player.h"
#include "version.h"
#include "TimeCounter.h"
#include <strings.h>
#include <ctype.h>
#define IO_PINS_COUNT 16
#define MODE_IO 1
#define MODE_LATCHED 2
#define MODE_SERIAL 3
#define GPIOA_MASK 0x19FF
#define GPIOB_MASK 0x600
#define GPIOC_MASK 0xE000
// Latch
static volatile bool latched = false;
static volatile uint32_t latched_num;
static Player* latch_player = NULL;
static void latchInterrupt();
// Configuration
static PropConfig config;
static uint32_t sample_rate;
static float speakers_volume_db;
static float headphone_volume_db;
static uint8_t mode;
static uint32_t baudrate;
static bool disable_leds = false;
static bool latch_in_active_low = false;
static bool latch_io_active_low = false;
static uint32_t low_power_timeout;
// Initialization
static bool initialized = false;
// Serial protocol
static SerialProtocol serial_protocol = SerialProtocol::getInstance();
// LEDs
static BoardLed led1(LED1);
static BoardLed led2(LED2);
// Channels
static IoPin* io_pins[IO_PINS_COUNT];
// Players
PlayersPool players = PlayersPool::getInstance();
// Timers
TimeCounter low_power_timer;
bool readConfig()
{
// Set some defaults
sample_rate = 44100;
speakers_volume_db = 0;
headphone_volume_db = 0;
mode = MODE_IO;
baudrate = 115200;
disable_leds = false;
// Fixed configuration name
if (!config.begin("config.ini", false))
return false;
char szMode[32];
uint32_t szModeLen = sizeof(szMode);
if (config.readValue("settings", "mode", szMode, &szModeLen))
{
if (strncasecmp("serial", szMode, szModeLen) == 0)
mode = MODE_SERIAL;
else if (strncasecmp("io", szMode, szModeLen) == 0)
mode = MODE_IO;
else if (strncasecmp("latch", szMode, szModeLen) == 0)
mode = MODE_LATCHED;
}
config.readValue("settings", "sample_rate", &sample_rate);
config.readValue("settings", "speakers_volume", &speakers_volume_db);
config.readValue("settings", "headphone_volume", &headphone_volume_db);
config.readValue("settings", "baudrate", &baudrate);
config.readValue("settings", "disable_leds", &disable_leds);
config.readValue("settings", "low_power_timeout", &low_power_timeout);
low_power_timeout *= 1000;
return true;
}
void cleanupIoMode()
{
for (uint8_t i = 0; i < IO_PINS_COUNT; i++)
{
if (io_pins[i])
{
io_pins[i]->end();
delete io_pins[i];
}
}
}
static void strtolower(char* str)
{
char *p = str;
while (*p)
{
*p = tolower(*p);
p++;
}
}
bool initializeIoMode()
{
char io_name[32];
char tmp[256];
uint32_t str_len;
PinPolarity polarity;
PinTriggerType trigger;
PlayMode playback;
DeassertMode deassert;
uint32_t debounce = 20;
float volume;
// Initialize players list
players.initialize(true);
// Initialize every pin
for (uint8_t i = 0; i < IO_PINS_COUNT; i++)
{
// Set some defaults
playback = PlayModeNormal;
polarity = PinActiveHigh;
trigger = LevelTrigger;
deassert = DeassertRestart;
volume = 1.0f;
debounce = 5;
memset(tmp, 0, sizeof(tmp));
sprintf(io_name, "pin%i_mode", i + 1);
str_len = sizeof(tmp);
if (config.readValue("io", io_name, tmp, &str_len))
{
strtolower(io_name);
if (strstr(tmp, "loop") != NULL)
playback = PlayModeLoop;
if (strstr(tmp, "low") != NULL)
polarity = PinActiveLow;
if (strstr(tmp, "edge") != NULL)
trigger = EdgeTrigger;
if (strstr(tmp, "pause") != NULL)
deassert = DeassertPause;
else if (strstr(tmp, "stop") != NULL)
// This should have sense only when trigger = EdgeTrigger
deassert = DeassertStop;
} else continue;
// Read file name (if any)
sprintf(io_name, "pin%i_file", i + 1);
str_len = sizeof(tmp);
if (!config.readValue("io", io_name, tmp, &str_len) || !strlen(tmp))
sprintf(tmp, "%i.wav", i + 1);
// Read volume
sprintf(io_name, "pin%i_volume", i + 1);
if (!config.readValue("io", io_name, &volume))
volume = 1.0f;
// Read debouncer settings
sprintf(io_name, "pin%i_debounce", i + 1);
config.readValue("io", io_name, &debounce);
io_pins[i] = new IoPin(i, tmp, polarity, trigger, playback, volume, deassert, debounce);
if (!io_pins[i])
{
cleanupIoMode();
return false;
}
io_pins[i]->begin();
}
return true;
}
static bool initializeLatchedMode()
{
// Initialize players list
players.initialize(false);
latch_player = players.get(0);
latch_in_active_low = false;
latch_io_active_low = false;
config.readValue("latch", "latch_polarity", &latch_in_active_low);
config.readValue("latch", "input_polarity", &latch_io_active_low);
// Initialize channels pins
for (uint8_t i = 0; i < IO_PINS_COUNT; i++)
pinMode(i, (latch_io_active_low) ? INPUT_PULLUP : INPUT_PULLDOWN);
// Initialize latch pin
pinMode(LATCH, (latch_in_active_low) ? INPUT_PULLUP : INPUT_PULLDOWN);
attachInterrupt(LATCH, latchInterrupt, (latch_in_active_low) ? FALLING : RISING);
return true;
}
static bool initializeSerialMode()
{
// Initialize players list
players.initialize(false);
serial_protocol.begin(Serial, baudrate);
return true;
}
static void pollSerialMode()
{
bool activity = serial_protocol.poll();
if (activity && low_power_timer.active())
low_power_timer.startTimeoutCounter(low_power_timeout);
}
static void pollIoMode()
{
bool activity = false;
for (uint8_t i = 0; i < IO_PINS_COUNT; i++)
activity |= io_pins[i]->poll();
if (activity && low_power_timer.active())
low_power_timer.startTimeoutCounter(low_power_timeout);
}
static void pollLatchedMode()
{
uint16_t num;
// Max. file name is 65535.wav
char file[10];
if (latched)
{
__disable_irq();
num = latched_num;
latched = false;
__enable_irq();
if (latch_io_active_low)
num = ~num;
debugMsg(DebugInfo, "Latch detected, num = %i", num);
sprintf(file, "%i.wav", num);
if (latch_player->play(file))
debugMsg(DebugInfo, "Latched mode - playing %s", file);
else
debugMsg(DebugError, "Latched mode - error playing %s", file);
if (low_power_timer.active())
low_power_timer.startTimeoutCounter(low_power_timeout);
}
}
static void pollAudioActivityLED()
{
static bool playing = false;
if (disable_leds)
return;
if (Audio.isPlaying() && !playing)
{
playing = true;
led1.stopBlink();
led1.blink(250, 125);
} else if (!Audio.isPlaying() && playing)
{
playing = false;
led1.stopBlink();
led1.blink(1000, 500);
}
}
static void lowPowerMode()
{
// Stop audio
Audio.end();
if (!disable_leds)
{
led1.setOff();
led2.setOff();
led1.end();
led2.end();
}
if (mode == MODE_SERIAL)
Serial.end();
// Deinitialize every pin
for (uint8_t i = 0; i < IO_PINS_COUNT; i++)
{
if (io_pins[i])
io_pins[i]->end();
}
enterLowPowerMode();
}
void setup()
{
#if ENABLE_DEBUG
initDebug();
#endif
debugMsg(DebugInfo, "Artekit wavetooeasy version %i.%i.%i", VERSION_MAJOR, VERSION_MINOR, VERSION_FIX);
// Configure LEDs
led1.initialize();
led2.initialize();
if (!readConfig())
{
// Cannot read configuration file, blink both LEDs and wait for reset
led1.blink(500, 250);
led2.blink(500, 250);
while(true);
}
if (!disable_leds)
{
led2.setOn();
led1.blink(1000, 500);
}
Audio.begin(sample_rate);
Audio.setSpeakersVolume(speakers_volume_db);
Audio.setHeadphoneVolume(headphone_volume_db);
delay(500);
switch (mode)
{
case MODE_IO:
initialized = initializeIoMode();
break;
case MODE_LATCHED:
initialized = initializeLatchedMode();
break;
case MODE_SERIAL:
initialized = initializeSerialMode();
}
initialized = true;
if (initialized && low_power_timeout)
low_power_timer.startTimeoutCounter(low_power_timeout);
}
void loop()
{
if (!initialized)
return;
pollAudioActivityLED();
players.poll();
switch (mode)
{
case MODE_IO:
pollIoMode();
break;
case MODE_SERIAL:
pollSerialMode();
break;
case MODE_LATCHED:
pollLatchedMode();
break;
}
// Check if it's time to enter low power mode
if (low_power_timer.active() && low_power_timer.timeout())
{
// Check if we have players playing
if (!players.playing())
lowPowerMode();
else
low_power_timer.startTimeoutCounter(low_power_timeout);
}
}
void latchInterrupt()
{
// Collect IO pin states into a 16 bit number
latched_num = GPIOA->IDR & GPIOA_MASK;
latched_num |= GPIOB->IDR & GPIOB_MASK;
latched_num |= GPIOC->IDR & GPIOC_MASK;
latched = true;
}