-
Notifications
You must be signed in to change notification settings - Fork 64
/
OtherDisplays.py
217 lines (181 loc) · 7.3 KB
/
OtherDisplays.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
207
208
209
210
211
212
213
214
215
216
217
"""
OtherDisplays.py - This file contains the implementation of other displays
than the basic 16x2 LCD display that comes with the basic kits. This will
allow us to reduce the number of imports in the main code and also allow
us to keep the number of files in the project to a minimum.
# Currently supports 4-digit seven-segment displays - both with a tm1637 backpack
# and without (using PIO) - PIO 7 seg only supports number
# Also supports MAX7219 Dot Matrix displays
"""
from Displays import *
from Log import *
import tm1637
import max7219
import rp2
from rp2 import PIO
class SevenSegmentDisplay(Display):
"""
Seven Segment Display class - implements a 4-digit seven segment display
Decimal points not supported - colon can be used when showing two numbers
"""
def __init__(self, clk=16, dio=17):
self._tm = tm1637.TM1637(clk=Pin(clk), dio=Pin(dio))
def reset(self):
""" clear the display screen """
self._tm.write([0, 0, 0, 0])
def showNumber(self, number):
""" show a single number """
self._tm.number(number)
def showNumbers(self, num1, num2, colon=True):
""" Show two numbers optionally separated by a colon by default, the colon is shown """
self._tm.numbers(num1, num2, colon)
def showText(self, text):
"""
Show a string - only first 4 characters will be shown
for anything bigger than 4 characters.
"""
self._tm.show(text)
def scroll(self, text, speed=250):
"""
Scroll a longer text - note that this will use a sleep
call to pause between movements.
"""
self._tm.scroll(text, speed)
class DotMatrixDisplay(Display):
"""
An implementation of the MAX7219 Dot Matrix display
Fairly simplistic implementation - tested only with simulator so far
"""
def __init__(self, sck=18, mosi=19, cs=17):
self._spi = SPI(0, baudrate=10000000, polarity=1, phase=0, sck=Pin(sck), mosi=Pin(mosi))
# Create matrix display instant, which has four MAX7219 devices.
self._dot = max7219.Matrix8x8(self._spi, Pin(cs, Pin.OUT), 4)
self._dot.brightness(10)
self.reset()
def reset(self):
#Clear the display.
self._dot.fill(0)
self._dot.show()
def showNumber(self, number, row = 0, col = 0):
self._dot.text(str(number), row, col, 1)
self._dot.show()
def showText(self, text, row = 0, col = 0):
self._dot.text(text, row, col, 1)
self._dot.show()
def scroll(self, text, speed=50):
#Get the message length
length = len(text)
column = (length * 8)
for x in range(32, -column, -1):
#Clear the display
self._dot.fill(0)
# Write the scrolling text in to frame buffer
self._dot.text(text,x,0,1)
#Show the display
self._dot.show()
#Set the Scrolling speed. Here it is 50mS.
time.sleep(speed/1000)
class MorseDisplay(Display):
"""
This is a fun class - implement a Morse code display
This can play the Morse code via a Buzzer, or show
the code via a light, or even display it in another
display.
We need to make sure the other display is not another
MorseDisplay though
"""
def __init__(self, buz=None, light=None, otherDisplay=None):
self._buz = buz
self._light = light
if isinstance(otherDisplay, OLEDDisplay) or isinstance(otherDisplay, LCDDisplay):
self._dp = otherDisplay
else:
self._dp = None
Log.e(f"Sorry, {type(otherDisplay).__name__} isn't supported")
self._morsedict = {'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.', 'G': '--.', 'H': '....',
'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---', 'P': '.--.',
'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-',
'Y': '-.--', 'Z': '--..', '0': '-----', '1': '.----', '2': '..---', '3': '...--', '4': '....-',
'5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.'}
def showNumber(self, number):
""" Just convert the number to a string and show it """
self.showText(self, str(number))
def showText(self, text):
""" Displaying a Morse decoded text on the buzzer, light and possibly display """
morse = self._decodeText(text)
if self._dp is not None:
self._dp.showText(morse)
# go through the string each character at a time
for char in morse:
# if the character is in our dictionary, then get the code
if char == ".":
self._displaydida(100)
elif char == "-":
self._displaydida(250)
else:
time.sleep_ms(250)
####### Local private methods in MorseDisplay - should not be used from outside ####
def _decodeText(self, text):
morse = []
for char in text.upper():
if char in self._morsedict:
morse.append(self._morsedict[char])
elif char.isspace():
morse.append(' ')
return " ".join(morse)
def _displaydida(self, sleeptime):
if self._buz is not None:
self._buz.play()
if self._light is not None:
self._light.on()
time.sleep_ms(sleeptime)
if self._buz is not None:
self._buz.stop()
if self._light is not None:
self._light.off()
time.sleep_ms(100)
class SevenSegmentDisplayRaw(Display):
"""
A Raw 7 segment display that uses RPi PIO along with internal StateMachine
to poll 4 digits into the display. All digits are shown always so there will be
leading zeros for numbers under 4 digits
"""
def __init__(self, pinstart=2, digstart=10):
self._digits = [
0b11000000, # 0
0b11111001, # 1
0b10100100, # 2
0b10110000, # 3
0b10011001, # 4
0b10010010, # 5
0b10000010, # 6
0b11111000, # 7
0b10000000, # 8
0b10011000, # 9
]
self._sm = rp2.StateMachine(0, sevseg, freq=2000, out_base=Pin(pinstart), sideset_base=Pin(digstart))
self._sm.active(1)
def _segmentize(self, num):
return (
self._digits[num % 10] | self._digits[num // 10 % 10] << 8
| self._digits[num // 100 % 10] << 16
| self._digits[num // 1000 % 10] << 24
)
def showNumber(self, n):
self._sm.put(self._segmentize(n))
def reset(self):
self._sm.put(0xFFFFFFFF)
# Internals used by the PIO state machine
# THIS IS REQUIRED FOR THE SEVEN SEG RAW class
@rp2.asm_pio(out_init=[PIO.OUT_LOW]*8, sideset_init=[PIO.OUT_LOW]*4)
def sevseg():
wrap_target()
label("0")
pull(noblock) .side(0) # 0
mov(x, osr) .side(0) # 1
out(pins, 8) .side(1) # 2
out(pins, 8) .side(2) # 3
out(pins, 8) .side(4) # 4
out(pins, 8) .side(8) # 5
jmp("0") .side(0) # 6
wrap()