-
Notifications
You must be signed in to change notification settings - Fork 2
/
uCtrl.cpp
670 lines (568 loc) · 14.7 KB
/
uCtrl.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
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/*!
* @file uCtrl.cpp
* Project Arduino Library API interface for uMODULAR projects
* @brief ...
* @version 1.1.0
* @author Romulo Silva
* @date 30/10/22
* @license MIT - (c) 2022 - Romulo Silva - contact@midilab.co
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "uCtrl.h"
//
// General Arduino AVRs port
//
#if defined(ARDUINO_ARCH_AVR)
#include "platforms/avr.h"
#endif
//
// Teensyduino ARMs port
//
#if defined(TEENSYDUINO)
#include "platforms/teensy.h"
#endif
//
// Seedstudio XIAO M0 port
//
#if defined(SEEED_XIAO_M0)
#include "platforms/samd.h"
#endif
//
// ESP32 family
//
#if defined(ARDUINO_ARCH_ESP32) || defined(ESP32)
#include "platforms/esp32.h"
#endif
//
// STM32XX family
//
#if defined(ARDUINO_ARCH_STM32)
#include "platforms/stm32.h"
#endif
//
// RP2040 (Raspberry Pico) family
//
#if defined(ARDUINO_ARCH_RP2040)
#include "platforms/rp2040.h"
#endif
namespace uctrl {
//
// Platform specific timer setup/control
//
// initTimer(uint32_t us_interval) and setTimer(uint32_t us_interval)
// are called from architecture specific module included at the
// header of this file
void enableTimer()
{
// begin at 250us task
initTimer(250);
}
uCtrlClass::uCtrlClass()
{
loopCallback = nullptr;
on250usCallback = nullptr;
on1msCallback = nullptr;
}
// Not called on arduino
uCtrlClass::~uCtrlClass()
{
}
bool uCtrlClass::initRam(SPIClass * device, uint8_t chip_select, bool is_shared)
{
if ( ram == nullptr )
ram = &ram_module;
//ram = new uctrl::module::Ram();
if ( ram != nullptr ) {
ram->init(device, chip_select, is_shared);
return true;
}
return false;
}
bool uCtrlClass::initStorage(SPIClass * spi_device, bool is_shared)
{
if ( storage == nullptr )
storage = new uctrl::module::Storage();
if ( storage != nullptr ) {
storage->init(spi_device, is_shared);
return true;
} else {
storage->init();
return true;
}
return false;
}
bool uCtrlClass::initSdCard(SPIClass * spi_device, uint8_t chip_select, bool is_shared)
{
if ( sdcard == nullptr )
sdcard = new uctrl::module::SdCard();
if ( sdcard != nullptr ) {
sdcard->init(spi_device, chip_select, is_shared);
return true;
}
return false;
}
bool uCtrlClass::initDevice(uint8_t device_number, uint16_t event_buffer_size, uint8_t sysex_buffer_size, uint16_t device_label_buffer_size)
{
if ( device == nullptr )
device = new uctrl::module::Device();
if ( device != nullptr ) {
device->init(device_number, event_buffer_size, sysex_buffer_size, device_label_buffer_size);
return true;
}
return false;
}
bool uCtrlClass::initPage(uint8_t pages_size)
{
if ( page == nullptr )
page = new uctrl::module::Page();
if ( page != nullptr ) {
page->init(pages_size);
return true;
}
return false;
}
#if defined(USE_OLED_U8G2)
bool uCtrlClass::initOled(U8G2 * display)
#else // defined(USE_OLED_U8G2)
bool uCtrlClass::initOled(U8X8 * display)
#endif // defined(USE_OLED_U8G2)
{
if ( oled == nullptr )
oled = new uctrl::module::Oled();
if ( oled != nullptr ) {
// plug display
oled->plug(display);
return true;
}
return false;
}
//#if defined(USE_EXT_RAM)
//#if defined(USE_DEVICE)
void uCtrlClass::processDisplay()
{
if ( device->showDataFeedback() == true ) {
oled->setDisplayLockState(false);
device->dataFeedbackCallback();
oled->setDisplayLockState(true);
if ( ((millis() - device->getDataFeedbackTimeout()) >= 1000) && device->getCtrlMode() != 2 ) {
device->setDataFeedback(false);
oled->setDisplayLockState(false);
oled->clearDisplay(1,1,1);
}
}
}
//#endif // defined(USE_DEVICE)
//#endif // defined(USE_EXT_RAM)
bool uCtrlClass::initMidi()
{
if ( midi == nullptr ) {
midi = &midi_module;
//midi = new uctrl::module::Midi();
}
if ( midi != nullptr ) {
return true;
}
return false;
}
bool uCtrlClass::initDout(SPIClass * spi_device, uint8_t latch_pin, bool is_shared)
{
if ( dout == nullptr )
dout = new uctrl::module::Dout();
if ( dout != nullptr ) {
if (spi_device != nullptr) {
dout->setSpi(spi_device, latch_pin, is_shared);
}
return true;
}
return false;
}
bool uCtrlClass::initDin(SPIClass * spi_device, uint8_t latch_pin, bool is_shared)
{
if ( din == nullptr )
din = new uctrl::module::Din();
if ( din != nullptr ) {
if (spi_device != nullptr) {
din->setSpi(spi_device, latch_pin, is_shared);
}
return true;
}
return false;
}
bool uCtrlClass::initAin(int8_t pin1, int8_t pin2, int8_t pin3, int8_t pin4)
{
if ( ain == nullptr )
ain = new uctrl::module::Ain();
if ( ain != nullptr ) {
// pin1 as argument means mux request to register
if (pin1 >= 0) {
ain->setMuxPins(pin1, pin2, pin3, pin4);
}
return true;
}
return false;
}
void uCtrlClass::processAin()
{
uint8_t size_of_ports;
int16_t value;
uint8_t port;
size_of_ports = ain->sizeOf();
//
for ( port=0; port < size_of_ports; port++ ) {
if ( device != nullptr ) {
value = ain->getData(port, device->getCtrlAdcMin(port), device->getCtrlAdcMax(port));
} else {
value = ain->getData(port);
}
if ( value > -1 ) {
if ( device != nullptr ) {
// make midi signal smooth as posible
if ( device->handleAnalogEvent(port, value, 1) == true ) {
//continue;
}
} else {
// ain callback is processed inside a timmer interrupt, so always be short inside it!
if ( ain->rtCallback != nullptr ) {
ain->rtCallback(port, value);
continue;
}
}
// add event to non interrupted queue in case no device control setup
uint8_t tail = (_ain_event_queue.tail+1) >= _ain_event_queue.size ? 0 : (_ain_event_queue.tail+1);
if ( _ain_event_queue.head != tail )
{
_ain_event_queue.event[_ain_event_queue.tail].port = port;
_ain_event_queue.event[_ain_event_queue.tail].value = value;
_ain_event_queue.tail = tail;
}
}
}
}
bool uCtrlClass::initCapTouch(int8_t pin1, int8_t pin2, int8_t pin3, int8_t pin4)
{
if ( touch == nullptr ) {
//touch = &cap_touch_module;
touch = new uctrl::module::CapTouch();
}
if ( touch != nullptr ) {
touch->setControlPins(pin1, pin2, pin3, pin4);
return true;
} else {
return false;
}
}
void uCtrlClass::init()
{
// init of hardware configuration
// if we have sdcard support, then retrive hardware config info from ucontrol.cfg on sdcard root filesystem
if ( sdcard != nullptr ) {
// load default config
} else {
// load factory defaults
}
if (din != nullptr) {
din->init();
}
if (dout != nullptr) {
dout->init();
}
if (touch != nullptr) {
touch->init();
}
if (ain != nullptr) {
ain->init();
_ain_event_queue.head = 0;
_ain_event_queue.tail = 0;
_ain_event_queue.size = 8;
}
if (page != nullptr) {
// default page and subpge
if (page->getPageSize() > 0) {
page->setPage(0);
page->setSubPage(0);
}
}
// ...
enableTimer();
}
void uCtrlClass::run()
{
int16_t value;
uint8_t port, head;
uint8_t port_ref = 0;
bool discard_ain_data = false;
// timmer dependent UI visual effects
uint32_t time = millis();
if (dout != nullptr) {
uCtrl.dout->setTimer(time);
}
if ( oled != nullptr ) {
uCtrl.oled->setTimer(time);
#if defined(USE_OLED_U8G2)
uCtrl.oled->clearDisplay();
#endif // defined(USE_OLED_U8G2)
}
// ~2ms call
if (din != nullptr) {
// read while empty
while ( din->event_queue.head != din->event_queue.tail )
{
ATOMIC(
port = din->event_queue.event[din->event_queue.head].port;
value = din->event_queue.event[din->event_queue.head].value;
head = (din->event_queue.head+1)%(din->event_queue.size);
din->event_queue.head = head;
)
if ( device != nullptr ) {
if ( device->handleDigitalEvent(port, value, 0) == true ) {
continue;
}
}
if (page != nullptr) {
if (ain != nullptr) {
#if defined(USE_PAGE_COMPONENT)
// before each processEvent we need to: check if pot_ctrl is needed
if(page->_use_nav_pot) {
// if it is, check if it is inc or dec commands...
if (port == page->_nav_ctrl_port.incrementer ||
port == page->_nav_ctrl_port.decrementer ||
port == page->_nav_ctrl_port.incrementer_secondary ||
port == page->_nav_ctrl_port.decrementer_secondary ||
port == page->_nav_ctrl_port.up ||
port == page->_nav_ctrl_port.down ||
port == page->_nav_ctrl_port.left ||
port == page->_nav_ctrl_port.right)
{
// if it is. lock ain pot control to avoid mess with inc/dec changes
ain->lockControl(page->_nav_ctrl_port.pot);
// we also need to remove from our event queue any data from nav pot
discard_ain_data = true;
}
}
#endif // defined(USE_PAGE_COMPONENT)
}
page->processEvent(port, value, uctrl::module::DIGITAL_EVENT);
}
if ( din->callback != nullptr )
din->callback(port, value);
}
//return;
// set port_ref in case other digital modules were initialized
port_ref = din->sizeOf();
}
// ~3ms call
if (touch != nullptr) {
// read while empty
while ( touch->event_queue.head != touch->event_queue.tail )
{
// use port_ref in case din were initialized
ATOMIC(
port = touch->event_queue.event[touch->event_queue.head].port+port_ref;
value = touch->event_queue.event[touch->event_queue.head].value;
head = (touch->event_queue.head+1)%(touch->event_queue.size);
touch->event_queue.head = head;
)
if ( device != nullptr ) {
if ( device->handleDigitalEvent(port, value, 0) == true )
continue;
}
if (page != nullptr) {
if (ain != nullptr) {
// before each processEvent we need to: check if pot_ctrl is needed
if(page->_use_nav_pot) {
// if it is, check if it is inc or dec commands...
if (port == page->_nav_ctrl_port.incrementer ||
port == page->_nav_ctrl_port.decrementer ||
port == page->_nav_ctrl_port.incrementer_secondary ||
port == page->_nav_ctrl_port.decrementer_secondary ||
port == page->_nav_ctrl_port.up ||
port == page->_nav_ctrl_port.down ||
port == page->_nav_ctrl_port.left ||
port == page->_nav_ctrl_port.right)
{
// if it is. lock ain pot control to avoid mess with inc/dec changes
ain->lockControl(page->_nav_ctrl_port.pot);
// we also need to remove from our event queue any data from nav pot
discard_ain_data = true;
}
}
}
page->processEvent(port, (uint16_t)value, uctrl::module::DIGITAL_EVENT);
}
if ( touch->callback != nullptr )
touch->callback(port, value);
}
//return;
}
// ~10ms call
if (uCtrl.ain != nullptr) {
// read while empty
while ( _ain_event_queue.head != _ain_event_queue.tail )
{
ATOMIC(
port = _ain_event_queue.event[_ain_event_queue.head].port;
value = _ain_event_queue.event[_ain_event_queue.head].value;
head = (_ain_event_queue.head+1) >= _ain_event_queue.size ? 0 : (_ain_event_queue.head+1);
_ain_event_queue.head = head;
)
#if defined(USE_PAGE_COMPONENT)
if (discard_ain_data) {
if (port == page->_nav_ctrl_port.pot) {
if (ain->isLocked(page->_nav_ctrl_port.pot) == false) {
discard_ain_data = false;
} else {
continue;
}
}
}
#endif // defined(USE_PAGE_COMPONENT)
if ( device != nullptr ) {
// device process are done inside interrupt to keep smooth for realtime controllers events
// EDIT MODE HANDLER
if ( device->getCtrlMode() == 2 ) {
device->setupCtrl(port, value);
}
if ( device->handleAnalogEvent(port, value, 0) == true ) {
continue;
}
}
if (page != nullptr) {
page->processEvent(port, value, uctrl::module::ANALOG_EVENT);
}
if ( ain->callback != nullptr ) {
ain->callback(port, value);
}
}
//return;
}
if ( page != nullptr ) {
#if defined(USE_PAGE_COMPONENT)
page->clearComponentMap();
#endif // defined(USE_PAGE_COMPONENT)
page->processView();
}
// no page module? loop callback set up?
if ( loopCallback != nullptr ) {
loopCallback();
}
if ( oled != nullptr ) {
if ( device != nullptr && ram != nullptr ) {
processDisplay();
}
#if defined(USE_OLED_U8G2)
oled->refreshDisplay();
#endif // defined(USE_OLED_U8G2)
}
if (dout != nullptr) {
uCtrl.dout->flushBuffer();
}
}
uint8_t uCtrlClass::getAnalogPorts()
{
if (ain != nullptr) {
return ain->sizeOf();
}
return 0;
}
uint8_t uCtrlClass::getDigitalPorts()
{
if (din != nullptr) {
return din->sizeOf();
}
return 0;
}
uint8_t uCtrlClass::getOutputPorts()
{
return midi->sizeOf();
/*
#if defined(UMODULAR_DMX)
#endif // defined(UMODULAR_DMX)
#if defined(UMODULAR_CV)
#endif // defined(UMODULAR_CV)
*/
}
} // end namespace uctrl
uctrl::uCtrlClass uCtrl;
//
// 250 microseconds base interrupt for
// realtime processment tasks(midi input, scan input/output modules...)
//
uint8_t _timerCounter1ms = 0;
uint8_t _timerCounterAin = 0;
uint8_t _timerCapTouch = 0;
uint8_t _timerCounterDin = 0;
uint8_t _timerCounterDout = 0;
#if defined(ARDUINO_ARCH_AVR)
#if defined(__AVR_ATmega32U4__)
ISR(TIMER3_COMPA_vect)
#else
ISR(TIMER2_COMPA_vect)
#endif
#else
void uCtrlHandler()
#endif
{
// 250us call
if (uCtrl.on250usCallback) {
uCtrl.on250usCallback();
}
if (uCtrl.on1msCallback) {
// ~1ms call
if(++_timerCounter1ms == 4) {
_timerCounter1ms = 0;
uCtrl.on1msCallback();
return;
}
}
if (uCtrl.din != nullptr) {
// ~2ms call
if (++_timerCounterDin == 8) {
_timerCounterDin = 0;
uCtrl.din->read(1);
return;
}
}
if (uCtrl.touch != nullptr) {
// ~3ms call
if (++_timerCapTouch == 12)
{
_timerCapTouch = 0;
uCtrl.touch->read();
return;
}
}
if (uCtrl.ain != nullptr) {
// ~10ms call
if (++_timerCounterAin == 40)
{
_timerCounterAin = 0;
uCtrl.processAin();
return;
}
}
if (uCtrl.dout != nullptr) {
// ~30ms call
if (++_timerCounterDout == 120) {
_timerCounterDout = 0;
uCtrl.dout->flush(1);
return;
}
}
}