44from dumbdisplay .layer_lcd import LayerLcd
55
66try :
7- from .piohwtone import HWPlayTone
7+ # https://docs.micropython.org/en/latest/library/rp2.html
8+ import time
9+ import rp2
10+ from machine import Pin
11+ @rp2 .asm_pio (
12+ set_init = rp2 .PIO .OUT_LOW ,
13+ in_shiftdir = rp2 .PIO .SHIFT_LEFT ,
14+ out_shiftdir = rp2 .PIO .SHIFT_LEFT ,
15+ )
16+ def wave_prog ():
17+ pull (block )
18+ mov (x , osr ) # waves
19+ pull (block )
20+ label ("loop" )
21+ mov (y , osr ) # wave half len # cycles
22+ set (pins , 1 ) # high
23+ label ("high" )
24+ jmp (y_dec , "high" )
25+ mov (y , osr ) # wave half len # cycles
26+ set (pins , 0 ) # low
27+ label ("low" )
28+ jmp (y_dec , "low" )
29+ jmp (x_dec , "loop" )
30+ set (x , 1 )
31+ mov (isr , x )
32+ push ()
33+ sm = rp2 .StateMachine (0 , wave_prog , freq = 10000 , set_base = Pin (15 ))
34+ def HWPlayToneBlocked (freq : int , duration : int ):
35+ halfWaveNumCycles = int (10000 / freq / 2 )
36+ waveCount = int (duration / freq / 2 )
37+ #print("halfWaveNumCycles", halfWaveNumCycles)
38+ #print("waveCount", waveCount)
39+ sm .active (1 )
40+ start_ms = time .ticks_ms ()
41+ sm .put (waveCount )
42+ sm .put (halfWaveNumCycles ) # 2 * (x / 10) == blink time
43+ res = sm .get ()
44+ taken_ms = time .ticks_ms () - start_ms
45+ #print(f"got result {res} in {taken_ms:.2} ms")
46+ sm .active (0 )
847except :
9- HWPlayTone = None
48+ print ("*****" )
49+ print ("* No HWPlayToneBlocked" )
50+ print ("*****" )
51+ HWPlayToneBlocked = None
52+
1053
1154Song = "G C E C E D C A G G C E C E D G E G E G E C G A C C A G G C E C E D C Z"
1255Octave = "0 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 0 0 0 1 1 1 1 1 1 Z"
@@ -68,8 +111,8 @@ def GetNoteFreq(octave, noteIdx):
68111
69112def PlayTone (freq : int , duration : int , playToSpeaker : bool ):
70113 if playToSpeaker :
71- if HWPlayTone :
72- HWPlayTone (freq , duration )
114+ if HWPlayToneBlocked :
115+ HWPlayToneBlocked (freq , duration )
73116 else :
74117 dd .tone (freq , duration )
75118 dd .sleep_ms (duration )
@@ -84,7 +127,7 @@ class MelodyApp:
84127
85128 def __init__ (self ):
86129 self .play = False
87- self .playToSpeaker = False
130+ self .playToSpeaker = HWPlayToneBlocked != None
88131 self .restart = False
89132 self .adhocFreq = - 1
90133
@@ -97,9 +140,12 @@ def __init__(self):
97140 self .setupKey (0 , i )
98141 self .setupKey (1 , 0 )
99142
100- self .playLayer = self .setupButton ("⏯" );
101- self .restartLayer = self .setupButton ("⏮" );
102- self .targetLayer = self .setupButton ("📱" );
143+ self .playLayer = self .setupButton ("⏯" )
144+ self .restartLayer = self .setupButton ("⏮" )
145+ self .targetLayer = self .setupButton ("📢" )
146+
147+ if not HWPlayToneBlocked :
148+ self .targetLayer .disabled ()
103149
104150 dd .pinAutoPinLayers (AutoPin ("H" , self .playLayer , self .restartLayer , self .targetLayer ).build (), 0 , 0 , 9 * WIDTH , TOP_HEIGHT )
105151
0 commit comments