1010import atexit
1111import threading
1212import time
13+ from datetime import timedelta
1314
15+ import gpiod
16+ import gpiodevice
17+ from gpiod .line import Bias , Direction , Edge , Value
1418from smbus2 import SMBus
1519
16- try :
17- import RPi .GPIO as GPIO
18- except ImportError :
19- raise ImportError ("This library requires the RPi.GPIO module\n Install with: sudo pip install RPi.GPIO" )
20-
2120__version__ = "0.1.4"
2221
2322# DEVICE MAP
@@ -260,8 +259,8 @@ def __init__(
260259 self ,
261260 i2c_addr = DEFAULT_ADDR ,
262261 i2c_bus = 1 ,
263- alert_pin = - 1 ,
264- reset_pin = - 1 ,
262+ alert_pin = None ,
263+ reset_pin = None ,
265264 on_touch = None ,
266265 skip_init = False ,
267266 ):
@@ -275,16 +274,25 @@ def __init__(
275274 self .reset_pin = reset_pin
276275 self ._delta = 50
277276
278- GPIO .setmode (GPIO .BCM )
279- if not self .alert_pin == - 1 :
280- GPIO .setup (self .alert_pin , GPIO .IN , pull_up_down = GPIO .PUD_UP )
277+ if self .reset_pin is not None or self .alert_pin is not None :
278+ _gpiochip = gpiodevice .find_chip_by_platform ()
279+
280+ lines = {}
281+
282+ if self .alert_pin is not None :
283+ self .alert_pin = _gpiochip .line_offset_from_id (self .alert_pin )
284+ lines [self .alert_pin ] = gpiod .LineSettings (direction = Direction .INPUT , bias = Bias .PULL_UP , edge_detection = Edge .FALLING , debounce_period = timedelta (milliseconds = 1 ))
285+
286+ if self .reset_pin is not None :
287+ self .reset_pin = _gpiochip .line_offset_from_id (self .reset_pin )
288+ lines [self .reset_pin ] = gpiod .LineSettings (direction = Direction .OUTPUT , bias = Bias .DISABLED , output_value = Value .INACTIVE )
281289
282- if not self .reset_pin == - 1 :
283- GPIO . setup ( self . reset_pin , GPIO . OUT )
284- GPIO . setup ( self .reset_pin , GPIO . LOW )
285- GPIO . output (self .reset_pin , GPIO . HIGH )
286- time .sleep (0.01 )
287- GPIO . output (self .reset_pin , GPIO . LOW )
290+ self ._gpiolines = _gpiochip . request_lines ( consumer = "cap1xxx" , config = lines )
291+
292+ if self .reset_pin is not None :
293+ self . _gpiolines . set_value (self .reset_pin , Value . ACTIVE )
294+ time .sleep (0.01 )
295+ self . _gpiolines . set_value (self .reset_pin , Value . INACTIVE )
288296
289297 self .handlers = {
290298 "press" : [None ] * self .number_of_inputs ,
@@ -388,18 +396,25 @@ def clear_interrupt(self):
388396 self ._write_byte (R_MAIN_CONTROL , main )
389397
390398 def _interrupt_status (self ):
391- if self .alert_pin == - 1 :
399+ if self .alert_pin is None :
392400 return self ._read_byte (R_MAIN_CONTROL ) & 1
393401 else :
394- return not GPIO . input (self .alert_pin )
402+ return self . _gpiolines . get_value (self .alert_pin ) == Value . INACTIVE
395403
396404 def wait_for_interrupt (self , timeout = 100 ):
397405 """Wait for, interrupt, bit 0 of the main
398406 control register to be set, indicating an
399407 input has been triggered."""
408+ if self .alert_pin is not None :
409+ events = self ._gpiolines .wait_edge_events (timedelta (milliseconds = timeout ))
410+ if events :
411+ self ._gpiolines .read_edge_events ()
412+ return True
413+ return False
414+
400415 start = self ._millis ()
401416 while True :
402- status = self ._interrupt_status ()
417+ status = self ._interrupt_status (timeout )
403418 if status :
404419 return True
405420 if self ._millis () > start + timeout :
@@ -412,29 +427,13 @@ def on(self, channel=0, event="press", handler=None):
412427 return True
413428
414429 def start_watching (self ):
415- if not self .alert_pin == - 1 :
416- try :
417- GPIO .add_event_detect (
418- self .alert_pin ,
419- GPIO .FALLING ,
420- callback = self ._handle_alert ,
421- bouncetime = 1 ,
422- )
423- self .clear_interrupt ()
424- except IOError :
425- pass
426- return True
427-
428430 if self .async_poll is None :
429431 self .async_poll = AsyncWorker (self ._poll )
430432 self .async_poll .start ()
431433 return True
432434 return False
433435
434436 def stop_watching (self ):
435- if not self .alert_pin == - 1 :
436- GPIO .remove_event_detect (self .alert_pin )
437-
438437 if self .async_poll is not None :
439438 self .async_poll .stop ()
440439 self .async_poll = None
0 commit comments