Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8013

Python • Re: Where are docs for gpiod python bindings (SOLVED)

$
0
0
I managed to install gpiod 2.3.0 and changed the example so it worked. If anyone is interested.

The docs do not make it easier. However, looking at the docs, python dir() and help(), and, the source code...
Many, many, many , ..., trials and error later:

Code:

#!/bin/python'''Updated to use gpiod Version 2.3.0Connect 1k between SENS and TRIG GPIO pins.GPL'''import threadingimport timeimport gpiodfrom gpiod.line import Direction, Value, Edgeimport statistics as stCONSUMER = "Consumer"GPIO_CHIP = "/dev/gpiochip0"SENS = 17TRIG = 18SLEEP_TIME = 100.0/1000000.0 ## 100 usecNSEC_PER_SEC = 1000000000.0tup = tdown = 0 ## trigger timeeup = edown = 0 ## event timeidata = []tdata = []def thread_function(chip, stop_event):    global eup, edown, idata    request = gpiod.request_lines(GPIO_CHIP,                                  consumer='thread',                                  config = {                                      SENS: gpiod.LineSettings(                                          direction=Direction.INPUT,                                          edge_detection=Edge.BOTH                                      )                                  },  )    retry = 10    while (retry > 0) and (not stop_event.is_set()):        ##rc = chip.wait_info_event(timeout=11000000.0/1000000000) ## 11 msec        rc = request.wait_edge_events(timeout=11000000.0/1000000000) ## 11 msec        if rc and not stop_event.is_set():            retry = 10            ##event = chip.read_info_event()            event = request.read_edge_events()            ##print(event[0])            ##print(type(event[0]))            ##print(event[0].event_type, event[0].timestamp_ns, event[0].global_seqno, event[0].line_seqno)            if event[0].event_type == gpiod.edge_event.EdgeEvent.Type.RISING_EDGE:                eup = (event[0].timestamp_ns) / NSEC_PER_SEC            else:                edown = (event[0].timestamp_ns) / NSEC_PER_SEC                idata.append(edown - eup)def show_stats(name, data):    minv = min(data)    maxv = max(data)    meanv = st.mean(data)    stdev = st.stdev(data)    s = name + ':      '    s = s[:6]    s += ' min {:.6f} mean {:.6f} max {:.6f} stddev {:.6f}'    print(s.format(minv, meanv, maxv, stdev))    def main():    chip = gpiod.Chip(GPIO_CHIP)    num_loops = 100    now = time.time()    print('Running {} trials starting at {} seconds.'.format(num_loops, now))    print('Timing up {} down {} period {} seconds'.format(SLEEP_TIME, SLEEP_TIME, 2*SLEEP_TIME))        stop_event = threading.Event()    th = threading.Thread(target=thread_function, args=(chip,stop_event, ))    th.start()    time.sleep(1)    request = gpiod.request_lines(GPIO_CHIP, consumer='main',                                  config={                                      TRIG: gpiod.LineSettings(                                          direction=Direction.OUTPUT,                                          output_value=Value.ACTIVE                                      )                                  }, )       for idx in range(num_loops):        try:            tup = time.time()            request.set_value(TRIG, Value.ACTIVE)            time.sleep(SLEEP_TIME)            tdown = time.time()            request.set_value(TRIG, Value.INACTIVE)            tdata.append(tdown - tup)            time.sleep(SLEEP_TIME)        except KeyboardInterrupt:            print('Program interrupted by user. Waiting for cleanup...')    stop_event.set()               ##print(idata)    if len(idata) > 0:        ##print('idata len {}'.format(len(idata)))        show_stats('event', idata)    if len(tdata) > 0:        ##print('tdata len {}'.format(len(tdata)))        show_stats('trig ', tdata)        th.join()  ## wait for thread to exit    request.set_value(TRIG, Value.INACTIVE) ## leave line at boot value    chip.close() ## exit gpiodif __name__ == '__main__':    main()
Tom Dean

Statistics: Posted by tomdean — Tue Jun 10, 2025 3:42 am



Viewing all articles
Browse latest Browse all 8013

Trending Articles