_gpio.py 4.2 KB

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