chrissteffen
Member
Im working on an Audio project and needed a custom class to stream audio to a class controlling an hbridge.
Problem is the update function isnt being called.
For context, Task_Abstract is a task scheduler that will call the thread function at given rates via the loop function (no interrupts used, just big loop).
Code:
Problem is the update function isnt being called.
For context, Task_Abstract is a task scheduler that will call the thread function at given rates via the loop function (no interrupts used, just big loop).
Code:
Code:
#include "hbridge_bts7960_driver.h"
#include <Audio.h>
class HBridgeAudioDriver: public AudioStream, public Task_Abstract {
public:
HBridgeAudioDriver() : AudioStream(1, inputQueueArray), Task_Abstract("Audio Output HBridge", AUDIO_SAMPLE_RATE_EXACT*1.2, eTaskPriority_t::eTaskPriority_Realtime) {
}
void update(void) override {
if (mutexBlock) return;
mutexBlock = true;
if (sampleBuffer.availableSpace() < AUDIO_BLOCK_SAMPLES) bufferOverflow = true;
for (uint32_t i = 0; i < AUDIO_BLOCK_SAMPLES; i++) sampleBuffer.placeFront(inputQueueArray[0]->data[i], true);
mutexBlock = false;
}
void init() override {
hbridgeDriver.init();
}
void thread() override {
if (mutexBlock) return;
mutexBlock = true;
int16_t sample;
if (sampleBuffer.takeBack(sample)) {
mutexBlock = false;
bias_.update(sample);
float val = (float(sample) - bias_.getValue())*3;
hbridgeDriver.setOutput(val);
Serial.println(String("Sample: ") + sample);
}
mutexBlock = false;
}
//private:
PWM_HAL positivePWM = 23;
PWM_HAL negativePWM = 22;
LowPassFilter<float> bias_ = 0.04;
HBridge_BTS7960_Driver hbridgeDriver = HBridge_BTS7960_Driver(positivePWM, negativePWM, 20000);
audio_block_t *inputQueueArray[1];
bool mutexBlock = false;
Buffer<int16_t, AUDIO_BLOCK_SAMPLES*2> sampleBuffer;
bool bufferOverflow = false;
};
class ObserverTask: public Task_Abstract {
public:
ObserverTask(): Task_Abstract("Observer", 1, eTaskPriority_t::eTaskPriority_Middle), adcAudioInput(15), patchCord1(adcAudioInput, 0, task, 0) {}
void init() override {
Serial.begin(115200);
}
void thread() {
Serial.println(String("Task Rate: ") + task.getLoopRate() + ", cpu: " + task.getTaskSystemUsage()*100 + " Overflow: " + task.bufferOverflow);
task.bufferOverflow = false;
}
HBridgeAudioDriver task;
AudioInputAnalog adcAudioInput;
AudioConnection patchCord1;
};
ObserverTask mainTask;
void setup() {
AudioMemory(12);
Serial.begin(115200);
}
void loop() {
Task_Abstract::schedulerTick();
}