From 91ed686ea8f0ddf33d462e8c744d8b69e76c028f Mon Sep 17 00:00:00 2001 From: doctea Date: Tue, 26 Mar 2024 00:03:18 +0000 Subject: [PATCH] proof-of-concept of a board-agnostic, simple micros()-check-based mode --- src/platforms/generic.h | 30 +++++++++++++++++ src/uClock.cpp | 71 +++++++++++++++++++++++++---------------- 2 files changed, 74 insertions(+), 27 deletions(-) create mode 100644 src/platforms/generic.h diff --git a/src/platforms/generic.h b/src/platforms/generic.h new file mode 100644 index 0000000..9b543a4 --- /dev/null +++ b/src/platforms/generic.h @@ -0,0 +1,30 @@ +#include + +//#define ATOMIC(X) noInterrupts(); X; interrupts(); +#define ATOMIC(X) X; + +// forward declaration of ISR +void uClockHandler(); + +uint32_t uclock_last_time_ticked; +uint32_t uclock_us_interval; + +// call this as often as possible to tick the uClock +void uClockCheckTime(uint32_t micros_time) { + if (micros_time - uclock_last_time_ticked >= uclock_us_interval) { + uclock_last_time_ticked = micros(); + uClockHandler(); + } +} + +void initTimer(uint32_t init_clock) +{ + // basically nothing to do for software-implemented version..? + uclock_last_time_ticked = micros(); +} + +void setTimer(uint32_t us_interval) +{ + uclock_us_interval = us_interval; + Serial.printf("setTimer(%d)\n", us_interval); Serial.flush(); +} \ No newline at end of file diff --git a/src/uClock.cpp b/src/uClock.cpp index 3ad2473..1525fae 100755 --- a/src/uClock.cpp +++ b/src/uClock.cpp @@ -28,36 +28,52 @@ #include "uClock.h" // -// General Arduino AVRs port +// Generic, board-agnostic, not-accurate, no-interrupt, software-only port // -#if defined(ARDUINO_ARCH_AVR) - #include "platforms/avr.h" -#endif -// -// Teensyduino ARMs port -// -#if defined(TEENSYDUINO) - #include "platforms/teensy.h" -#endif -// -// Seedstudio XIAO M0 port -// -#if defined(SEEED_XIAO_M0) - #include "platforms/samd.h" +#if !defined(USE_UCLOCK_GENERIC) + // + // General Arduino AVRs port + // + #if defined(ARDUINO_ARCH_AVR) + #include "platforms/avr.h" + #define UCLOCK_PLATFORM_FOUND + #endif + // + // Teensyduino ARMs port + // + #if defined(TEENSYDUINO) + #include "platforms/teensy.h" + #define UCLOCK_PLATFORM_FOUND + #endif + // + // Seedstudio XIAO M0 port + // + #if defined(SEEED_XIAO_M0) + #include "platforms/samd.h" + #define UCLOCK_PLATFORM_FOUND + #endif + // + // ESP32 family + // + #if defined(ARDUINO_ARCH_ESP32) || defined(ESP32) + #include "platforms/esp32.h" + #define UCLOCK_PLATFORM_FOUND + #endif + // + // STM32XX family + // + #if defined(ARDUINO_ARCH_STM32) + #include "platforms/stm32.h" + #define UCLOCK_PLATFORM_FOUND + #endif #endif -// -// ESP32 family -// -#if defined(ARDUINO_ARCH_ESP32) || defined(ESP32) - #include "platforms/esp32.h" -#endif -// -// STM32XX family -// -#if defined(ARDUINO_ARCH_STM32) - #include "platforms/stm32.h" + +#if !defined(UCLOCK_PLATFORM_FOUND) + #pragma message ("NOTE: uClock is using the 'generic' approach instead of specific board support, because board is not supported or because of USE_UCLOCK_GENERIC build flag.") + #include "platforms/generic.h" #endif + // // Platform specific timer setup/control // @@ -72,6 +88,7 @@ void uclockInitTimer() void setTimerTempo(float bpm) { + Serial.printf("setTimerTempo(%3.3f)..", bpm); setTimer(uClock.bpmToMicroSeconds(bpm)); } @@ -119,7 +136,7 @@ void uClockClass::init() uint32_t uClockClass::bpmToMicroSeconds(float bpm) { - return (60000000 / ppqn / bpm); + return (60000000.0f / (float)ppqn / bpm); } void uClockClass::setPPQN(PPQNResolution resolution)