Skip to content

Commit 2574d92

Browse files
authored
Merge pull request #2 from trevorwslee/develop
Develop
2 parents 9d8bdd1 + a358fd8 commit 2574d92

File tree

8 files changed

+90
-34
lines changed

8 files changed

+90
-34
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ MicroPythonDumbDisplay.iml
77

88
dumbdisplay_experimental
99

10-
/_my_secret.py
1110
/_test.py
1211
/_dd.py
1312
/_test.ipynb

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# DumbDisplay MicroPython Library (v0.3.0)
22

3-
DumbDisplay MicroPython Library is a port of the Arduino DumbDisplay Library (https://github.com/trevorwslee/Arduino-DumbDisplay)
4-
for the DumbDisplay Android app -- https://play.google.com/store/apps/details?id=nobody.trevorlee.dumbdisplay
3+
DumbDisplay MicroPython Library is a port of the [DumbDisplay Arduino Library](https://github.com/trevorwslee/Arduino-DumbDisplay)
4+
for the [DumbDisplay Android app](https://play.google.com/store/apps/details?id=nobody.trevorlee.dumbdisplay)
55

6-
For a video introduction, please watch the YouTube video: Introducing DumbDisplay MicroPython Library --
7-
with ESP32, Raspberry Pi Pico, and Raspberry Pi Zero -- https://www.youtube.com/watch?v=KVU26FyXs5M
6+
For a video introduction, please watch the YouTube video: [Introducing DumbDisplay MicroPython Library --
7+
with ESP32, Raspberry Pi Pico, and Raspberry Pi Zero](https://www.youtube.com/watch?v=KVU26FyXs5M)
88

99
Although the porting is not complete, nevertheless, a large portion of DumbDisplay functionalities have been ported.
1010
Hopefully, this should already be helpful for friends that develop programs for microcontroller boards in Micro-Python.

_my_secret.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
WIFI_SSID="your wifi router ssid"
3+
WIFI_PWD="your wifi router password"

dumbdisplay/_ddimpl.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,21 @@ def setReconnectRCId(self, rc_id: str):
5858
self.reconnect_keep_alive_ms = 0
5959
def validateConnection(self) -> bool:
6060
#print("validateConnection")
61-
diff_ms = -1
61+
keep_alive_diff_ms = None
6262
need_reconnect = False
6363
if self.last_keep_alive_ms > 0:
6464
now = time.ticks_ms()
65-
diff_ms = now - self.last_keep_alive_ms
66-
if diff_ms > _RECONNECT_NO_KEEP_ALIVE_MS:
65+
keep_alive_diff_ms = now - self.last_keep_alive_ms
66+
if keep_alive_diff_ms > _RECONNECT_NO_KEEP_ALIVE_MS:
6767
need_reconnect = True
6868
if True:
6969
if need_reconnect:
7070
if self.reconnect_enabled:
71-
print("disconnected ... reconnecting ... ", self.reconnect_RC_id, diff_ms)
71+
# reconnecting not working yet
72+
print(f"disconnected {self.reconnect_RC_id} ... for {keep_alive_diff_ms} ms")
73+
#print("disconnected ... reconnecting ... ", self.reconnect_RC_id, diff_ms)
7274
else:
73-
print("disconnected")
75+
print(f"disconnected ... for {keep_alive_diff_ms} ms")
7476
if need_reconnect:
7577
self.reconnecting = True
7678
if need_reconnect and self.reconnect_enabled:
@@ -88,7 +90,7 @@ def validateConnection(self) -> bool:
8890
#_ConnectVersion = _ConnectVersion + 1;
8991
self.reconnect_keep_alive_ms = 0
9092
self.last_keep_alive_ms = time.ticks_ms()
91-
return not need_reconnect
93+
return (need_reconnect, keep_alive_diff_ms)
9294
def isReconnecting(self) -> bool:
9395
return self.reconnecting;
9496

@@ -152,7 +154,7 @@ def release(self):
152154
# pass
153155
# def switchDebugLed(self, on):
154156
# pass
155-
def onDetectedDisconnect(self):
157+
def onDetectedDisconnect(self, for_ms: int):
156158
pass
157159
def onSendCommandException(self, error):
158160
pass
@@ -327,8 +329,9 @@ def _checkForFeedback(self):
327329
except:
328330
pass
329331
def _readFeedback(self) -> str:
330-
if not self._validateConnection():
331-
self.onDetectedDisconnect()
332+
(need_reconnect, keep_alive_diff_ms) = self._validateConnection()
333+
if need_reconnect:
334+
self.onDetectedDisconnect(keep_alive_diff_ms)
332335
if not self._connected_iop.available():
333336
return None
334337
feedback = self._connected_iop.read()
@@ -343,11 +346,11 @@ def _validateConnection(self):
343346
now = time.ticks_ms()
344347
diff_ms = now - self.last_validate_ms
345348
if diff_ms >= validate_gap:
346-
res = self._connected_iop.validateConnection()
349+
(need_reconnect, keep_alive_diff_ms) = self._connected_iop.validateConnection()
347350
self.last_validate_ms = now
348351
else:
349-
res = True
350-
return res
352+
(need_reconnect, keep_alive_diff_ms) = (None, None)
353+
return (need_reconnect, keep_alive_diff_ms)
351354
def _setReconnectRCId(self, rc_id: str):
352355
if self._connected_iop:
353356
self._connected_iop.setReconnectRCId(rc_id)

dumbdisplay/_dumbdisplay.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ 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_when_failed_to_send_command: bool = True, reset_machine_if_detected_disconnect: bool = False):
49+
def __init__(self, io: DDInputOutput, reset_machine_when_failed_to_send_command: bool = True, reset_machine_if_detected_disconnect_for_s: int = 15):
5050
super().__init__(io)
5151
#self.debug_led = None
5252
self.reset_machine_when_failed_to_send_command = reset_machine_when_failed_to_send_command
53-
self.reset_machine_if_detected_disconnect = reset_machine_if_detected_disconnect # _DD_HAS_LED and len(sys.argv) != 0
53+
self.reset_machine_if_detected_disconnect_for_s = reset_machine_if_detected_disconnect_for_s # _DD_HAS_LED and len(sys.argv) != 0
5454

5555
# def debugSetup(self, debug_led_pin):
5656
# '''setup debug use flashing LED pin number'''
@@ -133,8 +133,8 @@ def notone(self):
133133
# self.debug_led.on()
134134
# else:
135135
# self.debug_led.off()
136-
def onDetectedDisconnect(self):
137-
if self.reset_machine_if_detected_disconnect:
136+
def onDetectedDisconnect(self, for_ms: int):
137+
if self.reset_machine_if_detected_disconnect_for_s and for_ms >= (1000 * self.reset_machine_if_detected_disconnect_for_s):
138138
print("xxxxxxxxx")
139139
print("xxx detected disconnection ==>")
140140
try:
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
import machine
3+
from machine import Pin
4+
@rp2.asm_pio(
5+
set_init=rp2.PIO.OUT_LOW,
6+
in_shiftdir=rp2.PIO.SHIFT_LEFT,
7+
out_shiftdir=rp2.PIO.SHIFT_LEFT,
8+
)
9+
def wave_prog():
10+
pull(block)
11+
mov(x, osr) # waveCount
12+
pull(block)
13+
label("loop")
14+
mov(y, osr) # halfWaveNumCycles
15+
set(pins, 1) # high
16+
label("high")
17+
jmp(y_dec, "high")
18+
mov(y, osr) # halfWaveNumCycles
19+
set(pins, 0) # low
20+
label("low")
21+
jmp(y_dec, "low")
22+
jmp(x_dec, "loop")
23+
sm = rp2.StateMachine(0, wave_prog, freq=1953125, set_base=Pin(5)) # the clock frequency of Raspberry Pi Pico is 125MHz; 1953125 is 125MHz / 64
24+
sm.active(1)
25+
def HWPlayTone(freq: int, duration: int):
26+
halfWaveNumCycles = round(1953125.0 / freq / 2) # count 1 cycle for jmp() ==> 1 cycle per half wave ==> 2 cycles per wave
27+
waveCount = round(duration * freq / 1000.0)
28+
sm.put(waveCount)
29+
sm.put(halfWaveNumCycles)
30+
31+
import time
32+
HWPlayTone(262, 1000) # Do
33+
time.sleep(1)
34+
HWPlayTone(294, 1000) # Re
35+
time.sleep(1)
36+
HWPlayTone(330, 1000) # Mi
37+
time.sleep(1)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
import time
3+
import rp2
4+
from machine import Pin
5+
@rp2.asm_pio(set_init=rp2.PIO.OUT_LOW)
6+
def pin_onoff():
7+
wrap_target()
8+
set(pins, 1) # high
9+
set(pins, 0) # low
10+
wrap()
11+
sm = rp2.StateMachine(0, pin_onoff, freq=4000, set_base=Pin(5))
12+
sm.active(1)
13+
time.sleep(1)
14+
sm.active(0)
15+
16+
# import machine
17+
# speaker = machine.Pin(5)
18+
# while True:
19+
# speaker.on()
20+
# speaker.off()

samples/melody/main.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44
from dumbdisplay.layer_lcd import LayerLcd
55

66
try:
7-
# https://docs.micropython.org/en/latest/library/rp2.html
8-
import machine
9-
if machine.unique_id() == b'\xe6aA\x04\x03,D+': # unique_id() is unique to my board
10-
SPEAKER_PIN = 5
11-
import time
7+
# REFERENCE: https://docs.micropython.org/en/latest/library/rp2.html
8+
SPEAKER_PIN = 5
129
import rp2
1310
from machine import Pin
1411
@rp2.asm_pio(
@@ -18,7 +15,7 @@
1815
)
1916
def wave_prog():
2017
pull(block)
21-
mov(x, osr) # waveCount
18+
mov(x, osr) # waveCount
2219
pull(block)
2320
label("loop")
2421
mov(y, osr) # halfWaveNumCycles
@@ -30,16 +27,13 @@ def wave_prog():
3027
label("low")
3128
jmp(y_dec, "low")
3229
jmp(x_dec, "loop")
33-
# set(x, 1)
34-
# mov(isr, x)
35-
# push()
36-
sm = rp2.StateMachine(0, wave_prog, freq=100000, set_base=Pin(SPEAKER_PIN))
30+
sm = rp2.StateMachine(0, wave_prog, freq=1953125, set_base=Pin(SPEAKER_PIN)) # the clock frequency of Raspberry Pi Pico is 125MHz; 1953125 is 125MHz / 64
3731
sm.active(1)
3832
def HWPlayTone(freq: int, duration: int):
39-
halfWaveNumCycles = round((100000.0 / 2) / freq) # 2 is the number of cycles per half wave
33+
halfWaveNumCycles = round(1953125.0 / freq / 2) # count 1 cycle for jmp() ==> 1 cycle per half wave ==> 2 cycles per wave
4034
waveCount = round(duration * freq / 1000.0)
4135
sm.put(waveCount)
42-
sm.put(halfWaveNumCycles) # 2 * (x / 10) == blink time
36+
sm.put(halfWaveNumCycles)
4337
except:
4438
print("*****")
4539
print("* No HWPlayTone")
@@ -183,7 +177,7 @@ def __init__(self):
183177

184178
dd.pinAutoPinLayers(
185179
AutoPin("V",
186-
AutoPin("H", self.playLayer, self.restartLayer, self.targetLayer),
180+
AutoPin("H", self.playLayer, self.restartLayer, self.targetLayer),
187181
self.lyricLayer).build(),
188182
0, 0, 9 * KEY_WIDTH, TOP_HEIGHT)
189183

0 commit comments

Comments
 (0)