@@ -9,6 +9,8 @@ defmodule Delux do
99 alias Delux.Pattern
1010 alias Delux.Program
1111
12+ require Logger
13+
1214 @ default_slot :status
1315 @ default_slots [ :status , :notification , :user_feedback ]
1416
@@ -60,13 +62,19 @@ defmodule Delux do
6062 * `:backend` - options for the backend
6163 * `:led_path` - the path to the LED directories (defaults to `"/sys/class/leds"`)
6264 * `:hz` - the Linux kernel's `HZ` setting. Delux will adjust its timing based on this setting (defaults to 1000)
65+ * `:initial` - a program or a map of indicators to programs to run on initialization. If
66+ unset, then Delux turns off all indicators on initialization.
6367 """
6468 @ type options ( ) :: [
6569 led_path: String . t ( ) ,
6670 slots: [ slot ( ) ] ,
6771 indicators: % { indicator_name ( ) => indicator_config ( ) } ,
6872 name: atom ( ) | nil ,
69- backend: keyword ( )
73+ backend: keyword ( ) ,
74+ initial:
75+ Program . t ( )
76+ | { Program . t ( ) , slot ( ) }
77+ | { % { indicator_name ( ) => indicator_config ( ) } , slot ( ) }
7078 ]
7179
7280 @ doc """
@@ -226,18 +234,22 @@ defmodule Delux do
226234 slots = options [ :slots ] || options [ :priorities ] || @ default_slots
227235 indicator_configs = options [ :indicators ] || @ default_indicator_config
228236 backend_config = options [ :backend ] || [ ]
237+ initial = options [ :initial ]
229238
230- state = % {
231- indicator_names: Map . keys ( indicator_configs ) ,
232- backend: open_indicators ( backend_config , indicator_configs ) ,
233- slot_to_priority: slots |> Enum . reverse ( ) |> Enum . with_index ( ) |> Map . new ( ) ,
234- active: [ ] ,
235- brightness: 100 ,
236- current: % { } ,
237- refresh_time: :infinity
238- }
239+ state =
240+ % {
241+ indicator_names: Map . keys ( indicator_configs ) ,
242+ backend: open_indicators ( backend_config , indicator_configs ) ,
243+ slot_to_priority: slots |> Enum . reverse ( ) |> Enum . with_index ( ) |> Map . new ( ) ,
244+ active: [ ] ,
245+ brightness: 100 ,
246+ current: % { } ,
247+ refresh_time: :infinity
248+ }
249+ |> initialize_indicators ( initial )
250+ |> refresh_indicators ( )
239251
240- { :ok , refresh_indicators ( state ) }
252+ { :ok , state }
241253 end
242254
243255 @ impl GenServer
@@ -383,6 +395,36 @@ defmodule Delux do
383395 [ entry | pop_entry ( rest , indicator_name ) ]
384396 end
385397
398+ # initialize_indicators takes options that are analogous to those passed to render/3
399+ defp initialize_indicators ( state , nil ) do
400+ state
401+ end
402+
403+ defp initialize_indicators ( state , % Program { } = program ) do
404+ initialize_indicators ( state , { % { @ default_indicator => program } , @ default_slot } )
405+ end
406+
407+ defp initialize_indicators ( state , { % Program { } = program , slot } ) when is_atom ( slot ) do
408+ initialize_indicators ( state , { % { @ default_indicator => program } , slot } )
409+ end
410+
411+ defp initialize_indicators ( state , { indicators , slot } )
412+ when is_map ( indicators ) and is_atom ( slot ) do
413+ case do_render ( state , slot , indicators ) do
414+ { :ok , new_state } ->
415+ new_state
416+
417+ error ->
418+ Logger . error ( "Error initializing indicators to #{ inspect ( indicators ) } : #{ inspect ( error ) } " )
419+ state
420+ end
421+ end
422+
423+ defp initialize_indicators ( state , other ) do
424+ Logger . error ( "Don't know how to initialize indicators to #{ inspect ( other ) } " )
425+ state
426+ end
427+
386428 defp refresh_indicators ( state ) do
387429 current_time = System . monotonic_time ( :millisecond )
388430
0 commit comments