forked from johnlauer/serial-port-json-server
-
Notifications
You must be signed in to change notification settings - Fork 101
/
gpio.go
executable file
·239 lines (212 loc) · 4.93 KB
/
gpio.go
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// +build ignore
//+build !linux,!arm
// Ignore this file for now, but it would be nice to get GPIO going natively
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"os/signal"
)
var (
gpio GPIO
)
type GPIO struct {
pinStates map[string]PinState
pinStateChanged chan PinState
pinAdded chan PinState
pinRemoved chan string
}
type Direction int
type PullUp int
type PinState struct {
Pin interface{} `json:"-"`
PinId string
Dir Direction
State byte
Pullup PullUp
Name string
}
type PinDef struct {
ID string
Aliases []string
Capabilities []string
DigitalLogical int
AnalogLogical int
}
const STATE_FILE = "pinstates.json"
const (
In Direction = 0
Out Direction = 1
PWM Direction = 2
Pull_None PullUp = 0
Pull_Up PullUp = 1
Pull_Down PullUp = 2
)
type GPIOInterface interface {
PreInit()
Init(chan PinState, chan PinState, chan string, map[string]PinState) error
Close() error
PinMap() ([]PinDef, error)
Host() (string, error)
PinStates() (map[string]PinState, error)
PinInit(string, Direction, PullUp, string) error
PinSet(string, byte) error
PinRemove(string) error
}
func (g *GPIO) CleanupGpio() {
pinStates, err := gpio.PinStates()
if err != nil {
log.Println("Error getting pinstates on cleanup: " + err.Error())
} else {
data, err := json.Marshal(pinStates)
if err != nil {
log.Println("Error marshalling pin states : " + err.Error())
}
ioutil.WriteFile(STATE_FILE, data, 0644)
}
gpio.Close()
os.Exit(1)
}
// I took what Ben had in his main.go file and moved it here
func (g *GPIO) PreInit() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for sig := range c {
// sig is a ^C, handle it
log.Printf("captured %v, cleaning up gpio and exiting..", sig)
gpio.CleanupGpio()
}
}()
stateChanged := make(chan PinState)
pinRemoved := make(chan string)
pinAdded := make(chan PinState)
go func() {
for {
// start listening on stateChanged and pinRemoved channels and update hub as appropriate
select {
case pinState := <-stateChanged:
go h.sendMsg("PinState", pinState)
case pinName := <-pinRemoved:
go h.sendMsg("PinRemoved", pinName)
case pinState := <-pinAdded:
go h.sendMsg("PinAdded", pinState)
}
}
}()
pinStates := make(map[string]PinState)
// read existing pin states
if _, err := os.Stat(STATE_FILE); err == nil {
log.Println("Reading prexisting pinstate file : " + STATE_FILE)
dat, err := ioutil.ReadFile(STATE_FILE)
if err != nil {
log.Println("Failed to read state file : " + STATE_FILE + " : " + err.Error())
return
}
err = json.Unmarshal(dat, &pinStates)
if err != nil {
log.Println("Failed to unmarshal json : " + err.Error())
return
}
}
gpio.Init(stateChanged, pinAdded, pinRemoved, pinStates)
}
func (g *GPIO) Init(pinStateChanged chan PinState, pinAdded chan PinState, pinRemoved chan string, states map[string]PinState) error {
g.pinStateChanged = pinStateChanged
g.pinRemoved = pinRemoved
g.pinAdded = pinAdded
g.pinStates = states
// now init pins
for key, pinState := range g.pinStates {
if pinState.Name == "" {
pinState.Name = pinState.PinId
}
g.PinInit(key, pinState.Dir, pinState.Pullup, pinState.Name)
g.PinSet(key, pinState.State)
}
return nil
}
func (g *GPIO) Close() error {
return nil
}
func (g *GPIO) PinMap() ([]PinDef, error) {
// return a mock pinmap for this mock interface
pinmap := []PinDef{
{
"P8_07",
[]string{"66", "GPIO_66", "TIMER4"},
[]string{"analog", "digital", "pwm"},
66,
0,
}, {
"P8_08",
[]string{"67", "GPIO_67", "TIMER7"},
[]string{"analog", "digital", "pwm"},
67,
0,
}, {
"P8_09",
[]string{"69", "GPIO_69", "TIMER5"},
[]string{"analog", "digital", "pwm"},
69,
0,
}, {
"P8_10",
[]string{"68", "GPIO_68", "TIMER6"},
[]string{"analog", "digital", "pwm"},
68,
0,
}, {
"P8_11",
[]string{"45", "GPIO_45"},
[]string{"analog", "digital", "pwm"},
45,
0,
},
}
return pinmap, nil
}
func (g *GPIO) Host() (string, error) {
return "fake", nil
}
func (g *GPIO) PinStates() (map[string]PinState, error) {
return g.pinStates, nil
}
func (g *GPIO) PinInit(pinId string, dir Direction, pullup PullUp, name string) error {
// add a pin
// look up internal ID (we're going to assume its correct already)
// make a pinstate object
pinState := PinState{
nil,
pinId,
dir,
0,
pullup,
name,
}
g.pinStates[pinId] = pinState
g.pinAdded <- pinState
return nil
}
func (g *GPIO) PinSet(pinId string, val byte) error {
// change pin state
if pin, ok := g.pinStates[pinId]; ok {
// we have a value....
pin.State = val
g.pinStates[pinId] = pin
// notify channel of new pinstate
g.pinStateChanged <- pin
}
return nil
}
func (g *GPIO) PinRemove(pinId string) error {
// remove a pin
if _, ok := g.pinStates[pinId]; ok {
// normally you would close the pin here
delete(g.pinStates, pinId)
g.pinRemoved <- pinId
}
return nil
}