Skip to content

Commit

Permalink
Merge pull request #44 from mnishiguchi/mnishiguchi/fix-eeprom-dimens…
Browse files Browse the repository at this point in the history
…ions

Correct dimensions from EEPROM
  • Loading branch information
lawik authored Nov 9, 2021
2 parents 5eec35f + 7d8c50e commit 2060e89
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 9 deletions.
19 changes: 10 additions & 9 deletions lib/display/eeprom.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ defmodule Inky.EEPROM do
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
@doc """
Parses data from Inky's EEPROM.
"""
@spec parse_data(<<_::232>>) :: {:ok, t()} | {:error, {:invalid_data, any()}}
def parse_data(
<<width::little-16, height::little-16, color, pcb_variant, display_variant, _,
timestamp::binary>>
)
when color in 0..5 and display_variant in 0..14 do
{:ok,
__struct__(
width: width,
Expand All @@ -75,7 +76,7 @@ defmodule Inky.EEPROM do
)}
end

defp parse_data(data) do
def parse_data(data) do
{:error, {:invalid_data, data}}
end
end
40 changes: 40 additions & 0 deletions test/eeprom_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
defmodule Inky.EEPROMTest do
@moduledoc false

use ExUnit.Case
import Inky.EEPROM

describe "parse_data/1" do
test "returns correct struct for Inky pHAT SSD1608" do
inky_ssd1608_eeprom =
<<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>>

assert {:ok,
%Inky.EEPROM{
color: :black,
display_variant: "Black pHAT (SSD1608)",
height: 104,
pcb_variant: 12,
timestamp: "2021-07-12 10:11:49.8",
width: 212
}} = parse_data(inky_ssd1608_eeprom)
end

test "returns correct struct for Inky Impression" do
inky_impression_eeprom =
<<88, 2, 192, 1, 0, 12, 14, 21, 50, 48, 50, 49, 45, 48, 56, 45, 50, 53, 32, 49, 55, 58,
49, 50, 58, 49, 48, 46, 51>>

assert {:ok,
%Inky.EEPROM{
color: :unknown,
display_variant: "7-Color (UC8159)",
height: 448,
pcb_variant: 12,
timestamp: "2021-08-25 17:12:10.3",
width: 600
}} = parse_data(inky_impression_eeprom)
end
end
end

0 comments on commit 2060e89

Please sign in to comment.