-
Notifications
You must be signed in to change notification settings - Fork 0
/
maintest.go
95 lines (76 loc) · 1.7 KB
/
maintest.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
package main
import (
"encoding/json"
"log"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/itsjamie/gin-cors"
"github.com/tarm/serial"
)
func main() {
arduino, err := serial.OpenPort(&serial.Config{Name: "COM4", Baud: 9600})
panicif(err)
sensorOutput := make([]float32, 6)
steps := 0
// server stuff here
router := gin.Default()
router.Use(cors.Middleware(cors.Config{
Origins: "*",
Methods: "GET, PUT, POST, DELETE",
RequestHeaders: "Origin, Authorization, Content-Type",
ExposedHeaders: "",
MaxAge: 50 * time.Second,
Credentials: true,
ValidateHeaders: false,
}))
router.GET("/", func(c *gin.Context) {
c.JSON(200, sensorOutput)
})
router.GET("/steps", func(c *gin.Context) {
c.JSON(200, steps)
})
go router.Run()
// [broken, outside ball, heel bottom, inside ball, tip toes, arch]
var rawOutput string
stepping := false
for {
buf := make([]byte, 128)
n, err := arduino.Read(buf)
panicif(err)
rawOutput += string(buf[:n])
if strings.Contains(rawOutput, "\n") {
start := strings.Index(rawOutput, "[")
if start == -1 {
rawOutput = ""
continue
}
json.Unmarshal([]byte(rawOutput)[:strings.Index(rawOutput, "\n")], &sensorOutput)
rawOutput = ""
heelPressure := sensorOutput[2]
if heelPressure > 900 {
stepping = true
} else if heelPressure < 600 {
if stepping {
steps++
}
stepping = false
}
}
}
}
func nearTen(input int, val int) bool {
return (val-10 <= input) && (val+10 >= input)
}
func panicif(err error) {
if err != nil {
log.Fatal(err)
}
}
func mustMarshal(data interface{}) []byte {
out, err := json.Marshal(data)
if err != nil {
panic(err)
}
return out
}