status = sbc.gpio_set_watchdog_micros(9999, 8888, 4000) check("gpio_set_watchdog_micros 2", sbc.BAD_HANDLE, status) status = sbc.gpio_claim_alert(9999, 8888, 7777, 6666, 5555) check("gpio_claim_alert 1", sbc.BAD_HANDLE, status) status = sbc.gpio_claim_input(9999, 8888) check("gpio_claim_input 1", sbc.BAD_HANDLE, status) status = sbc.group_claim_input(9999, [8888, 7777, 6666]) check("group_claim_input 1", sbc.BAD_HANDLE, status) status = sbc.gpio_claim_output(9999, 8888, 7777) check("gpio_claim_output 1", sbc.BAD_HANDLE, status) status = sbc.group_claim_output(9999, [8888, 7777, 6666]) check("group_claim_output 1", sbc.BAD_HANDLE, status) status = sbc.gpio_write(9999, 8888, 7777) check("gpio_write 1", sbc.BAD_HANDLE, status) err = sbc.error_text(0) check("error_text 1", "No error", err) err = sbc.error_text(-1) check("error_text 2", "initialisation failed", err) err = sbc.error_text(1) check("error_text 3", "unknown error", err)
gpio = 3 # button is connected to GPIO debounce_micros = 100000 # open the gpio chip. # gets gpio chip's handle upon success handle = sbc.gpiochip_open(chip) if handle < 0: print(f"Couldn't open gpio chip {chip} ({sbc.error_text(handle)})") exit() # 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)
else: import lgpio as sbc EDGE = sbc.FALLING_EDGE def cbf(chip, gpio, level, tick): print(adc.read_voltage()) if ALERT_RDY: 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)
if gpio >= 0: if handle < 0: # 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()