forked from tim-koehler/NetCoreGpioLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Class1.cs
117 lines (99 loc) · 3.15 KB
/
Class1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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
using System;
using System.Collections.Generic;
using System.IO;
namespace NetCoreGpioLibrary
{
public class Gpio
{
private static List<Port> configuredPorts = new List<Port>();
public static void SetUpPort(Port port, Direction direction, bool invert = false)
{
if(!IsPortDirectoryExisting(port))
CreatePortDirectory(port);
SetPortDirection(port, direction);
SetPortInvert(port, invert);
configuredPorts.Add(port);
}
public static void SetPortState(Port port, State state)
{
if(!IsPortConfigured(port))
throw new Exception(port + " is not set up!");
if(state == State.HIGH)
File.WriteAllText("/sys/class/gpio/gpio" + (int)port + "/value", "1");
else
File.WriteAllText("/sys/class/gpio/gpio" + (int)port + "/value", "0");
}
public static State GetPortState(Port port)
{
if(!IsPortConfigured(port))
throw new Exception("Port: " + (int)port + " is not set up!");
string value = File.ReadAllText("/sys/class/gpio/gpio" + (int)port + "/value");
if (value.Contains("1"))
return State.HIGH;
else
return State.LOW;
}
private static bool IsPortDirectoryExisting(Port port)
{
return Directory.Exists("/sys/class/gpio/gpio" + (int)port);
}
private static void CreatePortDirectory(Port port)
{
File.WriteAllText("/sys/class/gpio/export", ((int)port).ToString());
}
private static void SetPortDirection(Port port, Direction direction)
{
File.WriteAllText("/sys/class/gpio/gpio" + (int)port + "/direction", direction.ToString().ToLower());
}
private static void SetPortInvert(Port port, bool invert)
{
if(invert)
File.WriteAllText("/sys/class/gpio/gpio" + (int)port + "/active_low", "1");
else
File.WriteAllText("/sys/class/gpio/gpio" + (int)port + "/active_low", "0");
}
private static bool IsPortConfigured(Port port)
{
return configuredPorts.Contains(port);
}
}
public enum Direction
{
IN,
OUT
}
public enum State
{
HIGH,
LOW
}
public enum Port
{
PORT2 = 2,
PORT3 = 3,
PORT4 = 4,
PORT17 = 17,
PORT27 = 27,
PORT22 = 22,
PORT10 = 10,
PORT9 = 9,
PORT11 = 11,
PORT5 = 5,
PORT6 = 6,
PORT13 = 13,
PORT19 = 19,
PORT26 = 26,
PORT14 = 14,
PORT15 = 15,
PORT18 = 18,
PORT23 = 23,
PORT24 = 24,
PORT25 = 25,
PORT8 = 8,
PORT7 = 7,
PORT12 = 12,
PORT16 = 16,
PORT20 = 20,
PORT21 = 21
}
}