Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions lib/lcd_display.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,43 @@ defmodule LcdDisplay do
atom => any
}

@doc """
Returns a specification to start this module under a supervisor.
"""
@spec child_spec(list()) :: Supervisor.child_spec()
def child_spec(arg) do
%{
id: __MODULE__,
start: {__MODULE__, :start_link, arg}
}
end

@doc """
Starts a display controller process for a specified.
"""
@spec start_link(config) :: {:ok, pid} | {:error, any}
def start_link(config) when is_map(config) do
@spec start_link(config, GenServer.options()) :: {:ok, pid} | {:error, any}
def start_link(config, opts \\ []) when is_map(config) do
{%{driver_module: driver_module}, other_config} = Map.split(config, [:driver_module])
display = initialize_display(driver_module, other_config)
LcdDisplay.DisplayController.start_link(display)

case initialize_display(driver_module, other_config) do
{:ok, display} ->
LcdDisplay.DisplayController.start_link(display, opts)

{:error, _reason} ->
:ignore
end
end

@doc """
Executes a supported command.
"""
@spec execute(pid, display_command) :: {:ok, display_driver} | {:error, any}
def execute(pid, command) when is_pid(pid) do
LcdDisplay.DisplayController.execute(pid, command)
@spec execute(GenServer.server(), display_command) :: {:ok, display_driver} | {:error, any}
def execute(server, command) do
LcdDisplay.DisplayController.execute(server, command)
end

@spec initialize_display(atom, map) :: {:ok, display_driver} | {:error, any}
defp initialize_display(driver_module, config) when is_atom(driver_module) and is_map(config) do
case driver_module.start(config) do
{:ok, display} -> display
{:error, reason} -> {:error, reason}
end
driver_module.start(config)
end
end
8 changes: 4 additions & 4 deletions lib/lcd_display/display_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ defmodule LcdDisplay.DisplayController do
Starts a display driver process and registers the process with a composite key
of driver module and display name.
"""
@spec start_link(display_driver) :: {:ok, pid} | {:error, any}
def start_link(display_driver) do
GenServer.start_link(__MODULE__, display_driver)
@spec start_link(display_driver, keyword()) :: {:ok, pid} | {:error, any}
def start_link(display_driver, opts \\ []) do
GenServer.start_link(__MODULE__, display_driver, opts)
end

@doc """
Expand All @@ -32,7 +32,7 @@ defmodule LcdDisplay.DisplayController do

@impl true
def handle_call(command, _from, display) do
Logger.info(inspect(command))
# Logger.info(inspect(command))

case result = execute_display_command(command, display) do
{:ok, new_display} -> {:reply, result, Map.merge(display, new_display)}
Expand Down