forked from rjbatista/tm1638-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTM1638QYF.cpp
151 lines (112 loc) · 3.95 KB
/
TM1638QYF.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
/*
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 "TM1638QYF.h"
#include "string.h"
TM1638QYF::TM1638QYF(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 TM1638QYF::setDisplay(const byte values[], unsigned int length)
{
for (int i = 0; i < displays; i++) {
int val = 0;
for (int j = 0; j < length; j++) {
val |= ((values[j] >> i) & 1) << (displays - j - 1);
}
sendData(i << 1, val);
}
}
void TM1638QYF::clearDisplay()
{
setDisplay(NULL, 0);
}
void TM1638QYF::setDisplayToString(const char* string, const word dots, const byte ignored, const byte font[])
{
byte values[displays];
boolean done = false;
memset(values, 0, displays * sizeof(byte));
for (int i = 0; i < displays; i++) {
if (!done && string[i] != '\0') {
values[i] = font[string[i] - 32] | (((dots >> (displays - i - 1)) & 1) << 7);
} else {
done = true;
values[i] = (((dots >> (displays - i - 1)) & 1) << 7);
}
}
setDisplay(values, displays);
}
void TM1638QYF::setDisplayToString(String string, const word dots, const byte ignored, const byte font[])
{
byte values[displays];
int stringLength = string.length();
memset(values, 0, displays * sizeof(byte));
for (int i = 0; i < displays; i++) {
if (i < stringLength) {
values[i] = font[string.charAt(i) - 32] | (((dots >> (displays - i - 1)) & 1) << 7);
} else {
values[i] = (((dots >> (displays - i - 1)) & 1) << 7);
}
}
setDisplay(values, displays);
}
void TM1638QYF::setDisplayToBinNumber(byte number, byte dots, const byte numberFont[])
{
byte values[displays];
memset(values, 0, displays * sizeof(byte));
for (int i = 0; i < displays; i++) {
values[i] = numberFont[(number >> (displays - i - 1)) & 1] | (((dots >> (displays - i - 1)) & 1) << 7);
}
setDisplay(values, displays);
}
void TM1638QYF::setDisplayToHexNumber(unsigned long number, byte dots, boolean leadingZeros,
const byte numberFont[])
{
char values[displays + 1];
snprintf(values, displays + 1, leadingZeros ? "%08X" : "%X", number); // ignores display size
setDisplayToString(values, dots, 0, numberFont);
}
void TM1638QYF::setDisplayToDecNumber(unsigned long number, byte dots, boolean leadingZeros,
const byte numberFont[])
{
char values[displays + 1];
snprintf(values, displays + 1, leadingZeros ? "%08ld" : "%ld", number); // ignores display size
Serial.println(values);
setDisplayToString(values, dots, 0, numberFont);
}
void TM1638QYF::setDisplayToSignedDecNumber(signed long number, byte dots, boolean leadingZeros,
const byte numberFont[])
{
char values[displays + 1];
snprintf(values, displays + 1, leadingZeros ? "%08d" : "%d", number); // ignores display size
setDisplayToString(values, dots, 0, numberFont);
}
word TM1638QYF::getButtons(void)
{
word keys = 0;
digitalWrite(strobePin, LOW);
send(0x42); // B01000010 Read the key scan data
for (int i = 0; i < 4; i++) {
byte rec = receive();
rec = (((rec & B1000000) >> 3 | (rec & B100)) >> 2) | (rec & B100000) | (rec & B10) << 3;
keys |= ((rec & 0x000F) << (i << 1)) | (((rec & 0x00F0) << 4) << (i << 1));
}
digitalWrite(strobePin, HIGH);
return keys;
}