Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
36 changes: 29 additions & 7 deletions TM1637Display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();

Expand Down Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down
16 changes: 15 additions & 1 deletion TM1637Display.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);


Expand All @@ -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__
31 changes: 31 additions & 0 deletions examples/TM1637Colon/TM1637Colon.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <Arduino.h>
#include <TM1637Display.h>

// 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();
}
}
16 changes: 9 additions & 7 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down