Build the Service Layer
Adapters are boundary layers that run on a specified target to control hardware and also in host environments for convenience. With the adapters in hand, the clock is almost complete. It can translate an Elixir time to LEDs upon request. The last problem is making that request at one-second intervals.
Recall that the adapters already send one second :tick messages. The new service layer needs only respond to them.
Build the Service
This GenServer in lib/clock/server.ex will be short and sweet. It will need to keep an adapter, a time zone, and the local time, like this:
| defmodule Clock.Server do |
| defstruct [:adapter, :time, :timezone] |
| use GenServer |
| @spi_bus_name "spidev0.0" |
| @timezone "US/Eastern" ... |
Get Build a Binary Clock with Elixir and Nerves now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.