forked from lionyhw/PlanetX_MicroPython
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathled.py
More file actions
46 lines (34 loc) · 1.01 KB
/
led.py
File metadata and controls
46 lines (34 loc) · 1.01 KB
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
from microbit import *
from enum import *
class LED(object):
"""基本描述
设置LED亮度模式
Args:
RJ_pin (pin): 连接端口
"""
def __init__(self, RJ_pin):
if RJ_pin == J1:
self.__pin = pin1
elif RJ_pin == J2:
self.__pin = pin2
elif RJ_pin == J3:
self.__pin = pin13
elif RJ_pin == J4:
self.__pin = pin15
def set_led(self, state, brightness=100):
"""基本描述
点亮或者熄灭LED
Args:
state (numbers): 1点亮 0熄灭
brightness (numbers): 亮度百分比,state为1时使能 0-100
"""
if state == 0:
self.__pin.write_analog(0)
elif state == 1:
brightness = ((brightness - 0) * (1023 - 0)) / (100 - 0) + 0;
self.__pin.write_analog(brightness)
else:
print("brightness error,must 0 <= brightness <= 100")
if __name__ == "__main__":
l = LED(J1)
l.set_led(1, 80)