0.3.7 HT16K33

This commit is contained in:
rob tillaart 2022-11-20 10:12:01 +01:00
parent 7fdc79cf82
commit 7b9b3d225c
9 changed files with 290 additions and 62 deletions

View File

@ -6,6 +6,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.3.7] - 2022-11-19
- add displayUnit(float, char);
- add top c symbol
- add degree symbol
- moved code to .cpp file (prep 0.4.0)
## [0.3.6] - 2022-11-09
- add changelog.md
- add rp2040 to build-CI

View File

@ -1,7 +1,7 @@
//
// FILE: HT16K33.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.3.6
// VERSION: 0.3.7
// DATE: 2019-02-07
// PURPOSE: Arduino Library for HT16K33 4x7segment display
// URL: https://github.com/RobTillaart/HT16K33
@ -12,7 +12,7 @@
#include "HT16K33.h"
// Commands
// Commands
#define HT16K33_ON 0x21 // 0=off 1=on
#define HT16K33_STANDBY 0x20 // bit xxxxxxx0
@ -42,32 +42,34 @@
// 10 04
// 08
//
static const uint8_t charmap[] = { // TODO PROGMEM ?
static const uint8_t charmap[] = { // TODO PROGMEM ?
0x3F, // 0
0x06, // 1
0x5B, // 2
0x4F, // 3
0x66, // 4
0x6D, // 5
0x7D, // 6
0x07, // 7
0x7F, // 8
0x6F, // 9
0x77, // A
0x7C, // B
0x39, // C
0x5E, // D
0x79, // E
0x71, // F
0x00, // space
0x40, // minus
0x3F, // 0
0x06, // 1
0x5B, // 2
0x4F, // 3
0x66, // 4
0x6D, // 5
0x7D, // 6
0x07, // 7
0x7F, // 8
0x6F, // 9
0x77, // A
0x7C, // B
0x39, // C
0x5E, // D
0x79, // E
0x71, // F
0x00, // space
0x40, // minus
0x61, // TOP_C
0x63, // degree °
};
////////////////////////////////////////////////////
//
// CONSTRUCTOR
// CONSTRUCTOR
//
HT16K33::HT16K33(const uint8_t address, TwoWire *wire)
{
@ -118,6 +120,10 @@ void HT16K33::reset()
}
////////////////////////////////////////////////////
//
// CACHE
//
void HT16K33::clearCache()
{
for (uint8_t i = 0; i < 5; i++)
@ -127,6 +133,29 @@ void HT16K33::clearCache()
}
void HT16K33::cacheOn()
{
_cache = true;
}
void HT16K33::cacheOff()
{
_cache = false;
}
void HT16K33::refresh()
{
_refresh();
}
////////////////////////////////////////////////////
//
// DISPLAY
//
void HT16K33::displayOn()
{
writeCmd(HT16K33_ON);
@ -142,12 +171,6 @@ void HT16K33::displayOff()
}
void HT16K33::refresh()
{
_refresh();
}
void HT16K33::blink(uint8_t value)
{
if (value > 0x03) value = 0x00;
@ -178,7 +201,7 @@ void HT16K33::suppressLeadingZeroPlaces(uint8_t value)
//////////////////////////////////////////
//
// display functions
// display functions
//
void HT16K33::displayClear()
{
@ -188,7 +211,7 @@ void HT16K33::displayClear()
}
// DIV10 & DIV100 optimize?
// DIV10 & DIV100 optimize?
bool HT16K33::displayInt(int n)
{
bool inRange = ((-1000 < n) && (n < 10000));
@ -224,7 +247,7 @@ bool HT16K33::displayInt(int n)
}
// 0000..FFFF
// 0000..FFFF
bool HT16K33::displayHex(uint16_t n)
{
uint8_t x[4], h, l;
@ -239,7 +262,7 @@ bool HT16K33::displayHex(uint16_t n)
}
// 00.00 .. 99.99
// 00.00 .. 99.99
bool HT16K33::displayDate(uint8_t left, uint8_t right, bool lz)
{
bool inRange = ((left < 100) && (right < 100));
@ -256,7 +279,7 @@ bool HT16K33::displayDate(uint8_t left, uint8_t right, bool lz)
}
// 00:00 .. 99:99
// 00:00 .. 99:99
bool HT16K33::displayTime(uint8_t left, uint8_t right, bool colon, bool lz)
{
bool inRange = ((left < 100) && (right < 100));
@ -273,7 +296,7 @@ bool HT16K33::displayTime(uint8_t left, uint8_t right, bool colon, bool lz)
}
// seconds / minutes max 6039 == 99:99
// seconds / minutes max 6039 == 99:99
bool HT16K33::displaySeconds(uint16_t seconds, bool colon, bool lz)
{
uint8_t left = seconds / 60;
@ -316,7 +339,7 @@ bool HT16K33::displayFloat(float f, uint8_t decimals)
x[1] = h - x[0] * 10;
x[2] = l / 10;
x[3] = l - x[2] * 10;
if (neg) // corrections for neg => all shift one position
if (neg) // corrections for neg => all shift one position
{
x[3] = x[2];
x[2] = x[1];
@ -324,7 +347,7 @@ bool HT16K33::displayFloat(float f, uint8_t decimals)
x[0] = HT16K33_MINUS;
point++;
}
// add leading spaces
// add leading spaces
while (point + decimals < 3)
{
x[3] = x[2];
@ -340,9 +363,65 @@ bool HT16K33::displayFloat(float f, uint8_t decimals)
}
bool HT16K33::displayUnit(float f, uint8_t decimals, uint8_t unitChar)
{
bool inRange = ((-99.5 < f) && (f < 999.5));
bool neg = (f < 0);
if (neg) f = -f;
if (decimals == 2) f = round(f * 100) * 0.01;
if (decimals == 1) f = round(f * 10) * 0.1;
if (decimals == 0) f = round(f);
int whole = f;
int point = 2;
if (whole < 100) point = 1;
if (whole < 10) point = 0;
if (f >= 1)
{
while (f < 100) f *= 10;
whole = round(f);
}
else
{
whole = round(f * 100);
}
uint8_t x[4];
x[0] = whole / 100;
whole = whole - x[0] * 100;
x[1] = whole / 10;
x[2] = whole % 10;
x[3] = unitChar;
if (neg) // corrections for neg => all shift one position
{
x[3] = unitChar;
x[2] = x[1];
x[1] = x[0];
x[0] = HT16K33_MINUS;
point++;
}
// add leading spaces
while (point + decimals < 2)
{
x[3] = unitChar;
x[2] = x[1];
x[1] = x[0];
x[0] = HT16K33_SPACE;
point++;
}
display(x, point);
return inRange;
}
/////////////////////////////////////////////////////////////////////
//
// EXPERIMENTAL
// EXPERIMENTAL
//
bool HT16K33::displayFixedPoint0(float f)
{
@ -400,13 +479,13 @@ void HT16K33::displayRaw(uint8_t *array, bool colon)
bool HT16K33::displayVULeft(uint8_t value)
{
bool inRange = (value < 9); // can display 0..8 bars
bool inRange = (value < 9); // can display 0..8 bars
uint8_t ar[4];
for (int idx = 3; idx >=0; idx--)
{
if (value >= 2)
{
ar[idx] = 0x36; // ||
ar[idx] = 0x36; // ||
value -= 2;
}
else if (value == 1)
@ -429,7 +508,7 @@ bool HT16K33::displayVURight(uint8_t value)
{
if (value >= 2)
{
ar[idx] = 0x36; // ||
ar[idx] = 0x36; // ||
value -= 2;
}
else if (value == 1)
@ -456,15 +535,16 @@ void HT16K33::display(uint8_t *array)
writePos(3, charmap[array[2]]);
writePos(4, charmap[array[3]]);
// debug to Serial
// dumpSerial(array, 0);
// debug to Serial
// dumpSerial(array, 0);
}
void HT16K33::display(uint8_t *array, uint8_t point)
{
// debug to Serial
// dumpSerial(array, point);
// debug to Serial
// dumpSerial(array, point);
// dumpSerial();
writePos(0, charmap[array[0]], point == 0);
writePos(1, charmap[array[1]], point == 1);
@ -491,22 +571,23 @@ void HT16K33::displayExtraLeds(uint8_t value)
void HT16K33::dumpSerial(uint8_t *array, uint8_t point)
{
// to debug without display
// to debug without display
for (int i = 0; i < 4; i++)
{
if (array[i] == HT16K33_SPACE) Serial.print(" ");
if (array[i] == HT16K33_SPACE) Serial.print("_");
else if (array[i] == HT16K33_MINUS) Serial.print("-");
else Serial.print(array[i]);
if (i == point) Serial.print(".");
}
Serial.print(" ");
Serial.println(point);
Serial.print(" (");
Serial.print(point);
Serial.println(")");
}
void HT16K33::dumpSerial()
{
// to debug without display
// to debug without display
for (int i = 0; i < 4; i++)
{
if (_displayCache[i] < 0x10) Serial.print("0");
@ -554,7 +635,7 @@ void HT16K33::writePos(uint8_t pos, uint8_t mask)
void HT16K33::writePos(uint8_t pos, uint8_t mask, bool point)
{
if (point) mask |= 0x80;
// if (_overflow) mask |= 0x80;
// if (_overflow) mask |= 0x80;
else mask &= 0x7F;
writePos(pos, mask);
}

View File

@ -2,7 +2,7 @@
//
// FILE: HT16K33.h
// AUTHOR: Rob Tillaart
// VERSION: 0.3.6
// VERSION: 0.3.7
// DATE: 2019-02-07
// PURPOSE: Arduino Library for HT16K33 4x7segment display
// http://www.adafruit.com/products/1002
@ -13,7 +13,7 @@
#include "Wire.h"
#define HT16K33_LIB_VERSION (F("0.3.6"))
#define HT16K33_LIB_VERSION (F("0.3.7"))
// Characters
@ -35,8 +35,12 @@
#define HT16K33_F 15
#define HT16K33_SPACE 16
#define HT16K33_MINUS 17
#define HT16K33_TOP_C 18 // c
#define HT16K33_DEGREE 19 // °
#define HT16K33_NONE 99
// P for Pascal / Pressure ?
// J for joule?
class HT16K33
{
@ -55,9 +59,9 @@ public:
// on the I2C and wants to force refresh one can disable caching
// for one or more calls.
void clearCache();
void cacheOn() { _cache = true; };
void cacheOff() { _cache = false; };
void refresh(); // force writing of cache to display
void cacheOn();
void cacheOff();
void refresh(); // force writing of cache to display
void displayOn();
void displayOff();
@ -82,7 +86,12 @@ public:
bool displayTime(uint8_t left, uint8_t right, bool colon = true, bool lz = true); // 00:00 .. 99:99
bool displaySeconds(uint16_t seconds, bool colon = true, bool lz = true); // 00:00 .. 99:99
bool displayFloat(float f, uint8_t decimals = 3); // -999 .. 0.000 .. 9999
// -999 .. 0.000 .. 9999
bool displayFloat(float f, uint8_t decimals = 3);
// -99 .. 0.00 .. 999
bool displayUnit(float f, uint8_t decimals = 2, uint8_t unitChar = HT16K33_SPACE);
void display(uint8_t *array); // array with 4 elements
void display(uint8_t *array, uint8_t point); // point = digit with . (0..3)

View File

@ -95,9 +95,14 @@ Optional the colon is set to false (to simulate blink).
Optional the leading zero (lz) can be replaced by a space to look more natural e.g 1:54 instead of 01:54
- **bool displayFloat(float f, uint8_t decimals = 3)** values -999..0.000..9999
The number of decimals = 0,1,2,3 = default. When less decimals are displayed, the number will be right aligned.
- **bool displayUnit(float f, uint8_t decimals = 2, uint8_t unitChar = HT16K33_SPACE)** values -99..0.000..999
The number of decimals = 0,1,2 = default. When less decimals are displayed, the number will be right aligned.
The unitChar is a postFix character like C or F for temperature H for humidity.
The unitChar must be one of the chars supported like HT16K33_C, HT16K33_TOP_C or HT16K33_DEGREE (see below).
So **displayUnit(25.6, 1, HT16K33_DEGREE)** will display **23.5°**.
### Experimental
### Experimental fixed point
These functions are new and still under investigation.
@ -107,7 +112,7 @@ These functions are new and still under investigation.
- **bool displayFixedPoint3(float f)** displays values 0.000 .. 9.999 with 3 decimals.
### Special
### Special VU meters
- **bool displayVULeft(uint8_t value)** display used as sort VU meter, values 0..8 Vales > 8 are treated as 8 (but return false).
- **bool displayVURight(uint8_t value)** display used as sort VU meter, values 0..8 Vales > 8 are treated as 8 (but return false).
@ -122,7 +127,7 @@ These functions are new and still under investigation.
- **void displayExtraLeds(uint8_t value)** switch on extra leds.
value is in fact a bit mask see table below. 0 = all off.
#### Extra Leds table
#### Extra LEDs table
| mask | description |
|:------:|:--------------|
@ -138,13 +143,45 @@ value is in fact a bit mask see table below. 0 = all off.
### Debugging
- **void displayTest(uint8_t del)** debugging / test function.
- **void dumpSerial(uint8_t \* array, uint8_t point)** debugging equivalent of display.
- **void dumpSerial(uint8_t \* array, uint8_t point)** debugging equivalent of the display.
Prints to Serial.
- **void dumpSerial()** print HEX codes equivalent of the display to Serial.
### Obsolete
- **void suppressLeadingZeroPlaces(uint8_t value)** obsolete, replaced by setDigits
## Characters supported
from .h file, elaborate
```cpp
#define HT16K33_0 0
#define HT16K33_1 1
#define HT16K33_2 2
#define HT16K33_3 3
#define HT16K33_4 4
#define HT16K33_5 5
#define HT16K33_6 6
#define HT16K33_7 7
#define HT16K33_8 8
#define HT16K33_9 9
#define HT16K33_A 10
#define HT16K33_B 11
#define HT16K33_C 12
#define HT16K33_D 13
#define HT16K33_E 14
#define HT16K33_F 15
#define HT16K33_SPACE 16
#define HT16K33_MINUS 17
#define HT16K33_TOP_C 18 // c
#define HT16K33_DEGREE 19 // °
#define HT16K33_NONE 99
```
If other chars are needed please file an issue.
## Operation

View File

@ -0,0 +1,66 @@
//
// FILE: demo_displayUnit.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo
// URL: http://www.adafruit.com/products/1002
// URL: https://github.com/RobTillaart/HT16K33
#include "HT16K33.h"
HT16K33 seg(0x70);
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
seg.begin();
Wire.setClock(100000);
seg.displayOn();
seg.setDigits(4);
// default followed by HT16K33_SPACE
seg.displayUnit(1.23, 0); // no unit
seg.displayUnit(1.23, 1);
seg.displayUnit(1.23, 2);
Serial.println();
// now followed by HT16K33_A
seg.displayUnit(1.23, 0, HT16K33_A); // Ampere
seg.displayUnit(1.23, 1, HT16K33_A);
seg.displayUnit(1.23, 2, HT16K33_A);
Serial.println();
seg.displayUnit(-1.23, 0, HT16K33_C); // Celsius
seg.displayUnit(-1.23, 1, HT16K33_C);
seg.displayUnit(-1.23, 2, HT16K33_C);
Serial.println();
seg.displayUnit(12.3, 0, HT16K33_F); // Fahrenheit
seg.displayUnit(12.3, 1, HT16K33_F);
seg.displayUnit(12.3, 2, HT16K33_F);
Serial.println();
seg.displayUnit(12.3, 0, HT16K33_TOP_C); // c
seg.displayUnit(12.3, 1, HT16K33_TOP_C);
seg.displayUnit(12.3, 2, HT16K33_TOP_C);
Serial.println();
seg.displayUnit(12.3, 0, HT16K33_DEGREE); // °
seg.displayUnit(12.3, 1, HT16K33_DEGREE);
seg.displayUnit(12.3, 2, HT16K33_DEGREE);
Serial.println();
Serial.println("done");
}
void loop()
{
}
// -- END OF FILE --

View File

@ -32,6 +32,7 @@ displayTime KEYWORD2
displaySeconds KEYWORD2
displayFloat KEYWORD2
displayUnit KEYWORD2
display KEYWORD2
displayColon KEYWORD2

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/HT16K33.git"
},
"version": "0.3.6",
"version": "0.3.7",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",

View File

@ -1,5 +1,5 @@
name=HT16K33
version=0.3.6
version=0.3.7
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino Library for HT16K33 I2C 4x7segment display

View File

@ -59,6 +59,33 @@ unittest(test_constructor)
assertEqual(1, 1);
}
unittest(test_constants)
{
assertEqual(HT16K33_0 , 0);
assertEqual(HT16K33_1 , 1);
assertEqual(HT16K33_2 , 2);
assertEqual(HT16K33_3 , 3);
assertEqual(HT16K33_4 , 4);
assertEqual(HT16K33_5 , 5);
assertEqual(HT16K33_6 , 6);
assertEqual(HT16K33_7 , 7);
assertEqual(HT16K33_8 , 8);
assertEqual(HT16K33_9 , 9);
assertEqual(HT16K33_A , 10);
assertEqual(HT16K33_B , 11);
assertEqual(HT16K33_C , 12);
assertEqual(HT16K33_D , 13);
assertEqual(HT16K33_E , 14);
assertEqual(HT16K33_F , 15);
assertEqual(HT16K33_SPACE , 16);
assertEqual(HT16K33_MINUS , 17);
assertEqual(HT16K33_TOP_C , 18);
assertEqual(HT16K33_DEGREE, 19);
assertEqual(HT16K33_NONE , 99);
}
unittest_main()
// --------