コード例 #1
0
 def add(self, chip):
     try:
         return self.chips[chip]
     except KeyError:
         if isinstance(chip, str):
             c = gpio.Chip(label=chip)
         else:
             c = gpio.Chip(num=chip)
         c = self.enter_context(c)
         return c
コード例 #2
0
ファイル: async_gpio.py プロジェクト: Arce11/vernePi
async def async_pin_listener(pin_to_listen):
    print(f"Starting pin listener - Thread: {threading.current_thread().name}")
    with asyncgpio.Chip(0) as c:
        in_ = c.line(pin_to_listen)
        with in_.monitor(asyncgpio.REQUEST_EVENT_FALLING_EDGE):
            async for event in in_:
                print("asdf")
コード例 #3
0
ファイル: line_echo_polled.py プロジェクト: uSpike/trio-gpio
async def main():
    async with anyio.create_task_group() as n:
        with gpio.Chip(0) as c:
            with c.line(19).open(direction=gpio.DIRECTION_OUTPUT) as out_, \
                    c.line(20).open(direction=gpio.DIRECTION_INPUT) as in_:
                await n.spawn(pling, out_)
                while True:
                    print(in_.value)
                    await anyio.sleep(0.3)
コード例 #4
0
async def main():
    async with anyio.create_task_group() as n:
        with gpio.Chip(0) as c:
            with c.line(19).open(direction=gpio.DIRECTION_OUTPUT) as out_:
                in_ = c.line(20)
                await n.spawn(pling, out_)
                with in_.monitor(gpio.REQUEST_EVENT_BOTH_EDGES):
                    async for e in in_:
                        print(e, "on" if e.value else "off", "at",
                              e.time.strftime("%H:%M:%S"))
コード例 #5
0
    async def set_pin_level(self, pin_watcher):
        """Set an output pin to a level based on the state of another pin that is being watched.
        Waits for initialization from pin_watcher. 

        Based on the state (True|False) of the pin being watched, sets the output of another pin to 0 or 1.

        Arguments::
            pin_watcher: an instance of the PinWatcher class, which represents the state of the pin being watched.
        """
        with pin_watcher.cancel_on_shutdown():
            # Trio checkpoint to facilitate checking on shutdown signals
            await trio.sleep(0)
            with gpio.Chip(label=self._chip_id) as chip:
                pin_in = chip.line(self._pin)
                with pin_in.open(
                        direction=gpio.DIRECTION_OUTPUT) as current_pin:
                    await pin_watcher.initialized.wait()
                    logger.info(
                        f'pin_watcher has been initialized, {self._name} monitoring on and off events'
                    )
                    logger.debug(
                        f'pin_watcher initial state in {self._name} is {pin_watcher.state}'
                    )
                    while True:
                        if pin_watcher.state:
                            current_pin.value = self._out_true
                            logger.debug(
                                f'in {self._name}, pin {self._pin} set to {self._out_true}'
                            )
                            pin_watcher.off_notify = event_to_watch = trio.Event(
                            )
                            await event_to_watch.wait()
                            if pin_watcher.shutting_down:
                                break
                        else:
                            current_pin.value = self._out_false
                            logger.debug(
                                f'in {self._name}, pin {self._pin} set to {self._out_false}'
                            )
                            pin_watcher.on_notify = event_to_watch = trio.Event(
                            )
                            await event_to_watch.wait()
                            if pin_watcher.shutting_down:
                                break
        logger.info(f'method set_pin_level in {self._name} has shutdown')
        if pin_watcher.shutting_down:
            return
コード例 #6
0
ファイル: push_button_event.py プロジェクト: uSpike/trio-gpio
 async def push(self):
     with gpio.Chip(0) as c:
         in_ = c.line(self.y)
         with in_.monitor(gpio.REQUEST_EVENT_RISING_EDGE):
             last = 0
             async for e in in_:
                 # This section is for debouncing the button.
                 # As a button is pushed and released the voltage can rapidly go up and down many times
                 # when the user only meant one push. To limit this, a delay is add to ignore changes.
                 # This can be adjusted depending on the button and the respose.
                 secs, ns_secs = e.timestamp
                 now = float(str(secs) + '.' + str(ns_secs))
                 if now >= last + .25:
                     print('button', e.value, secs, ns_secs, now)
                     if self._on.is_set():
                         await self._off.set()
                     else:
                         await self._on.set()
                 last = now
コード例 #7
0
 async def _pin_monitor(self):
     """Monitor a pin and notify any functions and/or class instances that are waiting for events to be set when state changes.
     """
     with self.cancel_on_shutdown():
         # Trio checkpoint to facilitate checking on shutdown signals
         await trio.sleep(0)
         with gpio.Chip(label=self._chip_id) as chip:
             pin_in = chip.line(self._pin)
             with pin_in.open(
                     direction=gpio.DIRECTION_INPUT) as current_pin:
                 self._current_state = True if current_pin.value else False
                 logger.debug(
                     f'---- pin {self._pin} initial state is {self._current_state} ----'
                 )
                 # Notify dependent class instances and functions that monitoring has started
                 self.initialized.set()
                 logger.info(f'_pin_monitor has started')
                 # Trio checkpoint so that waiting lists are ready
                 await trio.sleep(0)
                 if self._current_state:
                     self._notify_on_waiting()
                 else:
                     self._notify_off_waiting()
             with pin_in.monitor(gpio.REQUEST_EVENT_BOTH_EDGES):
                 async for pin_event in pin_in:
                     if pin_event.value:
                         logger.debug(f'---- pin {self._pin} is true ----')
                         self._current_state = True
                         self._notify_on_waiting()
                     else:
                         logger.debug(f'---- pin {self._pin} is false ----')
                         self._current_state = False
                         self._notify_off_waiting()
     logger.info(f'pin_monitor has shutdown')
     if self.shutting_down:
         return
コード例 #8
0
import asyncgpio as gpio
import time
"""Flash an output manually.

On the Pi3, control the LEDs thus:
# cd /sys/class/leds/led0 ## or /led1
# echo gpio >trigger
# echo 16 >gpio

Enjoy.

NB: The red LED can go much faster. Have fun.
The green LED cannot (limited by hardware,
so that you can still see very fast flashes)

"""
if __name__ == "__main__":
    with gpio.Chip(0) as c:
        with c.line(16).open(gpio.DIRECTION_OUTPUT) as l:

            try:
                while True:
                    l.value = 1
                    time.sleep(0.1)
                    l.value = 0
                    time.sleep(0.1)
            finally:
                l.value = 0