_gpio.py 4.8 KB

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