diff --git a/library.properties b/library.properties index aeac01b..a3c24dd 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SparkFun Toolkit -version=1.2.0 +version=1.2.1 author=SparkFun Electronics maintainer=SparkFun Electronics sentence=A utility library that other SparkFun libraries can take advantage of. 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();