NanoKVMGo.Display.Canvas (nano_kvm_go v0.4.0)

Copy Markdown View Source

Pictures for NanoKVMGo.Display: filled rectangles and text, drawn in the order given over a background, then rendered as RGB565.

alias NanoKVMGo.Display.Canvas

Canvas.new(240, 284, Canvas.rgb(0, 0, 0))
|> Canvas.rect(0, 0, 240, 40, Canvas.rgb(0, 80, 160))
|> Canvas.text(8, 4, "NanoKVM-Go", Canvas.rgb(255, 255, 255), scale: 2)
|> Canvas.render()

Text is in an 8 × 16 pixel font (Spleen) of printable ASCII, other characters shown as ?, scaled by a whole factor.

Summary

Types

A colour in RGB565, see rgb/3.

t()

Functions

Fills a rectangle; what falls outside the picture is left out.

The picture as rows of little-endian RGB565 pixels, top to bottom, as a framebuffer takes them.

The RGB565 colour nearest to 8-bit red, green and blue.

Writes text from its top left corner; the background shows between the strokes.

The width of text in pixels, a character each Unicode code point, as text/6 draws it.

Types

color()

@type color() :: 0..65535

A colour in RGB565, see rgb/3.

t()

@type t() :: %NanoKVMGo.Display.Canvas{
  background: color(),
  height: pos_integer(),
  items: [item()],
  width: pos_integer()
}

Functions

new(width, height, background \\ 0)

@spec new(pos_integer(), pos_integer(), color()) :: t()

An empty picture.

rect(canvas, x, y, width, height, color)

@spec rect(t(), integer(), integer(), non_neg_integer(), non_neg_integer(), color()) ::
  t()

Fills a rectangle; what falls outside the picture is left out.

render(canvas, opts \\ [])

@spec render(t(), [{:rotate, 0 | 180}]) :: binary()

The picture as rows of little-endian RGB565 pixels, top to bottom, as a framebuffer takes them.

Options

  • :rotate - 0 (the default) or 180 degrees, for a display mounted upside down

rgb(r, g, b)

@spec rgb(0..255, 0..255, 0..255) :: color()

The RGB565 colour nearest to 8-bit red, green and blue.

iex> NanoKVMGo.Display.Canvas.rgb(255, 255, 255)
0xFFFF
iex> NanoKVMGo.Display.Canvas.rgb(255, 0, 0)
0xF800

text(canvas, x, y, string, color, opts \\ [])

@spec text(t(), integer(), integer(), String.t(), color(), [{:scale, pos_integer()}]) ::
  t()

Writes text from its top left corner; the background shows between the strokes.

Options

  • :scale - the size, 1 for 8 × 16 pixels a character (the default)

text_width(string, scale \\ 1)

@spec text_width(String.t(), pos_integer()) :: non_neg_integer()

The width of text in pixels, a character each Unicode code point, as text/6 draws it.

iex> NanoKVMGo.Display.Canvas.text_width("NanoKVM", 2)
112