-
Notifications
You must be signed in to change notification settings - Fork 0
/
led2.py
56 lines (46 loc) · 1.31 KB
/
led2.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import machine
from machine import Pin, PWM, Timer, WDT
class LED:
def __init__(self,pin,strong=100,pwm=True):
self.pin = pin
self.strong=strong
self.state = False
self.timer = None
self.isBlink = False
if pwm:
self.pwm = PWM(Pin(pin))
self.pwm.freq(1024)
self.pwm.duty(0)
self.off()
def on(self,strong=-1):
self.state = True
if not strong == -1:
self.strong= strong
self.pwm.duty(self.strong)
def off(self):
self.state = False
self.pwm.duty(0)
def run(self,n):
if self.isBlink:
if self.state:
self.off()
else:
self.on()
def blink(self,peroid):
_p_ = int(peroid*1000)
if _p_ == 0:
self.isBlink = False
self.off()
return
else:
self.isBlink = True
self.on()
if self.timer == None:
self.timer = Timer(1)
self.timer.init(period=_p_, mode=Timer.PERIODIC, callback=self.run)
else:
self.timer.deinit()
self.timer = Timer(1)
self.timer.init(period=_p_, mode=Timer.PERIODIC, callback=self.run)
led = LED(2)
led.on(100)