refactor: make relay a class
This commit is contained in:
30
relay.py
30
relay.py
@@ -3,27 +3,29 @@ import RPi.GPIO as GPIO
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import time
|
import time
|
||||||
|
|
||||||
# Config those variables
|
class Relay:
|
||||||
PIN = 12
|
def __init__(self, pin):
|
||||||
|
self.pin = pin
|
||||||
|
|
||||||
def setup():
|
def setup(self):
|
||||||
GPIO.setmode(GPIO.BOARD)
|
GPIO.setmode(GPIO.BOARD)
|
||||||
GPIO.setup(PIN, GPIO.OUT, initial=GPIO.HIGH)
|
GPIO.setup(self.pin, GPIO.OUT, initial=GPIO.HIGH)
|
||||||
|
|
||||||
def turnOn():
|
def turn_on(self):
|
||||||
GPIO.output(PIN, GPIO.LOW)
|
GPIO.output(self.pin, GPIO.LOW)
|
||||||
|
|
||||||
def turnOff():
|
def turn_off(self):
|
||||||
GPIO.output(PIN, GPIO.HIGH)
|
GPIO.output(self.pin, GPIO.HIGH)
|
||||||
|
|
||||||
def tearDown():
|
def teardown(self):
|
||||||
GPIO.cleanup(PIN)
|
GPIO.cleanup(self.pin)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
setup()
|
relay = Relay(12)
|
||||||
turnOn()
|
relay.setup()
|
||||||
|
relay.turn_on()
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
turnOff()
|
relay.turn_off()
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
tearDown()
|
relay.teardown()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user