def main(addon): badge.show_bitmap('drivers/assets/speaker.bmp') audio = PWMOut(board.GPIO1, duty_cycle=0, frequency=440, variable_frequency=True) led = digitalio.DigitalInOut(board.GPIO2) led.switch_to_output(True) try: for (notename, eigths) in sequence: length = eigths * 0.1 if notename: led.value = False audio.frequency = round(note(notename)) audio.duty_cycle = 0x8000 time.sleep(length) led.value = True audio.duty_cycle = 0 time.sleep(0.025) time.sleep(3) finally: led.deinit() audio.deinit()
class PWMOut(object): """PWM output.""" def __init__(self, pin, freq=5000, duty=0): self._pID = pin self._pin = PWMOut_(pin, frequency=int(freq), duty_cycle=duty) def deinit(self): self._pin.deinit() @property def duty_percent(self): """ duty in percent """ return self._pin.duty_cycle /MAX_DUTY *100 @duty_percent.setter def duty_percent(self, value): self._pin.duty_cycle = int(min(max(0, value/100.0 *MAX_DUTY), MAX_DUTY)) @property def duty(self): """ duty as raw value """ return self._pin.duty_cycle @duty.setter def duty(self, value): self._pin.duty_cycle = int(value) @property def freq_Hz(self): """ frequency in [Hz] """ return self._pin.frequency @freq_Hz.setter def freq_Hz(self, value): # Frequency cannot be changed unless variable frequency option is set, # therefore here just recreate instance with same duty cycle but new # frequency d = self._pin.duty_cycle self._pin.deinit() self._pin = PWMOut_(self._pID, frequency=int(value), duty_cycle=d) @property def max_duty(self): return MAX_DUTY