class DigitalPiDevice: """ Digital modulated devices in combination with Raspberry Pi GPIO Setup: https://gpiozero.readthedocs.io/en/stable/remote_gpio.html """ def __init__(self, PIN, BOARD_IP: str = None): """ :param BOARD_IP: IP adress of board connected to the Device """ if BOARD_IP is not None: self._factory = PiGPIOFactory(host=BOARD_IP) self._device = DigitalOutputDevice(PIN, pin_factory=self._factory) else: self._factory = None self._device = DigitalOutputDevice(PIN) self._running = False def turn_on(self): self._device.on() self._running = True def turn_off(self): self._device.off() self._running = False def toggle(self): self._device.toggle() self._running = self._device.is_active
class Relay(): "Simple wrapper for a power relay" def __init__(self, pin: int): self.device = DigitalOutputDevice(pin) @property def value(self) -> bool: return bool(self.device.value) def set(self) -> None: self.device.on() def unset(self) -> None: self.device.off() def toggle(self) -> None: self.device.toggle()
# direction = DigitalOutputDevice(27) # # speed_control = PWMOutputDevice(17, frequency=1000) motor_enable = DigitalOutputDevice(13) direction = DigitalOutputDevice(6) speed_control = PWMOutputDevice(5, frequency=1000) speed_control.value = 0.0 # speed = SmoothedInputDevice(18) while True: key = input() if key == ' ': motor_enable.toggle() elif key == 'd': direction.toggle() elif key == 'f': speed_control.value = min(1, speed_control.value + 0.1) elif key == 's': speed_control.value = max(0, speed_control.value - 0.1) else: print(f"unknown key {key}") print( f"Enabled: {motor_enable.is_active}, Forward: {direction.is_active}, Speed Set: {speed_control.value}" )
from gpiozero.pins.native import NativeFactory from gpiozero import Device, DigitalOutputDevice from time import sleep Device.pin_factory = NativeFactory() relay = DigitalOutputDevice(5) while True: relay.toggle() sleep(1)