forked from avishorp/TM1637
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTM1637Display.cpp
More file actions
346 lines (299 loc) · 7.09 KB
/
Copy pathTM1637Display.cpp
File metadata and controls
346 lines (299 loc) · 7.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Author: avishorp@gmail.com
// Extended to 6 digits and key-scan: Guido Dampf
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
extern "C" {
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
}
#include <TM1637Display.h>
#include <Arduino.h>
#define TM1637_I2C_COMM1 0x40
#define TM1637_I2C_COMM2 0xC0
#define TM1637_I2C_COMM3 0x80
#define TM1637_I2C_COMM4 0x42
//
// A
// ---
// F | | B
// -G-
// E | | C
// ---
// D
const uint8_t digitToSegment[] = {
// XGFEDCBA
0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111101, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111, // 9
0b01110111, // A
0b01111100, // b
0b00111001, // C
0b01011110, // d
0b01111001, // E
0b01110001 // F
};
static const uint8_t minusSegments = 0b01000000;
TM1637Display::TM1637Display(uint8_t pinClk, uint8_t pinDIO, unsigned int bitDelay, uint8_t noDigits)
{
// Copy the pin numbers
m_pinClk = pinClk;
m_pinDIO = pinDIO;
m_bitDelay = bitDelay;
m_noDigits = noDigits;
// Set the pin direction and default value.
// Both pins are set as inputs, allowing the pull-up resistors to pull them up
pinMode(m_pinClk, INPUT);
pinMode(m_pinDIO,INPUT);
digitalWrite(m_pinClk, LOW);
digitalWrite(m_pinDIO, LOW);
}
void TM1637Display::setBrightness(uint8_t brightness, bool on)
{
m_brightness = (brightness & 0x7) | (on? 0x08 : 0x00);
}
void TM1637Display::setSegments(const uint8_t segments[], uint8_t length, uint8_t pos)
{
if (length==0) length = m_noDigits;
// Write COMM1
start();
if (writeByte(TM1637_I2C_COMM1))
{
stop();
return;
}
stop();
// Write COMM2 + first digit address
start();
if (pos > 5) pos &= 0x03;
if (writeByte(TM1637_I2C_COMM2 + pos))
{
stop();
return;
}
// Write the data bytes
for (uint8_t k=0; k < length; k++)
if(writeByte(segments[k]))
{
stop();
return;
}
stop();
// Write COMM3 + brightness
start();
if (writeByte(TM1637_I2C_COMM3 + (m_brightness & 0x0f)))
{
stop();
return;
}
stop();
// Write COMM4
start();
if (writeByte(TM1637_I2C_COMM4))
{
stop();
return;
}
m_KeyCode = readByte();
stop();
}
void TM1637Display::clear()
{
uint8_t data[] = { 0, 0, 0, 0, 0, 0 };
setSegments(data);
}
void TM1637Display::showNumberDec(long num, bool leading_zero, uint8_t length, uint8_t pos)
{
if (length==0) length = m_noDigits;
showNumberDecEx(num, 0, leading_zero, length, pos);
}
void TM1637Display::showNumberDecEx(long num, uint8_t dots, bool leading_zero,
uint8_t length, uint8_t pos)
{
if (length==0) length = m_noDigits;
showNumberBaseEx(num < 0? -10 : 10, num < 0? -num : num, dots, leading_zero, length, pos);
}
void TM1637Display::showNumberHexEx(uint16_t num, uint8_t dots, bool leading_zero,
uint8_t length, uint8_t pos)
{
if (length==0) length = m_noDigits;
showNumberBaseEx(16, num, dots, leading_zero, length, pos);
}
void TM1637Display::showNumberBaseEx(int8_t base, uint32_t num, uint8_t dots, bool leading_zero,
uint8_t length, uint8_t pos)
{
if (length==0) length = m_noDigits;
bool negative = false;
if (base < 0) {
base = -base;
negative = true;
}
uint8_t digits[6];
if (num == 0 && !leading_zero) {
// Singular case - take care separately
for(uint8_t i = 0; i < (length-1); i++)
digits[i] = 0;
digits[length-1] = encodeDigit(0);
}
else {
//uint8_t i = length-1;
//if (negative) {
// // Negative number, show the minus sign
// digits[i] = minusSegments;
// i--;
//}
for(int i = length-1; i >= 0; --i)
{
uint8_t digit = num % base;
if (digit == 0 && num == 0 && leading_zero == false)
// Leading zero is blank
digits[i] = 0;
else
digits[i] = encodeDigit(digit);
if (digit == 0 && num == 0 && negative) {
digits[i] = minusSegments;
negative = false;
}
num /= base;
}
if(dots != 0)
{
showDots(dots, digits);
}
}
setSegments(digits, length, pos);
}
void TM1637Display::bitDelay()
{
delayMicroseconds(m_bitDelay);
}
void TM1637Display::start()
{
pinMode(m_pinDIO, OUTPUT);
bitDelay();
}
void TM1637Display::stop()
{
pinMode(m_pinDIO, OUTPUT);
bitDelay();
pinMode(m_pinClk, OUTPUT);
bitDelay();
bitDelay();
pinMode(m_pinClk, INPUT);
bitDelay();
bitDelay();
pinMode(m_pinDIO, INPUT);
bitDelay();
}
bool TM1637Display::writeByte(uint8_t b)
{
uint8_t data = b;
// 8 Data Bits
for(uint8_t i = 0; i < 8; i++) {
bitDelay();
// CLK low
pinMode(m_pinClk, OUTPUT);
bitDelay();
// Set data bit
if (data & 0x01)
pinMode(m_pinDIO, INPUT);
else
pinMode(m_pinDIO, OUTPUT);
bitDelay();
// CLK high
pinMode(m_pinClk, INPUT);
bitDelay();
data = data >> 1;
}
// Wait for acknowledge (Cycle 9)
// CLK to zero
pinMode(m_pinClk, OUTPUT);
bitDelay();
pinMode(m_pinDIO, INPUT);
bitDelay();
// CLK to high
pinMode(m_pinClk, INPUT);
uint8_t ack = digitalRead(m_pinDIO);
bitDelay();
bitDelay();
return ack; // (Cycle 9 finished)
}
void TM1637Display::showDots(uint8_t dots, uint8_t* digits)
{
for(int i = 0; i < m_noDigits; ++i)
{
digits[i] |= (dots & 0x80);
dots <<= 1;
}
}
uint8_t TM1637Display::encodeDigit(uint8_t digit)
{
return digitToSegment[digit & 0x0f];
}
uint8_t TM1637Display::readByte()
{
uint8_t b = 0;
pinMode(m_pinDIO, INPUT);
for(uint8_t i=0; i<8; i++)
{
pinMode(m_pinClk, OUTPUT);
bitDelay();
bitDelay();
pinMode(m_pinClk, INPUT);
bitDelay();
b = (b << 1) | (digitalRead(m_pinDIO)==LOW?0:1);
bitDelay();
}
// Wait for acknowledge (Cycle 9)
// CLK to zero
pinMode(m_pinClk, OUTPUT);
bitDelay();
bitDelay();
// CLK to high
pinMode(m_pinClk, INPUT);
uint8_t ack = digitalRead(m_pinDIO);
bitDelay();
bitDelay();
if (ack != 0) return 0xFF;
return b;
}
uint8_t key(uint8_t code)
{
if (code == 0xff) return 0;
return (code >> 4) + 1;
}
uint8_t TM1637Display::lastKeyCode()
{
return key(m_KeyCode);
}
uint8_t TM1637Display::getKeyCode()
{
start();
if (writeByte(TM1637_I2C_COMM4))
{
stop();
return 0;;
}
m_KeyCode = readByte();
stop();
return key(m_KeyCode);
}