/
main.go
98 lines (88 loc) · 2.39 KB
/
main.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
package main
import (
"fmt"
"github.com/codegangsta/negroni"
"github.com/gorilla/mux"
"github.com/luismesas/goPi/piface"
"github.com/luismesas/goPi/spi"
"github.com/unrolled/render"
"net/http"
"strconv"
"time"
)
var running bool
func main() {
// Render engine
r := render.New(render.Options{
Layout: "layout",
})
// Handlers
router := mux.NewRouter()
router.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
r.HTML(w, http.StatusOK, "home", nil)
})
router.HandleFunc("/about", func(w http.ResponseWriter, req *http.Request) {
r.HTML(w, http.StatusOK, "about", nil)
})
router.HandleFunc("/start", func(w http.ResponseWriter, req *http.Request) {
time := req.FormValue("time")
stepsPerMinute, _ := strconv.Atoi(req.FormValue("steps"))
var hours, minutes, seconds int
fmt.Sscanf(time, "%d:%d:%d", &hours, &minutes, &seconds)
fmt.Println(hours, minutes, seconds)
go startLapse((hours*3600)+(minutes*60)+seconds, stepsPerMinute)
r.HTML(w, http.StatusOK, "home", nil)
}).Methods("POST")
// HTTP Server
n := negroni.Classic()
n.UseHandler(router)
n.Run(":3000")
}
func startLapse(seconds int, stepsPerMinute int) {
go spin(stepsPerMinute, false)
time.Sleep(time.Second * time.Duration(seconds))
running = false
}
func spin(stepsPerMinute int, invert bool) {
var pauseTime float64
pauseTime = float64(60) / float64(stepsPerMinute)
pfd := piface.NewPiFaceDigital(spi.DEFAULT_HARDWARE_ADDR, spi.DEFAULT_BUS, spi.DEFAULT_CHIP)
// initializes pifacedigital board
err := pfd.InitBoard()
if err != nil {
fmt.Printf("Error on init board: %s", err)
return
}
if invert {
pfd.Relays[0].SetValue(0)
pfd.Relays[1].SetValue(1)
} else {
pfd.Relays[0].SetValue(1)
pfd.Relays[1].SetValue(0)
}
pfd.OutputPins[2].SetValue(0)
running = true
for running {
pfd.OutputPins[2].Toggle()
time.Sleep(time.Second / 10)
pfd.OutputPins[2].Toggle()
if pfd.InputPins[0].Value() == 1 {
pfd.Relays[0].Toggle()
pfd.Relays[1].Toggle()
}
time.Sleep(time.Duration(float64(time.Second) * pauseTime))
}
}
func testMotor() {
// creates a new pifacedigital instance
pfd := piface.NewPiFaceDigital(spi.DEFAULT_HARDWARE_ADDR, spi.DEFAULT_BUS, spi.DEFAULT_CHIP)
// initializes pifacedigital board
err := pfd.InitBoard()
if err != nil {
fmt.Printf("Error on init board: %s", err)
return
}
pfd.OutputPins[0].Toggle()
time.Sleep(time.Second / 10)
pfd.OutputPins[0].Toggle()
}