Skip to content

Commit

Permalink
Merge pull request #39 from mnishiguchi/mnishiguchi/eeprom
Browse files Browse the repository at this point in the history
Add Inky.EEPROM.read/1
  • Loading branch information
lawik authored Nov 7, 2021
2 parents 1f2e69e + 3804deb commit 5eec35f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
81 changes: 81 additions & 0 deletions lib/display/eeprom.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
defmodule Inky.EEPROM do
@moduledoc false

@eep_address 0x50

@display_variants {
:unknown,
"Red pHAT (High-Temp)",
"Yellow wHAT",
"Black wHAT",
"Black pHAT",
"Yellow pHAT",
"Red wHAT",
"Red wHAT (High-Temp)",
"Red wHAT",
:unknown,
"Black pHAT (SSD1608)",
"Red pHAT (SSD1608)",
"Yellow pHAT (SSD1608)",
:unknown,
"7-Color (UC8159)"
}

@colors {:unknown, :black, :red, :yellow, :unknown, :seven_color}

@fields [:width, :height, :color, :pcb_variant, :display_variant, :timestamp]

@enforce_keys @fields
defstruct @fields

@type t :: %__MODULE__{}

@doc """
Reads the device info from the EEPROM. This operation seems to work successfully only once.
## Examples
iex> Inky.EEPROM.read()
{:ok,
%Inky.EEPROM{
color: :red,
display_variant: "Red pHAT (SSD1608)",
height: 104,
pcb_variant: 12,
timestamp: "2021-03-30 08:58:28.9",
width: 212
}}
"""
@spec read(atom()) :: {:ok, Inky.EEPROM.t()} | {:error, any()}
def read(i2c_mod \\ Circuits.I2C) do
with {:ok, ref} <- i2c_mod.open("i2c-1"),
{:ok, data} <- i2c_mod.write_read(ref, @eep_address, <<0>>, 29) do
i2c_mod.close(ref)
parse_data(data)
end
end

# The data might look like this:
#
# <<212, 0, 104, 0, 1, 12, 10, 21, 50, 48, 50, 49, 45, 48, 55, 45, 49, 50, 32, 49, 48, 58, 49, 49, 58, 52, 57, 46, 56>>
#
defp parse_data(
<<width, _, height, _, color, pcb_variant, display_variant, _, timestamp::binary>>
)
when color in 0..5 and display_variant in 0..14 do
{:ok,
__struct__(
width: width,
height: height,
color: elem(@colors, color),
pcb_variant: pcb_variant,
display_variant: elem(@display_variants, display_variant),
timestamp: timestamp
)}
end

defp parse_data(data) do
{:error, {:invalid_data, data}}
end
end
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ defmodule Inky.MixProject do
[
{:circuits_gpio, "~> 0.4"},
{:circuits_spi, "~> 0.1"},
{:circuits_i2c, "~> 0.3"},
{:credo, "~> 1.0.0", only: [:dev, :test], runtime: false},
{:ex_doc, ">= 0.0.0", only: :dev}
]
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
%{
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"},
"circuits_gpio": {:hex, :circuits_gpio, "0.4.1", "344dd34f2517687fd28723e5552571babff5469db05b697181cab860fe7eff23", [:make, :mix], [{:elixir_make, "~> 0.5", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "8031c53c1813f52ccd2926c5e806d192ec5625cb12c3a2bf6b63fecd34999010"},
"circuits_i2c": {:hex, :circuits_i2c, "0.3.4", "d86951092e44487fe6fecbd6544511d894510be2856280eb93258a171bdc1552", [:make, :mix], [{:elixir_make, "~> 0.5", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "3845f9f8140a791b7db0b103899e88c58e437295a76309bb5e4bdab216209955"},
"circuits_spi": {:hex, :circuits_spi, "0.1.3", "a94889abc874e9976f397c649152776d9c0863e5fd3377203c7f0cf992d9609c", [:make, :mix], [{:elixir_make, "~> 0.5", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "09569214ccdff871149dc5d738a20206647b60769a919b5bb1a6a7f843a10f66"},
"credo": {:hex, :credo, "1.0.5", "fdea745579f8845315fe6a3b43e2f9f8866839cfbc8562bb72778e9fdaa94214", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "16105fac37c5c4b3f6e1f70ba0784511fec4275cd8bb979386e3c739cf4e6455"},
"earmark": {:hex, :earmark, "1.3.2", "b840562ea3d67795ffbb5bd88940b1bed0ed9fa32834915125ea7d02e35888a5", [:mix], [], "hexpm", "e3be2bc3ae67781db529b80aa7e7c49904a988596e2dbff897425b48b3581161"},
Expand Down

0 comments on commit 5eec35f

Please sign in to comment.