Skip to content

Instantly share code, notes, and snippets.

@jbaiter
Created May 1, 2014 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbaiter/11460130 to your computer and use it in GitHub Desktop.
Save jbaiter/11460130 to your computer and use it in GitHub Desktop.
import logging
import threading
import time
import RPi.GPIO as GPIO
from spreads.plugin import HookPlugin, TriggerHooksMixin
from spreads.util import DeviceException
class GpioTrigger(HookPlugin, TriggerHooksMixin):
__name__ = 'gpiotrigger'
_loop_thread = None
_exit_event = None
def configuration_template(cls):
return { 'pin_number': PluginOption(
value=17, docstring="GPIO Pin to watch")}
def __init__(self, config):
self._logger = logging.getLogger('spreadsplug.gpiotrigger')
self._logger.debug("Initializing GpioTrigger plugin")
self._trigger_pin = config['pin_number'].get(int)
def start_trigger_loop(self, capture_callback):
GPIO.setmode(GPIO.BCM)
GPIO.setup(buttonPin,GPIO.IN)
self._exit_event = threading.Event()
self._loop_thread = threading.Thread(target=self._trigger_loop,
args=(capture_callback, ))
self._logger.debug("Starting trigger loop")
self._loop_thread.start()
def stop_trigger_loop(self):
if self._exit_event is None:
# Return if no loop thread is running
return
self._logger.debug("Stopping trigger loop")
self._exit_event.set()
self._loop_thread.join()
def _trigger_loop(self, capture_func):
# Polls all attached HID devices for a press->release event and
# trigger a capture.
while not self._exit_event.is_set():
if (GPIO.input(self._trigger_pin)):
capture_func()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment