@@ -17,7 +17,18 @@ namespace audio_tools {
1717 * @copyright GPLv3
1818 */
1919class 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:
2132 /* *
2233 * @brief Default constructor: for available AudioBoard values check
2334 * the audioboard variables in
@@ -44,6 +55,32 @@ class AudioBoardStream : public I2SCodecStream {
4455 yield ();
4556 }
4657
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+
4784 /* *
4885 * @brief Defines a new action that is executed when the indicated pin is
4986 * active
@@ -72,12 +109,12 @@ class AudioBoardStream : public I2SCodecStream {
72109 void addAction (int pin, void (*action)(bool , int , void *),
73110 AudioActions::ActiveLogic activeLogic, void *ref = nullptr) {
74111 TRACEI ();
75- actions.add (pin, action, activeLogic, ref == nullptr ? this : ref);
112+ actions.add (pin, action, activeLogic, ref == nullptr ? this : ref);
76113 }
77114
78115 // / Provides access to the AudioActions
79116 AudioActions &audioActions () { return actions; }
80-
117+
81118 AudioActions &getActions () { return actions; }
82119
83120 /* *
@@ -98,7 +135,7 @@ class AudioBoardStream : public I2SCodecStream {
98135 */
99136 static void actionVolumeUp (bool , int , void *ref) {
100137 TRACEI ();
101- AudioBoardStream *self = (AudioBoardStream*)ref;
138+ AudioBoardStream *self = (AudioBoardStream *)ref;
102139 self->incrementVolume (+self->actionVolumeIncrementValue ());
103140 }
104141
@@ -108,18 +145,17 @@ class AudioBoardStream : public I2SCodecStream {
108145 */
109146 static void actionVolumeDown (bool , int , void *ref) {
110147 TRACEI ();
111- AudioBoardStream *self = (AudioBoardStream*)ref;
148+ AudioBoardStream *self = (AudioBoardStream *)ref;
112149 self->incrementVolume (-self->actionVolumeIncrementValue ());
113150 }
114151
115-
116152 /* *
117153 * @brief Toggle start stop
118154 *
119155 */
120156 static void actionStartStop (bool , int , void *ref) {
121157 TRACEI ();
122- AudioBoardStream *self = (AudioBoardStream*)ref;
158+ AudioBoardStream *self = (AudioBoardStream *)ref;
123159 self->active = !self->active ;
124160 self->setActive (self->active );
125161 }
@@ -130,7 +166,7 @@ class AudioBoardStream : public I2SCodecStream {
130166 */
131167 static void actionStart (bool , int , void *ref) {
132168 TRACEI ();
133- AudioBoardStream *self = (AudioBoardStream*)ref;
169+ AudioBoardStream *self = (AudioBoardStream *)ref;
134170 self->active = true ;
135171 self->setActive (self->active );
136172 }
@@ -140,7 +176,7 @@ class AudioBoardStream : public I2SCodecStream {
140176 */
141177 static void actionStop (bool , int , void *ref) {
142178 TRACEI ();
143- AudioBoardStream *self = (AudioBoardStream*)ref;
179+ AudioBoardStream *self = (AudioBoardStream *)ref;
144180 self->active = false ;
145181 self->setActive (self->active );
146182 }
@@ -151,9 +187,8 @@ class AudioBoardStream : public I2SCodecStream {
151187 * This method complies with the
152188 */
153189 static void actionHeadphoneDetection (bool , int , void *ref) {
154- AudioBoardStream *self = (AudioBoardStream*)ref;
190+ AudioBoardStream *self = (AudioBoardStream *)ref;
155191 if (self->pinHeadphoneDetect () >= 0 ) {
156-
157192 // detect changes
158193 bool isConnected = self->headphoneStatus ();
159194 if (self->headphoneIsConnected != isConnected) {
@@ -289,7 +324,7 @@ class AudioBoardStream : public I2SCodecStream {
289324 addAction (input_mode, actionStartStop);
290325 }
291326 }
292-
327+
293328 // / add volume up and volume down action
294329 void addVolumeActions () {
295330 // pin conflicts with SD Lyrat SD CS GpioPin and buttons / Conflict on
@@ -319,8 +354,9 @@ class AudioBoardStream : public I2SCodecStream {
319354 }
320355
321356 /* *
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+ */
324360 void addDefaultActions () {
325361 TRACEI ();
326362 addHeadphoneDetectionAction ();
@@ -329,15 +365,18 @@ class AudioBoardStream : public I2SCodecStream {
329365 }
330366
331367 // / Defines the increment value used by actionVolumeDown/actionVolumeUp
332- void setActionVolumeIncrementValue (float value){
368+ void setActionVolumeIncrementValue (float value) {
333369 action_increment_value = value;
334370 }
335371
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);
338377 }
339378
340- protected:
379+ protected:
341380 AudioActions actions;
342381 bool headphoneIsConnected = false ;
343382 bool active = true ;
@@ -346,8 +385,7 @@ class AudioBoardStream : public I2SCodecStream {
346385 int getSdCsPin () {
347386 static GpioPin sd_cs = -2 ;
348387 // execute only once
349- if (sd_cs != -2 )
350- return sd_cs;
388+ if (sd_cs != -2 ) return sd_cs;
351389
352390 auto sd_opt = getPins ().getSPIPins (PinFunction::SD);
353391 if (sd_opt) {
@@ -365,20 +403,19 @@ class AudioBoardStream : public I2SCodecStream {
365403 AudioActions::ActiveLogic getActionLogic (int pin) {
366404 auto opt = board ().getPins ().getPin (pin);
367405 PinLogic logic = PinLogic::Input;
368- if (opt)
369- logic = opt.value ().pin_logic ;
406+ if (opt) logic = opt.value ().pin_logic ;
370407 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;
380417 }
381418 }
382419};
383420
384- } // namespace audio_tools
421+ } // namespace audio_tools
0 commit comments