-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathTM1638.py
159 lines (135 loc) · 4.42 KB
/
TM1638.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
#!/usr/bin/env python
# vim: set fileencoding=utf-8 expandtab shiftwidth=4 tabstop=4 softtabstop=4:
# A library for controlling TM1638 displays from a Raspberry Pi
# Based on original work in C from https://code.google.com/p/tm1638-library/
# Converted to python by Jacek Fedorynski <[email protected]> (common cathode)
# Converted for TM1638 common anode by John Blackmore <[email protected]>
import RPi.GPIO as GPIO
GPIO.setwarnings(False) # suppresses warnings on RasPi
class TM1638(object):
FONT = {
'0': 0b00111111,
'1': 0b00000110,
'2': 0b01011011,
'3': 0b01001111,
'4': 0b01100110,
'5': 0b01101101,
'6': 0b01111101,
'7': 0b00000111,
'8': 0b01111111,
'9': 0b01101111,
'a': 0b01110111,
'b': 0b01111100,
'c': 0b01011000,
'd': 0b01011110,
'e': 0b01111001,
'f': 0b01110001,
'g': 0b01011111,
'h': 0b01110100,
'i': 0b00010000,
'j': 0b00001110,
'l': 0b00111000,
'n': 0b01010100,
'o': 0b01011100,
'p': 0b01110011,
'r': 0b01010000,
's': 0b01101101,
't': 0b01111000,
'u': 0b00111110,
'y': 0b01101110,
'C': 0b00111001,
'P': 0b01110011,
'U': 0b00111110
}
def __init__(self, dio, clk, stb):
self.dio = dio
self.clk = clk
self.stb = stb
def enable(self, intensity=7):
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.dio, GPIO.OUT)
GPIO.setup(self.clk, GPIO.OUT)
GPIO.setup(self.stb, GPIO.OUT)
GPIO.output(self.stb, True)
GPIO.output(self.clk, True)
self.send_command(0x40)
self.send_command(0x80 | 8 | min(7, intensity))
GPIO.output(self.stb, False)
self.send_byte(0xC0)
for i in range(16):
self.send_byte(0x00)
GPIO.output(self.stb, True)
def send_command(self, cmd):
GPIO.output(self.stb, False)
self.send_byte(cmd)
GPIO.output(self.stb, True)
def send_data(self, addr, data):
self.send_command(0x44)
GPIO.output(self.stb, False)
self.send_byte(0xC0 | addr)
self.send_byte(data)
GPIO.output(self.stb, True)
def send_byte(self, data):
for i in range(8):
GPIO.output(self.clk, False)
GPIO.output(self.dio, (data & 1) == 1)
data >>= 1
GPIO.output(self.clk, True)
def set_led(self, n, color):
self.send_data((n << 1) + 1, color)
def send_char(self, pos, data, dot=False):
self.send_data(pos << 1, data | (128 if dot else 0))
def set_digit(self, pos, digit, dot=False):
for i in range(0, 6):
self.send_char(i, self.get_bit_mask(pos, digit, i), dot)
def get_bit_mask(self, pos, digit, bit):
return ((self.FONT[digit] >> bit) & 1) << pos
def set_text(self, text):
dots = 0b00000000
pos = text.find('.')
if pos != -1:
dots = dots | (128 >> pos+(8-len(text)))
text = text.replace('.', '')
self.send_char(7, self.rotate_bits(dots))
text = text[0:8]
text = text[::-1]
text += " "*(8-len(text))
for i in range(0, 7):
byte = 0b00000000;
for pos in range(8):
c = text[pos]
if c == 'c':
byte = (byte | self.get_bit_mask(pos, c, i))
elif c != ' ':
byte = (byte | self.get_bit_mask(pos, c, i))
self.send_char(i, self.rotate_bits(byte))
def receive(self):
temp = 0
GPIO.setup(self.dio, GPIO.IN, pull_up_down=GPIO.PUD_UP)
for i in range(8):
temp >>= 1
GPIO.output(self.clk, False)
if GPIO.input(self.dio):
temp |= 0x80
GPIO.output(self.clk, True)
GPIO.setup(self.dio, GPIO.OUT)
return temp
def get_buttons(self):
keys = 0
GPIO.output(self.stb, False)
self.send_byte(0x42)
for i in range(4):
keys |= self.receive() << i
GPIO.output(self.stb, True)
return keys
def rotate_bits(self, num):
for i in range(0, 4):
num = self.rotr(num, 8)
return num
def rotr(self, num, bits):
num &= (2**bits-1)
bit = num & 1
num >>= 1
if bit:
num |= (1 << (bits-1))
return num