refactor: make relay a class
This commit is contained in:
32
relay.py
32
relay.py
@@ -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():
|
||||
GPIO.setmode(GPIO.BOARD)
|
||||
GPIO.setup(PIN, GPIO.OUT, initial=GPIO.HIGH)
|
||||
def setup(self):
|
||||
GPIO.setmode(GPIO.BOARD)
|
||||
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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user