git.haldean.org sousvide / bd976ef
Save PID values and load on start Will Haldean Brown 8 years ago
3 changed file(s) with 42 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
00 sousvide
11 test_temp.txt
2 pid.json
7777 s.Pid.I = i
7878 s.Pid.D = d
7979 s.checkpoint()
80 s.SavePid()
8081 http.Redirect(resp, req, "/", http.StatusSeeOther)
8182 })
8283
11
22 import (
33 "bufio"
4 "encoding/json"
45 "flag"
56 "fmt"
7 "io/ioutil"
68 "math"
79 "os"
810 "sync"
1921
2022 var StubGpio = flag.Bool("stub_gpio", false, "stub GPIO calls for testing")
2123 var FakeTemp = flag.Bool("fake_temp", false, "use fake temperature values")
24 var PidFile = flag.String("pid_file", "pid.json", "file to save PID values in")
2225
2326 type SousVide struct {
2427 Heating bool
121124 return s.Target - s.Temp
122125 }
123126
127 func (s *SousVide) SavePid() {
128 fd, err := os.Create(*PidFile)
129 if err != nil {
130 fmt.Printf("could not save PID values: %v\n", err)
131 return
132 }
133 defer fd.Close()
134
135 b, err := json.Marshal(s.Pid)
136 if err != nil {
137 fmt.Printf("could not save PID values: %v\n", err)
138 return
139 }
140 fd.Write(b)
141 }
142
143 func (s *SousVide) LoadPid() error {
144 bytes, err := ioutil.ReadFile(*PidFile)
145 if err != nil {
146 fmt.Printf("could not load PID values: %v\n", err)
147 return err
148 }
149 err = json.Unmarshal(bytes, &s.Pid)
150 if err != nil {
151 fmt.Printf("could not load PID values: %v\n", err)
152 return err
153 }
154 return nil
155 }
156
124157 func main() {
125158 flag.Parse()
126159
127160 s := New()
128 s.Pid.P = 10
129 s.Pid.I = 0.1
130 s.Pid.D = 10
161 err := s.LoadPid()
162 if err != nil {
163 s.Pid.P = 10
164 s.Pid.D = 20
165 s.SavePid()
166 }
131167 s.Gpio.Stub = *StubGpio
132168
133 err := s.InitGpio()
169 err = s.InitGpio()
134170 if err != nil {
135171 fmt.Printf("could not initialize gpio: %v\n", err)
136172 return