forked from malcolmholmes/pico-clock-green-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeaker.py
More file actions
30 lines (24 loc) · 753 Bytes
/
speaker.py
File metadata and controls
30 lines (24 loc) · 753 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from machine import Pin, Timer
import time
from util import singleton
@singleton
class Speaker:
def __init__(self, scheduler):
self.buzz = Pin(14, Pin.OUT)
self.buzz.value(0)
self.buzz_start = 0
self.duration = 0
scheduler.schedule("beeps", 1, self.beep_callback)
def beep(self, duration):
self.buzz.value(1)
self.buzz_start = time.ticks_ms()
self.duration = duration
def beep_off(self, t):
self.buzz.value(0)
self.duration = 0
self.buzz_start = 0
def beep_callback(self, t):
if self.buzz_start != 0:
tm = time.ticks_ms()
if time.ticks_diff(tm, self.buzz_start) > self.duration:
self.beep_off(t)