Hi, I faced a problem with 2 types of display board with 2 library. At beginning, I used "CATALEX" label display board with "TM1637Display.h" on Wemos D1 mini. It worked OK. After that, I bought another batch of 4 digit display, but there is no "CATALEX" label . The board does not work with "TM1637Display.h" . I found "TM1637.h" then tested on Arduino UNO, it worked but does not worked on Wemos D1 mini .
After I searched a library worked on no "CATALEX" label on Wemos D1 mini, I found "SevenSegmentTM1637.h" . It works on "CATALEX" labeled board, too.
I made override class library for me to make compatible with "TM1637Display.h", which I used to use. I want to share the code with TM1637Display sample called "TM1637TEST". Maybe my coding is not good. You can modify by yourself if needed.
#include "SevenSegmentTM1637.h"
const uint8_t digitToSegment[] = {
// XGFEDCBA
0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111101, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111, // 9
0b01110111, // A
0b01111100, // b
0b00111001, // C
0b01011110, // d
0b01111001, // E
0b01110001 // F
};
class TM1637Display : public SevenSegmentTM1637
{
public :
TM1637Display(uint8_t pinClk, uint8_t pinDIO);
void setBrightness(uint8_t brightness, bool on = true);
void showNumberDec(int num, bool leading_zero = false, uint8_t length = 4, uint8_t pos = 0);
void setSegments(const uint8_t segments[], uint8_t length = 4, uint8_t pos = 0);
uint8_t encodeDigit(uint8_t digit);
void showNumberDecEx(int num, uint8_t dots = 0, bool leading_zero = false, uint8_t length = 4, uint8_t pos = 0);
};
TM1637Display::TM1637Display(uint8_t pinClk, uint8_t pinDIO) : SevenSegmentTM1637(pinClk, pinDIO)
{
SevenSegmentTM1637(pinClk, pinDIO);
}
void TM1637Display::setBrightness(uint8_t brightness, bool on)
{
uint8_t setDisplayCmd = B10000000; // bit 6,7
uint8_t brightnessBits = B111; // bit 0,1,2 (7 = max)
uint8_t displayOnBit = B1000; // bit 3
// construct complete command
uint8_t command = setDisplayCmd | brightnessBits | displayOnBit;
// turn display on and set brightness to max (7)
bool ack = SevenSegmentTM1637::command(command);
// write init to display using automatic address
uint8_t setDataCmd = B01000000; // bit 6,7
ack = SevenSegmentTM1637::command(setDataCmd);
// setBacklight(brightness);
}
void TM1637Display::showNumberDec(int num, bool leading_zero, uint8_t length, uint8_t pos)
{
showNumberDecEx(num, 0, leading_zero, length, pos);
}
void TM1637Display::showNumberDecEx(int num, uint8_t dots, bool leading_zero, uint8_t length, uint8_t)
{
bool ack;
uint8_t digits[4];
const static int divisors[] = { 1, 10, 100, 1000 };
bool leading = true;
for(int8_t k = 0; k < 4; k++) {
int divisor = divisors[4 - 1 - k];
int d = num / divisor;
uint8_t digit = 0;
if (d == 0) {
if (leading_zero || !leading || (k == 3))
digit = encodeDigit(d);
else
digit = 0;
}
else {
digit = encodeDigit(d);
num -= d * divisor;
leading = false;
}
// Add the decimal point/colon to the digit
digit |= (dots & 0x80);
dots <<= 1;
digits[k] = digit;
}
uint8_t commands[5];
commands[0] = TM1637_COM_SET_ADR|B0000000;
commands[1] = digits[0];
if(dots!=0) {
commands[2] = digits[1]|B10000000;
} else {
commands[2] = digits[1];
}
commands[3] = digits[2];
commands[4] = digits[3];
ack = SevenSegmentTM1637::command(commands, 5);
}
void TM1637Display::setSegments(const uint8_t segments[], uint8_t length, uint8_t pos)
{
uint8_t commands[5];
commands[0] = B11000000+pos;
commands[1] = segments[0];
commands[2] = segments[1];
commands[3] = segments[2];
commands[4] = segments[3];
TM1637Display::command(commands,length+1);
}
uint8_t TM1637Display::encodeDigit(uint8_t digit)
{
//return SevenSegmentTM1637::encode((int16_t) digit)
return digitToSegment[digit & 0x0f];
}
#define SEG_A 0b00000001
#define SEG_B 0b00000010
#define SEG_C 0b00000100
#define SEG_D 0b00001000
#define SEG_E 0b00010000
#define SEG_F 0b00100000
#define SEG_G 0b01000000
#define TEST_DELAY 2000
const uint8_t SEG_DONE[] = {
SEG_B | SEG_C | SEG_D | SEG_E | SEG_G, // d
SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F, // O
SEG_C | SEG_E | SEG_G, // n
SEG_A | SEG_D | SEG_E | SEG_F | SEG_G // E
};
/* Above is class definition to be replaced with #include "TM1637Display.h" */
const int CLK = D2; //Set the CLK pin connection to the display
const int DIO = D3; //Set the DIO pin connection to the display
TM1637Display display(CLK, DIO); //set up the 4-Digit Display.
void setup()
{
}
void loop()
{
int k;
uint8_t data[] = { 0xff, 0xff, 0xff, 0xff };
display.setBrightness(0x07);
// All segments on
display.setSegments(data);
delay(TEST_DELAY);
// Selectively set different digits
data[0] = 0b01001001;
data[1] = display.encodeDigit(1);
data[2] = display.encodeDigit(2);
data[3] = display.encodeDigit(3);
for(k = 3; k >= 0; k--) {
display.setSegments(data, 1, k);
delay(TEST_DELAY);
}
display.setSegments(data+2, 2, 2);
delay(TEST_DELAY);
display.setSegments(data+2, 2, 1);
delay(TEST_DELAY);
display.setSegments(data+1, 3, 1);
delay(TEST_DELAY);
// Show decimal numbers with/without leading zeros
bool lz = false;
for (uint8_t z = 0; z < 2; z++) {
for(k = 0; k < 10000; k += k*4 + 7) {
display.showNumberDec(k, lz);
delay(TEST_DELAY);
}
lz = true;
}
// Show decimal number whose length is smaller than 4
for(k = 0; k < 4; k++)
data[k] = 0;
display.setSegments(data);
// Run through all the dots
for(k=0; k <= 4; k++) {
display.showNumberDecEx(0, (0x80 >> k), true);
delay(TEST_DELAY);
}
display.showNumberDec(153, false, 3, 1);
delay(TEST_DELAY);
display.showNumberDec(22, false, 2, 2);
delay(TEST_DELAY);
display.showNumberDec(0, true, 1, 3);
delay(TEST_DELAY);
display.showNumberDec(0, true, 1, 2);
delay(TEST_DELAY);
display.showNumberDec(0, true, 1, 1);
delay(TEST_DELAY);
display.showNumberDec(0, true, 1, 0);
delay(TEST_DELAY);
// Brightness Test
for(k = 0; k < 4; k++)
data[k] = 0xff;
for(k = 0; k < 7; k++) {
display.setBrightness(k);
display.setSegments(data);
delay(TEST_DELAY);
}
// On/Off test
for(k = 0; k < 4; k++) {
display.setBrightness(7, false); // Turn off
display.setSegments(data);
delay(TEST_DELAY);
display.setBrightness(7, true); // Turn on
display.setSegments(data);
delay(TEST_DELAY);
}
// Done!
display.setSegments(SEG_DONE);
while(1);
}