From d23f8725eeac76b800064ed488cde7d881137e95 Mon Sep 17 00:00:00 2001 From: Colin Clark Date: Sun, 31 Mar 2024 01:03:51 -0400 Subject: [PATCH] gh-22: Working bare board test for Patch Init/Lichen Medium. --- .../src/lichen-medium-bare-board-test.cpp | 11 ++++----- hosts/daisy/include/lichen-medium-module.h | 24 +++++++++++++------ hosts/daisy/include/sig-daisy-patch-sm.hpp | 2 +- hosts/daisy/include/signaletic-daisy-host.h | 14 +++++++---- hosts/daisy/src/sig-daisy-patch-sm.cpp | 20 ++++++++++------ 5 files changed, 46 insertions(+), 25 deletions(-) diff --git a/hosts/daisy/examples/lichen-medium/bare-board-test/src/lichen-medium-bare-board-test.cpp b/hosts/daisy/examples/lichen-medium/bare-board-test/src/lichen-medium-bare-board-test.cpp index f488ffb..5600a78 100644 --- a/hosts/daisy/examples/lichen-medium/bare-board-test/src/lichen-medium-bare-board-test.cpp +++ b/hosts/daisy/examples/lichen-medium/bare-board-test/src/lichen-medium-bare-board-test.cpp @@ -1,7 +1,5 @@ #include "daisy.h" #include -#include "../../../../include/signaletic-daisy-host.h" -#include "../../../../include/sig-daisy-patch-sm.hpp" #include "../../../../include/lichen-medium-module.h" #define SAMPLERATE 48000 @@ -93,7 +91,8 @@ void AudioCallback(daisy::AudioHandle::InputBuffer in, medium.Read(); freq->parameters.value = 1760.0f * - medium.adcController.channelBank.values[0]; + (medium.adcController.channelBank.values[0] + + medium.adcController.channelBank.values[6]); buttonValue->parameters.value = medium.buttonBank.values[0]; switchValue->parameters.value = medium.switchBank.values[0]; @@ -104,7 +103,7 @@ void AudioCallback(daisy::AudioHandle::InputBuffer in, out[0][i] = sig; out[1][i] = sig; - medium.dacChannelBank.values[0] = medium.gateBank.values[0] - 0.75f; + medium.dacOutputBank.values[0] = medium.gateBank.values[0] * 0.5f; medium.Write(); } } @@ -128,8 +127,8 @@ int main(void) { struct sig_SignalContext* context = sig_SignalContext_new(&allocator, &audioSettings); buildSignalGraph(&allocator, context, &signals, &audioSettings, &status); - medium.Start(); - medium.board.audio.Start(AudioCallback); + + medium.Start(AudioCallback); while (1) { diff --git a/hosts/daisy/include/lichen-medium-module.h b/hosts/daisy/include/lichen-medium-module.h index d951f58..646e35a 100644 --- a/hosts/daisy/include/lichen-medium-module.h +++ b/hosts/daisy/include/lichen-medium-module.h @@ -94,12 +94,14 @@ namespace medium { Toggle buttons[NUM_BUTTONS]; InputBank buttonBank; AnalogOutput dacChannels[NUM_DAC_CHANNELS]; - OutputBank dacChannelBank; + OutputBank dacOutputBank; struct sig_host_HardwareInterface hardware; void Init(struct sig_AudioSettings* audioSettings) { board.Init(audioSettings->blockSize, audioSettings->sampleRate); + // The DAC and ADC have to be initialized after the board. InitADCController(); + InitDAC(); InitControls(); hardware = { @@ -114,7 +116,7 @@ namespace medium { .numADCChannels = NUM_ADC_CHANNELS, .adcChannels = adcController.channelBank.values, .numDACChannels = NUM_DAC_CHANNELS, - .dacChannels = dacChannelBank.values, + .dacChannels = dacOutputBank.values, .numGateInputs = NUM_GATES, .gateInputs = gateBank.values, .numGPIOOutputs = 0, @@ -124,6 +126,7 @@ namespace medium { .numTriSwitches = NUM_TRISWITCHES, .triSwitches = switchBank.values }; + } void InitADCController() { @@ -131,25 +134,32 @@ namespace medium { } void InitDAC() { - dacChannels[0].Init( - &patchsm::sig_daisy_patch_sm_dac_output[0]); + for (size_t i = 0; i < NUM_DAC_CHANNELS; i++) { + dacChannels[i].Init(board.dacOutputValues, i); + } + + dacOutputBank.outputs = dacChannels; } void InitControls() { gates[0].Init(GATE_PINS[0]); gateBank.inputs = gates; - triSwitches[0].Init(TRISWITCH_PINS[0][1], TRISWITCH_PINS[0][2]); + triSwitches[0].Init(TRISWITCH_PINS[0]); switchBank.inputs = triSwitches; buttons[0].Init(BUTTON_PINS[0]); buttonBank.inputs = buttons; } - void Start() { + void Start(daisy::AudioHandle::AudioCallback callback) { adcController.Start(); + board.StartDac(); + board.audio.Start(callback); } void Stop () { adcController.Stop(); + board.dac.Stop(); + board.audio.Stop(); } inline void Read() { @@ -160,7 +170,7 @@ namespace medium { } inline void Write() { - dacChannelBank.Write(); + dacOutputBank.Write(); } }; diff --git a/hosts/daisy/include/sig-daisy-patch-sm.hpp b/hosts/daisy/include/sig-daisy-patch-sm.hpp index 7f1b33d..ee05694 100644 --- a/hosts/daisy/include/sig-daisy-patch-sm.hpp +++ b/hosts/daisy/include/sig-daisy-patch-sm.hpp @@ -68,7 +68,7 @@ namespace patchsm { daisy::Pcm3060 codec; size_t dac_buffer_size_; uint16_t* dacBuffer[2]; - uint16_t dacOutput[2]; + uint16_t* dacOutputValues; bool dac_running_; void Init(size_t blockSize, float sampleRate); diff --git a/hosts/daisy/include/signaletic-daisy-host.h b/hosts/daisy/include/signaletic-daisy-host.h index a816fe7..5aeaaa8 100644 --- a/hosts/daisy/include/signaletic-daisy-host.h +++ b/hosts/daisy/include/signaletic-daisy-host.h @@ -119,15 +119,16 @@ template class ADCController { class AnalogOutput { public: - uint16_t* dacBuffer; + uint16_t* dacOutputs; size_t channel; - void Init(uint16_t* inDACBuffer) { - dacBuffer = inDACBuffer; + void Init(uint16_t* inDACOutputs, size_t inChannel) { + dacOutputs = inDACOutputs; + channel = inChannel; } inline void Write(float value) { - *dacBuffer = sig_bipolarToUint12(value); + dacOutputs[channel] = sig_unipolarToUint12(value); } }; @@ -201,6 +202,11 @@ class TriSwitch { Toggle switchA; Toggle switchB; + void Init(dsy_gpio_pin pins[2]) { + switchA.Init(pins[0]); + switchB.Init(pins[1]); + } + void Init(dsy_gpio_pin pinA, dsy_gpio_pin pinB) { switchA.Init(pinA); switchB.Init(pinB); diff --git a/hosts/daisy/src/sig-daisy-patch-sm.cpp b/hosts/daisy/src/sig-daisy-patch-sm.cpp index 59f4706..c1d1f7d 100644 --- a/hosts/daisy/src/sig-daisy-patch-sm.cpp +++ b/hosts/daisy/src/sig-daisy-patch-sm.cpp @@ -4,13 +4,17 @@ using namespace daisy; /** outside of class static buffer(s) for DMA and DAC access */ uint16_t DMA_BUFFER_MEM_SECTION sig_daisy_patch_sm_dac_buffer[2][48]; -uint16_t sig_daisy_patch_sm_dac_output[2]; +// TODO: I think this should be a block-sized ring buffer, +// so that we're maximizing throughput between the audio callback +// and the DMA callback. +uint16_t sig_daisy_patch_sm_dac_outputValues[2]; void sig::libdaisy::patchsm::PatchSMBoard::Init(size_t blockSize, float sampleRate) { dac_running_ = false; dac_buffer_size_ = 48; - sig_daisy_patch_sm_dac_output[0] = 0; - sig_daisy_patch_sm_dac_output[1] = 0; + sig_daisy_patch_sm_dac_outputValues[0] = 0; + sig_daisy_patch_sm_dac_outputValues[1] = 0; + dacOutputValues = sig_daisy_patch_sm_dac_outputValues; dacBuffer[0] = sig_daisy_patch_sm_dac_buffer[0]; dacBuffer[1] = sig_daisy_patch_sm_dac_buffer[1]; @@ -84,7 +88,6 @@ void sig::libdaisy::patchsm::PatchSMBoard::Init(size_t blockSize, float sampleRa dsy_gpio_init(&userLED); InitDac(); - StartDac(); } @@ -99,7 +102,8 @@ void sig::libdaisy::patchsm::PatchSMBoard::InitDac() { dac.Init(dac_config); } -void sig::libdaisy::patchsm::PatchSMBoard::StartDac(DacHandle::DacCallback callback) { +void sig::libdaisy::patchsm::PatchSMBoard::StartDac( + DacHandle::DacCallback callback) { if (dac_running_) { dac.Stop(); } @@ -112,10 +116,12 @@ void sig::libdaisy::patchsm::PatchSMBoard::StartDac(DacHandle::DacCallback callb dac_running_ = true; } +// TODO: Implement more generic DMA DAC support +// that can support arbitrary channels. void sig::libdaisy::patchsm::PatchSMBoard::DefaultDacCallback(uint16_t **output, size_t size) { for (size_t i = 0; i < size; i++) { - output[0][i] = sig_daisy_patch_sm_dac_output[0]; - output[1][i] = sig_daisy_patch_sm_dac_output[1]; + output[0][i] = sig_daisy_patch_sm_dac_outputValues[0]; + output[1][i] = sig_daisy_patch_sm_dac_outputValues[1]; } }