From 4eb27586795f29fab6f797efdb30114b0563c377 Mon Sep 17 00:00:00 2001 From: heck Date: Tue, 5 Nov 2024 22:52:53 +0100 Subject: [PATCH] commit too many things in prototyping stage --- examples/Makefile | 33 +++++++------ examples/main_adc.cc | 13 +++-- examples/main_adc.hh | 3 +- examples/main_blink.cc | 3 +- examples/main_cli.cc | 16 ++++++ examples/main_console.cc | 58 ++++++++++++++++++++++ examples/main_density.cc | 18 +++---- examples/main_density.hh | 9 +--- src/cli.cc | 0 src/cli.hh | 58 ++++++++++++++++++++++ src/console.cc | 52 ++++++++++++++++++++ src/console.hh | 15 ++++++ src/density.cc | 4 +- src/density.hh | 10 ++-- src/dizzy.hh | 12 +++++ src/dizzy_types.hh | 16 ++++++ src/heck_types.hh | 22 +++++++++ src/types.hh | 36 -------------- src/utils.cc | 8 +-- src/utils.hh | 104 ++++++++++++++++++++++++++++++++------- 20 files changed, 384 insertions(+), 106 deletions(-) create mode 100644 examples/main_cli.cc create mode 100644 examples/main_console.cc create mode 100644 src/cli.cc create mode 100644 src/cli.hh create mode 100644 src/console.cc create mode 100644 src/console.hh create mode 100644 src/dizzy.hh create mode 100644 src/dizzy_types.hh create mode 100644 src/heck_types.hh delete mode 100644 src/types.hh diff --git a/examples/Makefile b/examples/Makefile index 170b25e..9bfa71e 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -10,22 +10,23 @@ CXXFLAGS += -I$(LIBDIZZY_DIR) LDFLAGS += -L$(LIBDIZZY_DIR) LDFLAGS += -ldizzy -SRC_EXCLUDED := \ - main_adc_direct.cc \ - main_memtest1.cc \ - main_minimal.c \ - main_cpuload.cc -# main_adc.cc \ -# main_blink.cc \ -# main_cli.cc \ -# main_density.cc \ -# main_framework_proto1.cc \ -# main_framework_proto2.cc \ -# main_memtest1.cc \ -# main_minimal.cc \ -# main_template_full.cc \ -# main_testtone.cc \ -# main_usbhost.cc \ +SRC_EXCLUDED := +#SRC_EXCLUDED += main_adc.cc +SRC_EXCLUDED += main_adc_direct.cc +#SRC_EXCLUDED += main_console.cc +#SRC_EXCLUDED += main_cli.cc +SRC_EXCLUDED += main_memtest1.cc +SRC_EXCLUDED += main_minimal.c +SRC_EXCLUDED += main_cpuload.cc +#SRC_EXCLUDED += main_blink.cc +#SRC_EXCLUDED += main_density.cc +SRC_EXCLUDED += main_framework_proto1.cc +SRC_EXCLUDED += main_framework_proto2.cc +SRC_EXCLUDED += main_memtest1.cc +SRC_EXCLUDED += main_minimal.cc +SRC_EXCLUDED += main_template_full.cc +SRC_EXCLUDED += main_testtone.cc +SRC_EXCLUDED += main_usbhost.cc SOURCES := $(wildcard *) diff --git a/examples/main_adc.cc b/examples/main_adc.cc index 22f36db..00ec79f 100644 --- a/examples/main_adc.cc +++ b/examples/main_adc.cc @@ -1,18 +1,21 @@ #include "main_adc.hh" -#include "utils.hh" +#include "dizzy.hh" namespace Heck { namespace { // anonymous namespace for internal linkage ld::DaisySeed seed{}; - Cache pot1{ []() { return seed.adc.Get(0); }, - [](Cache& cache) { seed.PrintLine("POT_1: %d", cache.read()); } }; + dz::Cache pot1{ []() { return seed.adc.Get(0); }, + [](dz::Cache& cache) { + seed.PrintLine("POT_1: %d", cache.read()); + } }; - void scan_pots(u32) { + void scan_pots(u32) + { pot1.update_and_notify_change(); } - PeriodicTaskCT task_scan_pots{}; + dz::PeriodicTaskCT task_scan_pots{}; void init() { diff --git a/examples/main_adc.hh b/examples/main_adc.hh index a84eb4c..5b8e229 100644 --- a/examples/main_adc.hh +++ b/examples/main_adc.hh @@ -1,8 +1,7 @@ #ifndef HECK_MAIN_ADC_HH #define HECK_MAIN_ADC_HH -#include -#include "types.hh" +#include "dizzy.hh" namespace Heck { namespace Constants { diff --git a/examples/main_blink.cc b/examples/main_blink.cc index a3fb4e4..7753657 100644 --- a/examples/main_blink.cc +++ b/examples/main_blink.cc @@ -1,5 +1,4 @@ -#include "daisy_seed.h" -#include "types.hh" +#include "dizzy.hh" namespace Heck { namespace Constants { diff --git a/examples/main_cli.cc b/examples/main_cli.cc new file mode 100644 index 0000000..f71e383 --- /dev/null +++ b/examples/main_cli.cc @@ -0,0 +1,16 @@ +#include "dizzy.hh" + +ld::DaisySeed seed{}; + +int main() +{ + seed.Init(); + ld::DaisySeed::StartLog(true); + ld::DaisySeed::PrintLine("booting..."); + dz::CLI::init(seed); + + while (true) { + dz::CLI::process(); + } + +} diff --git a/examples/main_console.cc b/examples/main_console.cc new file mode 100644 index 0000000..3560f74 --- /dev/null +++ b/examples/main_console.cc @@ -0,0 +1,58 @@ +#include "dizzy.hh" + +ld::DaisySeed seed{}; + +struct Time { + Time() + { + msec = seed.system.GetNow(); + usec = seed.system.GetUs(); + tick = seed.system.GetTick(); + } + + u32 msec; + u32 usec; + u32 tick; +}; + + +std::string to_string(Time &obj) +{ + std::string ret{ "Time: " }; + ret += std::to_string(obj.msec); + ret += " / "; + ret += std::to_string(obj.usec); + ret += " / "; + ret += std::to_string(obj.tick); + return ret; +} + +void print_uptime(u32 time) +{ + Time t{}; + std::string str{ to_string(t) + "\n\r" }; + dz::Console::tx(str.c_str(), str.size()); +} + +dz::PeriodicTaskCT task1{}; + +int main(void) +{ + seed.Init(); + + dz::Console::init(); + while (!dz::Console::has_next()) {}; + + std::string motd{ "dizzy console\n" }; + dz::Console::tx(motd.c_str(), motd.size()); + + while (true) { + Time time_mainloop_begin{}; + while (dz::Console::has_next()) { + char b = dz::Console::rx(); + dz::Console::tx(&b, 1); + } + + task1.run_pending(time_mainloop_begin.msec); + } +} \ No newline at end of file diff --git a/examples/main_density.cc b/examples/main_density.cc index 4b61a85..3763733 100644 --- a/examples/main_density.cc +++ b/examples/main_density.cc @@ -1,7 +1,5 @@ #include "main_density.hh" -#include "utils.hh" #include -#include "density.hh" namespace Heck { namespace { // anonymous namespace for internal linkage @@ -14,14 +12,14 @@ namespace Heck { std::array pots_value{}; - Observer pot1{}; - Observer pot2{}; - Observer pot3{}; - Observer pot4{}; + dz::Observer pot1{}; + dz::Observer pot2{}; + dz::Observer pot3{}; + dz::Observer pot4{}; - SWTimer scan_pots{}; + dz::SWTimer scan_pots{}; - Density density{}; + dz::Density density{}; // function prototypes void audio_callback( @@ -109,7 +107,7 @@ namespace Heck { void midi_async_handler(const ld::MidiEvent& msg) { char strbuf[128]; - GetMidiTypeAsString(msg, &strbuf[0]); + dz::GetMidiTypeAsString(msg, &strbuf[0]); seed.PrintLine("%s", strbuf); return; } @@ -147,7 +145,7 @@ namespace Heck { bool heartbeat_led_state{ false }; - SWTimer heartbeat{}; + dz::SWTimer heartbeat{}; heartbeat.set_period(100); heartbeat.set_callback([&heartbeat_led_state](u32 time_now) { heartbeat_led_state = !heartbeat_led_state; diff --git a/examples/main_density.hh b/examples/main_density.hh index 6540df5..5f1b00c 100644 --- a/examples/main_density.hh +++ b/examples/main_density.hh @@ -1,15 +1,11 @@ #ifndef HECK_LIBDIZZY_MAIN_DENSITY_HH #define HECK_LIBDIZZY_MAIN_DENSITY_HH -#include -#include "daisy_seed.h" -#include "daisysp.h" -#include "types.hh" +#include "dizzy.hh" namespace Heck { namespace Constants { namespace Hardware { - // constexpr int PIN_BUTTON_RECORD = 28; constexpr int PIN_POT_1 = 15; constexpr int PIN_POT_2 = 16; constexpr int PIN_POT_3 = 17; @@ -18,7 +14,7 @@ namespace Heck { constexpr bool CPU_BOOST480MHZ = false; constexpr int AUDIO_BUFFERSIZE = 4; - constexpr Samplerate AUDIO_SAMPLERATE = Samplerate::SAI_48KHZ; + constexpr dz::Samplerate AUDIO_SAMPLERATE = dz::Samplerate::SAI_48KHZ; namespace Developer { constexpr bool LOG_BLOCKS_BOOT = false; @@ -27,7 +23,6 @@ namespace Heck { //Hardware extern ld::DaisySeed seed; - // extern ld::Switch but_rec; } // namespace Heck diff --git a/src/cli.cc b/src/cli.cc new file mode 100644 index 0000000..e69de29 diff --git a/src/cli.hh b/src/cli.hh new file mode 100644 index 0000000..57c805c --- /dev/null +++ b/src/cli.hh @@ -0,0 +1,58 @@ +#ifndef HECK_DIZZY_CLI_HH +#define HECK_DIZZY_CLI_HH + +#include +#include +#include "dizzy_types.hh" +#include "console.hh" +#include + + +namespace Heck::Dizzy { + namespace CLI { + using command_function = std::function; + + inline std::string command_buf; + inline std::map command_table; + + inline void command_test() + { + ld::DaisySeed::PrintLine("test"); + } + + inline void init(ld::DaisySeed& seed) + { + Console::init(); + ld::DaisySeed::PrintLine("Dizzy-CLI"); + ld::DaisySeed::Print("> "); + command_table.emplace("test", &command_test); + } + + inline void process() + { + while (Console::has_next()) { + u8 c = Console::rx(); + if (c == 13) { //Enter + //echo + ld::DaisySeed::PrintLine(""); + if (!command_buf.empty()) { + command_function func = command_table[command_buf]; + if (func) { + ld::DaisySeed::PrintLine("running command: %s", command_buf.data()); + func(); + } else { + ld::DaisySeed::PrintLine("error: unknown command: %s", command_buf.data()); + } + command_buf.clear(); + } + ld::DaisySeed::Print("> "); + } else { + //echo + ld::DaisySeed::Print("%c", c); + command_buf.push_back(static_cast(c)); + } + } + } + } // namespace CLI +} // namespace Heck::Dizzy +#endif \ No newline at end of file diff --git a/src/console.cc b/src/console.cc new file mode 100644 index 0000000..9b99381 --- /dev/null +++ b/src/console.cc @@ -0,0 +1,52 @@ +#include "console.hh" +#include +#include "hid/usb.h" + +namespace Heck::Dizzy { + namespace Console { + + struct Session { + std::deque rx_buff; + daisy::UsbHandle usb_handle; + }; + + static Session session_{}; + + + extern "C" void usb_rx_callback(u8* buff, u32* length) + { + if (buff && length) { + for (u32 i = 0; i < *length; i++) { + session_.rx_buff.emplace_back(buff[i]); + } + } + } + + void init() + { + session_.usb_handle.Init(daisy::UsbHandle::FS_INTERNAL); + session_.usb_handle.SetReceiveCallback( + usb_rx_callback, + daisy::UsbHandle::UsbPeriph::FS_INTERNAL); + } + + bool has_next() + { + return !session_.rx_buff.empty(); + } + + char rx() + { + char ret{ session_.rx_buff.front() }; + session_.rx_buff.pop_front(); + return ret; + } + + bool tx(const char* buffer, size_t len) + { + return daisy::UsbHandle::Result::OK == + session_.usb_handle.TransmitInternal((uint8_t*)buffer, len); + } + + } // namespace Console +} // namespace Heck::Dizzy diff --git a/src/console.hh b/src/console.hh new file mode 100644 index 0000000..35de8fb --- /dev/null +++ b/src/console.hh @@ -0,0 +1,15 @@ +#ifndef HECK_DIZZY_CONSOLE_HH +#define HECK_DIZZY_CONSOLE_HH + +#include "dizzy_types.hh" + +namespace Heck::Dizzy { + namespace Console { + void init(); + bool has_next(); + char rx(); + bool tx(const char *buffer, size_t len); + } // namespace Console +} // namespace Heck::Dizzy + +#endif \ No newline at end of file diff --git a/src/density.cc b/src/density.cc index 6ba8041..cba76eb 100644 --- a/src/density.cc +++ b/src/density.cc @@ -1,5 +1,5 @@ #include "density.hh" -namespace Heck { +namespace Heck::Dizzy { namespace Constants { constexpr Samplerate AUDIO_SAMPLERATE = Samplerate::SAI_48KHZ; @@ -111,4 +111,4 @@ namespace Heck { destP++; } } -} // namespace Heck \ No newline at end of file +} // namespace Heck::Dizzy \ No newline at end of file diff --git a/src/density.hh b/src/density.hh index 69220b3..ab444d3 100644 --- a/src/density.hh +++ b/src/density.hh @@ -1,10 +1,10 @@ -#ifndef HECK_OSP_DENSITY -#define HECK_OSP_DENSITY +#ifndef HECK_DIZZY_DENSITY_HH +#define HECK_DIZZY_DENSITY_HH -#include "types.hh" +#include "dizzy_types.hh" #include -namespace Heck { +namespace Heck::Dizzy { struct Density { using Float32 = f32; using Float64 = f64; @@ -24,5 +24,5 @@ namespace Heck { }; -} // namespace Heck +} // namespace Heck::Dizzy #endif \ No newline at end of file diff --git a/src/dizzy.hh b/src/dizzy.hh new file mode 100644 index 0000000..af90bea --- /dev/null +++ b/src/dizzy.hh @@ -0,0 +1,12 @@ +#ifndef HECK_DIZZY_HH +#define HECK_DIZZY_HH + +#include "dizzy_types.hh" +#include "utils.hh" +#include "console.hh" +#include "cli.hh" +#include "density.hh" + +namespace dz = Heck::Dizzy; + +#endif \ No newline at end of file diff --git a/src/dizzy_types.hh b/src/dizzy_types.hh new file mode 100644 index 0000000..2033bba --- /dev/null +++ b/src/dizzy_types.hh @@ -0,0 +1,16 @@ +#ifndef HECK_DIZZY_TYPES_HH +#define HECK_DIZZY_TYPES_HH + +#include "heck_types.hh" +#include "daisy_seed.h" +#include "daisysp.h" + +namespace ld = daisy; +namespace dsp = daisysp; +using namespace Heck::Types; + +namespace Heck::Dizzy { + using Samplerate = ld::SaiHandle::Config::SampleRate; +} // namespace Heck::Dizzy + +#endif \ No newline at end of file diff --git a/src/heck_types.hh b/src/heck_types.hh new file mode 100644 index 0000000..b9dd647 --- /dev/null +++ b/src/heck_types.hh @@ -0,0 +1,22 @@ +#ifndef HECK_TYPES_HH +#define HECK_TYPES_HH + +#include + +namespace Heck::Types { + // fundamental types + using u8 = uint8_t; + using u16 = uint16_t; + using u32 = uint32_t; + using u64 = uint64_t; + + using i8 = int8_t; + using i16 = int16_t; + using i32 = int32_t; + using i64 = int64_t; + + using f32 = float; + using f64 = double; +} // namespace Heck::Types + +#endif \ No newline at end of file diff --git a/src/types.hh b/src/types.hh deleted file mode 100644 index 5fb8d11..0000000 --- a/src/types.hh +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef HECK_DAISY_TYPES_HH -#define HECK_DAISY_TYPES_HH - -#include -#include "daisy_seed.h" -#include "daisysp.h" - -namespace Heck { - namespace Types { - // fundamental types - using u8 = uint8_t; - using u16 = uint16_t; - using u32 = uint32_t; - using u64 = uint64_t; - - using i8 = int8_t; - using i16 = int16_t; - using i32 = int32_t; - using i64 = int64_t; - - using f32 = float; - using f64 = double; - } - - using namespace Types; - // namespace aliases - namespace ld = daisy; - namespace dsp = daisysp; - - // type aliases from libs - using Samplerate = ld::SaiHandle::Config::SampleRate; - -} // namespace Heck - - -#endif \ No newline at end of file diff --git a/src/utils.cc b/src/utils.cc index 6411f63..d891433 100644 --- a/src/utils.cc +++ b/src/utils.cc @@ -1,6 +1,6 @@ #include "utils.hh" -namespace Heck { +namespace Heck::Dizzy { void GetMidiTypeAsString(const ld::MidiEvent& msg, char* str) { @@ -91,9 +91,9 @@ namespace Heck { float ret = min + (val * range); return ret; } -} // namespace Heck +} // namespace Heck::Dizzy -namespace Heck { +namespace Heck::Dizzy { void SWTimer::set_period(u32 time_units) { time_period_ = time_units; @@ -116,4 +116,4 @@ namespace Heck { } return false; } -} // namespace Heck +} // namespace Heck::Dizzy diff --git a/src/utils.hh b/src/utils.hh index ce49ce7..aeb717d 100644 --- a/src/utils.hh +++ b/src/utils.hh @@ -1,19 +1,31 @@ -#ifndef HECK_DAISY_UTILS_HH -#define HECK_DAISY_UTILS_HH +#ifndef HECK_DIZZY_UTILS_HH +#define HECK_DIZZY_UTILS_HH -#include "daisy_seed.h" -#include "types.hh" +#include "dizzy_types.hh" -namespace Heck { + +// --------------------------------------------------------- +// String representations +// --------------------------------------------------------- +namespace Heck::Dizzy { void GetMidiTypeAsString(const ld::MidiEvent& msg, char* str); void GetMidiRTTypeAsString(const ld::MidiEvent& msg, char* str); +} // namespace Heck::Dizzy +// --------------------------------------------------------- +// Scaling +// --------------------------------------------------------- +namespace Heck::Dizzy { float scalen_min_max(float val, float min, float max); float scalen_center_range(float val, float center, float range); -} // namespace Heck +} // namespace Heck::Dizzy + -namespace Heck { +// --------------------------------------------------------- +// SWTimer +// --------------------------------------------------------- +namespace Heck::Dizzy { class SWTimer { public: using Callback = std::function; @@ -28,6 +40,47 @@ namespace Heck { u32 time_period_{}; }; + // !!! + // constexpr function pointer + // !!! +// template +// struct FunctionPtr { +// constexpr FunctionPtr() = default; +// static void call() +// { +// F(); +// } +// }; + + + + template + class PeriodicTaskCT { + public: + PeriodicTaskCT() = default; + + PeriodicTaskCT(PeriodicTaskCT&) = delete; + PeriodicTaskCT& operator=(PeriodicTaskCT&) = delete; + + PeriodicTaskCT(PeriodicTaskCT&&) = delete; + PeriodicTaskCT& operator=(PeriodicTaskCT&&) = delete; + + ~PeriodicTaskCT() = default; + + bool run_pending(u32 time_now) + { + if (time_now - time_last_exec_ >= PERIOD) { + time_last_exec_ = time_now; + TASK(time_now); + return true; + } + return false; + } + + private: + u32 time_last_exec_{}; + }; + class PeriodicTask { public: using Task = std::function; @@ -80,10 +133,15 @@ namespace Heck { u32 time_last_exec_{}; u32 time_period_{}; }; -} // namespace Heck +} // namespace Heck::Dizzy -namespace Heck { - template class Observer { + +// --------------------------------------------------------- +// Observer +// --------------------------------------------------------- +namespace Heck::Dizzy { + template + class Observer { public: bool on_change(T val_new, std::function fn) @@ -110,7 +168,8 @@ namespace Heck { T val_current_{}; }; - template class PollingObserver { + template + class PollingObserver { public: using FetchCallback = std::function; @@ -173,28 +232,33 @@ namespace Heck { FetchCallback fetch_cb_{}; DeliverCallback deliver_cb_{}; }; -} // namespace Heck +} // namespace Heck::Dizzy + -namespace Heck { - template class Cache { +// --------------------------------------------------------- +// Cache +// --------------------------------------------------------- +namespace Heck::Dizzy { + template + class Cache { public: using UpdateCallback = std::function; using NotifyChangeCallback = std::function&)>; - Cache() = delete; - ~Cache() = default; explicit Cache(UpdateCallback update_cb, NotifyChangeCallback notify_change_cb) { init(update_cb, notify_change_cb); } + Cache() = delete; Cache(Cache&) = delete; Cache& operator=(Cache) = delete; Cache(Cache&&) = delete; Cache& operator=(Cache&&) = delete; + ~Cache() = default; T read() { @@ -245,6 +309,12 @@ namespace Heck { NotifyChangeCallback notify_change_cb_{}; }; +} // namespace Heck::Dizzy + +// --------------------------------------------------------- +// DigitalIn +// --------------------------------------------------------- +namespace Heck::Dizzy { struct DigitalIn { public: DigitalIn() = default; @@ -279,5 +349,5 @@ namespace Heck { }; -} // namespace Heck +} // namespace Heck::Dizzy #endif // HECK_DAISY_UTILS_HH` \ No newline at end of file