james1236 / buzzer_music

RPI Pico / Micropython library to play music through one or more buzzers, can automatically replace chords with fast arpeggios to simulate polyphony with a single buzzer. Music can be easily taken from onlinesequencer.net
MIT License
83 stars 17 forks source link

Incompatible with ESP8266 (pwm.duty) #8

Closed jose1711 closed 1 year ago

jose1711 commented 2 years ago

On Wemos D1 mini (ESP8266) I am getting:

Traceback (most recent call last):
  File "<stdin>", line 34, in <module>
  File "/lib/buzzer_music.py", line 263, in tick
AttributeError: 'PWM' object has no attribute 'duty_u16'

The lack of said attribute can be confirmed by:

from machine import Pin, PWM
p = PWM(Pin(12))
print(dir(p))
# returns ['__class__', 'deinit', 'duty', 'freq', 'init'] on ESP8266 platform

In order to make it work on ESP8266 while retaining Pico compatibility this can be done:

if hasattr(pwm, 'duty_u16'):
    pwm.duty_u16(self.duty)
else:
    pwm.duty(self.duty)

While testing with D1 mini buzzer shield (https://www.wemos.cc/en/latest/d1_mini_shield/buzzer.html) I also had to lower duty (set to 100) as the default value (2512) only produced almost inaudible clicks.

james1236 commented 1 year ago

Thank you, sorry for taking so long to get around to implementing this. I have not tested the committed code, so lets hope it doesn't break anything!