-
Notifications
You must be signed in to change notification settings - Fork 1
/
max7219.py
executable file
·206 lines (170 loc) · 5.28 KB
/
max7219.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
206
#!/usr/bin/env python
#
import time
import spi
from RPi import GPIO
import numpy as np
SPI_MOSI = 16
SPI_CS = 20
SPI_CLK = 21
def apply_cols(spicomm, value, delay=0.0):
"""Apply a pattern to each column"""
for col in range(8):
cmd = (col+1) << 8
cmd |= value
spicomm.put(cmd, 16)
if delay > 0.0:
time.sleep(delay)
def apply_rows(spicomm, value, delay=0.0):
"""Apply a pattern to each row"""
value_state = value
row_state = 0
print "++++++++"
for row in range(8):
mask = 2**row
if (value & mask) != 0:
row_state |= mask
value_state <<= 1
for col in range(8):
cmd = (col+1) << 8
cmd |= row_state
spicomm.put(cmd, 16)
if delay > 0.0:
print "--------"
time.sleep(delay)
def apply_matrix(spicomm, matrix, delay=0.0):
for col in range(8):
cmd = (col+1) << 8
values = 0
for i in np.flipud(matrix[:,col]):
values <<= 1
if i != 0:
values |= 1
spicomm.put(cmd|values, 16)
if delay > 0.0:
time.sleep(delay)
def set_coordinate(spicomm, pos, state):
"""
Given an x, y tuple (pos), set the state for that LED. Because entire
columns are addressed at once, we have to set the state of the whole column
in changing the one LED though. This shortcoming could be addressed by
retaining the LED matrix state in this application.
Remember that row addresses start at 1 (e.g., digit 0's address is 0001)
"""
x = pos[0]
y = pos[1]
### we use x-1 because digit address 0 begins at address 1
register_addr = (x+1) << 8
register_val = state
for _ in range(y):
register_val <<= 1
spicomm.put(register_addr|register_val, 12)
def run_demo(spicomm):
"""
Light up specific patterns
"""
### Enable test mode
spicomm.put(int("111100000001",2), 16)
time.sleep(1.0)
### Disable test mode
spicomm.put(int("111100000000",2), 16)
### Set all LEDs to off
apply_cols(spicomm, int("00000000",2))
### Enable each "digit" one by one
apply_cols(spicomm, int("10010101",2), delay=0.1)
time.sleep(1.0)
### Enable everything
apply_cols(spicomm, 2**8-1)
time.sleep(1.0)
### Enable each row one by one
apply_rows(spicomm, int("10010101",2), delay=0.1)
time.sleep(1.0)
### Enable a specific set of LEDs
### Go into smiley face loop
smiley = np.array( [
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 1, 1, 0, 0, 1, 1, 0 ],
[ 0, 1, 1, 0, 0, 1, 1, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 1, 1, 0, 0, 0, 0, 1, 1 ],
[ 0, 1, 1, 0, 0, 1, 1, 0 ],
[ 0, 0, 1, 1, 1, 1, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
] )
### Show a smiley face, then an inverse smiley face
try:
while True:
apply_matrix(spicomm, smiley, delay=0.1)
time.sleep(1.0)
smiley = abs(smiley -1)
except KeyboardInterrupt:
pass
### Raster a single illuminated LED
try:
while True:
state = [0] * 64
for i in range(64):
state[i-1] = 0
state[i] = 1
apply_matrix(spicomm, np.reshape(state,(8,8)))
time.sleep(0.1)
except KeyboardInterrupt:
pass
try:
input("Press any key to shut down.")
except:
pass
def run_coordinate_setter(spicomm):
try:
while True:
xy = input("Enter x, y position to illuminate:")
if isinstance(xy, tuple):
x, y = xy
else:
x, y = xy.split()
set_coordinate(spicomm, (x,y), 1)
except KeyboardInterrupt:
pass
def run_pulse(spicomm, delay=0.1,max_intensity=16):
### Enable everything
apply_cols(spicomm, 2**8-1)
state = np.array( [
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 1, 1, 0, 0, 1, 1, 0 ],
[ 1, 1, 1, 1, 1, 1, 1, 1 ],
[ 1, 1, 1, 1, 1, 1, 1, 1 ],
[ 0, 1, 1, 1, 1, 1, 1, 0 ],
[ 0, 0, 1, 1, 1, 1, 0, 0 ],
[ 0, 0, 0, 1, 1, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
] )
### Show a smiley face, then an inverse smiley face
apply_matrix(spicomm, state)
try:
while True:
### Circular loop over intensities
for value in range(max_intensity) + range(max_intensity-2,0,-1):
cmd = int("1010",2) << 8
spicomm.put(cmd|value, 12)
time.sleep(delay)
except KeyboardInterrupt:
pass
def main():
max7219 = spi.SPI(clk=SPI_CLK, cs=SPI_CS, mosi=SPI_MOSI, miso=None, verbose=True)
### Disable code B decode mode on all digits
max7219.put(int("100100000000",2), 16)
### Set intensity low
max7219.put(int("101000000000",2), 16)
### Enable all digits in scan-limit register
max7219.put(int("101100000111",2), 16)
### Disable test mode
max7219.put(int("111100000000",2), 16)
### Disable shutdown mode
max7219.put(int("110000000001",2), 16)
# run_demo(max7219)
# run_coordinate_setter(max7219)
run_pulse(max7219, delay=0.1, max_intensity=6)
### Enable shutdown mode
max7219.put(int("110000000000",2), 16)
if __name__ == '__main__':
main()