Skip to content

Commit

Permalink
v1.5.0 Added flipDisplay() #2
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonacox committed May 11, 2022
1 parent de6b8f0 commit bea5916
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ The library provides a single class named TM1637TinyDisplay with the following f
* `setSegments` - Directly set the value of the LED segments in each digit
* `setBrightness` - Sets the brightness of the display
* `setScrolldelay` - Sets the speed for text scrolling
* `flipDisplay` - Sets/flips the orientation of the display

PROGMEM functions: Large string or animation data can be left in Flash instead of being loaded in to SRAM to save memory.

Expand Down
4 changes: 2 additions & 2 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ display.showAnimation(ANIMATION, FRAMES(ANIMATION), TIME_MS(10));
display.showNumber(1.23, 2, 3, 3);
```

## v1.5.0 (Beta)
## v1.5.0
- Added support for device orientation, flipping display upside down if selected during
initialization or through a function call (4-Digit in Beta)
initialization or through a function call.
```cpp
// Flip display
display.flipDisplay(true);
Expand Down
3 changes: 2 additions & 1 deletion TM1637TinyDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ 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, bool flip)
TM1637TinyDisplay::TM1637TinyDisplay(uint8_t pinClk, uint8_t pinDIO, unsigned int bitDelay,
unsigned int scrollDelay, bool flip)
{
// Pin settings
m_pinClk = pinClk;
Expand Down
34 changes: 30 additions & 4 deletions TM1637TinyDisplay6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,17 @@ const uint8_t asciiToSegment[] PROGMEM = {
static const uint8_t minusSegments = 0b01000000;
static const uint8_t degreeSegments = 0b01100011;

TM1637TinyDisplay6::TM1637TinyDisplay6(uint8_t pinClk, uint8_t pinDIO, unsigned int bitDelay, unsigned int scrollDelay)
TM1637TinyDisplay6::TM1637TinyDisplay6(uint8_t pinClk, uint8_t pinDIO,
unsigned int bitDelay, unsigned int scrollDelay, bool flip)
{
// Pin settings
m_pinClk = pinClk;
m_pinDIO = pinDIO;
// 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
Expand All @@ -180,6 +183,11 @@ TM1637TinyDisplay6::TM1637TinyDisplay6(uint8_t pinClk, uint8_t pinDIO, unsigned
digitalWrite(m_pinDIO, LOW);
}

void TM1637TinyDisplay6::flipDisplay(bool flip)
{
m_flipDisplay = flip;
}

void TM1637TinyDisplay6::setBrightness(uint8_t brightness, bool on)
{
m_brightness = (brightness & 0x07) | (on? 0x08 : 0x00);
Expand All @@ -192,6 +200,10 @@ void TM1637TinyDisplay6::setScrolldelay(unsigned int scrollDelay)

void TM1637TinyDisplay6::setSegments(const uint8_t segments[], uint8_t length, uint8_t pos)
{
uint8_t dot = 0;
// Adjust for flip
if(m_flipDisplay) pos = MAXDIGITS - pos - length;

// Adjust pos for 6 digit display
int index = pos + length - 1;
if (index > MAXDIGITS) index = index - MAXDIGITS;
Expand All @@ -207,9 +219,23 @@ void TM1637TinyDisplay6::setSegments(const uint8_t segments[], uint8_t length, u
writeByte(TM1637_I2C_COMM2 + (pos & 0x07));

// Write the data bytes
for (uint8_t k=0; k < length; k++) {
// 6 digit display - send in reverse order
writeByte(segments[length - 1 - k]);
if(m_flipDisplay) {
for (uint8_t k=0; k < length; k++) {
dot = 0;
if((k - 1) >= 0) {
dot = segments[k - 1] & 0b10000000;
}
uint8_t orig = segments[k];
uint8_t flip = ((orig >> 3) & 0b00000111) +
((orig << 3) & 0b00111000) + (orig & 0b01000000) + dot;
writeByte(flip);
}
}
else {
for (uint8_t k=0; k < length; k++) {
// 6 digit display - send in reverse order
writeByte(segments[length - 1 - k]);
}
}
stop();

Expand Down
16 changes: 14 additions & 2 deletions TM1637TinyDisplay6.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@

#define DEFAULT_BIT_DELAY 100
#define DEFAULT_SCROLL_DELAY 100
#define DEFAULT_FLIP false

#define FRAMES(a) sizeof(a)/6
#define TIME_MS(t) t
Expand All @@ -123,7 +124,17 @@ class TM1637TinyDisplay6 {
//! @param pinDIO - The number of the digital pin connected to the DIO pin of the module
//! @param bitDelay - The delay, in microseconds, between bit transition on the serial
//! bus connected to the display
TM1637TinyDisplay6(uint8_t pinClk, uint8_t pinDIO, unsigned int bitDelay = DEFAULT_BIT_DELAY, unsigned int scrollDelay = DEFAULT_SCROLL_DELAY);
//! @param flip - Flip display orientation (default=false)
TM1637TinyDisplay6(uint8_t pinClk, uint8_t pinDIO, unsigned int bitDelay = DEFAULT_BIT_DELAY,
unsigned int scrollDelay = DEFAULT_SCROLL_DELAY, bool flip=DEFAULT_FLIP);

//! Sets the orientation of the display.
//!
//! Setting this parameter to true will cause the rendering on digits to be displayed
//! upside down.
//!
//! @param flip Flip display upside down true/false
void flipDisplay(bool flip = true);

//! Sets the brightness of the display.
//!
Expand Down Expand Up @@ -389,10 +400,11 @@ class TM1637TinyDisplay6 {
private:
uint8_t m_pinClk;
uint8_t m_pinDIO;
uint8_t m_brightness;
unsigned int m_bitDelay;
unsigned int m_scrollDelay;
bool m_flipDisplay;
uint8_t digits[MAXDIGITS];
uint8_t m_brightness;
};

#endif // __TM1637TINYDISPLAY6__

0 comments on commit bea5916

Please sign in to comment.