breadboard v0.0.5 Breadboard.Switch View Source
Manage the 'switch' operation on gpio.
Any switch is supervised in the application, but if the switch (child) process crashed is never restarted.
Examples
Turn on/off the switch
iex> if(Breadboard.get_platform()==:stub ) do
iex> {:ok, switch} = Breadboard.Switch.connect([pin: :gpio18, direction: :output])
iex> Breadboard.Switch.turn_on(switch)
iex> 1 = Breadboard.Switch.get_value(switch)
iex> Breadboard.Switch.turn_off(switch)
iex> 0 = Breadboard.Switch.get_value(switch)
iex> Breadboard.Switch.set_value(switch, 1)
iex> 1 = Breadboard.Switch.get_value(switch)
iex> Breadboard.Switch.set_value(switch, 0)
iex> 0 = Breadboard.Switch.get_value(switch)
iex> nil
iex> end
nil
Pin/Label of the switch
iex> if(Breadboard.get_platform()==:stub ) do
iex> {:ok, switch} = Breadboard.Switch.connect([pin: :gpio5, direction: :output])
iex> 5 = Breadboard.Switch.pin_number(switch)
iex> :gpio5 = Breadboard.Switch.pin_label(switch)
iex> nil
iex> end
nil
iex> if(Breadboard.get_platform()==:sunxi ) do
iex> {:ok, switch} = Breadboard.Switch.connect([pin: :pa6, direction: :input])
iex> 6 = Breadboard.Switch.pin_number(switch)
iex> :pa6 = Breadboard.Switch.pin_label(switch)
iex> nil
iex> end
nil
Switch with initial value
iex> if(Breadboard.get_platform()==:stub ) do
iex> {:ok, switch1} = Breadboard.Switch.connect([pin: :gpio18, direction: :output, initial_value: 1])
iex> {:ok, switch0} = Breadboard.Switch.connect([pin: "GPIO9", direction: :output, initial_value: 0])
iex> 0 = Breadboard.Switch.get_value(switch0)
iex> 1 = Breadboard.Switch.get_value(switch1)
iex> nil
iex> end
nil
Switch as input and output (Stub HAL with pair of GPIOs is connected)
iex> if(Breadboard.get_platform()==:stub ) do # only for Unit Test purpose
...> {:ok, switch0} = Breadboard.Switch.connect([pin: :gpio0, direction: :input, initial_value: 0])
...> :ok = Breadboard.Switch.set_direction(switch0, :output)
...> {:ok, switch1} = Breadboard.Switch.connect([pin: :gpio1, direction: :input, initial_value: 0])
...> Breadboard.Switch.get_value(switch0)
...> Breadboard.Switch.get_value(switch1)
...> Breadboard.Switch.turn_on(switch0)
...> 1 = Breadboard.Switch.get_value(switch1)
...> Breadboard.Switch.disconnect(switch0)
...> Breadboard.Switch.disconnect(switch1)
...> nil
...> end
nil
Simple Interrupt test, by function:
iex> if(Breadboard.get_platform()==:stub ) do # only for Unit Test purpose
...> defmodule InterruptsTest do
...> def interrupt_service_routine(_irq_info=%Breadboard.IRQInfo{}) do
...> # on interrupt turn on 'test pin' value
...> {:ok, switch_test} = Breadboard.Switch.connect([pin: :gpio3, direction: :output])
...> Breadboard.Switch.turn_on(switch_test)
...> Breadboard.Switch.disconnect(switch_test)
...> end
...> end
...> # open two pin as in and out ...
...> {:ok, switch_in} = Breadboard.Switch.connect([pin: :gpio1, direction: :input])
...> {:ok, switch_out} = Breadboard.Switch.connect([pin: :gpio1, direction: :output])
...> # ... plus a pin as test raised to one on irq notification
...> {:ok, switch_test} = Breadboard.Switch.connect([pin: :gpio3, direction: :output, initial_value: 0])
...> # pin value is 0
...> 0 = Breadboard.Switch.get_value(switch_test)
...> :ok = Breadboard.Switch.set_interrupts(switch_in, [interrupts_receiver: &InterruptsTest.interrupt_service_routine/1, trigger: :both, opts: []])
...> Breadboard.Switch.turn_on(switch_out)
...> Process.sleep(50)
...> 1 = Breadboard.Switch.get_value(switch_test)
...> Breadboard.Switch.disconnect(switch_in)
...> Breadboard.Switch.disconnect(switch_out)
...> Breadboard.Switch.disconnect(switch_test)
...> nil
...> end
nil
... and by message:
iex> if(Breadboard.get_platform()==:stub ) do # only for Unit Test purpose
...> {:ok, switch_in} = Breadboard.Switch.connect([pin: :gpio1, direction: :input])
...> {:ok, switch_out} = Breadboard.Switch.connect([pin: :gpio1, direction: :output, initial_value: 0])
...> # pin value is 0
...> 0 = Breadboard.Switch.get_value(switch_in)
...> :ok = Breadboard.Switch.set_interrupts(switch_in, [interrupts_receiver: self(), trigger: :rising, opts: []])
...> Breadboard.Switch.turn_on(switch_out)
...> receive do
...> {:irq_service_call, %Breadboard.IRQInfo{pin_number: 1, pin_label: :gpio1, new_value: 1}} ->
...> :ok
...> _bad_msg ->
...> raise(RuntimeError, "Unexpected message received")
...> after
...> 1000 ->
...> raise(RuntimeError, "NO message received")
...> end
...> 1 = Breadboard.Switch.get_value(switch_in)
...> nil
...> end
nil
Link to this section Summary
Types
Options for Breadboard.Switch.connect/1
Pull mode as defined in Circuits.GPIO.pull_mode/0
The Switch direction (input or output) - as in Circuits.GPIO.pin_direction/0
Switch value: 0/1 - as in Circuits.GPIO.value/0
Functions
Connect to a GPIO pin.
Disconnect the switch from the pin
Read the current value of a switch
Get the Switch pin label
Get the Switch pin number
Change the direction of the Switch
Enable or disable pin value change notifications
Enable/disable internal pull-up/pull-down resistor
Set the value for a switch (only for :output
switch)
Set the value 0 of a switch (only for :output
switch)
Set the value 1 of a switch (only for :output
switch)
Link to this section Types
connect_options()
View Sourceconnect_options() :: {:pin, any()} | {:direction, switch_direction()} | {:initial_value, value() | :not_set} | {:pull_mode, pull_mode()}
Options for Breadboard.Switch.connect/1
Pull mode as defined in Circuits.GPIO.pull_mode/0
switch_direction()
View Sourceswitch_direction() :: Circuits.GPIO.pin_direction()
The Switch direction (input or output) - as in Circuits.GPIO.pin_direction/0
Switch value: 0/1 - as in Circuits.GPIO.value/0
Link to this section Functions
connect(options)
View Sourceconnect(connect_options()) :: {:ok, reference()} | {:error, atom()}
Connect to a GPIO pin.
Options:
:pin
- any valid 'pin label' managed byBreadboard.Pinout.label_to_pin/1
:direction
- as defined in the Circuit.GPIO.open:initial_value
- as defined in the Circuit.GPIO.open:pull_mode
- as defined in the Circuit.GPIO.open
Return values:
On success the function returns {:ok, switch}
, where switch
is the PID of the supervised 'Switch'
disconnect(switch)
View Sourcedisconnect(reference()) :: :ok | {:error, :not_found}
Disconnect the switch from the pin
Read the current value of a switch
Get the Switch pin label
Get the Switch pin number
set_direction(switch, switch_direction)
View Sourceset_direction(reference(), switch_direction()) :: :ok | {:error, atom()}
Change the direction of the Switch
Enable or disable pin value change notifications:
switch
- the switchirq_opts
- keyword list with:trigger
- as defined inCircuits.GPIO.set_interrupts/3
opts
- as defined inCircuits.GPIO.set_interrupts/3
interrupts_receiver
- where notifications are sent: a function or a receiver
interrupts_receiver:
- when function: notifications are sent invoking the specific function with a single argument: a struct of type
Breadboard.IRQInfo
- when receiver: notifications are sent by sending a message to the given destination identified by receiver as defined in
Kernel.send/2
in the form:{:irq_service_call, Breadboard.IRQInfo}
Return values
:ok
on success
Enable/disable internal pull-up/pull-down resistor
Exaple
iex> if(Breadboard.get_platform()==:stub ) do
iex> {:ok, switch} = Breadboard.Switch.connect([pin: :gpio18, direction: :output])
iex> :ok = Breadboard.Switch.set_pull_mode(switch, :pullup)
iex> nil
iex> end
nil
Set the value for a switch (only for :output
switch)
Set the value 0 of a switch (only for :output
switch)
Set the value 1 of a switch (only for :output
switch)