forked from WaltonSimons/TM1637
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtm1637.py
205 lines (186 loc) · 4.77 KB
/
tm1637.py
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
#!/usr/bin/python
import Adafruit_BBIO.GPIO as GPIO
import time
HEXDECIMAL = {
'0.': 0xbf,
'1.': 0x86,
'2.': 0xdb,
'3.': 0xcf,
'4.': 0xe6,
'5.': 0xed,
'6.': 0xfd,
'7.': 0x87,
'8.': 0xff,
'9.': 0xef
}
HEXDIGITS = {
'0': 0x3f,
'1': 0x06,
'2': 0x5b,
'3': 0x4f,
'4': 0x66,
'5': 0x6d,
'6': 0x7d,
'7': 0x07,
'8': 0x7f,
'9': 0x6f
}
HEXLETTERS = {
'A': 0x77,
'B': 0x7f,
'b': 0x7C,
'C': 0x39,
'c': 0x58,
'D': 0x3f,
'd': 0x5E,
'E': 0x79,
'F': 0x71,
'G': 0x7d,
'H': 0x76,
'h': 0x74,
'I': 0x06,
'J': 0x1f,
'K': 0x76,
'L': 0x38,
'l': 0x06,
'n': 0x54,
'O': 0x3f,
'o': 0x5c,
'P': 0x73,
'r': 0x50,
'S': 0x6d,
'U': 0x3e,
'V': 0x3e,
'Y': 0x66,
'Z': 0x5b,
' ': 0x00,
'\xc2\xb0': 0x63,
'-': 0x40,
'_': 0x08,
'^': 0x23,
'T1': 0x07,
'T2': 0x31,
'M1': 0x33,
'M2': 0x27,
'W1': 0x3c,
'W2': 0x1e,
'S1': 0x01,
'S2': 0x02,
'S3': 0x04,
'S4': 0x08,
'S5': 0x10,
'S6': 0x20,
'S7': 0x40
}
ADDR_AUTO = 0x40
ADDR_FIXED = 0x44
STARTADDR = 0xC0
BRIGHT_DARKEST = 0
BRIGHT_DEFAULT = 2
BRIGHT_HIGHEST = 7
OUTPUT = GPIO.OUT
INPUT = GPIO.IN
LOW = GPIO.LOW
HIGH = GPIO.HIGH
class TM1637:
__doublepoint = False
__clk_pin = 0
__data_pin = 0
__brightness = BRIGHT_DEFAULT
__current_data = [' ', ' ', ' ', ' ']
def __init__(self, clock_pin, data_pin, brightness=BRIGHT_DARKEST):
self.__clk_pin = clock_pin
self.__data_pin = data_pin
self.__brightness = brightness
GPIO.setup(self.__clk_pin, OUTPUT)
GPIO.setup(self.__data_pin, OUTPUT)
def clear(self):
b = self.__brightness
point = self.__doublepoint
self.__brightness = 0
self.__doublepoint = False
data = [' ', ' ', ' ', ' ']
self.set_values(data)
self.__brightness = b
self.__doublepoint = point
def set_values(self, data):
for i in range(4):
self.__current_data[i] = data[i]
self.start()
self.write_byte(ADDR_AUTO)
self.stop()
self.start()
self.write_byte(STARTADDR)
for i in range(4):
self.write_byte(self.encode(data[i]))
self.stop()
self.start()
self.write_byte(0x88 + self.__brightness)
self.stop()
def set_value(self, value, index):
if index not in range(4):
pass
self.__current_data[index] = value;
self.start()
self.write_byte(ADDR_FIXED)
self.stop()
self.start()
self.write_byte(STARTADDR | index)
self.write_byte(self.encode(value))
self.stop()
self.start()
self.write_byte(0x88 + self.__brightness)
self.stop()
def set_brightness(self, brightness):
if brightness not in range(8):
pass
self.__brightness = brightness
self.set_values(self.__current_data)
self.__current_data = [' ', ' ', ' ', ' ']
def set_doublepoint(self, value):
self.__doublepoint = value
self.set_values(self.__current_data)
def encode(self, data):
point = 0x80 if self.__doublepoint else 0x00;
if data == 0x7F:
data = 0
elif HEXLETTERS.has_key(data):
data = HEXLETTERS[data] + point
elif HEXDIGITS.has_key(data):
data = HEXDIGITS[data] + point
else:
data = HEXDECIMAL[data] + point
return data
def write_byte(self, data):
for i in range(8):
GPIO.output(self.__clk_pin, LOW)
if data & 0x01:
GPIO.output(self.__data_pin, HIGH)
else:
GPIO.output(self.__data_pin, LOW)
data >>= 1
GPIO.output(self.__clk_pin, HIGH)
GPIO.output(self.__clk_pin, LOW)
GPIO.output(self.__data_pin, HIGH)
GPIO.output(self.__clk_pin, HIGH)
GPIO.setup(self.__data_pin, INPUT)
while GPIO.input(self.__data_pin):
#time.sleep(0.001)
if GPIO.input(self.__data_pin):
GPIO.setup(self.__data_pin, OUTPUT)
GPIO.output(self.__data_pin, LOW)
GPIO.setup(self.__data_pin, INPUT)
GPIO.setup(self.__data_pin, OUTPUT)
def start(self):
GPIO.output(self.__clk_pin, HIGH)
GPIO.output(self.__data_pin, HIGH)
GPIO.output(self.__data_pin, LOW)
GPIO.output(self.__clk_pin, LOW)
def stop(self):
GPIO.output(self.__clk_pin, LOW)
GPIO.output(self.__data_pin, LOW)
GPIO.output(self.__clk_pin, HIGH)
GPIO.output(self.__data_pin, HIGH)
def cleanup(self):
GPIO.cleanup(self.__clk_pin)
GPIO.cleanup(self.__data_pin)