def add_event_detect(dio_number, callback): """ Wraps around the GPIO.add_event_detect function :param dio_number: DIO pin 0...5 :param callback: The function to call when the DIO triggers an IRQ. :return: None """ GPIO.add_event_detect(dio_number, GPIO.RISING, callback=callback)
def add_events(cb_dio0, cb_dio1, cb_dio2, cb_dio3, cb_dio4, cb_dio5, switch_cb=None): BOARD.add_event_detect(BOARD.DIO0, callback=cb_dio0) BOARD.add_event_detect(BOARD.DIO1, callback=cb_dio1) BOARD.add_event_detect(BOARD.DIO2, callback=cb_dio2) BOARD.add_event_detect(BOARD.DIO3, callback=cb_dio3) # the modtronix inAir9B does not expose DIO4 and DIO5 if switch_cb is not None: GPIO.add_event_detect(BOARD.SWITCH, GPIO.RISING, callback=switch_cb, bouncetime=300)
def enable_rx(self): """Enable RX, set up GPIO and add event detection.""" if self.tx_enabled: _LOGGER.error("TX is enabled, not enabling RX") return False if not self.rx_enabled: self.rx_enabled = True GPIO.setup(self.gpio, GPIO.IN) GPIO.add_event_detect(self.gpio, GPIO.BOTH) GPIO.add_event_callback(self.gpio, self.rx_callback) _LOGGER.debug("RX enabled") return True
def configGPIO(pin): if not settings['triggerActive']: if not settings['SIMULATE']: import CHIP_IO.GPIO as GPIO # Config GPIO pin for pull down # and detection of rising edge GPIO.cleanup(pin) GPIO.setup(pin, GPIO.IN, GPIO.PUD_DOWN) GPIO.add_event_detect(pin, GPIO.RISING) GPIO.add_event_callback(pin, S0Trigger) settings['triggerActive'] = True logMsg("Setting up S0-Logger on " + pin) else: logMsg("Trigger already active on " + pin)
def main(): logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s') GPIO.cleanup(pin) GPIO.setup(pin, GPIO.IN) # Add Callback for Both Edges using the add_event_detect() method GPIO.add_event_detect(pin, GPIO.FALLING, doorbell_handler) try: while (not time.sleep(5)): print("watching " + pin) except: GPIO.cleanup(pin)
def run(self): # GPIO.toggle_debug() try: # reset all ioutil.unexport_all() # GPIO.cleanup() -- broken? # led GPIO.setup(LED, GPIO.OUT) GPIO.output(LED, GPIO.LOW) # handset GPIO.setup(HANDSET, GPIO.IN, pull_up_down=GPIO.PUD_UP, initial=1) GPIO.add_event_detect(HANDSET, GPIO.BOTH, self.off_hook) # rotary dial GPIO.setup(ROT_ENGAGED, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.add_event_detect(ROT_ENGAGED, GPIO.BOTH, self.rotary_engaged) GPIO.setup(ROT_PULSE, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.add_event_detect(ROT_PULSE, GPIO.FALLING, self.count_pulse) except Exception as e: logging.exception("exception encountered") GPIO.output(LED, GPIO.LOW) # signal process has started successfully # busy loop while True: time.sleep(0.02)
def __init__(self): self.nom = '' # Pin configuration. self.LS = "XIO-P2" #left side self.RS = "XIO-P3" #right side self.TL = "XIO-P4" #top left self.MD = "XIO-P5" #top mid self.TR = "XIO-P6" #top right self.lookup = { "XIO-P2": "leftside", "XIO-P3": "rightside", "XIO-P4": "topleft", "XIO-P5": "mid", "XIO-P6": "topright" } # array makes setup a bit easier: self.buttons = [self.RS, self.LS, self.TL, self.MD, self.TR] for b in self.buttons: # Setup input for pin GPIO.setup(b, GPIO.IN) # Setup edge detects for pins. Rising and Both have different values for de-bounce time, for testing. GPIO.add_event_detect(b, GPIO.FALLING, self.fallcall, 300)
def __init__(self, pinA, pinB, button, callback): self.pinA = pinA self.pinB = pinB self.button = button self.callback = callback GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(self.pinA, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(self.pinB, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(self.button, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.add_event_detect(self.pinA, GPIO.FALLING, callback=self.switch_event) GPIO.add_event_detect(self.pinB, GPIO.FALLING, callback=self.switch_event) GPIO.add_event_detect(self.button, GPIO.BOTH, callback=self.button_event, bouncetime=200)
start = time.time() print "down " + channel bcount = bcount + 1 print "CALLBACK FALL " + channel def risecall(channel): print "CALLBACK RISE " + channel def bothcall(channel): print "CALLBACK BOTH " + channel # Setup edge detects for pins. Rising and Both have different values for de-bounce time, for testing. GPIO.add_event_detect(pinA, GPIO.FALLING, fallcall) GPIO.add_event_detect(pinB, GPIO.RISING, risecall, 200) GPIO.add_event_detect(pinC, GPIO.BOTH, bothcall, 400) GPIO.add_event_detect(pinD, GPIO.FALLING, fallcall) GPIO.add_event_detect(pinE, GPIO.FALLING, fallcall) GPIO.add_event_detect(pinF, GPIO.FALLING, fallcall) GPIO.add_event_detect(pinG, GPIO.FALLING, fallcall) GPIO.add_event_detect(pinH, GPIO.FALLING, fallcall) # make a loop try: while True: print "press a button..." time.sleep(1) except KeyboardInterrupt:
# VERIFY SIMPLE FUNCTIONALITY print "VERIFY SIMPLE FUNCTIONALITY" print "READING XIO-PI" GPIO.output("CSID0", GPIO.HIGH) print "HIGH", GPIO.input("XIO-P0") GPIO.output("CSID0", GPIO.LOW) print "LOW", GPIO.input("XIO-P0") # ============================================== # EDGE DETECTION - AP-EINT1 print "SETTING UP EDGE DETECTION ON AP-EINT1" GPIO.setup("AP-EINT1", GPIO.IN) GPIO.add_event_detect("AP-EINT1",GPIO.FALLING) print "VERIFYING EDGE DETECT" f = open("/sys/class/gpio/gpio193/edge","r") edge = f.read() f.close() print "EDGE: %s" % edge GPIO.remove_event_detect("AP-EINT1") # ============================================== # EDGE DETECTION - AP-EINT3 print "SETTING UP EDGE DETECTION ON AP-EINT3" GPIO.setup("AP-EINT3", GPIO.IN) GPIO.add_event_detect("AP-EINT3",GPIO.FALLING) print "VERIFYING EDGE DETECT"
GPIO.setup("XIO-P2", GPIO.IN) GPIO.setup("U14_31", GPIO.OUT) # CSID0 print("READING XIO-P2") GPIO.output("CSID0", GPIO.HIGH) assert(GPIO.input("XIO-P2") == GPIO.HIGH) GPIO.output("CSID0", GPIO.LOW) print "LOW", GPIO.input("GPIO1") assert(GPIO.input("GPIO1") == GPIO.LOW) # ============================================== # EDGE DETECTION - AP-EINT1 print("\nSETTING UP RISING EDGE DETECTION ON AP-EINT1") GPIO.setup("AP-EINT1", GPIO.IN) GPIO.add_event_detect("AP-EINT1", GPIO.RISING, myfuncallback) print("VERIFYING EDGE DETECT WAS SET PROPERLY") f = open("/sys/class/gpio/gpio193/edge", "r") edge = f.read() f.close() assert(edge == "rising\n") GPIO.remove_event_detect("AP-EINT1") # ============================================== # EDGE DETECTION - AP-EINT3 print("\nSETTING UP BOTH EDGE DETECTION ON AP-EINT3") GPIO.setup("AP-EINT3", GPIO.IN) GPIO.add_event_detect("AP-EINT3", GPIO.BOTH, myfuncallback) print("VERIFYING EDGE DETECT WAS SET PROPERLY") f = open("/sys/class/gpio/gpio35/edge", "r") edge = f.read()
config = SafeConfigParser() config.read(sys.argv[1]) sensor_pin = "XIO-P1" #sensor_pin = config.getint('main', 'SENSOR_PIN') begin_seconds = config.getint('main', 'SECONDS_TO_START') end_seconds = config.getint('main', 'SECONDS_TO_END') pushbullet_api_key = config.get('pushbullet', 'API_KEY') pushbullet_api_key2 = config.get('pushbullet', 'API_KEY2') start_message = config.get('main', 'START_MESSAGE') end_message = config.get('main', 'END_MESSAGE') twitter_api_key = config.get('twitter', 'api_key') twitter_api_secret = config.get('twitter', 'api_secret') twitter_access_token = config.get('twitter', 'access_token') twitter_access_token_secret = config.get('twitter', 'access_token_secret') #slack_api_token = config.get('slack', 'api_token') #slack_webhook = config.get('slack','webhook_url') iftt_maker_channel_event = config.get('iftt', 'maker_channel_event') iftt_maker_channel_key = config.get('iftt', 'maker_channel_key') send_alert(config.get('main', 'BOOT_MESSAGE')) GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(sensor_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.add_event_detect(sensor_pin, GPIO.RISING) GPIO.add_event_callback(sensor_pin, vibrated) print 'Running config file {} monitoring GPIO pin {}'\ .format(sys.argv[1], str(sensor_pin)) threading.Timer(1, heartbeat).start()
def after_setup(self): # threaded detection of button press GPIO.add_event_detect(self.__pconfig['button'], GPIO.FALLING, callback=self.detect_button, bouncetime=100)
GPIO.output(D_PIN, GPIO.HIGH) time.sleep(0.5) GPIO.output(D_PIN, GPIO.LOW) ## Setup GPIOs GPIO.toggle_debug() try: # Window Sensors GPIO.setup("CSID0", GPIO.OUT) # Door Sensors GPIO.setup("CSID0", GPIO.OUT) # Motion Sensors GPIO.setup("CSID0", GPIO.OUT) # Input GPIO.setup("XIO-P0", GPIO.IN) # Motion detector, 5v GPIO.setup("XIO-P0", GPIO.IN) GPIO.add_event_detect("XIO-P0", GPIO.FALLING) GPIO.add_event_callback("XIO-P0", motion_callback) except: print "Error" # Clean up every exported GPIO Pin finally: GPIO.cleanup()
def gpio_setup(): GPIO.setup(BEAM_1, GPIO.IN) GPIO.add_event_detect(BEAM_1, GPIO.RISING) GPIO.setup(BEAM_2, GPIO.IN) GPIO.add_event_detect(BEAM_2, GPIO.RISING)
GPIO.setup("XIO-P2", GPIO.IN) GPIO.setup("U14_31", GPIO.OUT) # CSID0 print("READING XIO-P2") GPIO.output("CSID0", GPIO.HIGH) assert (GPIO.input("XIO-P2") == GPIO.HIGH) GPIO.output("CSID0", GPIO.LOW) print "LOW", GPIO.input("GPIO1") assert (GPIO.input("GPIO1") == GPIO.LOW) # ============================================== # EDGE DETECTION - AP-EINT1 print("\nSETTING UP RISING EDGE DETECTION ON AP-EINT1") GPIO.setup("AP-EINT1", GPIO.IN) GPIO.add_event_detect("AP-EINT1", GPIO.RISING, myfuncallback) print("VERIFYING EDGE DETECT WAS SET PROPERLY") f = open("/sys/class/gpio/gpio193/edge", "r") edge = f.read() f.close() assert (edge == "rising\n") GPIO.remove_event_detect("AP-EINT1") # ============================================== # EDGE DETECTION - AP-EINT3 print("\nSETTING UP BOTH EDGE DETECTION ON AP-EINT3") GPIO.setup("AP-EINT3", GPIO.IN) GPIO.add_event_detect("AP-EINT3", GPIO.BOTH, myfuncallback) print("VERIFYING EDGE DETECT WAS SET PROPERLY") f = open("/sys/class/gpio/gpio35/edge", "r") edge = f.read()
import CHIP_IO.GPIO as GPIO import time # Define callback function def mycallback(channel): # Sample is just to print something, but other logic could go here print("Sensor Touch Activated!") # GPIO3 = XIO-P4, using PocketCHIP nomenclature GPIO.setup("GPIO1", GPIO.IN) # Trigger when we go from LOW to HIGH to match code above GPIO.add_event_detect("GPIO1", GPIO.RISING, mycallback) # Dummy loop to keep the script alive dead = False while not dead: try: time.sleep(0.2) except KeyboardInterrupt: dead = True # Cleanup GPIO.remove_event_detect("GPIO1") GPIO.cleanup()
processing_thread = threading.Thread(target=do_processing) processing_thread.start() #Setup "record" button pin_record = "XIO-P4" pin_record_led = 'XIO-P6' GPIO.setup(pin_record, GPIO.IN) GPIO.setup(pin_record_led, GPIO.OUT) # Specify pull up/pull down settings on a pin #GPIO.setup(pin_record, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Add Callback for Both Edges using the add_event_detect() method GPIO.add_event_detect(pin_record, GPIO.BOTH, pin_record_callback) running = True try: #main state while running: print("ready, acquiring recording_condition...") recording_condition.acquire() print("acquired...") recording_condition.wait() # sleep until item becomes available print("recording notify receieved...") finally: GPIO.cleanup()
def register_callbacks(self, turn=None, press=None): self.turn_cb = turn self.press_cb = press GPIO.add_event_detect(self.intr, GPIO.FALLING) GPIO.add_event_callback(self.intr, self._call_callbacks)
import subprocess import datetime import serial import random import psutil import sys import os GPIO.cleanup() sing_channel = "XIO-P1" record_channel = "XIO-P7" GPIO.setup(record_channel, GPIO.IN) GPIO.setup(sing_channel, GPIO.IN) GPIO.add_event_detect(record_channel, GPIO.RISING) GPIO.add_event_detect(sing_channel, GPIO.RISING) # hardcoded file locations codeLoc = "/home/chip/mewsician/" audioLoc = "/home/chip/audio/" queue = audioLoc + "queue" # auth passed in from (../start) authentication = sys.argv[1] recording = False listening = False singing = False play_pid = None fname = None bname = None
def after_setup(self): # threaded detection of button press GPIO.add_event_detect(self.__pconfig['button'], GPIO.FALLING, callback=self.detect_button, bouncetime=100)
GPIO.output(LED1, ON) if updateScore(0): blink(FLASH_TIME, FLASH_COUNT, LED1) else: GPIO.output(LED1, OFF) else: printlog("TEAM 2 GOAL!") GPIO.output(LED2, ON) if updateScore(1): blink(FLASH_TIME, FLASH_COUNT, LED2) else: GPIO.output(LED2, OFF) flashing = False GPIO.add_event_detect(BTN1, GPIO.FALLING, buttoncallback) GPIO.add_event_detect(BTN2, GPIO.FALLING, buttoncallback) printlog("Awaiting Button press") try: GPIO.wait_for_edge(BTNRESET, GPIO.FALLING) printlog("Reset button pressed") blink(0.1, 20, LED1, LED2) os.system("reboot") except: logging.exception("Exiting reset button loop:") GPIO.cleanup() printlog("Cleaning up")
def set_callbacks(self, callback=None): GPIO.add_event_detect(self.pin_dt, GPIO.BOTH, callback=callback) GPIO.add_event_detect(self.pin_clk, GPIO.BOTH, callback=callback)