General (canvas)

class canvas.LEDStrip(pin: str, num_leds: int)

This class provides an interface to control an LED strip, including the onboard RGB LED on the Sera NX040 DVK board. The LED strip is controlled using the WS2812 protocol.

The LED strip is initialized with the pin name and the number of LEDs in the strip. In this initial implementation, the pin is hardcoded to the onboard RGB LED pin on the Sera NX040 DVK board. The “output” from the onboard RGB LED is connected to the “input” of three-pin LED strip header to allow additional LEDs to be controlled.

The LED strip is controlled by setting the color of each LED in the strip using the set() method. The color is specified as a 24-bit value with the red, green, and blue components packed into the value. The RED_MASK, GREEN_MASK, and BLUE_MASK can be used to extract the individual color components from the color value.

The following colors are predefined for convenience: - COLOR_RED - COLOR_GREEN - COLOR_BLUE - COLOR_YELLOW - COLOR_MAGENTA - COLOR_CYAN - COLOR_WHITE - COLOR_OFF

Example to set the color of the first LED in the strip to red:

>>> from canvas import LEDStrip
>>> strip = LEDStrip('', 1)
>>> strip.set(0, LEDStrip.COLOR_RED)
Parameters:
  • pin (str) – The pin to use for the LED strip

  • num_leds (int) – The number of LEDs in the strip

set(index: int, color: int)

Set the color of an LED in a strip

Parameters:
  • index (int) – The index of the LED to set

  • color (int) – The color to set the LED to

BLUE_MASK: int

Mask for the blue portion of a color value

COLOR_BLUE: int

Blue color value, full brightness

COLOR_CYAN: int

Cyan color value, full brightness

COLOR_GREEN: int

Green color value, full brightness

COLOR_MAGENTA: int

Magenta color value, full brightness

COLOR_OFF: int

Off color value

COLOR_RED: int

Red color value, full brightness

COLOR_WHITE: int

White color value, full brightness

COLOR_YELLOW: int

Yellow color value, full brightness

GREEN_MASK: int

Mask for the green portion of a color value

RED_MASK: int

Mask for the red portion of a color value

class canvas.NFCTag

The NFCTag class is used to create a new NFC tag object. The object holds an internal tag data buffer, which can be read from and written to by the NFC reader. The initializer specifies the size of the tag data buffer. Data in the tag data buffer is expected to be in the NDEF format.

The object also holds a Python representation of the tag data buffer in a object attribute named records. This attribute is a Python list updated automatically when the tag data buffer is written to by the NFC reader. The list can also be updated manually by the user, and then written to the tag data buffer by calling sync_to_buffer().

Only one instance of the NFCTag class can be created at a time.

Parameters:

size – The size of the tag data buffer in bytes

clear()

Clears the tag data buffer and the Python representation of the tag data buffer.

close()

Release any memory associated with the NFC tag emulation. After calling this method, the NFCTag object is no longer valid. To use the NFCTag object again, a new instance must be created.

set_write_callback(cb: any)

Sets the callback function that will be called when the NFC reader writes to the tag data buffer.

Parameters:

cb – The callback function to be called. The callback function takes no arguments.

sync_to_buffer()

Updates the tag data buffer with the Python representation of the tag data buffer. That is, it takes the contents of the records list and writes them to the tag data buffer. The tag data buffer is always overwritten from the beginning.

class canvas.Timer(interval_ms: int, repeats: bool, cb: any, data: any)

Class for software timers. On expiration, the timer callback will be called with the data argument passed to the constructor. If the timer is repeating, it will continue to expire at the specified interval until stopped.

Example:

>>> def timer_cb(data):
...     print("Timer expired")
...
>>> timer = canvas.Timer(1000, True, timer_cb, None)
>>> timer.start()
Parameters:
  • interval_ms (int) – The timer interval in milliseconds

  • repeats (bool) – Whether the timer should repeat or not

  • cb – Callback function to be called when the timer expires. The callback function must accept a single argument, which will be the data argument passed to the constructor.

  • data – Data to be passed to the callback function when the timer expires

change_period(interval_ms: int)

Change the timer interval. If the timer is running, it will be restarted with the new interval. If the timer is stopped, the interval will be changed and the timer will start with the new interval.

Parameters:

interval_ms – The new interval in milliseconds

restart()

Restart the timer.

start()

Start the timer.

stop()

Stop the timer. The timer can be stopped even if it is already stopped.

If the timer had expired, but the callback function had not yet been called before the timer was stopped, the callback function may still be called after the timer is stopped.

canvas.zcbor_from_obj(input: any, in_map: bool) bytes

Convert an object (dictionary or list) to a CBOR byte string.

Parameters:
  • input – The object to convert.

  • in_map – True if the CBOR will already be wrapped inside of a map.

Returns:

The CBOR byte string.

canvas.zcbor_to_obj(input: any) any

Convert a CBOR byte string to an object (dictionary or list).

Parameters:

input – The CBOR byte string.

Returns:

The object.