1
1
//
2
2
// FILE: HT16K33.cpp
3
3
// AUTHOR: Rob Tillaart
4
- // VERSION: 0.1.4
4
+ // VERSION: 0.2.1
5
5
// DATE: 2019-02-07
6
- // PURPOSE: Class for HT16K33
6
+ // PURPOSE: Arduino Library for HT16K33 4x7segment display
7
+ // URL: https://github.com/RobTillaart/HT16K33
7
8
//
8
9
// 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
17
18
18
19
#include " HT16K33.h"
19
20
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
53
73
};
54
74
55
75
// //////////////////////////////////////////////////
56
76
57
- HT16K33::HT16K33 ()
77
+ HT16K33::HT16K33 (const uint8_t address )
58
78
{
79
+ _addr = address;
59
80
}
60
81
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 ()
62
97
{
63
- _addr = address;
64
98
displayOn ();
65
99
displayClear ();
66
100
suppressLeadingZeroPlaces (3 );
67
101
for (uint8_t i = 0 ; i < 5 ; i++)
68
102
{
69
103
_displayCache[i] = HT16K33_NONE;
70
104
}
105
+ brightness (8 );
71
106
}
72
107
73
108
void HT16K33::displayOn ()
74
109
{
75
110
writeCmd (HT16K33_ON);
76
111
writeCmd (HT16K33_DISPLAYON);
77
- brightness (8 );
112
+ brightness (_bright); // TODO needed?
78
113
}
79
114
80
115
void HT16K33::displayOff ()
@@ -85,14 +120,17 @@ void HT16K33::displayOff()
85
120
86
121
void HT16K33::blink (uint8_t val)
87
122
{
123
+ // TODO cache blink state too?
88
124
if (val > 0x03 ) val = 0x00 ;
89
125
writeCmd (HT16K33_BLINKOFF | (val << 1 ) );
90
126
}
91
127
92
128
void HT16K33::brightness (uint8_t val)
93
129
{
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);
96
134
}
97
135
98
136
void HT16K33::suppressLeadingZeroPlaces (uint8_t val)
@@ -155,14 +193,16 @@ void HT16K33::displayTime(uint8_t left, uint8_t right)
155
193
// TODO x.yEz
156
194
void HT16K33::displayFloat (float f)
157
195
{
158
- int pt = 0 ;
159
196
// uint8_t neg = 0;
160
197
// if (f < 0) { neg = -1; f = -f; }
161
198
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
+
166
206
if (f >= 1 )
167
207
{
168
208
while (f < 1000 ) f *= 10 ;
@@ -211,15 +251,15 @@ void HT16K33::displayVULeft(uint8_t val)
211
251
{
212
252
if (val >= 2 )
213
253
{
214
- ar[idx] = 0x36 ; // ||
254
+ ar[idx] = 0x36 ; // ||
215
255
val -= 2 ;
216
256
}
217
257
else if (val == 1 )
218
258
{
219
- ar[idx] = 0x06 ; // _|
259
+ ar[idx] = 0x06 ; // _|
220
260
val = 0 ;
221
261
}
222
- else ar[idx] = 0x00 ; // __
262
+ else ar[idx] = 0x00 ; // __
223
263
}
224
264
displayRaw (ar);
225
265
}
@@ -231,15 +271,15 @@ void HT16K33::displayVURight(uint8_t val)
231
271
{
232
272
if (val >= 2 )
233
273
{
234
- ar[idx] = 0x36 ; // ||
274
+ ar[idx] = 0x36 ; // ||
235
275
val -= 2 ;
236
276
}
237
277
else if (val == 1 )
238
278
{
239
- ar[idx] = 0x30 ; // |_
279
+ ar[idx] = 0x30 ; // |_
240
280
val = 0 ;
241
281
}
242
- else ar[idx] = 0x00 ; // __
282
+ else ar[idx] = 0x00 ; // __
243
283
}
244
284
displayRaw (ar);
245
285
}
@@ -262,6 +302,14 @@ void HT16K33::display(uint8_t *arr)
262
302
263
303
void HT16K33::display (uint8_t *arr, uint8_t pnt)
264
304
{
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
+
265
313
writePos (0 , charmap[arr[0 ]], pnt == 0 );
266
314
writePos (1 , charmap[arr[1 ]], pnt == 1 );
267
315
writePos (3 , charmap[arr[2 ]], pnt == 2 );
@@ -274,7 +322,9 @@ void HT16K33::displayColon(uint8_t on)
274
322
}
275
323
276
324
// ////////////////////////////////////////////////////////
277
-
325
+ //
326
+ // PRIVATE
327
+ //
278
328
void HT16K33::writeCmd (uint8_t cmd)
279
329
{
280
330
Wire.beginTransmission (_addr);
@@ -298,4 +348,4 @@ void HT16K33::writePos(uint8_t pos, uint8_t mask, bool pnt)
298
348
writePos (pos, mask);
299
349
}
300
350
301
- // -- END OF FILE --
351
+ // -- END OF FILE --
0 commit comments