mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.3.2 TM1637_RT
This commit is contained in:
parent
be2ee94b61
commit
2c2284c1ca
@ -48,6 +48,19 @@ See routine **ascii_to_7segment()** in the example TM1637_keyscan_cooked.ino. I
|
||||
|
||||
Routine **button_poll()** in the same example shows one way of polling and de-bouncing button presses.
|
||||
|
||||
|
||||
### Display support
|
||||
|
||||
The library is tested with a 6 (=2x3) digit - decimal point - display and a 4 (=1x4) digit - clock - display.
|
||||
At low level these displays differ in the order the digits have to be clocked in.
|
||||
To adjust the order for a not supported display, the following function can be used with care:
|
||||
|
||||
- **void setDigitOrder(uint8_t a, uint8_t b,... uint8_t h)** sets the order in which up to 8 digits should be clocked in.
|
||||
|
||||
If you have a (7 segment) display that is not supported by the library,
|
||||
please open an issue on Github so it can be build in.
|
||||
|
||||
|
||||
### Tuning function
|
||||
|
||||
To tune the timing of writing bytes.
|
||||
@ -141,5 +154,5 @@ See examples
|
||||
- testing
|
||||
- rename **init()** to **begin()** ?
|
||||
- **keyScan()** camelCase ?
|
||||
- separate releaseNotes ?
|
||||
- separate CHANGELOG.md
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
// FILE: TM1637.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// DATE: 2019-10-28
|
||||
// VERSION: 0.3.1
|
||||
// VERSION: 0.3.2
|
||||
// PURPOSE: TM1637 library for Arduino
|
||||
// URL: https://github.com/RobTillaart/TM1637_RT
|
||||
//
|
||||
@ -15,15 +15,17 @@
|
||||
// tested on ESP8266
|
||||
// 0.3.0 2021-10-27 improved keyscan + documentation - kudos to wfdudley
|
||||
// 0.3.1 2021-12-29 update library.json, license, readme, minor edits
|
||||
//
|
||||
// tested on 6 digits display only for now.
|
||||
// tested on 6 digits (decimal point) display
|
||||
// 0.3.2 2022-04-16 fix #15 support for 4 digits.
|
||||
// tested on 4 digit (clock) display.
|
||||
|
||||
|
||||
// NOTE: on the inexpensive TM1637 boards @wfdudley has used, keyscan
|
||||
// works if you add a 1000 ohm pull-up resistor from DIO to 3.3v
|
||||
// This reduces the rise time of the DIO signal when reading the key info.
|
||||
// If one only uses the pull-up inside the microcontroller,
|
||||
// the rise time is too long for the data to be read reliably.
|
||||
// NOTE:
|
||||
// on the inexpensive TM1637 boards @wfdudley has used, keyscan
|
||||
// works if you add a 1000 ohm pull-up resistor from DIO to 3.3v
|
||||
// This reduces the rise time of the DIO signal when reading the key info.
|
||||
// If one only uses the pull-up inside the microcontroller,
|
||||
// the rise time is too long for the data to be read reliably.
|
||||
|
||||
|
||||
#include "TM1637.h"
|
||||
@ -43,14 +45,14 @@
|
||||
| |
|
||||
---
|
||||
| |
|
||||
--- .
|
||||
--- .
|
||||
|
||||
|
||||
-01-
|
||||
20 | | 02
|
||||
-40-
|
||||
10 | | 04
|
||||
-08- .80
|
||||
-08- .80
|
||||
|
||||
*/
|
||||
|
||||
@ -92,6 +94,16 @@ void TM1637::init(uint8_t clockPin, uint8_t dataPin, uint8_t digits)
|
||||
digitalWrite(_clock, HIGH);
|
||||
pinMode(_data, OUTPUT);
|
||||
digitalWrite(_data, HIGH);
|
||||
|
||||
// TODO: replace _digits by a display enumeration?
|
||||
if (_digits == 4 )
|
||||
{
|
||||
setDigitOrder(3,2,1,0);
|
||||
}
|
||||
else // (_digits == 6 ) // default
|
||||
{
|
||||
setDigitOrder(3,4,5,0,1,2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -182,39 +194,49 @@ void TM1637::setBrightness(uint8_t b)
|
||||
}
|
||||
|
||||
|
||||
void TM1637::setDigitOrder(uint8_t a, uint8_t b,
|
||||
uint8_t c, uint8_t d, uint8_t e,
|
||||
uint8_t f, uint8_t g, uint8_t h)
|
||||
{
|
||||
_digitOrder[0] = a;
|
||||
_digitOrder[1] = b;
|
||||
_digitOrder[2] = c;
|
||||
_digitOrder[3] = d;
|
||||
_digitOrder[4] = e;
|
||||
_digitOrder[5] = f;
|
||||
_digitOrder[6] = g;
|
||||
_digitOrder[7] = h;
|
||||
}
|
||||
|
||||
|
||||
void TM1637::displayRaw(uint8_t * data, uint8_t pointPos)
|
||||
{
|
||||
uint8_t b, dp;
|
||||
uint8_t b = 0;
|
||||
|
||||
start();
|
||||
writeByte(TM1637_ADDR_AUTO);
|
||||
stop();
|
||||
|
||||
start();
|
||||
writeByte(TM1637_CMD_SET_ADDR);
|
||||
for (uint8_t i = 3; i < 6 ; i++)
|
||||
|
||||
for (uint8_t d = 0; d < _digits; d++)
|
||||
{
|
||||
dp = data[i] & 0x80;
|
||||
uint8_t i = _digitOrder[d];
|
||||
data[i] &= 0x7f;
|
||||
if(data[i] <= 17) {
|
||||
if (data[i] <= 17) // HEX DIGIT
|
||||
{
|
||||
b = seg[data[i]];
|
||||
}
|
||||
else if(data[i] <= 37) {
|
||||
b = alpha_seg[data[i]-18];
|
||||
else if (data[i] <= 37) // ASCII
|
||||
{
|
||||
b = alpha_seg[data[i] - 18];
|
||||
}
|
||||
if (i == pointPos || dp) b |= 0x80;
|
||||
writeByte(b);
|
||||
}
|
||||
for (uint8_t i = 0; i < 3 ; i++)
|
||||
{
|
||||
dp = data[i] & 0x80;
|
||||
data[i] &= 0x7f;
|
||||
if(data[i] <= 17) {
|
||||
b = seg[data[i]];
|
||||
// do we need a decimal point
|
||||
if ((i == pointPos) || (data[i] & 0x80))
|
||||
{
|
||||
b |= 0x80;
|
||||
}
|
||||
else if(data[i] <= 37) {
|
||||
b = alpha_seg[data[i]-18];
|
||||
}
|
||||
if (i == pointPos || dp) b |= 0x80;
|
||||
writeByte(b);
|
||||
}
|
||||
stop();
|
||||
@ -225,6 +247,93 @@ void TM1637::displayRaw(uint8_t * data, uint8_t pointPos)
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* previous version
|
||||
void TM1637::displayRaw(uint8_t * data, uint8_t pointPos)
|
||||
{
|
||||
uint8_t b = 0, dp = 0;
|
||||
|
||||
start();
|
||||
writeByte(TM1637_ADDR_AUTO);
|
||||
stop();
|
||||
|
||||
// for debugging new displays...
|
||||
// for (int i = 0; i< 6; i++)
|
||||
// {
|
||||
// Serial.print(data[i]);
|
||||
// Serial.print("\t");
|
||||
// }
|
||||
// Serial.println();
|
||||
|
||||
start();
|
||||
writeByte(TM1637_CMD_SET_ADDR);
|
||||
|
||||
// TODO: how to encode display digit order in a generic way?
|
||||
// 2nd array? (packed in nybbles?)
|
||||
// 6 digits == 2x3 display
|
||||
// [3,4,5,0,1,2]
|
||||
if (_digits == 6)
|
||||
{
|
||||
|
||||
for (uint8_t i = 3; i < 6 ; i++)
|
||||
{
|
||||
dp = data[i] & 0x80;
|
||||
data[i] &= 0x7f;
|
||||
if(data[i] <= 17) {
|
||||
b = seg[data[i]];
|
||||
}
|
||||
else if(data[i] <= 37) {
|
||||
b = alpha_seg[data[i]-18];
|
||||
}
|
||||
if (i == pointPos || dp) b |= 0x80;
|
||||
writeByte(b);
|
||||
}
|
||||
for (uint8_t i = 0; i < 3 ; i++)
|
||||
{
|
||||
dp = data[i] & 0x80;
|
||||
data[i] &= 0x7f;
|
||||
if(data[i] <= 17) {
|
||||
b = seg[data[i]];
|
||||
}
|
||||
else if(data[i] <= 37) {
|
||||
b = alpha_seg[data[i]-18];
|
||||
}
|
||||
if (i == pointPos || dp) b |= 0x80;
|
||||
writeByte(b);
|
||||
}
|
||||
}
|
||||
// 4 digit clock version. => : is the point at digit 2
|
||||
// [3,2,1,0]
|
||||
if (_digits == 4)
|
||||
{
|
||||
for (uint8_t j = 0; j < 4 ; j++)
|
||||
{
|
||||
uint8_t i = 3 - j;
|
||||
dp = data[i] & 0x80; // repeating block till writeByte()
|
||||
data[i] &= 0x7f;
|
||||
if(data[i] <= 17) {
|
||||
b = seg[data[i]];
|
||||
}
|
||||
else if(data[i] <= 37) {
|
||||
b = alpha_seg[data[i]-18];
|
||||
}
|
||||
if (i == pointPos || dp) b |= 0x80;
|
||||
writeByte(b);
|
||||
}
|
||||
}
|
||||
stop();
|
||||
|
||||
start();
|
||||
writeByte(TM1637_CMD_DISPLAY | _brightness);
|
||||
stop();
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////
|
||||
//
|
||||
// PRIVATE
|
||||
//
|
||||
uint8_t TM1637::writeByte(uint8_t data)
|
||||
{
|
||||
// shift out data 8 bits LSB first
|
||||
|
@ -3,7 +3,7 @@
|
||||
// FILE: TM1637.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// DATE: 2019-10-28
|
||||
// VERSION: 0.3.1
|
||||
// VERSION: 0.3.2
|
||||
// PUPROSE: TM1637 library for Arduino
|
||||
// URL: https://github.com/RobTillaart/TM1637_RT
|
||||
|
||||
@ -13,7 +13,7 @@
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
#define TM1637_LIB_VERSION (F("0.3.1"))
|
||||
#define TM1637_LIB_VERSION (F("0.3.2"))
|
||||
|
||||
|
||||
class TM1637
|
||||
@ -37,6 +37,11 @@ class TM1637
|
||||
uint8_t getBitDelay() { return _bitDelay; };
|
||||
uint8_t keyscan(void);
|
||||
|
||||
// the order the individual digits must be sent to the display.
|
||||
void setDigitOrder(uint8_t a = 0, uint8_t b = 1,
|
||||
uint8_t c = 2, uint8_t d = 3,
|
||||
uint8_t e = 4, uint8_t f = 5,
|
||||
uint8_t g = 6, uint8_t h = 7);
|
||||
|
||||
private:
|
||||
uint8_t _clock = -1;
|
||||
@ -45,6 +50,8 @@ class TM1637
|
||||
uint8_t _brightness = 3;
|
||||
uint8_t _bitDelay = 10;
|
||||
|
||||
uint8_t _digitOrder[8];
|
||||
|
||||
uint8_t writeByte(uint8_t data);
|
||||
void start();
|
||||
void stop();
|
||||
|
@ -5,6 +5,7 @@
|
||||
// DATE: 2021-02-15
|
||||
// URL: https://github.com/RobTillaart/TM1637
|
||||
|
||||
// test with 6 digits (decimal) display
|
||||
|
||||
#include "TM1637.h"
|
||||
|
||||
@ -22,6 +23,8 @@ void setup()
|
||||
|
||||
TM.init(2, 3, 6);
|
||||
TM.displayClear();
|
||||
delay(2000);
|
||||
|
||||
TM.displayHex(0xFEDCBA);
|
||||
delay(2000);
|
||||
}
|
||||
|
@ -0,0 +1,69 @@
|
||||
//
|
||||
// FILE: TM1637_clock_4digits.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// PURPOSE: demo TM1637 library
|
||||
// URL: https://github.com/RobTillaart/TM1637
|
||||
|
||||
|
||||
#include "TM1637.h"
|
||||
|
||||
TM1637 TM;
|
||||
|
||||
|
||||
uint32_t start, stop;
|
||||
volatile uint32_t val = 0;
|
||||
|
||||
uint8_t bright = 0;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(__FILE__);
|
||||
|
||||
TM.init(7, 6, 4);
|
||||
TM.displayClear();
|
||||
delay(2000);
|
||||
|
||||
TM.setBrightness(1);
|
||||
TM.displayHex(0xDCBA);
|
||||
delay(2000);
|
||||
|
||||
TM.setBrightness(3);
|
||||
TM.displayHex(0x4321);
|
||||
delay(2000);
|
||||
|
||||
TM.setBrightness(7);
|
||||
TM.displayInt(567);
|
||||
delay(2000);
|
||||
|
||||
TM.setBrightness(5);
|
||||
TM.displayFloat(12.341); // shows : in middle of clock
|
||||
delay(2000);
|
||||
}
|
||||
|
||||
// mimick clock, not ok under 10 seconds
|
||||
// left as exercise for the programmer ;)
|
||||
void loop()
|
||||
{
|
||||
uint32_t now = millis() % 100000; // 0 - 99999
|
||||
float value = now * 0.001; // 0 - 99.999
|
||||
if (value - int(value) < 0.5) value *= 100; // for blink :
|
||||
TM.displayFloat(value);
|
||||
}
|
||||
|
||||
|
||||
// 0-10 second, milliseconds timer?
|
||||
void loop2()
|
||||
{
|
||||
uint32_t now = millis() % 10000;
|
||||
float value = now * 0.001;
|
||||
TM.displayFloat(value);
|
||||
}
|
||||
|
||||
|
||||
// todo: make a HH:MM clock
|
||||
// with the : flashing every second.
|
||||
|
||||
|
||||
|
||||
// -- END OF FILE --
|
@ -22,6 +22,8 @@ getBitDelay KEYWORD2
|
||||
|
||||
keyscan KEYWORD2
|
||||
|
||||
setDigitOrder KEYWORD2
|
||||
|
||||
|
||||
# Constants (LITERAL1)
|
||||
TM1637_LIB_VERSION LITERAL1
|
||||
|
@ -15,7 +15,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/TM1637_RT"
|
||||
},
|
||||
"version": "0.3.1",
|
||||
"version": "0.3.2",
|
||||
"license": "MIT",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*",
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=TM1637_RT
|
||||
version=0.3.1
|
||||
version=0.3.2
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=TM1637 Library for Arduino.
|
||||
|
Loading…
x
Reference in New Issue
Block a user