Compare commits

...

4 Commits

  1. 2
      Makefile.conf
  2. 192
      examples/main_framework_proto2.cc
  3. 30
      examples/main_framework_proto2.hh
  4. 118
      src/utils.hh

2
Makefile.conf

@ -10,3 +10,5 @@ DAISYSP_DIR ?= ../../heck_DaisySP
CXX_STANDARD ?= -std=c++17 CXX_STANDARD ?= -std=c++17
CXXFLAGS += -Wall -Wno-unused -Wno-reorder-ctor -Wno-switch CXXFLAGS += -Wall -Wno-unused -Wno-reorder-ctor -Wno-switch
CXXFLAGS += -fdiagnostics-color=always CXXFLAGS += -fdiagnostics-color=always
LDFLAGS += -u_printf_float

192
examples/main_framework_proto2.cc

@ -0,0 +1,192 @@
#include "main_framework_proto2.hh"
#include "utils.hh"
//#include <functional>
namespace Heck {
namespace { // anonymous namespace for internal linkage
void audio_callback(
ld::AudioHandle::InputBuffer in,
ld::AudioHandle::OutputBuffer out,
size_t size);
// =============================================================================================
// STATIC INIT
// =============================================================================================
ld::DaisySeed seed{};
ld::MidiUartHandler midi{};
std::array<u16 *, 4> pots_raw{ nullptr, nullptr, nullptr, nullptr };
std::array<f32, 4> pot{};
std::array<DigitalIn, Constants::Hardware::BUT_COUNT> button{};
std::array<dsp::Oscillator, Constants::Hardware::POT_COUNT> osc{};
void fmtmon()
{
std::array<char, 500> record{};
for (u16 i = 0; i < pot.size(); i++) {
std::array<char, 10> field{};
snprintf(field.data(), field.size(), "%4f ", pot[i]);
strcat(record.data(), field.data());
}
strcat(record.data(), "-");
for (u16 i = 0; i < button.size(); i++) {
std::array<char, 5> field{};
snprintf(field.data(), field.size(), "%2d ", button[i].read());
strcat(record.data(), field.data());
}
seed.PrintLine(record.data());
}
// Tasks
PeriodicTask ui_task{ 1, [](u32 time_now) {
for (u16 i = 0; i < button.size(); i++) {
button[i].cache.update_and_notify_change();
}
for (u16 i = 0; i < pots_raw.size(); i++) {
pot[i] = float(*pots_raw[i]) / std::numeric_limits<u16>::max();
}
while (midi.HasEvents()) {
ld::MidiEvent msg = midi.PopEvent();
char strbuf[128];
GetMidiTypeAsString(msg, &strbuf[0]);
seed.PrintLine("%s", strbuf);
}
fmtmon();
for(u16 i=0; i < osc.size(); i++) {
osc[i].SetFreq(8000 * pot[i]);
}
} };
PeriodicTask heartbeat_task{ 100, [](u32 time) {
static bool heartbeat_led_state{ false };
heartbeat_led_state = !heartbeat_led_state;
seed.SetLed(heartbeat_led_state);
return;
} };
// =============================================================================================
// RUNTIME INIT
// =============================================================================================
void init()
{
seed.Configure();
seed.Init(Constants::CPU_BOOST480MHZ);
seed.StartLog(Constants::Developer::LOG_BLOCKS_BOOT);
{ // BUTTONS
for (int i = 0; i < Constants::Hardware::BUT_COUNT; i++) {
button[i].init(Constants::Hardware::PIN_BUT[i]);
button[i].cache.set_notify_change_callback([i](Cache<bool> &obj) {
fmtmon();
});
}
}
{ // POTS
std::array<ld::AdcChannelConfig, Constants::Hardware::POT_COUNT> adc_cfg{};
for (int i = 0; i < Constants::Hardware::POT_COUNT; i++) {
adc_cfg[i].InitSingle(ld::DaisySeed::GetPin(Constants::Hardware::PIN_POT[i]));
}
seed.adc.Init(adc_cfg.data(), Constants::Hardware::POT_COUNT);
for (int i = 0; i < Constants::Hardware::POT_COUNT; i++) {
pots_raw[i] = seed.adc.GetPtr(i);
}
seed.adc.Start();
}
seed.PrintLine("Setting Blocksize: %i", Constants::AUDIO_BUFFERSIZE);
seed.SetAudioBlockSize(Constants::AUDIO_BUFFERSIZE);
seed.PrintLine("Setting Samplerate: %i", Constants::AUDIO_SAMPLERATE);
seed.SetAudioSampleRate(Constants::AUDIO_SAMPLERATE);
seed.PrintLine("Initializing MIDI");
ld::MidiUartHandler::Config midi_config{};
midi.Init(midi_config);
seed.PrintLine("Starting MIDI Receive");
midi.StartReceiveRt([](const ld::MidiEvent &msg) {
switch (msg.srt_type) {
case ld::TimingClock: {
} break;
case ld::Start: {
} break;
case ld::Stop: {
} break;
case ld::Reset: {
} break;
case ld::Continue: {
} break;
}
});
midi.Listen();
seed.PrintLine("Starting Audio");
seed.StartAudio(audio_callback);
for(dsp::Oscillator& o : osc) {
o.Init(Constants::AUDIO_SAMPLERATE);
o.SetWaveform(o.WAVE_SIN);
}
}
// =============================================================================================
// RUN
// =============================================================================================
void audio_callback(ld::AudioHandle::InputBuffer in, ld::AudioHandle::OutputBuffer out, size_t size)
{
{
float osc_out{};
for (size_t i = 0; i < size; i++) {
for(dsp::Oscillator& o : osc) {
osc_out += o.Process();
}
osc_out *= 0.05;
out[0][i] = osc_out;
out[1][i] = osc_out;
}
}
if constexpr (false) {
// Channel 1
for (size_t i = 0; i < size; i++) {
out[0][i] = in[0][i];
}
// Channel 2
for (size_t i = 0; i < size; i++) {
out[1][i] = in[1][i];
}
}
}
void mainloop()
{
seed.PrintLine("Entering MainLoop");
u32 uptime_ms{};
while (true) {
uptime_ms = ld::System::GetNow();
ui_task.run_pending(uptime_ms);
heartbeat_task.run_pending(uptime_ms);
}
}
} // namespace
} // namespace Heck
int main()
{
Heck::init();
Heck::mainloop();
}

30
examples/main_framework_proto2.hh

@ -0,0 +1,30 @@
#ifndef HECK_LIBDIZZY_MAIN_FRAMEWORK_PROTO1_HH
#define HECK_LIBDIZZY_MAIN_FRAMEWORK_PROTO1_HH
#include <cstdint>
#include "daisy_seed.h"
#include "daisysp.h"
#include "types.hh"
namespace Heck {
namespace Constants {
namespace Hardware {
constexpr int BUT_COUNT = 4;
constexpr std::array<int, BUT_COUNT> PIN_BUT{ 27, 28, 29, 30 };
constexpr int POT_COUNT = 4;
constexpr std::array<int, POT_COUNT> PIN_POT{ 15, 16, 17, 18 };
} // namespace Hardware
constexpr bool CPU_BOOST480MHZ = false;
constexpr int AUDIO_BUFFERSIZE = 4;
constexpr Samplerate AUDIO_SAMPLERATE = Samplerate::SAI_48KHZ;
namespace Developer {
constexpr bool LOG_BLOCKS_BOOT = true;
}
} // namespace Constants
} // namespace Heck
#endif

118
src/utils.hh

@ -130,12 +130,6 @@ namespace Heck {
PollingObserver(PollingObserver&&) = delete; PollingObserver(PollingObserver&&) = delete;
PollingObserver& operator=(PollingObserver&&) = delete; PollingObserver& operator=(PollingObserver&&) = delete;
void init(FetchCallback fetch_cb, DeliverCallback deliver_cb)
{
set_fetch_callback(fetch_cb);
set_deliver_callback(deliver_cb);
}
void set_fetch_callback(FetchCallback cb) void set_fetch_callback(FetchCallback cb)
{ {
fetch_cb_ = cb; fetch_cb_ = cb;
@ -169,9 +163,121 @@ namespace Heck {
} }
private: private:
void init(FetchCallback fetch_cb, DeliverCallback deliver_cb)
{
set_fetch_callback(fetch_cb);
set_deliver_callback(deliver_cb);
}
T val_current_{}; T val_current_{};
FetchCallback fetch_cb_{}; FetchCallback fetch_cb_{};
DeliverCallback deliver_cb_{}; DeliverCallback deliver_cb_{};
}; };
} // namespace Heck } // namespace Heck
namespace Heck {
template<typename T> class Cache {
public:
using UpdateCallback = std::function<T(void)>;
using NotifyChangeCallback = std::function<void(Cache<T>&)>;
Cache() = delete;
~Cache() = default;
explicit Cache(UpdateCallback update_cb, NotifyChangeCallback notify_change_cb)
{
init(update_cb, notify_change_cb);
}
Cache(Cache&) = delete;
Cache& operator=(Cache) = delete;
Cache(Cache&&) = delete;
Cache& operator=(Cache&&) = delete;
T read()
{
return val_current_;
}
void set_update_callback(UpdateCallback cb)
{
update_cb_ = cb;
}
void set_notify_change_callback(NotifyChangeCallback cb)
{
notify_change_cb_ = cb;
}
bool update_and_notify_change()
{
T val_fetched{ update_cb_() };
if (val_fetched != val_current_) {
val_current_ = val_fetched;
notify_change_cb_(*this);
return true;
}
return false;
}
bool update_and_notify_change_fuzzy(T min_deviation)
{
T val_fetched{ update_cb_() };
if (std::abs(val_fetched - val_current_) >= min_deviation) {
val_current_ = val_fetched;
notify_change_cb_(*this);
return true;
}
return false;
}
private:
void init(UpdateCallback fetch_cb, NotifyChangeCallback deliver_cb)
{
set_update_callback(fetch_cb);
set_notify_change_callback(deliver_cb);
}
T val_current_{};
UpdateCallback update_cb_{};
NotifyChangeCallback notify_change_cb_{};
};
struct DigitalIn {
public:
DigitalIn() = default;
~DigitalIn() = default;
DigitalIn(u8 daisy_pin)
{
init(daisy_pin);
}
DigitalIn(DigitalIn&) = delete;
DigitalIn& operator=(DigitalIn&) = delete;
DigitalIn(DigitalIn&&) = delete;
DigitalIn& operator=(DigitalIn&&) = delete;
void init(u8 daisy_pin)
{
gpio_.Init(ld::DaisySeed::GetPin(daisy_pin), ld::GPIO::Mode::INPUT, ld::GPIO::Pull::PULLUP);
}
bool read()
{
return !gpio_.Read();
}
Cache<bool> cache{ [&]() -> bool { return read(); }, [](Cache<bool>&) {} };
private:
ld::GPIO gpio_{};
};
} // namespace Heck
#endif // HECK_DAISY_UTILS_HH` #endif // HECK_DAISY_UTILS_HH`
Loading…
Cancel
Save