_gpio.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. from typing import Callable
  2. GPIO_PIN_PC0: int
  3. """
  4. Constant identifier for GPIO pin PC0.
  5. * This pin can be used as ADC input.
  6. .. versionadded:: 1.2.0
  7. """
  8. GPIO_PIN_PC1: int
  9. """
  10. Constant identifier for GPIO pin PC1.
  11. * This pin can be used as ADC input.
  12. .. versionadded:: 1.2.0
  13. """
  14. GPIO_PIN_PC3: int
  15. """
  16. Constant identifier for GPIO pin PC3.
  17. * This pin can be used as ADC input.
  18. .. versionadded:: 1.2.0
  19. """
  20. GPIO_PIN_PB2: int
  21. """
  22. Constant identifier for GPIO pin PB2.
  23. .. versionadded:: 1.2.0
  24. """
  25. GPIO_PIN_PB3: int
  26. """
  27. Constant identifier for GPIO pin PB3.
  28. .. versionadded:: 1.2.0
  29. """
  30. GPIO_PIN_PA4: int
  31. """
  32. Constant identifier for GPIO pin PA4.
  33. * This pin can be used as ADC input.
  34. * This pin can be used as PWM output.
  35. .. versionadded:: 1.2.0
  36. """
  37. GPIO_PIN_PA6: int
  38. """
  39. Constant identifier for GPIO pin PA6.
  40. * This pin can be used as ADC input.
  41. .. versionadded:: 1.2.0
  42. """
  43. GPIO_PIN_PA7: int
  44. """
  45. Constant identifier for GPIO pin PA7.
  46. * This pin can be used as ADC input.
  47. * This pin can be used as PWM output.
  48. * This pin can be used to transmit an infrared signal with an IR LED.
  49. .. versionadded:: 1.2.0
  50. """
  51. GPIO_MODE_INPUT: int
  52. """
  53. Constant configuration value for the GPIO input mode.
  54. .. versionadded:: 1.2.0
  55. """
  56. GPIO_MODE_OUTPUT_PUSH_PULL: int
  57. """
  58. Constant configuration value for the GPIO output as push-pull mode.
  59. .. versionadded:: 1.2.0
  60. """
  61. GPIO_MODE_OUTPUT_OPEN_DRAIN: int
  62. """
  63. Constant configuration value for the GPIO output as open-drain mode.
  64. .. versionadded:: 1.2.0
  65. """
  66. GPIO_MODE_ANALOG: int
  67. """
  68. Constant configuration value for the GPIO analog mode.
  69. .. versionadded:: 1.2.0
  70. """
  71. GPIO_MODE_INTERRUPT_RISE: int
  72. """
  73. Constant configuration value for the GPIO interrupt on rising edges mode.
  74. .. versionadded:: 1.2.0
  75. """
  76. GPIO_MODE_INTERRUPT_FALL: int
  77. """
  78. Constant configuration value for the GPIO interrupt on falling edges mode.
  79. .. versionadded:: 1.2.0
  80. """
  81. GPIO_PULL_NO: int
  82. """
  83. Constant configuration value for the GPIO internal pull resistor disabled.
  84. .. versionadded:: 1.2.0
  85. """
  86. GPIO_PULL_UP: int
  87. """
  88. Constant configuration value for the GPIO internal pull-up resistor enabled.
  89. .. versionadded:: 1.2.0
  90. """
  91. GPIO_PULL_DOWN: int
  92. """
  93. Constant configuration value for the GPIO internal pull-down resistor enabled.
  94. .. versionadded:: 1.2.0
  95. """
  96. GPIO_SPEED_LOW: int
  97. """
  98. Constant configuration value for the GPIO in low speed.
  99. .. versionadded:: 1.2.0
  100. """
  101. GPIO_SPEED_MEDIUM: int
  102. """
  103. Constant configuration value for the GPIO in medium speed.
  104. .. versionadded:: 1.2.0
  105. """
  106. GPIO_SPEED_HIGH: int
  107. """
  108. Constant configuration value for the GPIO in high speed.
  109. .. versionadded:: 1.2.0
  110. """
  111. GPIO_SPEED_VERY_HIGH: int
  112. """
  113. Constant configuration value for the GPIO in very high speed.
  114. .. versionadded:: 1.2.0
  115. """
  116. def gpio_init_pin(pin: int, mode: int, pull: int = None, speed: int = None) -> bool:
  117. """
  118. Initialize a GPIO pin.
  119. :param pin: The pin to initialize (e.g. :const:`GPIO_PIN_PA4`).
  120. :param mode: The mode to use (e.g. :const:`GPIO_MODE_INPUT`).
  121. :param pull: The pull resistor to use. Default is :const:`GPIO_PULL_NO`.
  122. :param speed: The speed to use. Default is :const:`GPIO_SPEED_LOW`.
  123. :returns: :const:`True` on success, :const:`False` otherwise.
  124. .. versionadded:: 1.2.0
  125. .. versionchanged:: 1.3.0
  126. The return value changed from ``None`` to ``bool``.
  127. .. hint::
  128. The interrupt modes :const:`GPIO_MODE_INTERRUPT_RISE` and :const:`GPIO_MODE_INTERRUPT_FALL` can be combined using bitwise OR.
  129. This allows you to handle rising `and` falling edges.
  130. """
  131. pass
  132. def gpio_deinit_pin(pin: int) -> None:
  133. """
  134. Deinitialize a GPIO pin.
  135. :param pin: The pin to deinitialize (e.g. :const:`GPIO_PIN_PA4`).
  136. .. versionadded:: 1.3.0
  137. .. note::
  138. It's not strictly necessary to deinitialize your GPIO pins upon script termination, this is already covered by the interpreter.
  139. """
  140. pass
  141. def gpio_set_pin(pin: int, state: bool) -> None:
  142. """
  143. Set the state of an output pin.
  144. :param pin: The pin to set (e.g. :const:`GPIO_PIN_PA4`).
  145. :param state: The state to set.
  146. .. versionadded:: 1.2.0
  147. .. hint::
  148. Don't forget to initialize the pin first.
  149. """
  150. pass
  151. def gpio_get_pin(pin: int) -> bool:
  152. """
  153. Read the state of an input pin.
  154. :param pin: The pin to read (e.g. :const:`GPIO_PIN_PA4`).
  155. :returns: :const:`True` if the pin is high, :const:`False` on a low signal.
  156. .. versionadded:: 1.2.0
  157. .. hint::
  158. Don't forget to initialize the pin first.
  159. """
  160. pass
  161. def on_gpio() -> Callable[[int], None]:
  162. """
  163. Decorate a function to be used as GPIO interrupt handler. The decorated function will be invoked upon a GPIO interrupt.
  164. .. versionadded:: 1.0.0
  165. .. code-block::
  166. import flipperzero as f0
  167. f0.gpio_init_pin(f0.GPIO_PIN_PC0, f0.GPIO_MODE_INTERRUPT_RISE, f0.GPIO_PULL_UP)
  168. @f0.on_gpio
  169. def interrupt_handler(pin):
  170. if pin == f0.GPIO_PIN_PC0:
  171. ...
  172. .. warning::
  173. You can only decorate one function per application.
  174. """
  175. pass