-
Notifications
You must be signed in to change notification settings - Fork 0
/
mh_z19.go
97 lines (83 loc) · 1.64 KB
/
mh_z19.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
96
97
package main
import (
"errors"
"fmt"
"os"
"syscall"
"unsafe"
)
// for Termios
type tcflag_t uint
type speed_t uint
type cc_t byte
const NCCS = 19
type termios struct {
c_iflag tcflag_t
c_oflag tcflag_t
c_cflag tcflag_t
c_lflag tcflag_t
c_cc [NCCS]cc_t
c_ispeed speed_t
c_ospeed speed_t
}
func showCO2() error {
// for UART
file, err := os.OpenFile(
"/dev/serial0",
syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_NONBLOCK,
0600,
)
if err != nil {
return err
}
defer file.Close()
fmt.Println("debug 1")
const MINIMAMREADSIZE = 4
c := [NCCS]cc_t{}
c[syscall.VTIME] = cc_t(0)
c[syscall.VMIN] = cc_t(MINIMAMREADSIZE)
t := &termios{
c_cflag: syscall.CLOCAL | syscall.CREAD | syscall.CS8,
c_cc: c,
c_ispeed: speed_t(9600),
c_ospeed: speed_t(9600),
}
_ = syscall.SetNonblock(int(file.Fd()), false)
r, _, errno := syscall.Syscall(
syscall.SYS_IOCTL,
uintptr(file.Fd()),
uintptr(0x402C542B),
uintptr(unsafe.Pointer(t)),
)
if errno != 0 {
err := fmt.Errorf("failed to syscall.Syscall: %w", errno)
return err
}
if r != 0 {
err := errors.New("unknown error from SYS_IOCTL")
return err
}
fmt.Println("debug 2")
var co2 int64
writeN, err := file.Write([]byte{0xff, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79})
if err != nil {
return err
}
fmt.Println("debug 3")
for i := 0; i < writeN; i++ {
buf := make([]byte, 1)
_, err = file.Read(buf)
if err != nil {
return err
}
// TODO: checksum
switch i {
case 2: // high level concentration
co2 += int64(buf[0]) * 256
case 3: // low level concentration
co2 += int64(buf[0])
}
}
fmt.Printf("CO2: %d\n", co2)
return nil
}