-
Notifications
You must be signed in to change notification settings - Fork 0
/
bme280.go
56 lines (49 loc) · 1.02 KB
/
bme280.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
package main
import (
"errors"
"fmt"
"os"
"syscall"
)
func showPressTempHum() error {
// for I2C
// NOTE: for bme280
file, err := os.OpenFile(
"/dev/i2c-1",
os.O_RDWR,
os.ModeDevice,
)
if err != nil {
return err
}
defer file.Close()
r, _, errno := syscall.Syscall(
syscall.SYS_IOCTL,
uintptr(file.Fd()),
uintptr(0x0703),
uintptr(0x76),
)
if errno != 0 {
return fmt.Errorf("failed to syscall.Syscall: %w", errno)
}
if r != 0 {
return errors.New("unknown error from SYS_IOCTL")
}
_, err = file.Write([]byte{0xF7})
if err != nil {
return fmt.Errorf("faile to write reg: %w", err)
}
buf := make([]byte, 8)
_, err = file.Read(buf)
if err != nil {
return fmt.Errorf("faile to read reg: %w", err)
}
// TODO: 補償計算
press := int32(buf[0])<<12 | int32(buf[1])<<4 | int32(buf[2])>>4
temp := int32(buf[3])<<12 | int32(buf[4])<<4 | int32(buf[5])>>4
hum := int32(buf[6])<<8 | int32(buf[7])
fmt.Println("PRESS:", press)
fmt.Println("TEMP:", temp)
fmt.Println("HUM:", hum)
return nil
}