forked from chamie/TM1637
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TM1637.cpp
187 lines (148 loc) · 3.74 KB
/
TM1637.cpp
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
// Author: avishorp@gmail.com
//
// 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 <TM1637.h>
#include <Arduino.h>
TM1637::TM1637(uint8_t pinClk, uint8_t pinDIO)
{
// Copy the pin numbers
m_pinClk = pinClk;
m_pinDIO = pinDIO;
// Set colon off by default? Or change the constructor?
m_colon = false;
// 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 TM1637::setBrightness(uint8_t brightness)
{
m_brightness = brightness;
}
void TM1637::setColon(bool colon)
{
m_colon = colon;
}
void TM1637::setSegments(const uint8_t segments[], uint8_t length, uint8_t pos)
{
// Write COMM1
start();
writeByte(TM1637_I2C_COMM1);
stop();
// Write COMM2 + first digit address
start();
writeByte(TM1637_I2C_COMM2 + (pos & 0x03));
// Write the data bytes
for (uint8_t k=0; k < length; k++)
writeByte(segments[k]);
stop();
// Write COMM3 + brightness
start();
writeByte(TM1637_I2C_COMM3 + (m_brightness & 0x0f));
stop();
}
void TM1637::showNumberDec(int num, bool leading_zero, uint8_t length, uint8_t pos)
{
uint8_t digits[4];
const static int divisors[] = { 1, 10, 100, 1000 };
bool leading = true;
for(int8_t k = 0; k < 4; k++) {
int divisor = divisors[4 - 1 - k];
int d = num / divisor;
if (d == 0) {
if (leading_zero || !leading || (k == 3))
digits[k] = encodeDigit(d);
else
digits[k] = 0;
}
else {
digits[k] = encodeDigit(d);
num -= d * divisor;
leading = false;
}
}
if(m_colon) digits[COLON_POSITION] |= 0x80;
setSegments(digits + (4 - length), length, pos);
}
void TM1637::bitDelay()
{
delayMicroseconds(50);
}
void TM1637::start()
{
pinMode(m_pinDIO, OUTPUT);
bitDelay();
}
void TM1637::stop()
{
pinMode(m_pinDIO, OUTPUT);
bitDelay();
pinMode(m_pinClk, INPUT);
bitDelay();
pinMode(m_pinDIO, INPUT);
bitDelay();
}
bool TM1637::writeByte(uint8_t b)
{
uint8_t data = b;
// 8 Data Bits
for(uint8_t i = 0; i < 8; i++) {
// 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
// CLK to zero
pinMode(m_pinClk, OUTPUT);
pinMode(m_pinDIO, INPUT);
bitDelay();
// CLK to high
pinMode(m_pinClk, INPUT);
bitDelay();
uint8_t ack = digitalRead(m_pinDIO);
if (ack == 0)
pinMode(m_pinDIO, OUTPUT);
bitDelay();
pinMode(m_pinClk, OUTPUT);
bitDelay();
return ack;
}
uint8_t TM1637::encodeDigit(uint8_t digit)
{
return digitToSegment[digit & 0x0f];
}
void TM1637::clear(void)
{
uint8_t data[] = { 0,0,0,0 };
setSegments(data);
}