Instantly share code, notes, and snippets.
Created
June 27, 2022 00:45
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save mcassiano/3daa892af018aecb0b708da9af58cab8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import RPi.GPIO as GPIO | |
from threading import Thread | |
from threading import Lock | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setwarnings(False) | |
class HumidifierController: | |
fan_button = 24 | |
led_button = 27 | |
fan_out = 23 | |
led_out = 22 | |
def __init__(self): | |
self.fan_button = 24 | |
self.led_button = 27 | |
self.fan_out = 23 | |
self.led_out = 22 | |
self.lock = Lock() | |
self.thread = Thread(target=self.run) | |
self.virtual_operation = None | |
self.virtual_operation_done = False | |
self.operation_time_started = None | |
self.is_fan_on = False | |
self.is_led_on = False | |
GPIO.setup(self.led_button, GPIO.IN, GPIO.PUD_UP) | |
GPIO.setup(self.fan_button, GPIO.IN, GPIO.PUD_UP) | |
GPIO.setup(self.fan_out, GPIO.OUT) | |
GPIO.setup(self.led_out, GPIO.OUT) | |
self.thread.start() | |
print('turning on led') | |
self.led_on() | |
time.sleep(4) | |
print('turning off led') | |
self.led_off() | |
def run(self): | |
try: | |
while True: | |
operation = self.virtual_operation | |
print(f'operation = {operation}') | |
if operation is not None: | |
print(f'operation = {operation}') | |
self.virtual_operation_done = False | |
if operation == 'led_off': | |
if self.operation_time_started == None: | |
self.operation_time_started = time.time() | |
GPIO.output(self.led_out, GPIO.LOW) | |
else: | |
elapsed_time = time.time() - self.operation_time_started | |
print(f'operation elapsed time: {elapsed_time}') | |
if elapsed_time >= 3: | |
GPIO.output(self.led_out, GPIO.HIGH) | |
self.operation_time_started = None | |
self.virtual_operation_done = True | |
if operation == 'led_on': | |
if self.operation_time_started == None: | |
self.operation_time_started = time.time() | |
GPIO.output(self.led_out, GPIO.LOW) | |
else: | |
elapsed_time = time.time() - self.operation_time_started | |
print(f'operation elapsed time: {elapsed_time}') | |
if elapsed_time >= 1: | |
GPIO.output(self.led_out, GPIO.HIGH) | |
self.operation_time_started = None | |
self.virtual_operation_done = True | |
if operation == 'fan_off': | |
if self.operation_time_started == None: | |
self.operation_time_started = time.time() | |
GPIO.output(self.led_out, GPIO.LOW) | |
else: | |
elapsed_time = time.time() - self.operation_time_started | |
if elapsed_time >= 1: | |
GPIO.output(self.led_out, GPIO.HIGH) | |
self.operation_time_started = None | |
self.virtual_operation_done = True | |
if operation == 'fan_on': | |
if self.operation_time_started == None: | |
self.operation_time_started = time.time() | |
GPIO.output(self.led_out, GPIO.LOW) | |
else: | |
elapsed_time = time.time() - self.operation_time_started | |
if elapsed_time >= 1: | |
GPIO.output(self.led_out, GPIO.HIGH) | |
self.operation_time_started = None | |
self.virtual_operation_done = True | |
else: | |
led_state = GPIO.input(self.led_button) | |
fan_state = GPIO.input(self.fan_button) | |
GPIO.output(self.led_out, led_state) | |
GPIO.output(self.fan_out, fan_state) | |
time.sleep(0.1) | |
except Exception as e: | |
print(e) | |
finally: | |
GPIO.cleanup() | |
def run_operation(self, operation): | |
print('LOCKING') | |
with self.lock: | |
self.virtual_operation = operation | |
while not self.virtual_operation_done: | |
print('still running operation') | |
continue | |
self.virtual_operation = None | |
def led_on(self): | |
self.run_operation('led_on') | |
def led_off(self): | |
self.run_operation('led_off') | |
def fan_on(self): | |
self.run_operation('fan_on') | |
def fan_off(self): | |
self.run_operation('fan_off') | |
if __name__ == "__main__": | |
controller = HumidifierController() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment