-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
78 lines (66 loc) · 1.55 KB
/
config.go
File metadata and controls
78 lines (66 loc) · 1.55 KB
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
package main
import (
"bytes"
"encoding/json"
"machine"
)
type Config struct {
LowThreshold [4]float32 `json:"low"`
HighThreshold [4]float32 `json:"high"`
State byte `json:"state"`
}
func (c Config) Low(i byte) float32 { return c.LowThreshold[i] }
func (c Config) High(i byte) float32 { return c.HighThreshold[i] }
func (c Config) IsOn(i byte) bool {
mask := byte(1 << i)
return (c.State & mask) == mask
}
func (c *Config) SetLow(i byte, val float32) { c.LowThreshold[i] = val }
func (c *Config) SetHigh(i byte, val float32) { c.HighThreshold[i] = val }
func (c *Config) SetOn(i byte, val bool) {
if val {
mask := byte(1 << i)
c.State |= mask
} else {
mask := 0xFF ^ byte(1<<i)
c.State &= mask
}
}
func saveConfig(config Config) error {
msg, err := json.Marshal(config)
if err != nil {
return err
}
needed := int64(len(msg)) / machine.Flash.EraseBlockSize()
if needed == 0 {
needed = 1
}
err = machine.Flash.EraseBlocks(0, needed)
if err != nil {
return err
}
_, err = machine.Flash.WriteAt([]byte(msg), 0)
return err
}
func defaultConfig() Config {
return Config{
LowThreshold: [4]float32{.5, .5, .5, .5},
HighThreshold: [4]float32{.65, .65, .65, .65},
State: 0xff,
}
}
func loadConfig() (Config, error) {
saved := make([]byte, 1024)
_, err := machine.Flash.ReadAt(saved, 0)
if err != nil {
return defaultConfig(), err
}
var cfg Config
buf := bytes.NewReader(saved)
decoder := json.NewDecoder(buf)
err = decoder.Decode(&cfg)
if err != nil {
return defaultConfig(), err
}
return cfg, nil
}