Oliver Fabel 1 rok temu
rodzic
commit
35b4a1ed3a
3 zmienionych plików z 87 dodań i 2 usunięć
  1. 1 0
      CHANGELOG.md
  2. 8 0
      docs/pages/reference.rst
  3. 78 2
      flipperzero/_gpio.py

+ 1 - 0
CHANGELOG.md

@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
   * Initialize a pin.
   * Read from a pin.
   * Write to a pin.
+  * Handle interrupts.
 
 ### Fixed
 

+ 8 - 0
docs/pages/reference.rst

@@ -224,9 +224,17 @@ Access to the GPIO pins of your Flipper.
 .. autodata:: flipperzero.GPIO_MODE_ANALOG
 .. autodata:: flipperzero.GPIO_MODE_INTERRUPT_RISE
 .. autodata:: flipperzero.GPIO_MODE_INTERRUPT_FALL
+.. autodata:: flipperzero.GPIO_PULL_NO
+.. autodata:: flipperzero.GPIO_PULL_UP
+.. autodata:: flipperzero.GPIO_PULL_DOWN
+.. autodata:: flipperzero.GPIO_SPEED_LOW
+.. autodata:: flipperzero.GPIO_SPEED_MEDIUM
+.. autodata:: flipperzero.GPIO_SPEED_HIGH
+.. autodata:: flipperzero.GPIO_SPEED_VERY_HIGH
 .. autofunction:: flipperzero.gpio_init_pin
 .. autofunction:: flipperzero.gpio_set_pin
 .. autofunction:: flipperzero.gpio_get_pin
+.. autodecorator:: flipperzero.on_gpio
 
 Built-In
 --------

+ 78 - 2
flipperzero/_gpio.py

@@ -1,3 +1,5 @@
+from typing import Callable
+
 GPIO_PIN_PC0: int
 '''
 Constant identifier for GPIO pin PC0.
@@ -96,12 +98,63 @@ Constant configuration value for the GPIO interrupt on falling edges mode.
 .. versionadded:: 1.2.0
 '''
 
-def gpio_init_pin(pin: int, mode: int) -> None:
+GPIO_PULL_NO: int
+'''
+Constant configuration value for the GPIO internal pull resistor disabled.
+    
+.. versionadded:: 1.2.0
+'''
+
+GPIO_PULL_UP: int
+'''
+Constant configuration value for the GPIO internal pull-up resistor enabled.
+    
+.. versionadded:: 1.2.0
+'''
+
+GPIO_PULL_DOWN: int
+'''
+Constant configuration value for the GPIO internal pull-down resistor enabled.
+    
+.. versionadded:: 1.2.0
+'''
+
+GPIO_SPEED_LOW: int
+'''
+Constant configuration value for the GPIO in low speed.
+    
+.. versionadded:: 1.2.0
+'''
+
+GPIO_SPEED_MEDIUM: int
+'''
+Constant configuration value for the GPIO in medium speed.
+    
+.. versionadded:: 1.2.0
+'''
+
+GPIO_SPEED_HIGH: int
+'''
+Constant configuration value for the GPIO in high speed.
+    
+.. versionadded:: 1.2.0
+'''
+
+GPIO_SPEED_VERY_HIGH: int
+'''
+Constant configuration value for the GPIO in very high speed.
+    
+.. versionadded:: 1.2.0
+'''
+
+def gpio_init_pin(pin: int, mode: int, pull: int = None, speed: int = None) -> None:
     '''
     Initialize a GPIO pin.
 
     :param pin: The pin to initialize (e.g. :const:`GPIO_PIN_PA4`).
-    :param mode: The mode to use (e.g. :const:`GPIO_MODE_OUTPUT_PUSH_PULL`).
+    :param mode: The mode to use (e.g. :const:`GPIO_MODE_INPUT`).
+    :param pull: The pull resistor to use. Default is :const:`GPIO_PULL_NO`.
+    :param speed: The speed to use. Default is :const:`GPIO_SPEED_LOW`.
     
     .. versionadded:: 1.2.0
 
@@ -141,3 +194,26 @@ def gpio_get_pin(pin: int) -> bool:
         Don't forget to initialize the pin first.
     '''
     pass
+
+def on_gpio() -> Callable[[int], None]:
+    '''
+    Decorate a function to be used as GPIO interrupt handler. The decorated function will be invoked upon a GPIO interrupt.
+
+    .. versionadded:: 1.0.0
+
+    .. code-block::
+
+        import flipperzero as f0
+
+        f0.gpio_init_pin(f0.GPIO_PIN_PC0, f0.GPIO_MODE_INTERRUPT_RISE, f0.GPIO_PULL_UP)
+
+        @f0.on_gpio
+        def interrupt_handler(pin):
+            if pin == f0.GPIO_PIN_PC0:
+                ...
+    
+    .. warning::
+
+        You can only decorate one function per application.
+    '''
+    pass