_gpio.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. .. versionadded:: 1.2.0
  49. '''
  50. GPIO_MODE_INPUT: int
  51. '''
  52. Constant configuration value for the GPIO input mode.
  53. .. versionadded:: 1.2.0
  54. '''
  55. GPIO_MODE_OUTPUT_PUSH_PULL: int
  56. '''
  57. Constant configuration value for the GPIO output as push-pull mode.
  58. .. versionadded:: 1.2.0
  59. '''
  60. GPIO_MODE_OUTPUT_OPEN_DRAIN: int
  61. '''
  62. Constant configuration value for the GPIO output as open-drain mode.
  63. .. versionadded:: 1.2.0
  64. '''
  65. GPIO_MODE_ANALOG: int
  66. '''
  67. Constant configuration value for the GPIO analog mode.
  68. .. versionadded:: 1.2.0
  69. '''
  70. GPIO_MODE_INTERRUPT_RISE: int
  71. '''
  72. Constant configuration value for the GPIO interrupt on rising edges mode.
  73. .. versionadded:: 1.2.0
  74. '''
  75. GPIO_MODE_INTERRUPT_FALL: int
  76. '''
  77. Constant configuration value for the GPIO interrupt on falling edges mode.
  78. .. versionadded:: 1.2.0
  79. '''
  80. GPIO_PULL_NO: int
  81. '''
  82. Constant configuration value for the GPIO internal pull resistor disabled.
  83. .. versionadded:: 1.2.0
  84. '''
  85. GPIO_PULL_UP: int
  86. '''
  87. Constant configuration value for the GPIO internal pull-up resistor enabled.
  88. .. versionadded:: 1.2.0
  89. '''
  90. GPIO_PULL_DOWN: int
  91. '''
  92. Constant configuration value for the GPIO internal pull-down resistor enabled.
  93. .. versionadded:: 1.2.0
  94. '''
  95. GPIO_SPEED_LOW: int
  96. '''
  97. Constant configuration value for the GPIO in low speed.
  98. .. versionadded:: 1.2.0
  99. '''
  100. GPIO_SPEED_MEDIUM: int
  101. '''
  102. Constant configuration value for the GPIO in medium speed.
  103. .. versionadded:: 1.2.0
  104. '''
  105. GPIO_SPEED_HIGH: int
  106. '''
  107. Constant configuration value for the GPIO in high speed.
  108. .. versionadded:: 1.2.0
  109. '''
  110. GPIO_SPEED_VERY_HIGH: int
  111. '''
  112. Constant configuration value for the GPIO in very high speed.
  113. .. versionadded:: 1.2.0
  114. '''
  115. def gpio_init_pin(pin: int, mode: int, pull: int = None, speed: int = None) -> bool:
  116. '''
  117. Initialize a GPIO pin.
  118. :param pin: The pin to initialize (e.g. :const:`GPIO_PIN_PA4`).
  119. :param mode: The mode to use (e.g. :const:`GPIO_MODE_INPUT`).
  120. :param pull: The pull resistor to use. Default is :const:`GPIO_PULL_NO`.
  121. :param speed: The speed to use. Default is :const:`GPIO_SPEED_LOW`.
  122. :returns: :const:`True` on success, :const:`False` otherwise.
  123. .. versionadded:: 1.2.0
  124. .. versionchanged:: 1.3.0
  125. The return value changed from ``None`` to ``bool``.
  126. .. hint::
  127. The interrupt modes :const:`GPIO_MODE_INTERRUPT_RISE` and :const:`GPIO_MODE_INTERRUPT_FALL` can be combined using bitwise OR.
  128. This allows you to handle rising `and` falling edges.
  129. '''
  130. pass
  131. def gpio_deinit_pin(pin: int) -> None:
  132. '''
  133. Deinitialize a GPIO pin.
  134. :param pin: The pin to deinitialize (e.g. :const:`GPIO_PIN_PA4`).
  135. .. versionadded:: 1.3.0
  136. .. note::
  137. It's not strictly necessary to deinitialize your GPIO pins upon script termination, this is already covered by the interpreter.
  138. '''
  139. pass
  140. def gpio_set_pin(pin: int, state: bool) -> None:
  141. '''
  142. Set the state of an output pin.
  143. :param pin: The pin to set (e.g. :const:`GPIO_PIN_PA4`).
  144. :param state: The state to set.
  145. .. versionadded:: 1.2.0
  146. .. hint::
  147. Don't forget to initialize the pin first.
  148. '''
  149. pass
  150. def gpio_get_pin(pin: int) -> bool:
  151. '''
  152. Read the state of an input pin.
  153. :param pin: The pin to read (e.g. :const:`GPIO_PIN_PA4`).
  154. :returns: :const:`True` if the pin is high, :const:`False` on a low signal.
  155. .. versionadded:: 1.2.0
  156. .. hint::
  157. Don't forget to initialize the pin first.
  158. '''
  159. pass
  160. def on_gpio() -> Callable[[int], None]:
  161. '''
  162. Decorate a function to be used as GPIO interrupt handler. The decorated function will be invoked upon a GPIO interrupt.
  163. .. versionadded:: 1.0.0
  164. .. code-block::
  165. import flipperzero as f0
  166. f0.gpio_init_pin(f0.GPIO_PIN_PC0, f0.GPIO_MODE_INTERRUPT_RISE, f0.GPIO_PULL_UP)
  167. @f0.on_gpio
  168. def interrupt_handler(pin):
  169. if pin == f0.GPIO_PIN_PC0:
  170. ...
  171. .. warning::
  172. You can only decorate one function per application.
  173. '''
  174. pass