Commit 34d2a4b5 authored by Marcus Greiff's avatar Marcus Greiff

Added text based setting of GPIO allowing in/out configuration and refactor code

parent a083ea7d
Lowest form of communication woth the GPIO pins. The available pins are
listed in the "channel" parameter, and appear as directories in /sys/class.
Any of these GPIOs can be run in various ways, with any operation specified
by a sequence of three entries
(channel, operation, entry) = (int32, int32, String)
For instance, if the GPIO "gpio30" is to be configured as a output pin and set
to high, the following two writes aould be done.
write!(GPIO, 14, 2, "out")
write!(GPIO, 14, 1, "1")
struct GPIO
writeOperations = [
Dict("dir" => "value", "entries"=>["1", "0"])
Dict("dir" => "direction", "entries"=>["in", "out"])
readOperations = [
channels =[
function write!(::GPIO, index::Int32, operation::Int32, entry::Bool)
(index <= 0 || index > length(channels)) && error("Invalid GPIO index: $index")
(operation <= 0 || operation > length(writeOperations)) && error("Invalid GPIO operation: $operation")
filename = "/sys/class/gpio/$(channels[index])/$(writeOperations[operation]["dir"])"
value = writeOperations[operation]["entries"][entry ? 1 : 2]
file = open(filename, "r+")
write(file, "$(value)")
#function, ind::Int32)
# (ind < 0 || ind > length(channels)) && error("Invalid SysLEND ind: $ind")
# println("not yet supported")
# l = 0
# return l == "1"
using Base.Test
device = getdev("gpio")
gpio_state = true
@testset "GPIO Tests" begin
@testset "Error Handling" begin
# Attempt to initialize faulty device
@test_throws ErrorException getdev("wrong_device_name")
# Test that an exception is thrown when a faulty ID is given
@test_throws ErrorException write!(device, 100, 1, gpio_state)
# Test that an exception is thrown when a faulty ID is given
@test_throws ErrorException write!(device, 0, 1, gpio_state)
@testset "IO Communication" begin
# Instanciate all possible leds and perform 10 read/write commands
# with the set high/low operation (1)
operation = 1
for i = 1:10
for index = 1:length(channels)
write!(device, index, operation, gpio_state)
#for j = 1:4
#val = read(device, j)
#@test val == gpio_state
gpio_state = !gpio_state
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment