def _enable_event_detect(self): lgpio.gpio_claim_alert( self.factory._handle, self.number, self._edges, lgpio.gpio_get_mode(self.factory._handle, self.number) & self.GPIO_LINE_FLAGS_MASK) self._callback = lgpio.callback(self.factory._handle, self.number, self._edges, self._call_when_changed)
# register the gpio for alerts (edge detection) err = sbc.gpio_claim_alert(handle, gpio, sbc.FALLING_EDGE) if err < 0: print("GPIO in use {}:{} ({})".format( chip, gpio, sbc.error_text(err))) exit() # set the debounce time in microseconds so that the button is only pressed # once when it's actually pressed once sbc.gpio_set_debounce_micros(handle, gpio, debounce_micros) # callback function called when edge is detected def shutdown_callback(chip, gpio, level, timestamp): os.system('systemctl poweroff') # doesn't require sudo # register the callback function and attach it to the gpio pin cb1 = sbc.callback(0, gpio, sbc.FALLING_EDGE, shutdown_callback) print(cb1) # sleep to avoid busy-waiting, but still keep the program running try: while True: time.sleep(10) except KeyboardInterrupt: print(cb1.tally()) cb1.cancel()
ALERT = 21 # set chip = sbc.gpiochip_open(0) err = sbc.gpio_claim_alert(chip, ALERT, EDGE) if err < 0: print("GPIO in use: {} ({})".format(ALERT, sbc.error_text(err))) exit() adc = lg_ads1x15.ads1015(sbc, 1, 0x48) adc.set_voltage_range(3.3) adc.set_sample_rate(0) # minimum sampling rate adc.set_channel(adc.A0) if ALERT_RDY: adc.set_comparator_polarity(0) cb_id = sbc.callback(chip, ALERT, EDGE, cbf) adc.set_comparator_latch(True) adc.set_continuous_mode() #adc.alert_when_high_clear_when_low(3, 2) adc.alert_when_high_or_low(3, 0.3) #adc.alert_when_ready() time.sleep(120) # run for two minutes else: end_time = time.time() + 120 while time.time() < end_time: print(adc.read_voltage()) time.sleep(0.2) adc.close()
# get a handle to the gpiochip handle = sbc.gpiochip_open(chip) if handle >= 0: # got a handle, now open the GPIO for alerts err = sbc.gpio_claim_alert(handle, gpio, sbc.BOTH_EDGES) if err < 0: print("GPIO in use {}:{} ({})".format(chip, gpio, sbc.error_text(err))) exit() cb_id = sbc.callback(handle, gpio, sbc.BOTH_EDGES, cbf) else: print("can't open gpiochip {} ({})".format(chip, sbc.error_text(handle))) exit() else: print("don't understand {}".format(sys.argv[i])) exit() sbc.exceptions = True while True: