From eeb5d544f672c1ab627166bf3c58c55ca7fe70de Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Thu, 14 Mar 2024 14:15:43 +0800 Subject: [PATCH] refactor: make relay a class --- relay.py | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/relay.py b/relay.py index 7e9ff55..4f5f4c0 100644 --- a/relay.py +++ b/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()