Fakes API

UART and Pin fakes

Fakes of several MicroPython classes which are not available in UNIX

exception fakes.machine.MachineError[source]

Bases: Exception

Base class for exceptions in this module.

class fakes.machine.Pin(pin: int, mode: int)[source]

Bases: object

Fake Micropython Pin class

See https://docs.micropython.org/en/latest/library/machine.Pin.html

off() None[source]

Set pin to “0” output level

on() None[source]

Set pin to “1” output level

value(val: Optional[Union[int, bool]] = None) Optional[bool][source]

Set or get the value of the pin

Parameters

val (Optional[Union[int, bool]]) – The value

Returns

State of the pin if no value specifed, None otherwise

Return type

Optional[bool]

class fakes.machine.UART(uart_id: int, baudrate: int = 9600, bits: int = 8, parity: Optional[int] = None, stop: int = 1, tx: int = 1, rx: int = 2, timeout: int = 0)[source]

Bases: object

Fake Micropython UART class

See https://docs.micropython.org/en/latest/library/machine.UART.html

_configure_logger() Logger[source]

Create and configure a logger

Returns

Configured logger

Return type

logging.Logger

_serve(sock: <module 'socket' from '/home/docs/.asdf/installs/python/3.9.17/lib/python3.9/socket.py'>, send_queue: ~queue.Queue, receive_queue: ~queue.Queue, lock: int, logger: ~logging.Logger) None[source]

Internal socket server function

Parameters
  • sock (socket) – The socket

  • send_queue (Queue) – The send queue

  • receive_queue (Queue) – The receive queue

  • lock (int) – The lock flag

  • logger (logging.Logger) – The logger

any() int[source]

Return number of characters available.

Mock does not return the actual number of characters but the elements in the receive queue. Which is usually “0” or “1”

Returns

Number of characters available

Return type

int

deinit() None[source]

Turn off the UART bus

property is_server: bool

Determines if this object acts as server aka UART host.

Returns

True if server, False otherwise.

Return type

bool

no_socket_server_exists(host: str, port: int, timeout: Optional[float] = 1.0) bool[source]

Determines if no socket server exists.

Parameters
  • host (str) – The host IP

  • port (int) – The port

  • timeout (float) – The timeout

Returns

True if no socket server exists, False otherwise.

Return type

bool

read(nbytes: Optional[int] = None) Union[None, bytes][source]

Read characters

Parameters

nbytes (int) – The amount of bytes to read

Returns

Bytes read, None on timeout

Return type

Union[None, bytes]

readinto(buf: bytes, nbytes: Optional[int] = None) Union[None, bytes][source]

Read bytes into the buffer.

If nbytes is specified then read at most that many bytes. Otherwise, read at most len(buf) bytes

Parameters
  • buf (bytes) – The buffer

  • nbytes (Optional[int]) – The nbytes

Returns

Number of bytes read and stored in buffer, None on timeout

Return type

Union[None, bytes]

readline() Union[None, str][source]

Read a line, ending in a newline character

Returns

The line read, None on timeout.

Return type

Union[None, str]

sendbreak() None[source]

Send a break condition on the bus

property serving: bool

Get the server status.

Returns

Flag whether socket server is running or not.

Return type

bool

txdone() bool[source]

Tells whether all data has been sent or no data transfer is happening

Returns

If data transmission is ngoing return False, True otherwise

Return type

bool

write(buf: bytes) Union[None, int][source]

Write the buffer of bytes to the bus

Parameters

buf (bytes) – The buffer

Returns

Number of bytes written, None on timeout

Return type

Union[None, int]