-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pump.py
42 lines (33 loc) · 1.16 KB
/
Pump.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
from machine import PWM, Pin
import ujson
class Pump:
def __init__(self, data):
self.index = str(data["index"])
self.serial_no = data["serial_no"]
self.en = Pin(int(data["en"]), Pin.OUT)
self.gpio0 = Pin(int(data["gpio0"]), Pin.OUT)
self.gpio1 = Pin(int(data["gpio1"]), Pin.OUT)
self.gpio0.on()
self.type = data["type"]
self.pwm = PWM(self.en)
self.pwm.freq(100)
self.state = "off"
self.response_dict = {"serial_no": str(self.serial_no), "index": str(self.index), "type": self.type,
"state": "off"}
def set_state(self, state):
if self.state != state:
if state == "on":
self.turn_on()
elif state == "off":
self.turn_off()
return self.response_dict
def turn_on(self):
self.pwm.duty_u16(65025)
self.state = "on"
self.update_response_dict("on")
def turn_off(self):
self.pwm.duty_u16(0)
self.state = "off"
self.update_response_dict("off")
def update_response_dict(self, state):
self.response_dict["state"] = state