Last active
May 20, 2017 03:02
-
-
Save wsmoak/c1fd4e95578933e23388 to your computer and use it in GitHub Desktop.
Blink an external LED with elixir_ale
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule BlinkyAle do | |
require Logger | |
def start do | |
{ok,pid} = Gpio.start_link(4,:output) | |
blink_forever(pid) | |
end | |
def blink_forever(pid) do | |
Logger.debug "Turning ON" | |
Gpio.write(pid,1) | |
:timer.sleep 1000 | |
Logger.debug "turning OFF" | |
Gpio.write(pid,0) | |
:timer.sleep 1000 | |
blink_forever(pid) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule BlinkyProx do | |
require Logger | |
#register address | |
@cmd 0x80 | |
@prox_result_h 0x87 | |
@prox_result_l 0x88 | |
def start do | |
{:ok,led_pid} = Gpio.start_link(4,:output) | |
#blink_forever(led_pid) | |
{:ok,prox_pid} = I2c.start_link("i2c-1", 0x13) | |
IO.inspect prox_pid | |
# what is the current value of the command register? | |
I2c.write_read(prox_pid,<<@cmd>> ,1) |> Base.encode16 |> IO.inspect | |
# turn on proximity sensing by setting bits 0 and 1 | |
I2c.write(prox_pid, <<@cmd, 0x03>> ) | |
# check the command register | |
I2c.write_read(prox_pid,<<@cmd>> ,1) |> Base.encode16 |> IO.inspect | |
# use this to loop and print out proximity values | |
# measure(prox_pid) | |
prox_led(prox_pid,led_pid) | |
end | |
# turn the led on and off depending on proximity | |
def prox_led(prox_pid,led_pid) do | |
# binary pattern matching to get the two bytes as an int | |
<< val :: 16 >> = I2c.write_read(prox_pid,<<@prox_result_h>> ,2) | |
IO.inspect val | |
# value determined by running measure/1 and observing the output | |
if val > 2100 do | |
Logger.debug "ON" | |
Gpio.write(led_pid,1) | |
else | |
Logger.debug "OFF" | |
Gpio.write(led_pid,0) | |
end | |
:timer.sleep 513 | |
prox_led(prox_pid,led_pid) | |
end | |
def measure(prox_pid) do | |
# read the 2 bytes of proximity data and loop | |
val = I2c.write_read(prox_pid,<<@prox_result_h>> ,2) | |
# display the value in different ways | |
val |> IO.inspect(binaries: :as_binaries) | |
val |> Base.encode16 |> IO.inspect | |
<< my_int :: 16 >> = val | |
IO.inspect my_int | |
IO.puts "" | |
# by default the VCNL4010 reads data about twice a second | |
:timer.sleep 513 | |
measure(prox_pid) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Blinky.Mixfile do | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment