_uart.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. from typing import List
  2. UART_MODE_LPUART: int
  3. """
  4. Constant value for the low power UART mode.
  5. .. versionadded:: 1.5.0
  6. """
  7. UART_MODE_USART: int
  8. """
  9. Constant value for the USART mode.
  10. .. versionadded:: 1.5.0
  11. """
  12. class UART:
  13. """
  14. This represents an UART connection.
  15. The class has no :const:`__init__` method, use :func:`uart_open` to start an UART connection and receive an instance.
  16. .. versionadded:: 1.5.0
  17. An :class:`UART` instance is iterable:
  18. .. code-block::
  19. import flipperzero as f0
  20. with f0.open(f0.UART_MODE_USART, 115200) as uart:
  21. lines = [line for line in uart]
  22. An :class:`UART` instance can be used with a `context manager <https://docs.python.org/3/reference/datamodel.html#with-statement-context-managers>`_:
  23. .. code-block::
  24. import flipperzero as f0
  25. with f0.open(f0.UART_MODE_USART, 115200) as uart:
  26. ...
  27. .. hint::
  28. The read and write methods are non-blocking in terms of data availability.
  29. They don't block code execution upon data is available.
  30. Just an empty result will be returned.
  31. """
  32. def read(self, size: int = -1) -> bytes:
  33. """
  34. Read from the connection.
  35. The method will read up to ``size`` bytes and return them.
  36. If ``size`` is not specified, all available data will be returned.
  37. The method will return zero bytes, if no data is available.
  38. :param size: The maximum number of bytes to read.
  39. :returns: Up to ``size`` bytes.
  40. .. versionadded:: 1.5.0
  41. """
  42. pass
  43. def readline(self, size: int = -1) -> bytes:
  44. """
  45. Read and return one line from the connection.
  46. If ``size`` is specified, at most ``size`` bytes will be read.
  47. The line terminator is always ``b'\\n'``.
  48. :param size: The maximum number of bytes to read.
  49. :returns: Up to ``size`` bytes.
  50. .. versionadded:: 1.5.0
  51. """
  52. pass
  53. def readlines(self) -> List[bytes]:
  54. """
  55. Read and return a list of lines from the connection.
  56. The line terminator is always ``b'\\n'``.
  57. :returns: A list of bytes.
  58. .. versionadded:: 1.5.0
  59. """
  60. pass
  61. def write(self, data: bytes) -> int:
  62. """
  63. Write the given bytes to the connection stream.
  64. The number of written bytes will be returned.
  65. This can be less than the length of the provided data.
  66. Be aware, that the data is not sent synchronously.
  67. Call :meth:`flush` if you have to wait for the data to be sent.
  68. :param data: The data to transmit.
  69. :returns: The number of bytes sent.
  70. .. versionadded:: 1.5.0
  71. """
  72. pass
  73. def flush(self) -> None:
  74. """
  75. Flush the transmission buffer to the underlying UART connection.
  76. This method blocks until all data is sent.
  77. .. versionadded:: 1.5.0
  78. """
  79. pass
  80. def close(self) -> None:
  81. """
  82. Close the UART connection.
  83. .. versionadded:: 1.5.0
  84. """
  85. pass
  86. def __enter__(self) -> "UART":
  87. """
  88. This method is invoked, when the instance enters a runtime context.
  89. :returns: The :class:`UART` connection.
  90. .. versionadded:: 1.5.0
  91. """
  92. pass
  93. def __exit__(self, *args, **kwargs) -> None:
  94. """
  95. This method is invoked, when the instance leavs a runtime context.
  96. This basically calls :meth:`close` on the instance.
  97. .. versionadded:: 1.5.0
  98. """
  99. pass
  100. def __del__(self) -> None:
  101. """
  102. This method is invoked, when the garbage collector removes the object.
  103. This basically calls :meth:`close` on the instance.
  104. .. versionadded:: 1.5.0
  105. """
  106. pass
  107. def uart_open(mode: int, baud_rate: int) -> UART:
  108. """
  109. Open a connection to an UART enabled device by using the specified mode and baud rate.
  110. :param mode: The mode to use, either :const:`UART_MODE_LPUART` or :const:`UART_MODE_USART`.
  111. :param baud_rate: The baud rate to use.
  112. :returns: A :class:`UART` object on success, :const:`None` otherwise.
  113. .. versionadded:: 1.5.0
  114. .. code-block::
  115. import flipperzero as f0
  116. with f0.uart_open(f0.UART_MODE_USART, 115200) as uart:
  117. ...
  118. """
  119. pass