From de6b8f0eca13c3e50714443c043319a98871de5d Mon Sep 17 00:00:00 2001 From: "Jason A. Cox" Date: Mon, 9 May 2022 22:11:02 -0700 Subject: [PATCH] Add flipDisplay() function #2 --- RELEASE.md | 8 ++++++++ TM1637TinyDisplay.cpp | 30 +++++++++++++++++++++++++++--- keywords.txt | 1 + library.properties | 2 +- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/RELEASE.md b/RELEASE.md index 346dcb6..0904052 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -75,3 +75,11 @@ display.showAnimation(ANIMATION, FRAMES(ANIMATION), TIME_MS(10)); display.showNumber(9.87, 2, 3, 0); display.showNumber(1.23, 2, 3, 3); ``` + +## v1.5.0 (Beta) +- Added support for device orientation, flipping display upside down if selected during +initialization or through a function call (4-Digit in Beta) +```cpp + // Flip display + display.flipDisplay(true); +``` diff --git a/TM1637TinyDisplay.cpp b/TM1637TinyDisplay.cpp index 3e7ba13..ddebad6 100644 --- a/TM1637TinyDisplay.cpp +++ b/TM1637TinyDisplay.cpp @@ -161,7 +161,7 @@ const uint8_t asciiToSegment[] PROGMEM = { static const uint8_t minusSegments = 0b01000000; static const uint8_t degreeSegments = 0b01100011; -TM1637TinyDisplay::TM1637TinyDisplay(uint8_t pinClk, uint8_t pinDIO, unsigned int bitDelay, unsigned int scrollDelay) +TM1637TinyDisplay::TM1637TinyDisplay(uint8_t pinClk, uint8_t pinDIO, unsigned int bitDelay, unsigned int scrollDelay, bool flip) { // Pin settings m_pinClk = pinClk; @@ -169,6 +169,8 @@ TM1637TinyDisplay::TM1637TinyDisplay(uint8_t pinClk, uint8_t pinDIO, unsigned in // Timing configurations m_bitDelay = bitDelay; m_scrollDelay = scrollDelay; + // Flip + m_flipDisplay = flip; // Set the pin direction and default value. // Both pins are set as inputs, allowing the pull-up resistors to pull them up @@ -178,6 +180,11 @@ TM1637TinyDisplay::TM1637TinyDisplay(uint8_t pinClk, uint8_t pinDIO, unsigned in digitalWrite(m_pinDIO, LOW); } +void TM1637TinyDisplay::flipDisplay(bool flip) +{ + m_flipDisplay = flip; +} + void TM1637TinyDisplay::setBrightness(uint8_t brightness, bool on) { m_brightness = (brightness & 0x07) | (on? 0x08 : 0x00); @@ -190,6 +197,8 @@ void TM1637TinyDisplay::setScrolldelay(unsigned int scrollDelay) void TM1637TinyDisplay::setSegments(const uint8_t segments[], uint8_t length, uint8_t pos) { + uint8_t dot = 0; + // Write COMM1 start(); writeByte(TM1637_I2C_COMM1); @@ -197,11 +206,26 @@ void TM1637TinyDisplay::setSegments(const uint8_t segments[], uint8_t length, ui // Write COMM2 + first digit address start(); + if(m_flipDisplay) pos = MAXDIGITS - pos - length; writeByte(TM1637_I2C_COMM2 + (pos & 0x07)); // Write the data bytes - for (uint8_t k=0; k < length; k++) { - writeByte(segments[k]); + if(m_flipDisplay) { + for (uint8_t k=0; k < length; k++) { + dot = 0; + if((length - k - 2) >= 0) { + dot = segments[length - k - 2] & 0b10000000; + } + uint8_t orig = segments[length - k - 1]; + uint8_t flip = ((orig >> 3) & 0b00000111) + + ((orig << 3) & 0b00111000) + (orig & 0b01000000) + dot; + writeByte(flip); + } + } + else { + for (uint8_t k=0; k < length; k++) { + writeByte(segments[k]); + } } stop(); diff --git a/keywords.txt b/keywords.txt index d75435e..ef6acbd 100644 --- a/keywords.txt +++ b/keywords.txt @@ -23,6 +23,7 @@ showNumberDec KEYWORD2 showNumberHex KEYWORD2 encodeDigit KEYWORD2 encodeASCII KEYWORD2 +flipDisplay KEYWORD2 ####################################### # Constants (LITERAL1) diff --git a/library.properties b/library.properties index fb8cda7..425841e 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=TM1637TinyDisplay -version=1.4.4 +version=1.5.0 author=Jason Cox maintainer=Jason Cox sentence=A simple library to display numbers, text and animation on 4 and 6 digit 7-segment TM1637 based display modules.