GPIO_test.jl 3.78 KB
Newer Older
1
using LabConnections.BeagleBone
2
import LabConnections.BeagleBone: initdev, listdev, closedev, printdev, write!, read, gpio_channels
3 4 5

using Base.Test

6
@testset "GPIO tests" begin
7

8 9 10 11
    @testset "Inialization/Termination" begin
        # Initialize three devices
        initdev("gpio", 1)
        @test sum(listdev()) == 1
12

13 14
        initdev("gpio", 3)
        @test sum(listdev()) == 2
15

16 17
        initdev("gpio", 5)
        @test sum(listdev()) == 3
18

19
        #printdev("gpio", 3)
20

21 22 23 24
        # Attempt to initialize a device which has already been initialized
        @test_throws ErrorException initdev("gpio", 1)
        @test_throws ErrorException initdev("gpio", 3)
        @test_throws ErrorException initdev("gpio", 5)
25

26 27
        # Attempt to initialize a device with a very high index (no matching channel)
        @test_throws ErrorException initdev("gpio", 1000)
28

29 30 31 32
        # Attempt to remove devices which have not been initialized
        @test_throws ErrorException closedev("gpio", 2)
        @test_throws ErrorException closedev("gpio", 4)
        @test_throws ErrorException closedev("gpio", 6)
33

34
        #printdev("gpio", 3)
35

36 37 38
        # Remove devices from TOC
        closedev("gpio", 1)
        @test sum(listdev()) == 2
39

40 41
        closedev("gpio", 3)
        @test sum(listdev()) == 1
42

43 44
        closedev("gpio", 5)
        @test sum(listdev()) == 0
45

46
    end
47

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    @testset "Read/Write" begin

        # Fixture
        device = initdev("gpio", 1)

        # Test that an exception is thrown when an invalid operation is given
        # supported operations are 1,2,3
        @test_throws ErrorException write!(device, (0, "something"))
        @test_throws ErrorException write!(device, (4, "something"))

        # Test that exceptions are thrown for each individual operation
        @test_throws ErrorException write!(device, (1, "bad_entry"))
        @test_throws ErrorException write!(device, (2, "bad_entry"))
        @test_throws ErrorException write!(device, (3, "bad_entry"))

        # Test operation 1
        write!(device, (1, "1"))
        @test read(device, 1) == "1"
        write!(device, (1, "0"))
        @test read(device, 1) == "0"
        write!(device, (1, "1"))
        @test read(device, 1) == "1"
        write!(device, (1, "0"))
        @test read(device, 1) == "0"

        write!(device, (2, "in"))
        @test read(device, 2) == "in"
        write!(device, (2, "out"))
        @test read(device, 2) == "out"
        write!(device, (2, "in"))
        @test read(device, 2) == "in"
        write!(device, (2, "out"))
        @test read(device, 2) == "out"
81

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        # Test operation 3
        @test_throws ErrorException write!(device, (3, "none"))
        @test_throws ErrorException write!(device, (3, "rising"))
        @test_throws ErrorException write!(device, (3, "falling"))
        @test_throws ErrorException write!(device, (3, "both"))

        # Close Gpio
        closedev("gpio", 1)
     end

    @testset "All channels" begin
        # Instanciate all possible leds and perform 10 read/write commands
        # with the set high/low operation ("value")

        # Configure the GPIO for output usage
        devices = []
        for ii = 1:length(gpio_channels)
            device = initdev("gpio", ii)
            # Operation 2 -> in/out, set out
            write!(device, (2, "out"))
            # Append to list
            append!(devices, [device])
        end

        # Sets all available GPIO pins high/low in an alternating pattern
        for i = 1:10
            for ii = 1:length(gpio_channels)
                state = "$(i%2)"
                write!(devices[ii], (1, state))
                @test read(devices[ii], 1) == state
            end
            sleep(0.10)
        end

        # Closes all devices
        for ii = 1:length(gpio_channels)
            closedev("gpio", ii)
        end
120 121
    end
end