git.haldean.org sousvide / db7c902
Add fake temperature file, fix temperature parsing Will Haldean Brown 8 years ago
3 changed file(s) with 23 addition(s) and 14 deletion(s). Raw diff Collapse all Expand all
00 sousvide
1 test_temp.txt
1818 )
1919
2020 var StubGpio = flag.Bool("stub_gpio", false, "stub GPIO calls for testing")
21 var FakeTemp = flag.Bool("fake_temp", false, "use fake temperature values")
2122
2223 type SousVide struct {
2324 Heating bool
3030 }
3131
3232 func (s *SousVide) InitTherm() error {
33 if s.Gpio.Stub {
34 return nil
35 }
33 var err error
34 if s.Gpio.Stub && !*FakeTemp {
35 s.Gpio.ThermFd, err = os.OpenFile(
36 "test_temp.txt", os.O_RDONLY | os.O_SYNC, 0666)
37 if err != nil {
38 return err
39 }
40 } else {
41 serial, err := findSerial()
42 if err != nil {
43 return err
44 }
3645
37 serial, err := findSerial()
38 if err != nil {
39 return err
40 }
41
42 s.Gpio.ThermFd, err = os.OpenFile(
43 fmt.Sprintf("/sys/bus/w1/devices/%s/w1_slave", serial),
44 os.O_RDONLY | os.O_SYNC, 0666)
45 if err != nil {
46 return err
46 s.Gpio.ThermFd, err = os.OpenFile(
47 fmt.Sprintf("/sys/bus/w1/devices/%s/w1_slave", serial),
48 os.O_RDONLY | os.O_SYNC, 0666)
49 if err != nil {
50 return err
51 }
4752 }
4853
4954 s.Gpio.ThermReader = bufio.NewReader(s.Gpio.ThermFd)
5156 }
5257
5358 func (s *SousVide) MeasureTemp() error {
54 if s.Gpio.Stub {
59 if s.Gpio.Stub && *FakeTemp {
5560 if s.Heating {
5661 s.Temp += Celsius(10 * rand.Float64())
5762 } else {
6570 if err != nil {
6671 return err
6772 }
73 line = strings.TrimSpace(line)
6874 data := strings.Split(strings.Split(line, "=")[1], " ")
6975 if len(data) != 2 {
7076 return errors.New(
7985 if err != nil {
8086 return err
8187 }
88 line = strings.TrimSpace(line)
8289 val := strings.Split(line, "=")[1]
8390 floatVal, err := strconv.ParseFloat(val, 64)
8491 if err != nil {