@@ -17,7 +17,18 @@ namespace audio_tools {
17
17
* @copyright GPLv3
18
18
*/
19
19
class AudioBoardStream : public I2SCodecStream {
20
- public:
20
+ struct AudioBoardAction : public AudioActions ::Action {
21
+ AudioBoardAction (AudioBoard &board, AudioDriverKey key) {
22
+ this ->key = key;
23
+ this ->p_board = &board;
24
+ }
25
+ AudioDriverKey key;
26
+ AudioBoard *p_board;
27
+ int id () override { return key | 0x400 ; }
28
+ bool readValue () override { return p_board->isKeyPressed (key); }
29
+ };
30
+
31
+ public:
21
32
/* *
22
33
* @brief Default constructor: for available AudioBoard values check
23
34
* the audioboard variables in
@@ -44,6 +55,32 @@ class AudioBoardStream : public I2SCodecStream {
44
55
yield ();
45
56
}
46
57
58
+
59
+ /* *
60
+ * @brief Defines a new action that is executed when the Button is pressed
61
+ */
62
+ void addAction (AudioDriverKey key, void (*action)(bool , int , void *),
63
+ void *ref = nullptr) {
64
+ AudioBoardAction *abo = new AudioBoardAction (board (), key);
65
+ abo->actionOn = action;
66
+ abo->ref = (ref == nullptr ) ? this : ref;
67
+ actions.add (*abo);
68
+ }
69
+
70
+ /* *
71
+ * @brief Defines a new action that is executed when the Button is pressed and released
72
+ */
73
+ void addAction (AudioDriverKey key, void (*actionOn)(bool , int , void *),
74
+ void (*actionOff)(bool , int , void *),
75
+ void *ref = nullptr) {
76
+
77
+ AudioBoardAction *abo = new AudioBoardAction (board (), key);
78
+ abo->actionOn = actionOn;
79
+ abo->actionOn = actionOff;
80
+ abo->ref = (ref == nullptr ) ? this : ref;
81
+ actions.add (*abo);
82
+ }
83
+
47
84
/* *
48
85
* @brief Defines a new action that is executed when the indicated pin is
49
86
* active
@@ -72,12 +109,12 @@ class AudioBoardStream : public I2SCodecStream {
72
109
void addAction (int pin, void (*action)(bool , int , void *),
73
110
AudioActions::ActiveLogic activeLogic, void *ref = nullptr) {
74
111
TRACEI ();
75
- actions.add (pin, action, activeLogic, ref == nullptr ? this : ref);
112
+ actions.add (pin, action, activeLogic, ref == nullptr ? this : ref);
76
113
}
77
114
78
115
// / Provides access to the AudioActions
79
116
AudioActions &audioActions () { return actions; }
80
-
117
+
81
118
AudioActions &getActions () { return actions; }
82
119
83
120
/* *
@@ -98,7 +135,7 @@ class AudioBoardStream : public I2SCodecStream {
98
135
*/
99
136
static void actionVolumeUp (bool , int , void *ref) {
100
137
TRACEI ();
101
- AudioBoardStream *self = (AudioBoardStream*)ref;
138
+ AudioBoardStream *self = (AudioBoardStream *)ref;
102
139
self->incrementVolume (+self->actionVolumeIncrementValue ());
103
140
}
104
141
@@ -108,18 +145,17 @@ class AudioBoardStream : public I2SCodecStream {
108
145
*/
109
146
static void actionVolumeDown (bool , int , void *ref) {
110
147
TRACEI ();
111
- AudioBoardStream *self = (AudioBoardStream*)ref;
148
+ AudioBoardStream *self = (AudioBoardStream *)ref;
112
149
self->incrementVolume (-self->actionVolumeIncrementValue ());
113
150
}
114
151
115
-
116
152
/* *
117
153
* @brief Toggle start stop
118
154
*
119
155
*/
120
156
static void actionStartStop (bool , int , void *ref) {
121
157
TRACEI ();
122
- AudioBoardStream *self = (AudioBoardStream*)ref;
158
+ AudioBoardStream *self = (AudioBoardStream *)ref;
123
159
self->active = !self->active ;
124
160
self->setActive (self->active );
125
161
}
@@ -130,7 +166,7 @@ class AudioBoardStream : public I2SCodecStream {
130
166
*/
131
167
static void actionStart (bool , int , void *ref) {
132
168
TRACEI ();
133
- AudioBoardStream *self = (AudioBoardStream*)ref;
169
+ AudioBoardStream *self = (AudioBoardStream *)ref;
134
170
self->active = true ;
135
171
self->setActive (self->active );
136
172
}
@@ -140,7 +176,7 @@ class AudioBoardStream : public I2SCodecStream {
140
176
*/
141
177
static void actionStop (bool , int , void *ref) {
142
178
TRACEI ();
143
- AudioBoardStream *self = (AudioBoardStream*)ref;
179
+ AudioBoardStream *self = (AudioBoardStream *)ref;
144
180
self->active = false ;
145
181
self->setActive (self->active );
146
182
}
@@ -151,9 +187,8 @@ class AudioBoardStream : public I2SCodecStream {
151
187
* This method complies with the
152
188
*/
153
189
static void actionHeadphoneDetection (bool , int , void *ref) {
154
- AudioBoardStream *self = (AudioBoardStream*)ref;
190
+ AudioBoardStream *self = (AudioBoardStream *)ref;
155
191
if (self->pinHeadphoneDetect () >= 0 ) {
156
-
157
192
// detect changes
158
193
bool isConnected = self->headphoneStatus ();
159
194
if (self->headphoneIsConnected != isConnected) {
@@ -289,7 +324,7 @@ class AudioBoardStream : public I2SCodecStream {
289
324
addAction (input_mode, actionStartStop);
290
325
}
291
326
}
292
-
327
+
293
328
// / add volume up and volume down action
294
329
void addVolumeActions () {
295
330
// pin conflicts with SD Lyrat SD CS GpioPin and buttons / Conflict on
@@ -319,8 +354,9 @@ class AudioBoardStream : public I2SCodecStream {
319
354
}
320
355
321
356
/* *
322
- * @brief Setup the supported default actions (volume, start/stop, headphone detection)
323
- */
357
+ * @brief Setup the supported default actions (volume, start/stop, headphone
358
+ * detection)
359
+ */
324
360
void addDefaultActions () {
325
361
TRACEI ();
326
362
addHeadphoneDetectionAction ();
@@ -329,15 +365,18 @@ class AudioBoardStream : public I2SCodecStream {
329
365
}
330
366
331
367
// / Defines the increment value used by actionVolumeDown/actionVolumeUp
332
- void setActionVolumeIncrementValue (float value){
368
+ void setActionVolumeIncrementValue (float value) {
333
369
action_increment_value = value;
334
370
}
335
371
336
- float actionVolumeIncrementValue () {
337
- return action_increment_value;
372
+ float actionVolumeIncrementValue () { return action_increment_value; }
373
+
374
+ bool isKeyPressed (int key) {
375
+ if (!board ()) return false ;
376
+ return board ().isKeyPressed (key);
338
377
}
339
378
340
- protected:
379
+ protected:
341
380
AudioActions actions;
342
381
bool headphoneIsConnected = false ;
343
382
bool active = true ;
@@ -346,8 +385,7 @@ class AudioBoardStream : public I2SCodecStream {
346
385
int getSdCsPin () {
347
386
static GpioPin sd_cs = -2 ;
348
387
// execute only once
349
- if (sd_cs != -2 )
350
- return sd_cs;
388
+ if (sd_cs != -2 ) return sd_cs;
351
389
352
390
auto sd_opt = getPins ().getSPIPins (PinFunction::SD);
353
391
if (sd_opt) {
@@ -365,20 +403,19 @@ class AudioBoardStream : public I2SCodecStream {
365
403
AudioActions::ActiveLogic getActionLogic (int pin) {
366
404
auto opt = board ().getPins ().getPin (pin);
367
405
PinLogic logic = PinLogic::Input;
368
- if (opt)
369
- logic = opt.value ().pin_logic ;
406
+ if (opt) logic = opt.value ().pin_logic ;
370
407
switch (logic) {
371
- case PinLogic::Input:
372
- case PinLogic::InputActiveLow:
373
- return AudioActions::ActiveLow;
374
- case PinLogic::InputActiveHigh:
375
- return AudioActions::ActiveHigh;
376
- case PinLogic::InputActiveTouch:
377
- return AudioActions::ActiveTouch;
378
- default :
379
- return AudioActions::ActiveLow;
408
+ case PinLogic::Input:
409
+ case PinLogic::InputActiveLow:
410
+ return AudioActions::ActiveLow;
411
+ case PinLogic::InputActiveHigh:
412
+ return AudioActions::ActiveHigh;
413
+ case PinLogic::InputActiveTouch:
414
+ return AudioActions::ActiveTouch;
415
+ default :
416
+ return AudioActions::ActiveLow;
380
417
}
381
418
}
382
419
};
383
420
384
- } // namespace audio_tools
421
+ } // namespace audio_tools
0 commit comments