diff --git a/README.md b/README.md index 403028a..fd345a9 100644 --- a/README.md +++ b/README.md @@ -23,5 +23,7 @@ The library provides a single class named TM1637Display. An instance of this cla * `showNumberDec` - Display a decimal number * `showNumberDecEx` - Display a decimal number with decimal points or colon * `setBrightness` - Sets the brightness of the display +* `setColon` - Sets the colon on and off of the display +* `getColon` - Return the colon status of the display The information given above is only a summary. Please refer to TM1637Display.h for more information. An example is included, demonstrating the operation of most of the functions. diff --git a/TM1637Display.cpp b/TM1637Display.cpp index eada692..e9edab8 100644 --- a/TM1637Display.cpp +++ b/TM1637Display.cpp @@ -78,6 +78,18 @@ void TM1637Display::setBrightness(uint8_t brightness, bool on) m_brightness = (brightness & 0x7) | (on? 0x08 : 0x00); } +void TM1637Display::setColon(bool colon) +{ + m_colon = colon; + + setSegments(m_digit, 1, 1); // m_digit stores segment layout from last write +} + +bool TM1637Display::getColon() +{ + return m_colon; +} + void TM1637Display::setSegments(const uint8_t segments[], uint8_t length, uint8_t pos) { // Write COMM1 @@ -90,8 +102,18 @@ void TM1637Display::setSegments(const uint8_t segments[], uint8_t length, uint8_ writeByte(TM1637_I2C_COMM2 + (pos & 0x03)); // Write the data bytes - for (uint8_t k=0; k < length; k++) - writeByte(segments[k]); + for (uint8_t k{0}; k < length; k++) { + if (k + pos == 1) { + m_digit[0] = segments[k]; // Store second digit + if (m_colon) { + writeByte(segments[k] | 0x80); // Set colon ON + } else { + writeByte(segments[k] | 0x7f); // Set colon OFF + } + } else { + writeByte(segments[k]); + } + } stop(); @@ -149,17 +171,17 @@ void TM1637Display::showNumberBaseEx(int8_t base, uint16_t num, uint8_t dots, bo // digits[i] = minusSegments; // i--; //} - + for(int i = length-1; i >= 0; --i) { uint8_t digit = num % base; - + if (digit == 0 && num == 0 && leading_zero == false) // Leading zero is blank digits[i] = 0; else digits[i] = encodeDigit(digit); - + if (digit == 0 && num == 0 && negative) { digits[i] = minusSegments; negative = false; @@ -168,12 +190,12 @@ void TM1637Display::showNumberBaseEx(int8_t base, uint16_t num, uint8_t dots, bo num /= base; } } - + if(dots != 0) { showDots(dots, digits); } - + setSegments(digits, length, pos); } diff --git a/TM1637Display.h b/TM1637Display.h index f586c78..02484d9 100644 --- a/TM1637Display.h +++ b/TM1637Display.h @@ -51,6 +51,18 @@ class TM1637Display { //! @param on Turn display on or off void setBrightness(uint8_t brightness, bool on = true); + //! Sets the colon on/off. + //! + //! The setting takes immediate effect. + //! + //! @param colon : false (off-default) or true (on) + void setColon(bool colon = false); + + //! Get colon status + //! + //! @return the current status of the colon + bool getColon(); + //! Display arbitrary data on the module //! //! This function receives raw segment values as input and displays them. The segment data @@ -152,7 +164,7 @@ class TM1637Display { bool writeByte(uint8_t b); void showDots(uint8_t dots, uint8_t* digits); - + void showNumberBaseEx(int8_t base, uint16_t num, uint8_t dots = 0, bool leading_zero = false, uint8_t length = 4, uint8_t pos = 0); @@ -161,6 +173,8 @@ class TM1637Display { uint8_t m_pinDIO; uint8_t m_brightness; unsigned int m_bitDelay; + bool m_colon{false}; + uint8_t m_digit[1]; }; #endif // __TM1637DISPLAY__ diff --git a/examples/TM1637Colon/TM1637Colon.ino b/examples/TM1637Colon/TM1637Colon.ino new file mode 100644 index 0000000..11b6a65 --- /dev/null +++ b/examples/TM1637Colon/TM1637Colon.ino @@ -0,0 +1,31 @@ +#include +#include + +// Module connection pins (Digital Pins) +#define CLK 2 +#define DIO 3 + +TM1637Display display(CLK, DIO); + +unsigned long lastUpdatePoint{0}; + +void setup() +{ + display.setBrightness(0x0f); +} + +void loop() +{ + unsigned int hour{16}; + unsigned int minute{45}; + display.showNumberDec(hour, false, 2, 0); + display.showNumberDec(minute, true, 2, 2); + displayColon(1000); // Triggered every seconds +} + +void displayColon(const uint16_t &interval) { + if (millis() > (lastUpdatePoint + interval)) { + display.setColon(!display.getColon()); // Toggle the current state of the colon + lastUpdatePoint = millis(); + } +} \ No newline at end of file diff --git a/keywords.txt b/keywords.txt index 8a43483..4de2ec9 100644 --- a/keywords.txt +++ b/keywords.txt @@ -8,13 +8,15 @@ TM1637Display KEYWORD1 # Methods and Functions (KEYWORD2) ####################################### -setBrightness KEYWORD2 -setSegments KEYWORD2 -clear KEYWORD2 -showNumberDec KEYWORD2 -showNumberDecEx KEYWORD2 -showNumberHexEx KEYWORD2 -encodeDigit KEYWORD2 +setBrightness KEYWORD2 +setSegments KEYWORD2 +setColon KEYWORD2 +getColon KEYWORD2 +clear KEYWORD2 +showNumberDec KEYWORD2 +showNumberDecEx KEYWORD2 +showNumberHexEx KEYWORD2 +encodeDigit KEYWORD2 ####################################### # Constants (LITERAL1)