-
Notifications
You must be signed in to change notification settings - Fork 2
/
scd30.go
227 lines (185 loc) · 4.77 KB
/
scd30.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package scd30
import (
"bytes"
"encoding/binary"
"fmt"
"math"
"sync"
"time"
"periph.io/x/conn/v3/i2c"
"github.com/sigurn/crc8"
)
type Measurement struct {
CO2 float32
Temperature float32
Humidity float32
}
type SCD30 struct {
dev *i2c.Dev
}
var mutex sync.Mutex
func Open(bus i2c.Bus) (*SCD30, error) {
mutex.Lock()
defer mutex.Unlock()
dev := &i2c.Dev{Addr: 0x61, Bus: bus}
return &SCD30{dev: dev}, nil
}
// StartMeasurements starts continous measerements at given interval seconds
func (dev SCD30) StartMeasurements(interval uint16) error {
mutex.Lock()
defer mutex.Unlock()
// Set measurement interval
if err := dev.sendCommandArg(0x4600, interval); err != nil {
return err
}
// Start continuous measurements
if err := dev.sendCommandArg(0x0010, 0); err != nil {
return err
}
return nil
}
// StopMeasurements stops continuous measurements
func (dev SCD30) StopMeasurements() error {
mutex.Lock()
defer mutex.Unlock()
return dev.sendCommand(0x0104)
}
// GetTemperatureOffset gets temperature offset to compensate internal
// heating. Value is 1/100C
func (dev SCD30) GetTemperatureOffset() (uint16, error) {
mutex.Lock()
defer mutex.Unlock()
if err := dev.sendCommand(0x5403); err != nil {
return 0, err
}
data, err := dev.readData(3)
if err != nil {
return 0, err
}
data, err = readValid16(bytes.NewBuffer(data))
if err != nil {
return 0, err
} else {
return binary.BigEndian.Uint16(data), nil
}
}
// SetTemperatureOffset sets temperature offset to compensate internal
// heating. Value is 1/100C
func (dev SCD30) SetTemperatureOffset(offset uint16) error {
mutex.Lock()
defer mutex.Unlock()
return dev.sendCommandArg(0x5403, offset)
}
// GetMeasurement returns ready measurement. HasMeasurement should be
// used first to check if there is one.
func (dev SCD30) GetMeasurement() (*Measurement, error) {
mutex.Lock()
defer mutex.Unlock()
if err := dev.sendCommand(0x0300); err != nil {
return nil, err
}
if data, err := dev.readData(18); err != nil {
return nil, err
} else {
buf := bytes.NewBuffer(data)
co2, err := readValidFloat32(buf)
if err != nil {
return nil, err
}
temp, err := readValidFloat32(buf)
if err != nil {
return nil, err
}
hum, err := readValidFloat32(buf)
if err != nil {
return nil, err
}
return &Measurement{CO2: co2, Temperature: temp, Humidity: hum}, nil
}
}
// HasMeasurement checks if there is ready measurement
func (dev SCD30) HasMeasurement() (bool, error) {
mutex.Lock()
defer mutex.Unlock()
if err := dev.sendCommand(0x0202); err != nil {
return false, err
}
if data, err := dev.readData(3); err != nil {
return false, err
} else {
if data[2] != crc(data[:2]) {
return false, fmt.Errorf("crc error, expected %x got %x", crc(data[:2]), data[2])
}
return data[1] == 1, nil
}
}
// SetAutomaticSelfCalibration, 1 on, 0 off
func (dev SCD30) SetAutomaticSelfCalibration(value uint16) error {
mutex.Lock()
defer mutex.Unlock()
return dev.sendCommandArg(0x5306, value)
}
// SetForcedCalibration, co2 ppm
func (dev SCD30) SetForcedCalibration(value uint16) error {
mutex.Lock()
defer mutex.Unlock()
return dev.sendCommandArg(0x5204, value)
}
func (dev SCD30) readData(len int) ([]byte, error) {
data := make([]byte, len)
if err := dev.dev.Tx(nil, data); err != nil {
return nil, err
} else {
return data, nil
}
}
func (dev SCD30) sendCommand(cmd uint16) error {
cmdData := make([]byte, 2)
binary.BigEndian.PutUint16(cmdData, cmd)
return dev.writeAndWait(cmdData)
}
func (dev SCD30) sendCommandArg(cmd uint16, arg uint16) error {
cmdData := make([]byte, 2)
argData := make([]byte, 2)
binary.BigEndian.PutUint16(cmdData, cmd)
binary.BigEndian.PutUint16(argData, arg)
write := []byte{cmdData[0], cmdData[1], argData[0], argData[1], crc(argData)}
return dev.writeAndWait(write)
}
func (dev SCD30) writeAndWait(data []byte) error {
if err := dev.dev.Tx(data, nil); err != nil {
return err
}
time.Sleep(4 * time.Millisecond)
return nil
}
func crc(data []byte) byte {
return crc8.Checksum(data, crcTable)
}
func readValid16(buf *bytes.Buffer) ([]byte, error) {
data := buf.Next(2)
crc8, err := buf.ReadByte()
if err != nil {
return nil, err
}
if crc(data) != crc8 {
return nil, fmt.Errorf("crc error, expected %x got %x", crc(data), crc8)
}
return data, nil
}
func readValidFloat32(buf *bytes.Buffer) (float32, error) {
var out bytes.Buffer
for i := 0; i < 2; i++ {
if data, err := readValid16(buf); err != nil {
return float32(math.NaN()), err
} else {
out.Write(data)
}
}
uint := binary.BigEndian.Uint32(out.Bytes())
return math.Float32frombits(uint), nil
}
var crcTable *crc8.Table
func init() {
crcTable = crc8.MakeTable(crc8.Params{Poly: 0x31, Init: 0xff, RefIn: false, RefOut: false, XorOut: 0x00, Check: 0xff, Name: "Sensirion"})
}