1
0
mirror of https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git synced 2026-10-28 13:55:08 +03:00
Files
libgpiod/bindings/python/examples/gpiomon.py
Bartosz Golaszewski 1d87088b1e bindings: python: examples: close chip objects where needed
Unlike the C++ bindings: we can't expect the python runtime to call
the Chip object's destructor when all references are dropped. We need
to close each chip explicitly. Either call the close() method directly
or use controlled execution in all examples.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-07-31 14:30:46 +02:00

45 lines
1.2 KiB
Python
Executable File

#!/usr/bin/env python3
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This file is part of libgpiod.
#
# Copyright (C) 2017-2018 Bartosz Golaszewski <bartekgola@gmail.com>
#
'''Simplified reimplementation of the gpiomon tool in Python.'''
import gpiod
import sys
def print_event(event):
if event.type == gpiod.LineEvent.RISING_EDGE:
print(' RISING EDGE', end='')
elif event.type == gpiod.LineEvent.FALLING_EDGE:
print('FALLING EDGE', end='')
else:
raise TypeError('Invalid event type')
print(' {}.{} line: {}'.format(event.sec, event.nsec, event.source.offset()))
if len(sys.argv) < 3:
raise TypeError('usage: gpiomon.py <gpiochip> <offset1> <offset2> ...')
with gpiod.Chip(sys.argv[1]) as chip:
offsets = []
for off in sys.argv[2:]:
offsets.append(int(off))
lines = chip.get_lines(offsets)
lines.request(consumer=sys.argv[0], type=gpiod.LINE_REQ_EV_BOTH_EDGES)
try:
while True:
ev_lines = lines.event_wait(sec=1)
if ev_lines:
for line in ev_lines:
event = line.event_read()
print_event(event)
except KeyboardInterrupt:
sys.exit(130)