Skip to content

Commit

Permalink
Lyrat Mini Button Support
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed Nov 2, 2024
1 parent 9213f4c commit 81cbbb6
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 65 deletions.
51 changes: 51 additions & 0 deletions examples/examples-custom-boards/lyrat-mini/buttons/buttons.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

#include "AudioTools.h"
#include "AudioTools/AudioLibs/AudioBoardStream.h"

AudioBoardStream lyrat(LyratMini);

void rec(bool, int, void*) {
Serial.println("rec");
}

void mode(bool, int, void*) {
Serial.println("mode");
}

void play(bool, int, void*) {
Serial.println("play");
}

void set(bool, int, void*) {
Serial.println("set");
}

void volUp(bool, int, void*) {
Serial.println("vol+");
}

void volDown(bool, int, void*) {
Serial.println("vol-");
}


// Arduino Setup
void setup(void) {
Serial.begin(115200);
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Warning);

// start board
lyrat.begin(lyrat.defaultConfig(TX_MODE));

lyrat.addAction(KEY_REC, rec);
lyrat.addAction(KEY_MODE, mode);
lyrat.addAction(KEY_PLAY, play);
lyrat.addAction(KEY_SET, set);
lyrat.addAction(KEY_VOLUME_UP, volUp);
lyrat.addAction(KEY_VOLUME_DOWN, volDown);

}

void loop() {
lyrat.processActions();
}
29 changes: 29 additions & 0 deletions examples/examples-custom-boards/lyrat-mini/mic/mic.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@


#include "AudioTools.h"
#include "AudioTools/AudioLibs/AudioBoardStream.h"

AudioInfo info(44100, 1, 16);
AudioBoardStream i2s(LyratMini); // Access I2S as stream
CsvOutput<int16_t> csvStream(Serial);
StreamCopy copier(csvStream, i2s); // copy i2s to csvStream

// Arduino Setup
void setup(void) {
Serial.begin(115200);
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Warning);

auto cfg = i2s.defaultConfig(RX_MODE);
cfg.copyFrom(info);
//cfg.input_device = ADC_INPUT_LINE2;
i2s.begin(cfg);

// make sure that we have the correct number of channels set up
csvStream.begin(info);

}

// Arduino loop - copy data
void loop() {
copier.copy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void setup(void) {
Serial.println("starting I2S...");
auto config = out.defaultConfig(TX_MODE);
config.copyFrom(info);
out.begin(config);

// additinal settings
out.setVolume(0.5);
Expand Down
101 changes: 69 additions & 32 deletions src/AudioTools/AudioLibs/AudioBoardStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@ namespace audio_tools {
* @copyright GPLv3
*/
class AudioBoardStream : public I2SCodecStream {
public:
struct AudioBoardAction : public AudioActions::Action {
AudioBoardAction(AudioBoard &board, AudioDriverKey key) {
this->key = key;
this->p_board = &board;
}
AudioDriverKey key;
AudioBoard *p_board;
int id() override { return key | 0x400; }
bool readValue() override { return p_board->isKeyPressed(key); }
};

public:
/**
* @brief Default constructor: for available AudioBoard values check
* the audioboard variables in
Expand All @@ -44,6 +55,32 @@ class AudioBoardStream : public I2SCodecStream {
yield();
}


/**
* @brief Defines a new action that is executed when the Button is pressed
*/
void addAction(AudioDriverKey key, void (*action)(bool, int, void *),
void *ref = nullptr) {
AudioBoardAction *abo = new AudioBoardAction(board(), key);
abo->actionOn = action;
abo->ref = (ref == nullptr) ? this : ref;
actions.add(*abo);
}

/**
* @brief Defines a new action that is executed when the Button is pressed and released
*/
void addAction(AudioDriverKey key, void (*actionOn)(bool, int, void *),
void (*actionOff)(bool, int, void *),
void *ref = nullptr) {

AudioBoardAction *abo = new AudioBoardAction(board(), key);
abo->actionOn = actionOn;
abo->actionOn = actionOff;
abo->ref = (ref == nullptr) ? this : ref;
actions.add(*abo);
}

/**
* @brief Defines a new action that is executed when the indicated pin is
* active
Expand Down Expand Up @@ -72,12 +109,12 @@ class AudioBoardStream : public I2SCodecStream {
void addAction(int pin, void (*action)(bool, int, void *),
AudioActions::ActiveLogic activeLogic, void *ref = nullptr) {
TRACEI();
actions.add(pin, action, activeLogic, ref == nullptr ? this : ref);
actions.add(pin, action, activeLogic, ref == nullptr ? this : ref);
}

/// Provides access to the AudioActions
AudioActions &audioActions() { return actions; }

AudioActions &getActions() { return actions; }

/**
Expand All @@ -98,7 +135,7 @@ class AudioBoardStream : public I2SCodecStream {
*/
static void actionVolumeUp(bool, int, void *ref) {
TRACEI();
AudioBoardStream *self = (AudioBoardStream*)ref;
AudioBoardStream *self = (AudioBoardStream *)ref;
self->incrementVolume(+self->actionVolumeIncrementValue());
}

Expand All @@ -108,18 +145,17 @@ class AudioBoardStream : public I2SCodecStream {
*/
static void actionVolumeDown(bool, int, void *ref) {
TRACEI();
AudioBoardStream *self = (AudioBoardStream*)ref;
AudioBoardStream *self = (AudioBoardStream *)ref;
self->incrementVolume(-self->actionVolumeIncrementValue());
}


/**
* @brief Toggle start stop
*
*/
static void actionStartStop(bool, int, void *ref) {
TRACEI();
AudioBoardStream *self = (AudioBoardStream*)ref;
AudioBoardStream *self = (AudioBoardStream *)ref;
self->active = !self->active;
self->setActive(self->active);
}
Expand All @@ -130,7 +166,7 @@ class AudioBoardStream : public I2SCodecStream {
*/
static void actionStart(bool, int, void *ref) {
TRACEI();
AudioBoardStream *self = (AudioBoardStream*)ref;
AudioBoardStream *self = (AudioBoardStream *)ref;
self->active = true;
self->setActive(self->active);
}
Expand All @@ -140,7 +176,7 @@ class AudioBoardStream : public I2SCodecStream {
*/
static void actionStop(bool, int, void *ref) {
TRACEI();
AudioBoardStream *self = (AudioBoardStream*)ref;
AudioBoardStream *self = (AudioBoardStream *)ref;
self->active = false;
self->setActive(self->active);
}
Expand All @@ -151,9 +187,8 @@ class AudioBoardStream : public I2SCodecStream {
* This method complies with the
*/
static void actionHeadphoneDetection(bool, int, void *ref) {
AudioBoardStream *self = (AudioBoardStream*)ref;
AudioBoardStream *self = (AudioBoardStream *)ref;
if (self->pinHeadphoneDetect() >= 0) {

// detect changes
bool isConnected = self->headphoneStatus();
if (self->headphoneIsConnected != isConnected) {
Expand Down Expand Up @@ -289,7 +324,7 @@ class AudioBoardStream : public I2SCodecStream {
addAction(input_mode, actionStartStop);
}
}

/// add volume up and volume down action
void addVolumeActions() {
// pin conflicts with SD Lyrat SD CS GpioPin and buttons / Conflict on
Expand Down Expand Up @@ -319,8 +354,9 @@ class AudioBoardStream : public I2SCodecStream {
}

/**
* @brief Setup the supported default actions (volume, start/stop, headphone detection)
*/
* @brief Setup the supported default actions (volume, start/stop, headphone
* detection)
*/
void addDefaultActions() {
TRACEI();
addHeadphoneDetectionAction();
Expand All @@ -329,15 +365,18 @@ class AudioBoardStream : public I2SCodecStream {
}

/// Defines the increment value used by actionVolumeDown/actionVolumeUp
void setActionVolumeIncrementValue(float value){
void setActionVolumeIncrementValue(float value) {
action_increment_value = value;
}

float actionVolumeIncrementValue() {
return action_increment_value;
float actionVolumeIncrementValue() { return action_increment_value; }

bool isKeyPressed(int key) {
if (!board()) return false;
return board().isKeyPressed(key);
}

protected:
protected:
AudioActions actions;
bool headphoneIsConnected = false;
bool active = true;
Expand All @@ -346,8 +385,7 @@ class AudioBoardStream : public I2SCodecStream {
int getSdCsPin() {
static GpioPin sd_cs = -2;
// execute only once
if (sd_cs != -2)
return sd_cs;
if (sd_cs != -2) return sd_cs;

auto sd_opt = getPins().getSPIPins(PinFunction::SD);
if (sd_opt) {
Expand All @@ -365,20 +403,19 @@ class AudioBoardStream : public I2SCodecStream {
AudioActions::ActiveLogic getActionLogic(int pin) {
auto opt = board().getPins().getPin(pin);
PinLogic logic = PinLogic::Input;
if (opt)
logic = opt.value().pin_logic;
if (opt) logic = opt.value().pin_logic;
switch (logic) {
case PinLogic::Input:
case PinLogic::InputActiveLow:
return AudioActions::ActiveLow;
case PinLogic::InputActiveHigh:
return AudioActions::ActiveHigh;
case PinLogic::InputActiveTouch:
return AudioActions::ActiveTouch;
default:
return AudioActions::ActiveLow;
case PinLogic::Input:
case PinLogic::InputActiveLow:
return AudioActions::ActiveLow;
case PinLogic::InputActiveHigh:
return AudioActions::ActiveHigh;
case PinLogic::InputActiveTouch:
return AudioActions::ActiveTouch;
default:
return AudioActions::ActiveLow;
}
}
};

} // namespace audio_tools
} // namespace audio_tools
Loading

0 comments on commit 81cbbb6

Please sign in to comment.