Canvas class

class Canvas.Canvas(width, height)[source]

A canvas makes it easy to display using the matrix by providing a translation layer between pixels on a cartesian coordinate system and color data readable by the arduino and the WS2812B RGB leds.

The canvas uses colors from the colour library to represent the requested colors. See https://github.com/vaab/colour

The user functions are:

  • draw_pixel(x, y, color)
  • draw_line(x_start, y_start, x_end, y_end, color)
  • draw_rect(x, y, width, height, color)
  • draw_text(x, y, text, color, ignore_height_warning=False)
  • set_font(path, size)
  • clear(color)
clear(color: colour.Color = <Color black>)[source]

Set all pixels to some color

Parameters:color – the color that should be applied
draw_line(x_start: int, y_start: int, x_end: int, y_end: int, color: colour.Color)[source]

An implementation of bresenhams line drawing algorithm. Draws a line from <x_start, y_start> to <x_end, y_end> in the given color.

Parameters:
  • x_start – x position where the line should start
  • y_start – y position where the line should end
  • x_end – x position where the line should start
  • y_end – y position where the line should end
  • color – color the line should be drawn in
Returns:

nothing

draw_pixel(x: int, y: int, color: colour.Color)[source]

Set a pixel to a color. Most basic canvas function.

Parameters:
  • x – x position of pixel; counted from zero beginning on the left, must be smaller than the canvas width
  • y – y position of pixel; y is zero for the top row of pixels, must be smaller than the canvas height
  • color – the description of the color that should be set
draw_rect(x: int, y: int, width: int, height: int, color: colour.Color)[source]
Parameters:
  • x – x position of pixel; counted from zero beginning on the left, must be smaller than the canvas width
  • y – y position of pixel; y is zero for the top row of pixels, must be smaller than the canvas height
  • width – how wide the rectangle should be.
  • height – how high the rectangle should be
  • color – the color the rectangle should have
Returns:

nothing

draw_text(text: str, x: int, y: int, color: colour.Color, ignore_height_warning=False)[source]

Draw text on the canvas. Rendering over the borders is cut off, so you do not need boundary checking.

Parameters:
  • text – the text to be rendered
  • x – the top-left starting position of the text
  • y – the top-left starting position of the text
  • color – color of the text
  • ignore_height_warning – if true, no warning will be logged that the font does not fit into the available height. if false, a warning will be printed in the log on each such occasion
Returns:

nothing

get_buffer_for_arduino() → bytearray[source]

This method can be used to retrieve the internal data buffer. Modifications will probably do weird shit. Mostly useful for pushing the data out to the arduino, who actually understands what all the numbers mean.

Returns:a bytearray with all color values
get_color(x, y) → colour.Color[source]

Get a Color instance describing the color of the led at x,y

Parameters:
  • x – x position of pixel; counted from zero beginning on the left, must be smaller than the canvas width
  • y – y position of pixel; y is zero for the top row of pixels, must be smaller than the canvas height
Returns:

a Color instance

get_pixel_index(x, y)[source]

Convert a cartesian coordinate for an led into the index that represents red for that led in the buffer.

Currently, the chaining is assumed to be in a zig-zag, as follows:

  col0 col1 col2 col3 col4 col5 col6 col7 col8 col9
row0 99 98 97 96 95 94 93 92 91 90
row1 80 81 82 83 84 85 86 87 88 89
row2 79 78 77 76 75 74 73 72 71 70
row3 60 61 62 63 64 65 66 67 68 69
row4 59 58 57 56 55 54 53 52 51 50
row5 40 41 42 43 44 45 46 47 48 49
row6 39 38 37 36 35 34 33 32 31 30
row7 20 21 22 23 24 25 26 27 28 29
row8 19 18 17 16 15 14 13 12 11 10
row9 0 1 2 3 4 5 6 7 8 9
Parameters:
  • x – x coordinate of the led in the matrix (counted left-to-right)
  • y – y coordinate of the led in the matrix (counted top-to-bottom)
Returns:

index of the red value of that led (g, b, are +1, +2 of that position respectively) in the buffer

get_red_index(x, y)[source]

Pretty much like get_pixel_index, but this function returns the position of the red value of the given led in the byte buffer.

Parameters:
  • x – x coordinate of led in cartesian system
  • y – y coordinate of led in cartesian system
Returns:

position of “red” in the background buffer

set_font(path: str, size: int)[source]

Load a font to be used for rendering all following text. (see draw_text)

Parameters:
  • path – path to the font
  • size – size of the font. For a 10x10 matrix, 13 is an acceptable, if rather large, choice.
Returns:

nothing