GPIO.cs 5.24 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 * Copyright (c) 2016 Akinwale Ariwodola <akinwale@gmail.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

using System.IO;
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System.Threading;
using Nanite.Exceptions;

namespace Nanite.IO
{
    public static class GPIO
    {
        public enum Direction
        {
            Input = 0,
            Output = 1
        };

        public enum Value
        {
            Low = 0,
            High = 1
        };

        private const string GPIOPath = "/sys/class/gpio";

        private const string GPIODirection = "direction";

        private const string GPIOExport = "export";

        private const string GPIOUnexport = "unexport";

        private const string GPIOValue = "value";

        public static void PinMode(int pin, Direction direction)
        {
            ClosePin(pin);

            string pinPath = Path.Combine(GPIOPath, string.Format("gpio{0}", pin));
            if (!Directory.Exists(pinPath))
            {
                try
                {
56
57
                    using (StreamWriter writer = new StreamWriter(new FileStream(
                        Path.Combine(GPIOPath, GPIOExport), FileMode.Open, FileAccess.Write, FileShare.ReadWrite)))
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
                    {
                        writer.Write(pin);
                    }
                }
                catch (IOException ex)
                {
                    throw new GPIOException("Unable to export the pin.", ex);
                }
            }

            do
            {
                // Wait until the pin has been initialised properly before setting the direction
                Thread.Sleep(500);
            } while (!Directory.Exists(pinPath));

            try
            {
76
77
                using (StreamWriter writer = new StreamWriter(new FileStream(
                    Path.Combine(pinPath, GPIODirection), FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite)))
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
                {
                    writer.Write(direction == Direction.Input ? "in" : "out");
                }
            }
            catch (IOException ex)
            {
                throw new GPIOException("Unable to set the pin direction.", ex);
            }
        }

        public static void ClosePin(int pin)
        {
            string pinPath = Path.Combine(GPIOPath, string.Format("gpio{0}", pin));
            if (Directory.Exists(pinPath))
            {
                try
                {
95
96
                    using (StreamWriter writer = new StreamWriter(new FileStream(
                        Path.Combine(GPIOPath, GPIOUnexport), FileMode.Open, FileAccess.Write, FileShare.ReadWrite)))
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
                    {
                        writer.Write(pin);
                    }
                }
                catch (IOException ex)
                {
                    throw new GPIOException("Unable to close the pin.", ex);
                }
            }
        }

        public static void Write(int pin, Value value)
        {
            try
            {
                string pinPath = Path.Combine(GPIOPath, string.Format("gpio{0}", pin));
113
114
                using (StreamWriter writer = new StreamWriter(new FileStream(
                    Path.Combine(pinPath, GPIOValue), FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite)))
115
116
117
118
119
120
121
122
123
124
                {
                    writer.Write(value == Value.Low ? 0 : 1);
                }
            }
            catch (IOException ex)
            {
                throw new GPIOException("Unable to write the pin value.", ex);
            }
        }

125
        public static Value? Read(int pin)
126
        {
127
            int pinValue = -1;
128
129
130
131

            try
            {
                string pinPath = Path.Combine(GPIOPath, string.Format("gpio{0}", pin));
132
133
                using (StreamReader reader = new StreamReader(new FileStream(
                    Path.Combine(pinPath, GPIOValue), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
134
                {
135
                    int.TryParse(reader.ReadToEnd(), out pinValue);
136
137
138
139
140
141
142
                }
            }
            catch (IOException ex)
            {
                throw new GPIOException("Unable to read the pin value.", ex);
            }

143
144
145
146
147
148
            if (pinValue != 0 && pinValue != 1)
            {
                return null;
            }

            return (Value) pinValue;
149
150
151
        }
    }
}