This repository has been archived by the owner on Apr 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
_pin.py
79 lines (60 loc) · 1.87 KB
/
_pin.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import RPi.GPIO as gpio
from time import time, sleep
import threading
gpio.setmode(gpio.BOARD)
gpio.setwarnings(False)
class blinker(threading.Thread):
thePin: int
on_duration: float
off_duration: float
_stop_event: threading.Event
def __init__(self, thePin: int, on: float, t: float):
super(blinker, self).__init__()
self.thePin = thePin
self._stop_event = threading.Event()
self.on_duration = on/1000
self.off_duration = (t - on)/1000
def run(self):
while not self.stopped():
gpio.output(self.thePin, gpio.HIGH)
sleep(self.on_duration)
gpio.output(self.thePin, gpio.LOW)
sleep(self.off_duration)
def stop(self):
self._stop_event.set()
def stopped(self):
return self._stop_event.is_set()
class pin:
num: int
def __del__(self) -> None:
gpio.cleanup(self.num)
class outpin(pin):
thread: blinker = None
num: int
def __init__(self, num: int, init=gpio.LOW):
self.num = num
gpio.setup(self.num, gpio.OUT, initial=init)
def set(self):
if not self.thread is None:
self.thread.stop()
gpio.output(self.num, gpio.HIGH)
def clear(self):
if not self.thread is None:
self.thread.stop()
gpio.output(self.num, gpio.LOW)
def blink(self, ondur: int, t: int):
if not self.thread is None:
self.thread.stop()
self.thread = blinker(self.num, ondur, t)
self.thread.start()
def __del__(self) -> None:
super(type(self), self).__del__()
if self.thread is not None:
self.thread.stop()
class inpin(pin):
num: int
def __init__(self, num: int) -> None:
self.num = num
gpio.setup(self.num, gpio.IN)
def get(self) -> bool:
return not bool(gpio.input(self.num))