def initialize_sensors(): tsl = TSL2561() print "[*] Light sensor is initialized" pin = 1 pir = OnionGpio(pin) pir_status = pir.setInputDirection() print "[*] PIR sensor is initialized: ", pir_status return tsl, pir
def __init__(self, enable: int, sleep: int, step: int, dir: int, ignore_busy: bool = False) -> None: """init with gpio pins for the enable, sleep, step and direction pin and if they should be used even if they are exported by another program""" self.gpio_enable = OnionGpio(enable, ignore_busy=ignore_busy) self.gpio_enable.setDirection(Direction.OUTPUT_HIGH) # disable driver self.gpio_sleep = OnionGpio(sleep, ignore_busy=ignore_busy) self.gpio_sleep.setDirection(Direction.OUTPUT_HIGH) # do not sleep self.gpio_step = OnionGpio(step, ignore_busy=ignore_busy) self.gpio_step.setDirection(Direction.OUTPUT_LOW) self.gpio_dir = OnionGpio(dir, ignore_busy=ignore_busy) self.gpio_dir.setDirection(Direction.OUTPUT_LOW)
## EXAMPLE CODE # Set a GPIO to output, and alternate the output between LOW and HIGH every 5 seconds import time from onionGpio import OnionGpio, Value, Direction gpioNum = 1 with OnionGpio(gpioNum) as gpioObj: # set to output gpioObj.setDirection( Direction.OUTPUT_LOW) # initialize the GPIO to 0 (LOW) # alternate the value value = Value.LOW while True: # reverse the value if value == Value.LOW: value = Value.HIGH else: value = Value.LOW # set the new value gpioObj.setValue(value) print('GPIO%d set to: %s' % (gpioNum, value.name)) time.sleep(5)
from onionGpio import OnionGpio, ActiveLow, Direction, Value print('> Instantiating gpio object') with OnionGpio(14) as gpio14: print('> Set active-high') gpio14.setActiveLow(ActiveLow.HIGH) print('> Get active-low: ', gpio14.getActiveLow()) print('> Set direction to input...') gpio14.setDirection(Direction.INPUT) print('> Get direction: ', gpio14.getDirection()) print('> Read value: ', gpio14.getValue()) input('Ready to test output?') print('> Set direction to output...') gpio14.setDirection(Direction.OUTPUT) print('> Get direction: ', gpio14.getDirection()) print('> Read value: ', gpio14.getValue()) print('> Set value to 1...') gpio14.setValue(Value.HIGH) print('> Read value: ', gpio14.getValue())
else: mqtt_client.username_pw_set(username, config.get("mqtt", "password")) with ExitStack() as stack: # setup elevator elevator = stack.enter_context( OnionPwm( config.getint("elevator", "channel"), config.getint("elevator", "chip"), )) elevator.set_frequency(config.getint("elevator", "frequency")) elevator.set_duty_cycle(config.getfloat("elevator", "duty_cycle")) # setup landing_zone landing_zone = stack.enter_context( OnionGpio(config.getint("landing_zone", "gpio"))) landing_zone.setDirection(Direction.INPUT) landing_zone.setEdge(Edge.FALLING) # prepare for edge # setup driver driver = stack.enter_context( A4988(config.getint("driver", "enable"), config.getint("driver", "sleep"), config.getint("driver", "step"), config.getint("driver", "dir"))) # setup ramp ramp = Ramp( WormMotor(driver, Value(config.get("motor", "direction")), config.getfloat("motor", "step_width"), config.getfloat("motor", "pps"), config.getfloat("motor", "limit_lower"),
class A4988: """Class implementing the A4988 support""" def __init__(self, enable: int, sleep: int, step: int, dir: int, ignore_busy: bool = False) -> None: """init with gpio pins for the enable, sleep, step and direction pin and if they should be used even if they are exported by another program""" self.gpio_enable = OnionGpio(enable, ignore_busy=ignore_busy) self.gpio_enable.setDirection(Direction.OUTPUT_HIGH) # disable driver self.gpio_sleep = OnionGpio(sleep, ignore_busy=ignore_busy) self.gpio_sleep.setDirection(Direction.OUTPUT_HIGH) # do not sleep self.gpio_step = OnionGpio(step, ignore_busy=ignore_busy) self.gpio_step.setDirection(Direction.OUTPUT_LOW) self.gpio_dir = OnionGpio(dir, ignore_busy=ignore_busy) self.gpio_dir.setDirection(Direction.OUTPUT_LOW) def __enter__(self): return self def __exit__(self, type, value, traceback): self.shutdown() return False # we dont handle exceptions def shutdown(self) -> None: """shutdown driver and gpio""" self.sleep() # save energy self.disable() # prevent motor from overheating self.gpio_enable.release() self.gpio_sleep.release() self.gpio_step.release() self.gpio_dir.release() def enable(self) -> None: """enable driver""" self.gpio_enable.setValue(Value.LOW) def disable(self) -> None: """disable driver""" self.gpio_enable.setValue(Value.HIGH) def is_enabled(self) -> bool: """return if the driver is enabled""" return self.gpio_enable.getValue() is Value.LOW def sleep(self) -> None: """set the driver to sleep mode""" self.gpio_sleep.setValue(Value.LOW) def wake(self) -> None: """wake driver from sleep mode""" self.gpio_sleep.setValue(Value.HIGH) time.sleep(WAKE_SLEEP) # allow driver to wake up def is_sleeping(self) -> bool: """return if the driver is in sleep mode""" return self.gpio_sleep.getValue() is Value.LOW def set_direction(self, direction: Value) -> None: """set direction of the driver""" self.gpio_dir.setValue(direction) def get_direction(self) -> Value: """get direction of the driver""" return self.gpio_dir.getValue() def step(self) -> None: """make the motor do a step""" self.gpio_step.setValue(Value.HIGH) time.sleep(STEP_SLEEP) self.gpio_step.setValue(Value.LOW) time.sleep(STEP_SLEEP)