Skip to content

Commit

Permalink
lichen-community-systemsgh-22: Working bare board test for Patch Init…
Browse files Browse the repository at this point in the history
…/Lichen Medium.
  • Loading branch information
colinbdclark committed Mar 31, 2024
1 parent d37f38d commit d23f872
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "daisy.h"
#include <libsignaletic.h>
#include "../../../../include/signaletic-daisy-host.h"
#include "../../../../include/sig-daisy-patch-sm.hpp"
#include "../../../../include/lichen-medium-module.h"

#define SAMPLERATE 48000
Expand Down Expand Up @@ -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];

Expand All @@ -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();
}
}
Expand All @@ -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) {

Expand Down
24 changes: 17 additions & 7 deletions hosts/daisy/include/lichen-medium-module.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,14 @@ namespace medium {
Toggle buttons[NUM_BUTTONS];
InputBank<Toggle, NUM_BUTTONS> buttonBank;
AnalogOutput dacChannels[NUM_DAC_CHANNELS];
OutputBank<AnalogOutput, NUM_DAC_CHANNELS> dacChannelBank;
OutputBank<AnalogOutput, NUM_DAC_CHANNELS> 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 = {
Expand All @@ -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,
Expand All @@ -124,32 +126,40 @@ namespace medium {
.numTriSwitches = NUM_TRISWITCHES,
.triSwitches = switchBank.values
};

}

void InitADCController() {
adcController.Init(&board.adc, ADC_PINS);
}

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() {
Expand All @@ -160,7 +170,7 @@ namespace medium {
}

inline void Write() {
dacChannelBank.Write();
dacOutputBank.Write();
}
};

Expand Down
2 changes: 1 addition & 1 deletion hosts/daisy/include/sig-daisy-patch-sm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 10 additions & 4 deletions hosts/daisy/include/signaletic-daisy-host.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,16 @@ template<typename T, size_t numChannels> 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);
}
};

Expand Down Expand Up @@ -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);
Expand Down
20 changes: 13 additions & 7 deletions hosts/daisy/src/sig-daisy-patch-sm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down Expand Up @@ -84,7 +88,6 @@ void sig::libdaisy::patchsm::PatchSMBoard::Init(size_t blockSize, float sampleRa
dsy_gpio_init(&userLED);

InitDac();
StartDac();
}


Expand All @@ -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();
}
Expand All @@ -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];
}
}

0 comments on commit d23f872

Please sign in to comment.