Packages

A library for interfacing with SSD1322 based OLED displays

Current section

4 Versions

Jump to

Compare versions

4 files changed
+91 additions
-7 deletions
  @@ -21,4 +21,4 @@
21 21 {<<"optional">>,false},
22 22 {<<"repository">>,<<"hexpm">>},
23 23 {<<"requirement">>,<<"~> 0.1">>}]]}.
24 - {<<"version">>,<<"0.3.0">>}.
24 + {<<"version">>,<<"0.3.1">>}.
  @@ -5,39 +5,81 @@ defmodule SSD1322 do
5 5
6 6 use GenServer
7 7
8 + @doc """
9 + Starts a connection to an SSD1322 with the given parameters, wrapped in a GenServer to serialize access to the device.
10 + Returns an `{:ok, pid}` tuple where the pid is passed in to the other functions in this module.
11 +
12 + Options are passed as a keyword list with the following possible values:
13 +
14 + * `spi_connection_opts`: A nested keyword list containing any of the possible
15 + values below:
16 + * `spi_dev`: The name of the spi device to connect to. Defaults to `spidev0.0`
17 + * `dc_pin`: The GPIO pin number of the line to use for D/C select. Defaults to 24
18 + * `reset_pin`: The GPIO pin number of the line to use for reset. Defaults to 25
19 + * `conn`: A pre-existing SSD1322.SPIConnection struct, if you already have one
20 + * `width`: The width of the display in pixels. Must be a multiple of 4. Defaults to 256
21 + * `height`: The height of the display in pixels. Defaults to 64
22 + """
8 23 def start_link(args \\ []) do
9 24 name = args |> Keyword.get(:name)
10 25 GenServer.start_link(__MODULE__, args, name: name)
11 26 end
12 27
28 + @doc """
29 + Issues a reset to the SSD1322 device.
30 + """
13 31 def reset(pid) do
14 32 GenServer.call(pid, :reset)
15 33 end
16 34
35 + @doc """
36 + Turns the display on.
37 + """
17 38 def display_on(pid) do
18 39 GenServer.call(pid, :display_on)
19 40 end
20 41
42 + @doc """
43 + Turns the display off.
44 + """
21 45 def display_off(pid) do
22 46 GenServer.call(pid, :display_off)
23 47 end
24 48
49 + @doc """
50 + Sets the contrast of the display. Valid values range from 0 (lowest contrast) to 255 (highest contrast).
51 + """
25 52 def contrast(pid, contrast \\ 0xFF) do
26 53 GenServer.call(pid, {:contrast, contrast})
27 54 end
28 55
29 - def clear(pid, grey \\ 0x00) do
56 + @doc """
57 + Clears the display to the specified grey level.
58 + Valid values for `grey` are from 0 (black) to 15 (whatever colour your display is). Defaults to 0.
59 + """
60 + def clear(pid, grey \\ 0) do
30 61 GenServer.call(pid, {:clear, grey})
31 62 end
32 63
64 + @doc """
65 + Draws the specified bitmap. The bitmap must be in packed 4-bit greyscale format and the size
66 + of the full display as configured. For pixel format details see SSD1322.Device.draw.
67 + """
33 68 def draw(pid, bitmap) do
34 69 GenServer.call(pid, {:draw, bitmap})
35 70 end
36 71
72 + @doc """
73 + Draws the specified bitmap at coordinates `{x, y}`. The bitmap must be in packed 4-bit greyscale format and the size
74 + corresponding to the specified width & height. For pixel format details see SSD1322.Device.draw.
75 +
76 + Both `x` and `width` must be a multiple of 4.
77 + """
37 78 def draw(pid, bitmap, {x, y}, {w, h}) do
38 79 GenServer.call(pid, {:draw, bitmap, {x, y}, {w, h}})
39 80 end
40 81
82 + @doc false
41 83 def init(args) do
42 84 {:ok, SSD1322.Device.init(args)}
43 85 end
  @@ -11,10 +11,25 @@ defmodule SSD1322.Device do
11 11
12 12 defstruct conn: nil, width: 0, height: 0
13 13
14 - @gdram_row_width 480
15 -
16 14 alias SSD1322.SPIConnection
17 15
16 + @gdram_row_width 480
17 +
18 + @doc """
19 + Sets up a connection to an SSD1322. Returns an opaque struct of type
20 + SSD1322.Device suitable for passing to other functions in this module.
21 +
22 + Options are passed as a keyword list with the following possible values:
23 +
24 + * `spi_connection_opts`: A nested keyword list containing any of the possible
25 + values below:
26 + * `spi_dev`: The name of the spi device to connect to. Defaults to `spidev0.0`
27 + * `dc_pin`: The GPIO pin number of the line to use for D/C select. Defaults to 24
28 + * `reset_pin`: The GPIO pin number of the line to use for reset. Defaults to 25
29 + * `conn`: A pre-existing SSD1322.SPIConnection struct, if you already have one
30 + * `width`: The width of the display in pixels. Must be a multiple of 4. Defaults to 256
31 + * `height`: The height of the display in pixels. Defaults to 64
32 + """
18 33 def init(opts \\ []) do
19 34 spi_connection_opts = opts |> Keyword.get(:spi_connection_opts, [])
20 35
  @@ -31,6 +46,9 @@ defmodule SSD1322.Device do
31 46 session
32 47 end
33 48
49 + @doc """
50 + Issues a reset to the SSD1322 device.
51 + """
34 52 def reset(%__MODULE__{conn: conn}) do
35 53 SPIConnection.reset(conn)
36 54 SPIConnection.command(conn, <<0xFD>>, <<0x12>>)
  @@ -53,24 +71,48 @@ defmodule SSD1322.Device do
53 71 SPIConnection.command(conn, <<0xA9>>)
54 72 end
55 73
74 + @doc """
75 + Turns the display on.
76 + """
56 77 def display_on(%__MODULE__{conn: conn}) do
57 78 SPIConnection.command(conn, <<0xAF>>)
58 79 end
59 80
81 + @doc """
82 + Turns the display off.
83 + """
60 84 def display_off(%__MODULE__{conn: conn}) do
61 85 SPIConnection.command(conn, <<0xAE>>)
62 86 end
63 87
88 + @doc """
89 + Sets the contrast of the display. Valid values range from 0 (lowest contrast) to 255 (highest contrast).
90 + """
64 91 def contrast(%__MODULE__{conn: conn}, contrast) do
65 92 SPIConnection.command(conn, <<0xC1>>, <<contrast::8>>)
66 93 end
67 94
95 + @doc """
96 + Clears the display to the specified grey level.
97 + Valid values for `grey` are from 0 (black) to 15 (whatever colour your display is). Defaults to 0.
98 + """
68 99 def clear(%__MODULE__{width: width, height: height} = device, grey \\ 0) do
69 100 draw(device, :binary.copy(<<grey::4, grey::4>>, div(width * height, 2)), {0, 0}, {width, height})
70 101 end
71 102
72 - # This function does not perform any clipping or validation on the given binary other than to other than to
73 - # validate that its x offset and width are both multiples of 4
103 + @doc """
104 + Draws the specified bitmap. The bitmap must be in packed 4-bit greyscale format and the size
105 + of the full display as configured.
106 +
107 + The pixel format is packed 4-bit greyscale in the following format
108 + * Row length must be a multiple of 4
109 + * Each pixel is represented by 4 bits from 0 (fully off) to 15 (fully on)
110 + * Pixels are packed 2 per byte, in left-to-right order
111 + * Rows are packed in top-to-bottom order
112 +
113 + This function does not perform any clipping or validation on the given binary other than to other than to
114 + validate that its x offset and width are both multiples of 4.
115 + """
74 116 def draw(%__MODULE__{conn: conn, width: display_width}, binary, {x, y}, {width, height}) do
75 117 if rem(x, 4) != 0, do: raise("Cannot draw when x is not divisible by 4 (x=#{x})")
76 118 if rem(width, 4) != 0, do: raise("Cannot draw when width is not divisible by 4 (width=#{width})")
  @@ -4,7 +4,7 @@ defmodule SSD1322.MixProject do
4 4 def project do
5 5 [
6 6 app: :ssd1322,
7 - version: "0.3.0",
7 + version: "0.3.1",
8 8 elixir: "~> 1.8",
9 9 start_permanent: Mix.env() == :prod,
10 10 description: description(),