refactor: make relay a class

This commit is contained in:
Justin Lin
2024-03-14 14:15:43 +08:00
parent d3c315f603
commit eeb5d544f6

View File

@@ -3,27 +3,29 @@ import RPi.GPIO as GPIO
from pathlib import Path
import time
# Config those variables
PIN = 12
class Relay:
def __init__(self, pin):
self.pin = pin
def setup():
def setup(self):
GPIO.setmode(GPIO.BOARD)
GPIO.setup(PIN, GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(self.pin, GPIO.OUT, initial=GPIO.HIGH)
def turnOn():
GPIO.output(PIN, GPIO.LOW)
def turn_on(self):
GPIO.output(self.pin, GPIO.LOW)
def turnOff():
GPIO.output(PIN, GPIO.HIGH)
def turn_off(self):
GPIO.output(self.pin, GPIO.HIGH)
def tearDown():
GPIO.cleanup(PIN)
def teardown(self):
GPIO.cleanup(self.pin)
if __name__ == '__main__':
setup()
turnOn()
relay = Relay(12)
relay.setup()
relay.turn_on()
time.sleep(5)
turnOff()
relay.turn_off()
time.sleep(5)
tearDown()
relay.teardown()