From c954fb15de566c2a7251d90b6d2ea6292918e2cd Mon Sep 17 00:00:00 2001 From: Alexandre THOMAZO Date: Sat, 25 Sep 2021 20:35:49 +0200 Subject: [PATCH 1/3] Add support for ESP32 --- TM1637.cpp | 40 ++++++++++++++++++++++++++-------------- TM1637.h | 2 ++ 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/TM1637.cpp b/TM1637.cpp index 83939ad..c67c125 100644 --- a/TM1637.cpp +++ b/TM1637.cpp @@ -25,6 +25,10 @@ #define TM1637_CMD_SET_ADDR 0xC0 #define TM1637_CMD_DISPLAY 0x88 +#if defined(ESP32) + #define CLOCK_DELAY 1 +#endif + /*************** --- @@ -192,14 +196,15 @@ uint8_t TM1637::writeByte(uint8_t data) // shift out data 8 bits LSB first for (uint8_t i = 8; i > 0; i--) { - digitalWrite(_clock, LOW); - digitalWrite(_data, data & 0x01); - digitalWrite(_clock, HIGH); + writeSync(_clock, LOW); + writeSync(_data, data & 0x01); + writeSync(_clock, HIGH); data >>= 1; } - digitalWrite(_clock, LOW); - digitalWrite(_data, HIGH); - digitalWrite(_clock, HIGH); + + writeSync(_clock, LOW); + writeSync(_data, HIGH); + writeSync(_clock, HIGH); // get ACKNOWLEDGE pinMode(_data, INPUT); @@ -216,20 +221,27 @@ uint8_t TM1637::writeByte(uint8_t data) void TM1637::start() { - digitalWrite(_clock, HIGH); - digitalWrite(_data, HIGH); - digitalWrite(_data, LOW); - digitalWrite(_clock, LOW); + writeSync(_clock, HIGH); + writeSync(_data, HIGH); + writeSync(_data, LOW); + writeSync(_clock, LOW); } void TM1637::stop() { - digitalWrite(_clock, LOW); - digitalWrite(_data, LOW); - digitalWrite(_clock, HIGH); - digitalWrite(_data, HIGH); + writeSync(_clock, LOW); + writeSync(_data, LOW); + writeSync(_clock, HIGH); + writeSync(_data, HIGH); } +void TM1637::writeSync(uint8_t pin, uint8_t val) { + digitalWrite(pin, val); + + #ifdef CLOCK_DELAY + delayMicroseconds(CLOCK_DELAY); + #endif +} // -- END OF FILE -- diff --git a/TM1637.h b/TM1637.h index ff2a800..b461a09 100644 --- a/TM1637.h +++ b/TM1637.h @@ -46,6 +46,8 @@ class TM1637 uint8_t writeByte(uint8_t data); void start(); void stop(); + + void writeSync(uint8_t pin, uint8_t val); }; // -- END OF FILE -- From 2dadefe263197a432597260a872bbccc2705a0bb Mon Sep 17 00:00:00 2001 From: Alexandre THOMAZO Date: Sun, 26 Sep 2021 14:06:31 +0200 Subject: [PATCH 2/3] Rework ESP32 timing for less waiting --- TM1637.cpp | 18 +++++++++++------- TM1637.h | 1 + 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/TM1637.cpp b/TM1637.cpp index c67c125..129f3f5 100644 --- a/TM1637.cpp +++ b/TM1637.cpp @@ -25,10 +25,6 @@ #define TM1637_CMD_SET_ADDR 0xC0 #define TM1637_CMD_DISPLAY 0x88 -#if defined(ESP32) - #define CLOCK_DELAY 1 -#endif - /*************** --- @@ -236,12 +232,20 @@ void TM1637::stop() writeSync(_data, HIGH); } -void TM1637::writeSync(uint8_t pin, uint8_t val) { +void TM1637::writeSync(uint8_t pin, uint8_t val) +{ digitalWrite(pin, val); - #ifdef CLOCK_DELAY - delayMicroseconds(CLOCK_DELAY); + #if defined(ESP32) + nanoDelay(2); #endif + +} + +void TM1637::nanoDelay(uint16_t n) // 240 ==> at 240 MHz +{ + volatile uint16_t i; + for (i = 0; i < n; ++i); } // -- END OF FILE -- diff --git a/TM1637.h b/TM1637.h index b461a09..11caed2 100644 --- a/TM1637.h +++ b/TM1637.h @@ -48,6 +48,7 @@ class TM1637 void stop(); void writeSync(uint8_t pin, uint8_t val); + void nanoDelay(uint16_t n); }; // -- END OF FILE -- From afe0da82a02c2dfbacacf1b65c65095e1d0a1381 Mon Sep 17 00:00:00 2001 From: Alexandre THOMAZO Date: Sun, 26 Sep 2021 15:53:40 +0200 Subject: [PATCH 3/3] Tweaking a little more timing for ESP32 Using new nanoDelay, total time from 133us to 113us --- TM1637.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/TM1637.cpp b/TM1637.cpp index 129f3f5..48975ce 100644 --- a/TM1637.cpp +++ b/TM1637.cpp @@ -242,10 +242,10 @@ void TM1637::writeSync(uint8_t pin, uint8_t val) } -void TM1637::nanoDelay(uint16_t n) // 240 ==> at 240 MHz +void TM1637::nanoDelay(uint16_t n) { - volatile uint16_t i; - for (i = 0; i < n; ++i); + volatile uint16_t i = n; + while (i--); } // -- END OF FILE --