forked from rjbatista/tm1638-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TM1638.cpp
135 lines (114 loc) · 3.48 KB
/
TM1638.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
/*
TM1638.cpp - Library implementation for TM1638.
Copyright (C) 2011 Ricardo Batista (rjbatista <at> gmail <dot> com)
This program is free software: you can redistribute it and/or modify
it under the terms of the version 3 GNU General Public License as
published by the Free Software Foundation.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "TM1638.h"
#include "string.h"
TM1638::TM1638(byte dataPin, byte clockPin, byte strobePin, boolean activateDisplay, byte intensity)
: TM16XX(dataPin, clockPin, strobePin, 8, activateDisplay, intensity)
{
// nothing else to do - calling super is enough
}
void TM1638::setDisplayToHexNumber(unsigned long number, byte dots, boolean leadingZeros,
const byte numberFont[])
{
for (int i = 0; i < displays; i++) {
if (!leadingZeros && number == 0) {
clearDisplayDigit(displays - i - 1, (dots & (1 << i)) != 0);
} else {
setDisplayDigit(number & 0xF, displays - i - 1, (dots & (1 << i)) != 0, numberFont);
number >>= 4;
}
}
}
void TM1638::setDisplayToDecNumberAt(unsigned long number, byte dots, byte startingPos, boolean leadingZeros,
const byte numberFont[])
{
if (number > 99999999L) {
setDisplayToError();
} else {
for (int i = 0; i < displays - startingPos; i++) {
if (number != 0) {
setDisplayDigit(number % 10, displays - i - 1, (dots & (1 << i)) != 0, numberFont);
number /= 10;
} else {
if (leadingZeros) {
setDisplayDigit(0, displays - i - 1, (dots & (1 << i)) != 0, numberFont);
} else {
clearDisplayDigit(displays - i - 1, (dots & (1 << i)) != 0);
}
}
}
}
}
void TM1638::setDisplayToDecNumber(unsigned long number, byte dots, boolean leadingZeros,
const byte numberFont[])
{
setDisplayToDecNumberAt(number, dots, 0, leadingZeros, numberFont);
}
void TM1638::setDisplayToSignedDecNumber(signed long number, byte dots, boolean leadingZeros,
const byte numberFont[])
{
if (number >= 0) {
setDisplayToDecNumberAt(number, dots, 0, leadingZeros, numberFont);
} else {
if (-number > 9999999L) {
setDisplayToError();
} else {
setDisplayToDecNumberAt(-number, dots, 1, leadingZeros, numberFont);
sendChar(0, MINUS, (dots & (0x80)) != 0);
}
}
}
void TM1638::setDisplayToBinNumber(byte number, byte dots, const byte numberFont[])
{
for (int i = 0; i < displays; i++) {
setDisplayDigit((number & (1 << i)) == 0 ? 0 : 1, displays - i - 1, (dots & (1 << i)) != 0, numberFont);
}
}
void TM1638::setLED(byte color, byte pos)
{
sendData((pos << 1) + 1, color);
}
void TM1638::setLEDs(word leds)
{
for (int i = 0; i < displays; i++) {
byte color = 0;
if ((leds & (1 << i)) != 0) {
color |= TM1638_COLOR_RED;
}
if ((leds & (1 << (i + 8))) != 0) {
color |= TM1638_COLOR_GREEN;
}
setLED(color, i);
}
}
byte TM1638::getButtons(void)
{
byte keys = 0;
digitalWrite(strobePin, LOW);
send(0x42);
for (int i = 0; i < 4; i++) {
keys |= receive() << i;
}
digitalWrite(strobePin, HIGH);
return keys;
}
void TM1638::sendChar(byte pos, byte data, boolean dot)
{
sendData(pos << 1, data | (dot ? 0b10000000 : 0));
}