From 854b701477263d4c44fa07b7949b5816af034e5f Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Thu, 14 Mar 2024 13:49:24 +0800 Subject: [PATCH] refactor: export functions for controlling relay --- relay.py | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/relay.py b/relay.py index d2558d0..7e9ff55 100644 --- a/relay.py +++ b/relay.py @@ -6,17 +6,24 @@ import time # Config those variables PIN = 12 -def main(): - time.sleep(5) - GPIO.output(PIN, GPIO.LOW) - time.sleep(5) - GPIO.output(PIN, GPIO.HIGH) - time.sleep(5) - GPIO.output(PIN, GPIO.LOW) - time.sleep(5) - GPIO.cleanup() - -if __name__ == '__main__': +def setup(): GPIO.setmode(GPIO.BOARD) GPIO.setup(PIN, GPIO.OUT, initial=GPIO.HIGH) - main() + +def turnOn(): + GPIO.output(PIN, GPIO.LOW) + +def turnOff(): + GPIO.output(PIN, GPIO.HIGH) + +def tearDown(): + GPIO.cleanup(PIN) + +if __name__ == '__main__': + setup() + turnOn() + time.sleep(5) + turnOff() + time.sleep(5) + tearDown() +