Skip to content

Commit

Permalink
Fix data size check issue
Browse files Browse the repository at this point in the history
  • Loading branch information
luisgabrielroldan committed Nov 12, 2019
1 parent 2e4ec7e commit 5f3ad1b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/oled/display/impl/ssd_1306.ex
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ defmodule OLED.Display.Impl.SSD1306 do
end

def display_frame(%__MODULE__{} = state, data, opts) do
if data != state.width * state.height / 8 do
if byte_size(data) == state.width * state.height / 8 do
display(%{state | buffer: data}, opts)
else
{:error, :invalid_data_size}
Expand Down
39 changes: 39 additions & 0 deletions test/oled/display/impl/ssd_1306_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,43 @@ defmodule OLED.Display.Impl.SSD1306Test do
assert_received {:command, 2}
assert_received {:command, 3}
end

describe "display_frame/2" do
test "with valid data" do
state = %SSD1306{
dev: %DummyDev{},
width: 96,
height: 48
}

data =
for _ <- 1..576, into: <<>> do
<<0>>
end

assert %SSD1306{} = SSD1306.display_frame(state, data, memory_mode: :vertical)

assert_received {:command, 32}
assert_received {:command, 1}
assert_received {:transfer, ^data}
assert_received {:command, 32}
assert_received {:command, 0}
end

test "with invalid data" do
state = %SSD1306{
dev: %DummyDev{},
width: 96,
height: 48
}

data =
for _ <- 1..200, into: <<>> do
<<0>>
end

assert {:error, :invalid_data_size} =
SSD1306.display_frame(state, data, memory_mode: :vertical)
end
end
end
12 changes: 6 additions & 6 deletions test/support/dummy_dev.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ defmodule OLED.DummyDev do
end

@impl true
def reset(%__MODULE__{}) do
def reset(%__MODULE__{} = state) do
send(self(), :reset)
%__MODULE__{}
state
end

@impl true
def transfer(%__MODULE__{}, buffer) do
def transfer(%__MODULE__{} = state, buffer) do
send(self(), {:transfer, buffer})
%__MODULE__{}
state
end

@impl true
def command(%__MODULE__{}, cmd) do
def command(%__MODULE__{} = state, cmd) do
send(self(), {:command, cmd})
%__MODULE__{}
state
end
end

0 comments on commit 5f3ad1b

Please sign in to comment.