From f4977eb57f8437faf585526071af5e200be547d1 Mon Sep 17 00:00:00 2001 From: Kirk Benell Date: Wed, 30 Sep 2026 16:30:14 -0600 Subject: [PATCH] added function to support us delay - now on correct branch! --- src/sfTk/sfToolkit.h | 4 ++++ src/sfTkArduino.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/sfTk/sfToolkit.h b/src/sfTk/sfToolkit.h index c8b15ba..e0ffd2e 100644 --- a/src/sfTk/sfToolkit.h +++ b/src/sfTk/sfToolkit.h @@ -46,6 +46,10 @@ int32_t sftk_byte_swap(int32_t i); // delay in milliseconds void sftk_delay_ms(uint32_t ms); +// delay in microseconds +// see: https://docs.arduino.cc/language-reference/en/functions/time/delayMicroseconds/ +void sftk_delay_us(uint32_t us); + // ticks in milliseconds uint32_t sftk_ticks_ms(void); diff --git a/src/sfTkArduino.cpp b/src/sfTkArduino.cpp index 49b5aef..c49f380 100644 --- a/src/sfTkArduino.cpp +++ b/src/sfTkArduino.cpp @@ -17,9 +17,33 @@ void sftk_delay_ms(uint32_t ms) { + if (ms == 0) + return; delay(ms); } +// published max us delay for delayMicroseconds() on Arduino +static const uint32_t MAX_DELAY_US = 16383; + +void sftk_delay_us(uint32_t us) +{ + // no tick, not dice + if (us == 0) + return; + + // Can the built in just handle it? + if (us <= MAX_DELAY_US) + delayMicroseconds((unsigned int)us); + else + { + // break up microseconds into milliseconds and the remaining microseconds + delay(us / 1000); // whole ms + unsigned int rest = us % 1000; // always < 1000, safe cast + if (rest) + delayMicroseconds(rest); + } +} + uint32_t sftk_ticks_ms(void) { return millis();