Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
7d80cce
Start work on on Inky Impression
lawik Oct 10, 2021
8ee2dda
More work on setup and update for Inky Impression
lawik Oct 27, 2021
6e11741
Add all of the Impression code but not working, added cs0 but that wo…
lawik Oct 27, 2021
c56c9be
Kinda working!
axelson Nov 8, 2021
e415e0e
Kind of like a flag
axelson Nov 8, 2021
fc37b1f
Cleaning
axelson Nov 9, 2021
cc82eda
Clean up cs pin calls
axelson Nov 9, 2021
99d2cd5
clean
axelson Nov 9, 2021
7a636df
Merge pull request #42 from axelson/jax/inky-impression
lawik Nov 9, 2021
753d5a8
Fix log typo
Jul 3, 2022
3f46d9a
Adds comments to byte commands defined for Impression in hal
Jul 3, 2022
3142798
Fix typo in comment
Jul 3, 2022
ae2e48f
Update mix deps for development on jasonmj
Jul 3, 2022
07f75e6
Update display definition for impression
Jul 3, 2022
63cdb4f
Add handling for impression in packed_width and packed_height
Jul 3, 2022
13793cd
Fix usage of state in log function
Jul 3, 2022
853021b
Add set_dimensions to do_update for impression
Jul 3, 2022
46a3b40
Replace buffer generation with PixelUtil.pixels_to_bits
Jul 3, 2022
9dfa181
Add a color_map for the impression
Jul 3, 2022
5bfb57f
Add notes about WIP toward impression support
Jul 3, 2022
4689834
Reverting dev changes to mix.exs
Jul 3, 2022
3bcf4f1
Cleaning up unused code
Jul 8, 2022
590c25f
Adding bit_size option to PixelUtil.pixels_to_bits
Jul 8, 2022
d5bca11
Merge pull request #46 from jasonmj/impression
lawik Jul 11, 2022
9abc79f
Cleans up code for PR
Jul 12, 2022
80bdee8
Adds painter test
Jul 12, 2022
f86d1b8
Support circuits_gpio 1.0
axelson Jul 13, 2022
b30be19
Merge pull request #47 from axelson/impression
Jul 13, 2022
a68e31a
Add support for buttons on Inky Impression
Jul 23, 2022
40cda84
Support sending button messages to a process by registered atom name
Aug 1, 2022
6a6cd42
Merge pull request #48 from jasonmj/impression
lawik Aug 1, 2022
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
138 changes: 138 additions & 0 deletions lib/buttons.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
defmodule Inky.Buttons do
@moduledoc """
The Inky Impression has 4 buttons that are monitored independently from the display
and can be started in supervison.

Supply the `:handler` option as an atom, a pid, or `{module, function, args}` tuple
specifying where to send events to. If no handler is supplied, events are simply logged

```elixir
Inky.Buttons.start_link(handler: self())
```

You can also query the current value of a button at any time

```elixir
Inky.Buttons.get_value(:a)
```
"""

use GenServer

alias Circuits.GPIO

require Logger

@typedoc """
Button name for Inky Impression button

These are labelled A, B, X, and Y on the board.
"""
@type name() :: :a | :b | :x | :y

defmodule Event do
defstruct [:action, :name, :value, :timestamp]

@type t :: %Event{
action: :pressed | :released,
name: Buttons.name(),
value: 1 | 0,
timestamp: non_neg_integer()
}
end

@pin_a 5
@pin_b 6
@pin_x 16
@pin_y 24

@doc """
Start a GenServer to watch the buttons on the Inky Impression

Options:

* `:handler` - pass an atom, a pid, or an MFA to receive button events
"""
@spec start_link(keyword) :: GenServer.on_start()
def start_link(opts \\ []) do
GenServer.start_link(__MODULE__, opts, name: __MODULE__)
end

@doc """
Return the current state of the button

`0` - released
`1` - pressed
"""
@spec get_value(name()) :: 0 | 1
def get_value(button) do
GenServer.call(__MODULE__, {:get_value, button})
end

@impl GenServer
def init(opts) do
{:ok, %{button_to_ref: %{}, pin_to_button: %{}, handler: opts[:handler]}, {:continue, :init}}
end

@impl GenServer
def handle_continue(:init, state) do
{:ok, a} = GPIO.open(@pin_a, :input, pull_mode: :pullup)
{:ok, b} = GPIO.open(@pin_b, :input, pull_mode: :pullup)
{:ok, x} = GPIO.open(@pin_x, :input, pull_mode: :pullup)
{:ok, y} = GPIO.open(@pin_y, :input, pull_mode: :pullup)
:ok = GPIO.set_interrupts(a, :both)
:ok = GPIO.set_interrupts(b, :both)
:ok = GPIO.set_interrupts(x, :both)
:ok = GPIO.set_interrupts(y, :both)

button_to_ref = %{a: a, b: b, x: x, y: y}

pin_to_button = %{
@pin_a => :a,
@pin_b => :b,
@pin_x => :x,
@pin_y => :y
}

{:noreply, %{state | button_to_ref: button_to_ref, pin_to_button: pin_to_button}}
end

@impl GenServer
def handle_call({:get_value, name}, _from, state) do
inverted_value = GPIO.read(state.button_to_ref[name])
value = 1 - inverted_value

{:reply, value, state}
end

@impl GenServer
def handle_info({:circuits_gpio, pin, timestamp, inverted_value}, state) do
value = 1 - inverted_value
action = if value != 0, do: :pressed, else: :released

event = %Event{
action: action,
name: state.pin_to_button[pin],
value: value,
timestamp: timestamp
}

_ = send_event(state.handler, event)

{:noreply, state}
end

def handle_info(_other, state), do: {:noreply, state}

defp send_event(handler, event) when is_atom(handler), do: send(handler, event)

defp send_event(handler, event) when is_pid(handler), do: send(handler, event)

defp send_event({m, f, a}, event) when is_atom(m) and is_atom(f) and is_list(a) do
apply(m, f, [event | a])
end

defp send_event(_, event) do
Logger.info("[Inky] unhandled button event - #{inspect(event)}")
end
end
21 changes: 20 additions & 1 deletion lib/display/display.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,37 @@ defmodule Inky.Display do
"""

alias Inky.LookupTables

@type t() :: %__MODULE__{}

@enforce_keys [:type, :width, :height, :packed_dimensions, :rotation, :accent, :luts]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If these are required for one display but not the other, we should consider refactoring things so that we don't force re-usability where it is not required/meaningful (maybe that's exactly what you did here? :D). "False generalisation" only makes things unclear since you don't knew when/if something is required/missing.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've reverted this change too, since these keys can peacefully coexist in the struct the defines the impression display. We're not using packed dimensions because it's redundant/unnecessary, but if someone wants to use them in the future, it'll be possible.

As for luts, it's not used at all for the impression. Not sure we need to define a separate struct+key enforcement just for that though.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good food for thought, though... if the devices are diverting, perhaps our configurations should, too.

One way of dealing with it would be to have a protocol common for all types, for the things that are common to all displays and then have the specific drivers check if it is a value of the correct underlying type at runtime during init. But that might be a bit more than what you signed up for at this point and a bit of over-engineering before we have several displays where the fit is off... thoughts?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds like it might be a worthwhile refactor to open a separate issue for.

defstruct type: nil,
width: 0,
height: 0,
packed_dimensions: %{},
packed_resolution: nil,
rotation: 0,
accent: :black,
luts: <<>>

@spec spec_for(:impression) :: Inky.Display.t()
def spec_for(type = :impression) do
width = 600
height = 448

%__MODULE__{
type: type,
width: width,
height: height,
packed_dimensions: %{},
packed_resolution:
<<width::unsigned-big-integer-size(16)>> <> <<height::unsigned-big-integer-size(16)>>,
rotation: 0,
accent: nil,
luts: <<>>
}
end

@spec spec_for(:phat | :what, :black | :red | :yellow) :: Inky.Display.t()
def spec_for(type, accent \\ :black)

Expand Down
9 changes: 5 additions & 4 deletions lib/display/pixelutil.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Inky.PixelUtil do
PixelUtil maps pixels to bitstrings to be sent to an Inky screen
"""

def pixels_to_bits(pixels, width, height, rotation_degrees, color_map) do
def pixels_to_bits(pixels, width, height, rotation_degrees, color_map, bit_size \\ 1) do
{outer_axis, dimension_vectors} =
rotation_degrees
|> normalised_rotation()
Expand All @@ -13,7 +13,8 @@ defmodule Inky.PixelUtil do
|> rotated_ranges(width, height)
|> do_pixels_to_bits(
&pixels[pixel_key(outer_axis, &1, &2)],
&(color_map[&1] || color_map.miss)
&(color_map[&1] || color_map.miss),
bit_size
)
end

Expand Down Expand Up @@ -61,10 +62,10 @@ defmodule Inky.PixelUtil do
defp rotated_dimension(_width, height, {:y, 1}), do: 0..(height - 1)
defp rotated_dimension(_width, height, {:y, -1}), do: (height - 1)..0

defp do_pixels_to_bits({i_range, j_range}, pixel_picker, cmap) do
defp do_pixels_to_bits({i_range, j_range}, pixel_picker, cmap, bit_size) do
for i <- i_range,
j <- j_range,
do: <<cmap.(pixel_picker.(i, j))::size(1)>>,
do: <<cmap.(pixel_picker.(i, j))::size(bit_size)>>,
into: <<>>
end

Expand Down
Loading