/
oshiwasp.go
421 lines (335 loc) · 11 KB
/
oshiwasp.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package main
import (
"github.com/mrmorphic/hwio"
"fmt"
"time"
"os"
"net/http"
"regexp"
"errors"
"html/template"
"log"
"runtime"
)
type Page struct {
Title string
Body string
}
type Acquisition struct{
outputFileName string
outputFile *os.File
state string
time0 time.Time
}
func (acq *Acquisition) setTime0(){
acq.time0 = time.Now()
}
func (acq *Acquisition) getTime0() time.Time {
return acq.time0
}
func (acq *Acquisition) getState() string{
return acq.state
}
func (acq *Acquisition) setState(s string){
acq.state = s
log.Printf("State set to %s\n", acq.state)
}
func (acq *Acquisition) setOutputFileName(s string){
acq.outputFileName = s
log.Printf("Output Filename set to %s\n", acq.outputFileName)
}
func (acq *Acquisition) createOutputFile(){
var e error
acq.outputFile, e = os.Create(acq.outputFileName)
if e != nil {
panic(e)
}
statusLine := fmt.Sprintf("### %v Data Acquisition\n\n", time.Now())
acq.outputFile.WriteString(statusLine)
log.Printf("Cretated output File %s", acq.outputFileName)
}
func (acq *Acquisition) reopenOutputFile(){
var e error
acq.outputFile, e = os.OpenFile(acq.outputFileName,os.O_WRONLY|os.O_APPEND, 0666)
if e != nil {
panic(e)
}
log.Printf("Reopen output File %s", acq.outputFileName)
}
func (acq Acquisition) closeOutputFile(){ //close the output file
acq.outputFile.Close()
log.Printf("Closed output File %s", acq.outputFileName)
}
func (acq *Acquisition) initiate(){
acq.setState(stateNEW)
acq.setOutputFileName(dataPath+dataFileName)
acq.createOutputFile()
}
type Oshiwasp struct {
statusLed hwio.Pin
actionLed hwio.Pin
buttonA hwio.Pin
buttonB hwio.Pin
trackerA hwio.Pin
trackerB hwio.Pin
trackerC hwio.Pin
trackerD hwio.Pin
}
const (
// web related
tmplPath = "tmpl/" // path of the template files .html in the local file system
dataPath = "data/" // path of the data files in the local file system
dataFileName = "output.csv" // data file name in the local file system
// setup of the pinout in the raspberry
statusLedPin = "gpio7" // green
actionLedPin = "gpio8" // yellow
buttonAPin = "gpio24" // start
buttonBPin = "gpio23" // stop
trackerAPin = "gpio22"
trackerBPin = "gpio18"
trackerCPin = "gpio17"
trackerDPin = "gpio4"
//States for the acquisition
// resume <- PAUSED <- pause
// | ^
// | |
// +----+ +---+
// \ /
//0 -- new -> NEW -- start -> RUNNING -- stop -> STOPPED
// ^ ^ |
// | +------- start -------------+
// +--------------- new ---------------+
//
stateNEW = "NEW"
stateRUNNING = "RUNNING"
statePAUSED = "PAUSED"
stateSTOPPED = "STOPPED"
stateERROR = "ERROR"
)
var (
c chan int //channel initialitation
//actionLed hwio.Pin // indicating action in the system
templates = template.Must(template.ParseGlob(tmplPath+"*.html"))
validPath = regexp.MustCompile("^/(index|new|start|pause|resume|stop|download|data)/([a-zA-Z0-9]+)$")
theAcq=new(Acquisition)
theOshi=new(Oshiwasp)
)
func (oshi *Oshiwasp) initiate(){
var e error
// Set up 'trakers' as inputs
oshi.trackerA, e = hwio.GetPinWithMode(trackerAPin, hwio.INPUT)
if e != nil {
panic(e)
}
log.Printf("Set pin %s as trackerA\n",trackerAPin)
oshi.trackerB, e = hwio.GetPinWithMode(trackerBPin, hwio.INPUT)
if e != nil {
panic(e)
}
log.Printf("Set pin %s as trackerB\n",trackerBPin)
oshi.trackerC, e = hwio.GetPinWithMode(trackerCPin, hwio.INPUT)
if e != nil {
panic(e)
}
log.Printf("Set pin %s as trackerC\n",trackerCPin)
oshi.trackerD, e = hwio.GetPinWithMode(trackerDPin, hwio.INPUT)
if e != nil {
panic(e)
}
log.Printf("Set pin %s as trackerD\n",trackerDPin)
// Set up 'buttons' as inputs
oshi.buttonA, e = hwio.GetPinWithMode(buttonAPin, hwio.INPUT)
if e != nil {
panic(e)
}
log.Printf("Set pin %s as buttonA\n",buttonAPin)
oshi.buttonB, e = hwio.GetPinWithMode(buttonBPin, hwio.INPUT)
if e != nil {
panic(e)
}
log.Printf("Set pin %s as buttonB\n",buttonBPin)
// Set up 'leds' as outputs
oshi.statusLed, e = hwio.GetPinWithMode(statusLedPin, hwio.OUTPUT)
if e != nil {
panic(e)
}
log.Printf("Set pin %s as statusLed\n",statusLedPin)
oshi.actionLed, e = hwio.GetPinWithMode(actionLedPin, hwio.OUTPUT)
if e != nil {
panic(e)
}
log.Printf("Set pin %s as actionLed\n",actionLedPin)
}
func readTracker(name string, trackerPin hwio.Pin){
//value readed from tracker, initially set to 0, because the tracker was innactive
oldValue := 0
timeAction := time.Now() // time of the action detected
timeActionOld := time.Now() // time of the action-1 detected
// loop
for theAcq.getState() != stateSTOPPED {
// Read the tracker value
value, e := hwio.DigitalRead(trackerPin)
if e != nil {
panic(e)
}
timeActionOld=timeAction //store the last time
timeAction= time.Now() // time at this point
// Did value change?
if value != oldValue {
dataString := fmt.Sprintf("[%s] %v (%v) -> %d\n",
name,timeAction.Sub(theAcq.getTime0()),timeAction.Sub(timeActionOld),value)
if theAcq.getState() != statePAUSED {
log.Println(dataString)
theAcq.outputFile.WriteString(dataString)
}
oldValue = value
// Write the value to the led indicating somewhat is happened
if (value == 1) {
hwio.DigitalWrite(theOshi.actionLed, hwio.HIGH)
} else {
hwio.DigitalWrite(theOshi.actionLed, hwio.LOW)
}
}
}
}
func blinkingLed(ledPin hwio.Pin) int {
// loop
for {
hwio.DigitalWrite(ledPin, hwio.HIGH)
hwio.Delay(500)
hwio.DigitalWrite(ledPin, hwio.LOW)
hwio.Delay(500)
}
}
func waitTillButtonPushed(buttonPin hwio.Pin) int {
// loop
for {
// Read the tracker value
value, e := hwio.DigitalRead(buttonPin)
if e != nil {
panic(e)
}
// Was the button pressed, value = 1?
if value == 1 {
return value
}
}
}
//WWWWWWWWWWWWW
// http section
//WWWWWWWWWWWWW
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
err := templates.ExecuteTemplate(w, tmpl+".html", p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func getTitle(w http.ResponseWriter, r *http.Request) (string, error) {
m := validPath.FindStringSubmatch(r.URL.Path)
if m == nil {
http.NotFound(w, r)
return "", errors.New("Invalid Page Name")
}
return m[2], nil //the name is the second subexpression
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
theAcq.setState(stateSTOPPED)
p := &Page{Title: "Index", Body: "Make an action. State: "+theAcq.state}
renderTemplate(w,"index",p)
}
func newHandler(w http.ResponseWriter, r *http.Request) {
theAcq.setState(stateNEW)
theAcq.createOutputFile()
// create a new output file
//var e error
//theAcq.outputFile, e = os.Create(theAcq.outputFileName)
//if e != nil {
// panic(e)
// }
p := &Page{Title: "Index", Body: "New acquisition ready. Select Start to begin it. State: " + theAcq.state}
renderTemplate(w,"index",p)
}
func startHandler(w http.ResponseWriter, r *http.Request) {
// manage file depending the previous state
if theAcq.getState() == stateSTOPPED {
theAcq.reopenOutputFile()
log.Printf("Reopen file %s\n", theAcq.outputFile);
}
// sets the new time0 only with a new scenery
if theAcq.getState() == stateNEW {
theAcq.setTime0();
}
theAcq.setState(stateRUNNING)
//waitTillButtonPushed(buttonA)
p := &Page{Title: "Start", Body: "State: "+theAcq.state}
hwio.DigitalWrite(theOshi.statusLed, hwio.HIGH)
log.Println("Beginning.....");
renderTemplate(w,"start",p)
// launch the trackers
log.Printf("There are %v goroutines", runtime.NumGoroutine())
log.Printf("Launching the Gourutines")
go readTracker("A", theOshi.trackerA)
log.Println("Started Tracker A")
go readTracker("B", theOshi.trackerB)
log.Println("Started Tracker B")
go readTracker("C", theOshi.trackerC)
log.Println("Started Tracker C")
go readTracker("D", theOshi.trackerD)
log.Println("Started Tracker D")
log.Printf("There are %v goroutines", runtime.NumGoroutine())
}
func pauseHandler(w http.ResponseWriter, r *http.Request) {
theAcq.setState(statePAUSED)
p := &Page{Title: "Start", Body: "State: "+theAcq.state}
hwio.DigitalWrite(theOshi.statusLed, hwio.HIGH)
renderTemplate(w,"start",p)
}
func resumeHandler(w http.ResponseWriter, r *http.Request) {
theAcq.setState(stateRUNNING)
p := &Page{Title: "Start", Body: "State: "+theAcq.state}
hwio.DigitalWrite(theOshi.statusLed, hwio.HIGH)
renderTemplate(w,"start",p)
}
func stopHandler(w http.ResponseWriter, r *http.Request) {
theAcq.setState(stateSTOPPED)
hwio.DigitalWrite(theOshi.statusLed, hwio.LOW)
log.Println("Finnishing.....");
// close the GPIO pins
//hwio.CloseAll()
theAcq.closeOutputFile() //close the file when finished
p := &Page{Title: "Stop", Body:"State: "+theAcq.state}
renderTemplate(w,"stop",p)
//log.Printf("There are %v goroutines", runtime.NumGoroutine())
}
func downloadHandler(w http.ResponseWriter, r *http.Request) {
file_requested := "./"+r.URL.Path
http.ServeFile(w, r, file_requested)
}
func getRequest(w http.ResponseWriter, r *http.Request){
file_requested := "./"+r.URL.Path
http.ServeFile(w, r, file_requested)
}
func main() {
// setup
mux := http.NewServeMux()
//mux.Handle("/",http.FileServer(http.Dir("data")))
mux.HandleFunc("/", indexHandler)
mux.HandleFunc("/index", indexHandler)
mux.HandleFunc("/new", newHandler)
mux.HandleFunc("/start", startHandler)
mux.HandleFunc("/pause", pauseHandler)
mux.HandleFunc("/resume", resumeHandler)
mux.HandleFunc("/stop", stopHandler)
//mux.HandleFunc("/download",getRequest)
mux.HandleFunc("/"+dataPath+dataFileName, getRequest) // /data/output.csv
theAcq.initiate();
theOshi.initiate();
// starting the web service...
// http.Handle("/data", http.FileServer(http.Dir("./data")))
log.Println("Listennig on http://localhost:8080/")
log.Fatal(http.ListenAndServe(":8080", mux))
log.Println("Closed http://localhost:8080/")
// close the GPIO pins
hwio.CloseAll()
}