Skip to content

Commit 240158b

Browse files
authored
add displayUnit() (#24)
* add displayUnit * update readme.md
1 parent 2a496cb commit 240158b

File tree

9 files changed

+291
-63
lines changed

9 files changed

+291
-63
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.3.7] - 2022-11-19
10+
- add displayUnit(float, char);
11+
- add top c symbol
12+
- add degree symbol
13+
- moved code to .cpp file (prep 0.4.0)
14+
15+
916
## [0.3.6] - 2022-11-09
1017
- add changelog.md
1118
- add rp2040 to build-CI

HT16K33.cpp

Lines changed: 132 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: HT16K33.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.3.6
4+
// VERSION: 0.3.7
55
// DATE: 2019-02-07
66
// PURPOSE: Arduino Library for HT16K33 4x7segment display
77
// URL: https://github.com/RobTillaart/HT16K33
@@ -12,7 +12,7 @@
1212
#include "HT16K33.h"
1313

1414

15-
// Commands
15+
// Commands
1616
#define HT16K33_ON 0x21 // 0=off 1=on
1717
#define HT16K33_STANDBY 0x20 // bit xxxxxxx0
1818

@@ -42,32 +42,34 @@
4242
// 10 04
4343
// 08
4444
//
45-
static const uint8_t charmap[] = { // TODO PROGMEM ?
46-
47-
0x3F, // 0
48-
0x06, // 1
49-
0x5B, // 2
50-
0x4F, // 3
51-
0x66, // 4
52-
0x6D, // 5
53-
0x7D, // 6
54-
0x07, // 7
55-
0x7F, // 8
56-
0x6F, // 9
57-
0x77, // A
58-
0x7C, // B
59-
0x39, // C
60-
0x5E, // D
61-
0x79, // E
62-
0x71, // F
63-
0x00, // space
64-
0x40, // minus
45+
static const uint8_t charmap[] = { // TODO PROGMEM ?
46+
47+
0x3F, // 0
48+
0x06, // 1
49+
0x5B, // 2
50+
0x4F, // 3
51+
0x66, // 4
52+
0x6D, // 5
53+
0x7D, // 6
54+
0x07, // 7
55+
0x7F, // 8
56+
0x6F, // 9
57+
0x77, // A
58+
0x7C, // B
59+
0x39, // C
60+
0x5E, // D
61+
0x79, // E
62+
0x71, // F
63+
0x00, // space
64+
0x40, // minus
65+
0x61, // TOP_C
66+
0x63, // degree °
6567
};
6668

6769

6870
////////////////////////////////////////////////////
6971
//
70-
// CONSTRUCTOR
72+
// CONSTRUCTOR
7173
//
7274
HT16K33::HT16K33(const uint8_t address, TwoWire *wire)
7375
{
@@ -118,6 +120,10 @@ void HT16K33::reset()
118120
}
119121

120122

123+
////////////////////////////////////////////////////
124+
//
125+
// CACHE
126+
//
121127
void HT16K33::clearCache()
122128
{
123129
for (uint8_t i = 0; i < 5; i++)
@@ -127,6 +133,29 @@ void HT16K33::clearCache()
127133
}
128134

129135

136+
void HT16K33::cacheOn()
137+
{
138+
_cache = true;
139+
}
140+
141+
142+
143+
void HT16K33::cacheOff()
144+
{
145+
_cache = false;
146+
}
147+
148+
149+
void HT16K33::refresh()
150+
{
151+
_refresh();
152+
}
153+
154+
155+
////////////////////////////////////////////////////
156+
//
157+
// DISPLAY
158+
//
130159
void HT16K33::displayOn()
131160
{
132161
writeCmd(HT16K33_ON);
@@ -142,12 +171,6 @@ void HT16K33::displayOff()
142171
}
143172

144173

145-
void HT16K33::refresh()
146-
{
147-
_refresh();
148-
}
149-
150-
151174
void HT16K33::blink(uint8_t value)
152175
{
153176
if (value > 0x03) value = 0x00;
@@ -178,7 +201,7 @@ void HT16K33::suppressLeadingZeroPlaces(uint8_t value)
178201

179202
//////////////////////////////////////////
180203
//
181-
// display functions
204+
// display functions
182205
//
183206
void HT16K33::displayClear()
184207
{
@@ -188,7 +211,7 @@ void HT16K33::displayClear()
188211
}
189212

190213

191-
// DIV10 & DIV100 optimize?
214+
// DIV10 & DIV100 optimize?
192215
bool HT16K33::displayInt(int n)
193216
{
194217
bool inRange = ((-1000 < n) && (n < 10000));
@@ -224,7 +247,7 @@ bool HT16K33::displayInt(int n)
224247
}
225248

226249

227-
// 0000..FFFF
250+
// 0000..FFFF
228251
bool HT16K33::displayHex(uint16_t n)
229252
{
230253
uint8_t x[4], h, l;
@@ -239,7 +262,7 @@ bool HT16K33::displayHex(uint16_t n)
239262
}
240263

241264

242-
// 00.00 .. 99.99
265+
// 00.00 .. 99.99
243266
bool HT16K33::displayDate(uint8_t left, uint8_t right, bool lz)
244267
{
245268
bool inRange = ((left < 100) && (right < 100));
@@ -256,7 +279,7 @@ bool HT16K33::displayDate(uint8_t left, uint8_t right, bool lz)
256279
}
257280

258281

259-
// 00:00 .. 99:99
282+
// 00:00 .. 99:99
260283
bool HT16K33::displayTime(uint8_t left, uint8_t right, bool colon, bool lz)
261284
{
262285
bool inRange = ((left < 100) && (right < 100));
@@ -273,7 +296,7 @@ bool HT16K33::displayTime(uint8_t left, uint8_t right, bool colon, bool lz)
273296
}
274297

275298

276-
// seconds / minutes max 6039 == 99:99
299+
// seconds / minutes max 6039 == 99:99
277300
bool HT16K33::displaySeconds(uint16_t seconds, bool colon, bool lz)
278301
{
279302
uint8_t left = seconds / 60;
@@ -316,15 +339,15 @@ bool HT16K33::displayFloat(float f, uint8_t decimals)
316339
x[1] = h - x[0] * 10;
317340
x[2] = l / 10;
318341
x[3] = l - x[2] * 10;
319-
if (neg) // corrections for neg => all shift one position
342+
if (neg) // corrections for neg => all shift one position
320343
{
321344
x[3] = x[2];
322345
x[2] = x[1];
323346
x[1] = x[0];
324347
x[0] = HT16K33_MINUS;
325348
point++;
326349
}
327-
// add leading spaces
350+
// add leading spaces
328351
while (point + decimals < 3)
329352
{
330353
x[3] = x[2];
@@ -340,9 +363,65 @@ bool HT16K33::displayFloat(float f, uint8_t decimals)
340363
}
341364

342365

366+
bool HT16K33::displayUnit(float f, uint8_t decimals, uint8_t unitChar)
367+
{
368+
bool inRange = ((-99.5 < f) && (f < 999.5));
369+
370+
bool neg = (f < 0);
371+
if (neg) f = -f;
372+
373+
if (decimals == 2) f = round(f * 100) * 0.01;
374+
if (decimals == 1) f = round(f * 10) * 0.1;
375+
if (decimals == 0) f = round(f);
376+
377+
int whole = f;
378+
int point = 2;
379+
if (whole < 100) point = 1;
380+
if (whole < 10) point = 0;
381+
382+
if (f >= 1)
383+
{
384+
while (f < 100) f *= 10;
385+
whole = round(f);
386+
}
387+
else
388+
{
389+
whole = round(f * 100);
390+
}
391+
392+
uint8_t x[4];
393+
x[0] = whole / 100;
394+
whole = whole - x[0] * 100;
395+
x[1] = whole / 10;
396+
x[2] = whole % 10;
397+
x[3] = unitChar;
398+
if (neg) // corrections for neg => all shift one position
399+
{
400+
x[3] = unitChar;
401+
x[2] = x[1];
402+
x[1] = x[0];
403+
x[0] = HT16K33_MINUS;
404+
point++;
405+
}
406+
// add leading spaces
407+
while (point + decimals < 2)
408+
{
409+
x[3] = unitChar;
410+
x[2] = x[1];
411+
x[1] = x[0];
412+
x[0] = HT16K33_SPACE;
413+
point++;
414+
}
415+
416+
display(x, point);
417+
418+
return inRange;
419+
}
420+
421+
343422
/////////////////////////////////////////////////////////////////////
344423
//
345-
// EXPERIMENTAL
424+
// EXPERIMENTAL
346425
//
347426
bool HT16K33::displayFixedPoint0(float f)
348427
{
@@ -400,13 +479,13 @@ void HT16K33::displayRaw(uint8_t *array, bool colon)
400479

401480
bool HT16K33::displayVULeft(uint8_t value)
402481
{
403-
bool inRange = (value < 9); // can display 0..8 bars
482+
bool inRange = (value < 9); // can display 0..8 bars
404483
uint8_t ar[4];
405484
for (int idx = 3; idx >=0; idx--)
406485
{
407486
if (value >= 2)
408487
{
409-
ar[idx] = 0x36; // ||
488+
ar[idx] = 0x36; // ||
410489
value -= 2;
411490
}
412491
else if (value == 1)
@@ -429,7 +508,7 @@ bool HT16K33::displayVURight(uint8_t value)
429508
{
430509
if (value >= 2)
431510
{
432-
ar[idx] = 0x36; // ||
511+
ar[idx] = 0x36; // ||
433512
value -= 2;
434513
}
435514
else if (value == 1)
@@ -456,15 +535,16 @@ void HT16K33::display(uint8_t *array)
456535
writePos(3, charmap[array[2]]);
457536
writePos(4, charmap[array[3]]);
458537

459-
// debug to Serial
460-
// dumpSerial(array, 0);
538+
// debug to Serial
539+
// dumpSerial(array, 0);
461540
}
462541

463542

464543
void HT16K33::display(uint8_t *array, uint8_t point)
465544
{
466-
// debug to Serial
467-
// dumpSerial(array, point);
545+
// debug to Serial
546+
// dumpSerial(array, point);
547+
// dumpSerial();
468548

469549
writePos(0, charmap[array[0]], point == 0);
470550
writePos(1, charmap[array[1]], point == 1);
@@ -491,22 +571,23 @@ void HT16K33::displayExtraLeds(uint8_t value)
491571

492572
void HT16K33::dumpSerial(uint8_t *array, uint8_t point)
493573
{
494-
// to debug without display
574+
// to debug without display
495575
for (int i = 0; i < 4; i++)
496576
{
497-
if (array[i] == HT16K33_SPACE) Serial.print(" ");
577+
if (array[i] == HT16K33_SPACE) Serial.print("_");
498578
else if (array[i] == HT16K33_MINUS) Serial.print("-");
499579
else Serial.print(array[i]);
500580
if (i == point) Serial.print(".");
501581
}
502-
Serial.print(" ");
503-
Serial.println(point);
582+
Serial.print(" (");
583+
Serial.print(point);
584+
Serial.println(")");
504585
}
505586

506587

507588
void HT16K33::dumpSerial()
508589
{
509-
// to debug without display
590+
// to debug without display
510591
for (int i = 0; i < 4; i++)
511592
{
512593
if (_displayCache[i] < 0x10) Serial.print("0");
@@ -554,7 +635,7 @@ void HT16K33::writePos(uint8_t pos, uint8_t mask)
554635
void HT16K33::writePos(uint8_t pos, uint8_t mask, bool point)
555636
{
556637
if (point) mask |= 0x80;
557-
// if (_overflow) mask |= 0x80;
638+
// if (_overflow) mask |= 0x80;
558639
else mask &= 0x7F;
559640
writePos(pos, mask);
560641
}

0 commit comments

Comments
 (0)