Skip to content

Commit bd8d884

Browse files
committed
going for v0.3.0
1 parent d4517aa commit bd8d884

3 files changed

Lines changed: 82 additions & 27 deletions

File tree

dumbdisplay/_ddlayer_graphical.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def drawChar(self, x, y, char, color, bg_color = "", size = 0):
4646
def drawStr(self, x, y, string, color, bg_color = "", size = 0):
4747
'''
4848
:param bg_color: "" means default
49-
:param size: 0 means defajult
49+
:param size: 0 means default
5050
'''
5151
self.dd._sendCommand(self.layer_id, "drawstr", str(x), str(y), _DD_COLOR_ARG(color), _DD_COLOR_ARG(bg_color), str(size), string)
5252
def drawPixel(self, x, y, color):

dumbdisplay/_dumbdisplay.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ class DumbDisplay(DumbDisplayImpl):
4646
@staticmethod
4747
def runningWithMicropython():
4848
return hasattr(sys, 'implementation') and sys.implementation.name == 'micropython'
49-
def __init__(self, io: DDInputOutput, reset_machine_if_detected_disconnect: bool = True):
49+
def __init__(self, io: DDInputOutput, reset_machine_when_failed_to_send_command: bool = True, reset_machine_if_detected_disconnect: bool = False):
5050
super().__init__(io)
5151
#self.debug_led = None
52+
self.reset_machine_when_failed_to_send_command = reset_machine_when_failed_to_send_command
5253
self.reset_machine_if_detected_disconnect = reset_machine_if_detected_disconnect # _DD_HAS_LED and len(sys.argv) != 0
5354

5455
# def debugSetup(self, debug_led_pin):
@@ -89,6 +90,11 @@ def playbackLayerSetupCommands(self, layerSetupPersistId: str):
8990
self._sendCommand(None, "SAVEC", layerSetupPersistId, _DD_BOOL_ARG(True))
9091
self._sendCommand(None, "PLAYC")
9192
self._setReconnectRCId(layerSetupPersistId)
93+
def recordLayerCommands(self):
94+
self._connect()
95+
self._sendCommand(None, "RECC")
96+
def playbackLayerCommands(self):
97+
self._sendCommand(None, "PLAYC")
9298
def backgroundColor(self, color: str):
9399
'''set DD background color with common "color name"'''
94100
self._connect()
@@ -139,7 +145,15 @@ def onDetectedDisconnect(self):
139145
print("xxx x exit system")
140146
sys.exit()
141147
def onSendCommandException(self, error):
142-
print("xxx Error (send command) -- " + str(error) )
148+
print("xxx Error (send command) -- " + str(error))
149+
if self.reset_machine_when_failed_to_send_command:
150+
try:
151+
print("xxx x reset machine")
152+
import machine
153+
machine.reset()
154+
except:
155+
print("xxx x exit system")
156+
sys.exit()
143157
# if self.reset_machine_on_connection_error:
144158
# print("xxxxxxxxx")
145159
# print("xxx Error (send command) -- {}".format(error))

samples/melody/main.py

Lines changed: 65 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ def HWPlayToneBlocked(freq: int, duration: int):
8181
"1:lost", ],
8282
[
8383
"1:but",
84-
"1:now",
85-
"1:am",
84+
"2:now",
85+
"2:am",
8686
"1:found", ],
8787
[
8888
"1:Was",
@@ -171,8 +171,8 @@ def __init__(self):
171171
self.playToSpeaker = False
172172
self.restart = False
173173
self.adhocFreq = -1
174-
self.lyricX = -1
175-
self.lyricY = -1
174+
#self.lyricColIdx = -1
175+
#self.lyricRowIdx = -1
176176

177177
dd.recordLayerSetupCommands()
178178

@@ -186,10 +186,21 @@ def __init__(self):
186186
self.playLayer = self.setupButton("⏯")
187187
self.restartLayer = self.setupButton("⏮")
188188
self.targetLayer = self.setupButton("📢")
189-
self.lyricLayer = LayerLcd(dd, 40, 4)
189+
self.lyricLayer = LayerGraphical(dd, 260, 50)
190190
self.lyricLayer.margin(2)
191-
self.lyricLayer.border(1, "blue", "round")
192-
self.lyricLayer.writeCenteredLine("hello", 1)
191+
self.lyricLayer.border(2, "blue", "round")
192+
self.lyricLayer.backgroundColor("lightgray")
193+
self.lyricLayer.setTextColor("violet")
194+
if True:
195+
#self.lyricLayer.setTextSize(12)
196+
self.lyricLayer.print("hello ")
197+
self.lyricLayer.setTextColor("blue")
198+
#self.lyricLayer.setTextSize(16)
199+
self.lyricLayer.print("world ")
200+
# self.lyricLayer = LayerLcd(dd, 40, 4)
201+
# self.lyricLayer.margin(2)
202+
# self.lyricLayer.border(1, "blue", "round")
203+
# self.lyricLayer.writeCenteredLine("hello", 1)
193204

194205
if not HWPlayToneBlocked:
195206
self.targetLayer.disabled()
@@ -204,14 +215,20 @@ def __init__(self):
204215

205216
def run(self):
206217
while True:
207-
i = 0
218+
songIdx = 0
219+
targetLyricI = 0
220+
targetLyricSkip = 0
221+
lyricRowIdx = 0
222+
lyricColIdx = 0
208223
while True:
209224
dd.timeslice()
210225
if self.adhocFreq != -1:
211226
# key on DumbDisplay pressed ... play the note/tone of the key press
212227
if not self.play:
213228
self.lyricLayer.clear()
214-
self.lyricLayer.writeCenteredLine(f"🎵 {self.adhocFreq}", 1)
229+
self.lyricLayer.setCursor(0, 0)
230+
self.lyricLayer.setTextColor("blue")
231+
self.lyricLayer.print(f" 🎵 {self.adhocFreq}")
215232
PlayTone(self.adhocFreq, 200, self.playToSpeaker)
216233
self.adhocFreq = -1
217234
if self.restart:
@@ -221,15 +238,7 @@ def run(self):
221238
if not self.play:
222239
continue
223240

224-
if self.lyricX != -1 and self.lyricY != -1:
225-
lyeric1 = ""
226-
lyeric2 = ""
227-
lyerics1 = Lyrics[self.lyricY]
228-
for l1 in lyerics1:
229-
noteCount = int(l1[0:1])
230-
lyeric1 = lyeric1 + l1[2:] + " "
231-
self.lyricLayer.writeCenteredLine(lyeric1, 1)
232-
self.lyricLayer.writeCenteredLine(lyeric2, 2)
241+
i = songIdx * 2
233242
noteName = Song[i]
234243
if noteName == "Z":
235244
# reached end of song => break out of loop
@@ -244,11 +253,43 @@ def run(self):
244253
# get the how to to play the note/tone for
245254
duration = BeatSpeed * (ord(Beat[i]) - ord('0'))
246255

256+
# show the lyric
257+
dd.recordLayerCommands()
258+
self.lyricLayer.clear()
259+
self.lyricLayer.setCursor(0, 0)
260+
lyricRow = Lyrics[lyricRowIdx]
261+
for i, lyric in enumerate(lyricRow):
262+
if i == targetLyricI:
263+
noteCount = int(lyric[0:1])
264+
if (noteCount - targetLyricSkip) <= 1:
265+
# targetLyricI = targetLyricI + 1
266+
# targetLyricSkip = 0
267+
advance = True
268+
else:
269+
advance = False
270+
targetLyricSkip = targetLyricSkip + 1
271+
print(lyric[2:])
272+
lyric = " " + lyric[2:]
273+
self.lyricLayer.print(lyric)
274+
dd.playbackLayerCommands()
275+
if advance:
276+
if lyricColIdx < len(lyricRow) - 1:
277+
lyricColIdx = lyricColIdx + 1
278+
targetLyricI = targetLyricI + 1
279+
targetLyricSkip = 0
280+
else:
281+
lyricColIdx = 0
282+
targetLyricI = 0
283+
targetLyricSkip = 0
284+
if lyricRowIdx < len(Lyrics) - 1:
285+
lyricRowIdx = lyricRowIdx + 1
286+
else:
287+
lyricRowIdx = 0
288+
247289
# play the note/tone
248290
PlayTone(freq, duration, self.playToSpeaker)
249291

250-
# increment i by 2
251-
i = i + 2
292+
songIdx = songIdx + 1
252293

253294

254295
def setupKey(self, octaveOffset: int, noteIdx: int) -> LayerGraphical:
@@ -300,8 +341,8 @@ def feedbackHandler(self, layer, type, x, y):
300341
self.play = not self.play
301342
if self.play:
302343
self.playLayer.backgroundColor("lightgreen")
303-
self.lyricX = 0
304-
self.lyricY = 0
344+
#self.lyricColIdx = 0
345+
#self.lyricRowIdx = 0
305346
else:
306347
self.playLayer.noBackgroundColor()
307348
elif layer == self.targetLayer:
@@ -312,8 +353,8 @@ def feedbackHandler(self, layer, type, x, y):
312353
self.targetLayer.noBackgroundColor()
313354
elif layer == self.restartLayer:
314355
self.restart = True
315-
self.lyricX = 0
316-
self.lyricY = 0
356+
#self.lyricColIdx = 0
357+
#self.lyricRowIdx = 0
317358
else:
318359
octaveOffset = layer.octaveOffset
319360
noteIdx = layer.noteIdx

0 commit comments

Comments
 (0)