2022-11-19 18:52:01 +01:00
|
|
|
import atexit
|
|
|
|
try:
|
|
|
|
import RPi.GPIO as GPIO
|
|
|
|
GPIO.setmode(GPIO.BCM)
|
|
|
|
rpi = True
|
|
|
|
except:
|
|
|
|
rpi = False
|
|
|
|
|
|
|
|
|
|
|
|
class Actuator():
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class GPIOPin(Actuator):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.pin = (kwargs.pop('pin'))
|
|
|
|
self.initial = (kwargs.pop('initial'))
|
|
|
|
self.onstate = (kwargs.pop('onstate'))
|
|
|
|
self.offstate = (kwargs.pop('offstate'))
|
|
|
|
GPIO.setup(self.pin, GPIO.OUT, initial=self.initial)
|
|
|
|
atexit.register(self.deinit)
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def deinit(self):
|
|
|
|
# Be sure everything is off at exit
|
|
|
|
print('Cleaning before exit')
|
|
|
|
GPIO.output(self.pin, self.initial)
|
|
|
|
GPIO.cleanup()
|
|
|
|
|
|
|
|
def enable(self, enable=True):
|
|
|
|
GPIO.output(self.pin, self.onstate) if enable else self.disable()
|
|
|
|
|
|
|
|
def disable(self):
|
|
|
|
GPIO.output(self.pin, self.offstate)
|
|
|
|
|
|
|
|
class MockPIN(Actuator):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.pin = (kwargs.pop('pin'))
|
|
|
|
self.initial = (kwargs.pop('initial'))
|
|
|
|
self.onstate = (kwargs.pop('onstate'))
|
|
|
|
self.offstate = (kwargs.pop('offstate'))
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def enable(self, enable=True):
|
2022-11-19 21:42:54 +01:00
|
|
|
pass
|
|
|
|
# print('FAKE ENABLE') if enable else self.disable()
|
2022-11-19 18:52:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
def disable(self):
|
2022-11-19 21:42:54 +01:00
|
|
|
pass
|
|
|
|
# print('FAKE DISABLE')
|
2022-11-19 18:52:01 +01:00
|
|
|
|
|
|
|
actuators = {
|
2023-01-04 14:52:28 +01:00
|
|
|
'heater': (GPIOPin if rpi else MockPIN)(pin=22, initial=GPIO.HIGH, onstate=GPIO.LOW, offstate=GPIO.HIGH),
|
2022-11-19 18:52:01 +01:00
|
|
|
}
|