Skip to content

Commit 9478203

Browse files
committed
HT16K33 - fix printFloat
1 parent b7e337e commit 9478203

File tree

8 files changed

+245
-137
lines changed

8 files changed

+245
-137
lines changed

libraries/HT16K33/HT16K33.cpp

Lines changed: 112 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,115 @@
11
//
22
// FILE: HT16K33.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.4
4+
// VERSION: 0.2.1
55
// DATE: 2019-02-07
6-
// PURPOSE: Class for HT16K33
6+
// PURPOSE: Arduino Library for HT16K33 4x7segment display
7+
// URL: https://github.com/RobTillaart/HT16K33
78
//
89
// HISTORY:
9-
// 0.1.0 - 2019-02-07 initial version
10-
// 0.1.1 - 2019-02-07 first stable version
11-
// 0.1.2 - 2019-02-11 optimized performance
12-
// 0.1.3 - 2019-10-07 fixed clear, added suppressLeadingZeroPlaces();
13-
// 0.1.4 - 2019-11-28 added displayRaw(), displayVULeft(), displayVURight()
14-
//
15-
// Released to the public domain
16-
//
10+
// 0.1.0 2019-02-07 initial version
11+
// 0.1.1 2019-02-07 first stable version
12+
// 0.1.2 2019-02-11 optimized performance
13+
// 0.1.3 2019-10-07 fixed clear, added suppressLeadingZeroPlaces();
14+
// 0.1.4 2019-11-28 added displayRaw(), displayVULeft(), displayVURight()
15+
// 0.1.5 2019-11-30 refactor,
16+
// 0.2.0 2020-06-13 ESP32 support; fix brightness bug;
17+
// 0.2.1 2020-07-15 fix #160 - decimal point
1718

1819
#include "HT16K33.h"
1920

20-
#define HT16K33_ON 0x21 // 0=off 1=on
21-
#define HT16K33_STANDBY 0x20 // bit xxxxxxx0
22-
23-
#define HT16K33_DISPLAYON 0x81 // 0=off 1=on
24-
#define HT16K33_DISPLAYOFF 0x80 // bit xxxxxxx0
25-
26-
#define HT16K33_BLINKON1HZ 0x85 // 00=off 01=2Hz 10=1Hz 11=0.5Hz
27-
#define HT16K33_BLINKOFF 0x81 // bit xxxxx21x
28-
29-
#define HT16K33_BRIGHTNESS 0xE0 // mask to be ored with 0x00 - 0x0F
30-
31-
#define HT16K33_SPACE 16
32-
#define HT16K33_NONE 17
33-
34-
// TODO PROGMEM ?
35-
static const uint8_t charmap[] = {
36-
0x3F, // 0
37-
0x06, // 1
38-
0x5B, // 2
39-
0x4F, // 3
40-
0x66, // 4
41-
0x6D, // 5
42-
0x7D, // 6
43-
0x07, // 7
44-
0x7F, // 8
45-
0x6F, // 9
46-
0x77, // A
47-
0x7C, // B
48-
0x39, // C
49-
0x5E, // D
50-
0x79, // E
51-
0x71, // F
52-
0x00, // space
21+
// Commands
22+
#define HT16K33_ON 0x21 // 0=off 1=on
23+
#define HT16K33_STANDBY 0x20 // bit xxxxxxx0
24+
25+
// bit pattern 1000 0xxy
26+
// y = display on / off
27+
// xx = 00=off 01=2Hz 10=1Hz 11=0.5Hz
28+
#define HT16K33_DISPLAYON 0x81
29+
#define HT16K33_DISPLAYOFF 0x80
30+
#define HT16K33_BLINKON0_5HZ 0x87
31+
#define HT16K33_BLINKON1HZ 0x85
32+
#define HT16K33_BLINKON2HZ 0x83
33+
#define HT16K33_BLINKOFF 0x81
34+
35+
// bit pattern 1110 xxxx
36+
// xxxx = 0000 .. 1111 (0 - F)
37+
#define HT16K33_BRIGHTNESS 0xE0
38+
39+
// Special characters
40+
#define HT16K33_SPACE 16
41+
#define HT16K33_MINUS 17
42+
#define HT16K33_NONE 99
43+
44+
//
45+
// HEX codes 7 segment
46+
//
47+
// 01
48+
// 20 02
49+
// 40
50+
// 10 04
51+
// 08
52+
//
53+
static const uint8_t charmap[] = { // TODO PROGMEM ?
54+
55+
0x3F, // 0
56+
0x06, // 1
57+
0x5B, // 2
58+
0x4F, // 3
59+
0x66, // 4
60+
0x6D, // 5
61+
0x7D, // 6
62+
0x07, // 7
63+
0x7F, // 8
64+
0x6F, // 9
65+
0x77, // A
66+
0x7C, // B
67+
0x39, // C
68+
0x5E, // D
69+
0x79, // E
70+
0x71, // F
71+
0x00, // space
72+
0x40, // minus
5373
};
5474

5575
////////////////////////////////////////////////////
5676

57-
HT16K33::HT16K33()
77+
HT16K33::HT16K33(const uint8_t address)
5878
{
79+
_addr = address;
5980
}
6081

61-
void HT16K33::begin(const uint8_t address)
82+
#if defined (ESP8266) || defined(ESP32)
83+
void HT16K33::begin(uint8_t sda, uint8_t scl)
84+
{
85+
Wire.begin(sda, scl);
86+
reset();
87+
}
88+
#endif
89+
90+
void HT16K33::begin()
91+
{
92+
Wire.begin();
93+
reset();
94+
}
95+
96+
void HT16K33::reset()
6297
{
63-
_addr = address;
6498
displayOn();
6599
displayClear();
66100
suppressLeadingZeroPlaces(3);
67101
for (uint8_t i = 0; i < 5; i++)
68102
{
69103
_displayCache[i] = HT16K33_NONE;
70104
}
105+
brightness(8);
71106
}
72107

73108
void HT16K33::displayOn()
74109
{
75110
writeCmd(HT16K33_ON);
76111
writeCmd(HT16K33_DISPLAYON);
77-
brightness(8);
112+
brightness(_bright); // TODO needed?
78113
}
79114

80115
void HT16K33::displayOff()
@@ -85,14 +120,17 @@ void HT16K33::displayOff()
85120

86121
void HT16K33::blink(uint8_t val)
87122
{
123+
// TODO cache blink state too?
88124
if (val > 0x03) val = 0x00;
89125
writeCmd(HT16K33_BLINKOFF | (val << 1) );
90126
}
91127

92128
void HT16K33::brightness(uint8_t val)
93129
{
94-
if (val > 0x0F) val = 0x0F;
95-
writeCmd(HT16K33_BRIGHTNESS | val);
130+
if (val == _bright) return;
131+
_bright = val;
132+
if (_bright > 0x0F) _bright = 0x0F;
133+
writeCmd(HT16K33_BRIGHTNESS | _bright);
96134
}
97135

98136
void HT16K33::suppressLeadingZeroPlaces(uint8_t val)
@@ -155,14 +193,16 @@ void HT16K33::displayTime(uint8_t left, uint8_t right)
155193
// TODO x.yEz
156194
void HT16K33::displayFloat(float f)
157195
{
158-
int pt = 0;
159196
// uint8_t neg = 0;
160197
// if (f < 0) { neg = -1; f = -f; }
161198
if (f > 9999 || f < 0 ) return;
162-
int w = round(f);
163-
if (w > 9) pt = 1;
164-
if (w > 99) pt = 2;
165-
if (w > 999) pt = 3;
199+
int w = f;
200+
201+
int pt = 3;
202+
if (w < 1000) pt = 2;
203+
if (w < 100) pt = 1;
204+
if (w < 10) pt = 0;
205+
166206
if (f >= 1)
167207
{
168208
while (f < 1000) f *= 10;
@@ -211,15 +251,15 @@ void HT16K33::displayVULeft(uint8_t val)
211251
{
212252
if (val >= 2)
213253
{
214-
ar[idx] = 0x36; // ||
254+
ar[idx] = 0x36; // ||
215255
val -= 2;
216256
}
217257
else if (val == 1)
218258
{
219-
ar[idx] = 0x06; // _|
259+
ar[idx] = 0x06; // _|
220260
val = 0;
221261
}
222-
else ar[idx] = 0x00; // __
262+
else ar[idx] = 0x00; // __
223263
}
224264
displayRaw(ar);
225265
}
@@ -231,15 +271,15 @@ void HT16K33::displayVURight(uint8_t val)
231271
{
232272
if (val >= 2)
233273
{
234-
ar[idx] = 0x36; // ||
274+
ar[idx] = 0x36; // ||
235275
val -= 2;
236276
}
237277
else if (val == 1)
238278
{
239-
ar[idx] = 0x30; // |_
279+
ar[idx] = 0x30; // |_
240280
val = 0;
241281
}
242-
else ar[idx] = 0x00; // __
282+
else ar[idx] = 0x00; // __
243283
}
244284
displayRaw(ar);
245285
}
@@ -262,6 +302,14 @@ void HT16K33::display(uint8_t *arr)
262302

263303
void HT16K33::display(uint8_t *arr, uint8_t pnt)
264304
{
305+
// to debug without display
306+
// Serial.print(arr[0]);
307+
// Serial.print(arr[1]);
308+
// Serial.print(arr[2]);
309+
// Serial.print(arr[3]);
310+
// Serial.print(" ");
311+
// Serial.println(pnt);
312+
265313
writePos(0, charmap[arr[0]], pnt == 0);
266314
writePos(1, charmap[arr[1]], pnt == 1);
267315
writePos(3, charmap[arr[2]], pnt == 2);
@@ -274,7 +322,9 @@ void HT16K33::displayColon(uint8_t on)
274322
}
275323

276324
//////////////////////////////////////////////////////////
277-
325+
//
326+
// PRIVATE
327+
//
278328
void HT16K33::writeCmd(uint8_t cmd)
279329
{
280330
Wire.beginTransmission(_addr);
@@ -298,4 +348,4 @@ void HT16K33::writePos(uint8_t pos, uint8_t mask, bool pnt)
298348
writePos(pos, mask);
299349
}
300350

301-
// -- END OF FILE --
351+
// -- END OF FILE --

libraries/HT16K33/HT16K33.h

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,55 @@
1+
#pragma once
12
//
23
// FILE: HT16K33.h
34
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.4
5+
// VERSION: 0.2.1
56
// DATE: 2019-02-07
6-
// PURPOSE: Class for HT16K33 7segment I2C display
7+
// PURPOSE: Arduino Library for HT16K33 4x7segment display
78
// http://www.adafruit.com/products/1002
8-
// URL: https://github.com/RobTillaart/Arduino.git
9+
// URL: https://github.com/RobTillaart/HT16K33.git
910
//
10-
// Released to the public domain
11-
//
12-
// TODO: negative values
13-
//
14-
15-
#ifndef HT16K33_h
16-
#define HT16K33_h
1711

1812
#include "Arduino.h"
1913
#include "Wire.h"
2014

21-
#define HT16K33_LIB_VERSION "0.1.4"
15+
#define HT16K33_LIB_VERSION "0.2.1"
2216

2317
class HT16K33
2418
{
25-
public:
26-
HT16K33();
19+
public:
20+
HT16K33(const uint8_t address); // 0x70 .. 0x77
21+
22+
#if defined (ESP8266) || defined(ESP32)
23+
void begin(uint8_t sda, uint8_t scl);
24+
#endif
25+
void begin();
26+
void reset();
2727

28-
void begin(const uint8_t address); // 0x70 .. 0x77
2928
void displayOn();
3029
void displayOff();
30+
3131
void brightness(uint8_t val); // 0 .. 15
3232
void blink(uint8_t val); // 0 .. 3 0 = off
33-
void suppressLeadingZeroPlaces(uint8_t val); // 0 = off, 1,2,3,4 digits space iso 0
33+
34+
// 0 = off, 1,2,3,4 digits space iso 0
35+
void suppressLeadingZeroPlaces(uint8_t val);
3436

3537
void displayClear();
3638
void displayInt(int n); // 0000 .. 9999
3739
void displayHex(uint16_t n); // 0000 .. FFFF
3840
void displayTime(uint8_t left, uint8_t right); // 00:00 .. 99:99
39-
void displayFloat(float f); // 0.000 .. 9999.
41+
void displayFloat(float f); // 0.000 .. 9999
42+
4043
void display(uint8_t *arr); // array with 4 elements
4144
void display(uint8_t *arr, uint8_t pt); // pt = digit with . (0..3)
4245
void displayColon(uint8_t on); // 0 = off
4346

44-
void displayTest(uint8_t del);
45-
void displayRaw(uint8_t *arr);// control
46-
void displayVULeft(uint8_t val);// 0..8
47-
void displayVURight(uint8_t val);// 0..8
47+
void displayTest(uint8_t del); // debug
48+
void displayRaw(uint8_t *arr); // max control
49+
void displayVULeft(uint8_t val); // 0..8
50+
void displayVURight(uint8_t val); // 0..8
4851

49-
private:
52+
private:
5053

5154
void writeCmd(uint8_t cmd);
5255
void writePos(uint8_t pos, uint8_t mask);
@@ -55,7 +58,7 @@ class HT16K33
5558
uint8_t _addr;
5659
uint8_t _displayCache[5]; // for performance
5760
uint8_t _leadingZeroPlaces;
61+
uint8_t _bright;
5862
};
5963

60-
#endif
61-
// -- END OF FILE --
64+
// -- END OF FILE --

libraries/HT16K33/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019-2020 Rob Tillaart
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

0 commit comments

Comments
 (0)