|
25 | 25 | from pydos_ui import input |
26 | 26 | except: |
27 | 27 | pass |
| 28 | +try: |
| 29 | + from pydos_hw import Pydos_hw |
| 30 | +except: |
| 31 | + Pydos_hw.sndPin = None |
| 32 | + |
28 | 33 | if implementation.name.upper() == 'MICROPYTHON': |
29 | | - from machine import Pin, PWM |
| 34 | + if Pydos_hw.sndPin: |
| 35 | + from machine import PWM |
30 | 36 | from time import ticks_ms as monotonic |
| 37 | + |
31 | 38 | elif implementation.name.upper() == 'CIRCUITPYTHON': |
32 | 39 | from time import monotonic |
33 | 40 | from board import board_id |
34 | | - if board_id != 'raspberrypi_zero2w': # temporary? until broadcom port supports pwmio |
35 | | - from pwmio import PWMOut |
36 | | - if board_id == "arduino_nano_rp2040_connect": |
37 | | - #A5 is GPIO D19 on Nano Connect |
38 | | - from board import A5 as sndPin |
39 | | - elif board_id == "raspberry_pi_pico": |
40 | | - #D12 is GP11 on the Raspberry PICO |
41 | | - from board import GP11 as sndPin |
42 | | - elif board_id == "cytron_maker_pi_rp2040": |
43 | | - from board import GP22 as sndPin |
44 | | - else: |
45 | | - try: |
46 | | - #Use D12 on Feathers |
47 | | - from board import D12 as sndPin |
48 | | - except: |
49 | | - pass |
| 41 | + if Pydos_hw.sndPin: |
| 42 | + if board_id != 'raspberrypi_zero2w': # temporary? until broadcom port supports pwmio |
| 43 | + from pwmio import PWMOut |
50 | 44 | else: |
51 | 45 | import winsound |
52 | 46 | from time import monotonic |
| 47 | + |
53 | 48 | import gc |
54 | 49 | gc.collect() |
55 | | -#if implementation.name.upper() == 'MICROPYTHON': |
56 | | - #gc.threshold(gc.mem_free() // 4 + gc.mem_alloc()) |
57 | 50 |
|
58 | 51 | """Implements a BASIC array, which may have up |
59 | 52 | to three dimensions of fixed size. |
@@ -129,10 +122,11 @@ def __init__(self): |
129 | 122 | self.__file_handles = {} |
130 | 123 |
|
131 | 124 | if implementation.name.upper() == 'MICROPYTHON': |
132 | | - try: |
133 | | - self.__pwm = PWM(Pin(19),freq=0) |
134 | | - except: |
135 | | - self.__pwm = PWM(Pin(19)) |
| 125 | + if Pydos_hw.sndPin: |
| 126 | + try: |
| 127 | + self.__pwm = PWM(Pydos_hw.sndPin,freq=0) |
| 128 | + except: |
| 129 | + self.__pwm = PWM(Pydos_hw.sndPin) |
136 | 130 |
|
137 | 131 | def parse(self, tokenlist, line_number, cstmt_number, infile, tmpfile, datastmts): |
138 | 132 | """Must be initialised with the list of |
@@ -894,23 +888,26 @@ def __soundstmt(self): |
894 | 888 | volume = 800 |
895 | 889 |
|
896 | 890 | if implementation.name.upper() == 'MICROPYTHON': |
897 | | - self.__pwm.freq(freq) |
898 | | - if "duty_u16" in dir(self.__pwm): |
899 | | - self.__pwm.duty_u16(volume) |
900 | | - sleep(duration/18.2) |
901 | | - self.__pwm.duty_u16(0) |
902 | | - else: |
903 | | - self.__pwm.duty(int((volume/65535)*1023)) |
904 | | - sleep(duration/18.2) |
905 | | - self.__pwm.duty(0) |
| 891 | + if Pydos_hw.sndPin: |
| 892 | + self.__pwm.freq(freq) |
| 893 | + if "duty_u16" in dir(self.__pwm): |
| 894 | + self.__pwm.duty_u16(volume) |
| 895 | + sleep(duration/18.2) |
| 896 | + self.__pwm.duty_u16(0) |
| 897 | + else: |
| 898 | + self.__pwm.duty(int((volume/65535)*1023)) |
| 899 | + sleep(duration/18.2) |
| 900 | + self.__pwm.duty(0) |
906 | 901 | elif implementation.name.upper() == 'CIRCUITPYTHON': |
907 | 902 | try: |
908 | | - audioPin = PWMOut(sndPin, duty_cycle=0, frequency=440, variable_frequency=True) |
| 903 | + Pydos_hw.sndGPIO.deinit() # Workaround for ESP32-S2 GPIO issue |
| 904 | + audioPin = PWMOut(Pydos_hw.sndPin, duty_cycle=0, frequency=440, variable_frequency=True) |
909 | 905 | audioPin.frequency = freq |
910 | 906 | audioPin.duty_cycle = volume |
911 | 907 | sleep(duration/18.2) |
912 | 908 | audioPin.duty_cycle = 0 |
913 | 909 | audioPin.deinit() |
| 910 | + Pydos_hw.quietSnd() # Workaround for ESP32-S2 GPIO issue |
914 | 911 | except: |
915 | 912 | pass |
916 | 913 | else: |
|
0 commit comments