forked from BrianLeishman/burbzilla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
91 lines (75 loc) · 1.62 KB
/
types.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
package main
import (
"fmt"
"strings"
)
type config struct {
Boards []board `yaml:"boards"`
}
type boardType int
const (
boardTypeADS1115 boardType = iota + 1
)
func (t boardType) String() string {
switch t {
case boardTypeADS1115:
return "ADS1115"
}
panic(fmt.Errorf(`unhandled string value for board type "%d"`, t))
}
type board struct {
Address int
Type boardType
Sensors map[string]sensor
}
func (brd *board) UnmarshalYAML(unmarshal func(interface{}) error) error {
var tmp struct {
Address int `yaml:"address"`
Type string `yaml:"type"`
Sensors map[string]sensor `yaml:"sensors"`
}
if err := unmarshal(&tmp); err != nil {
return err
}
brd.Address = tmp.Address
switch strings.ToLower(tmp.Type) {
case "ads1115":
brd.Type = boardTypeADS1115
default:
return fmt.Errorf(`board type of "%s" is not supported`, tmp.Type)
}
brd.Sensors = tmp.Sensors
return nil
}
type sensorType int
const (
sensorTypeVolts sensorType = iota + 1
)
func (t sensorType) String() string {
switch t {
case sensorTypeVolts:
return "Volts"
}
panic(fmt.Errorf(`unhandled string value for sensor type "%d"`, t))
}
type sensor struct {
Channel int
Type sensorType
}
func (snsr *sensor) UnmarshalYAML(unmarshal func(interface{}) error) error {
var tmp struct {
Channel int `yaml:"channel"`
Type string `yaml:"type"`
}
if err := unmarshal(&tmp); err != nil {
return err
}
snsr.Channel = tmp.Channel
switch strings.ToLower(tmp.Type) {
case "volts":
snsr.Type = sensorTypeVolts
default:
return fmt.Errorf(`sensor type of "%s" is not supported`, tmp.Type)
}
return nil
}