-
Notifications
You must be signed in to change notification settings - Fork 2
/
scd30_test.go
118 lines (95 loc) · 2.45 KB
/
scd30_test.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
package scd30
import (
"testing"
"periph.io/x/conn/v3/i2c/i2ctest"
)
const addr = 0x61
func TestOpen(t *testing.T) {
scd30, _ := Open(&i2ctest.Record{})
if scd30.dev.Addr != 0x61 {
t.Fatalf("Invalid addr %v", scd30.dev.Addr)
}
}
func TestGetTemperatureOffset(t *testing.T) {
bus := &i2ctest.Playback{
Ops: []i2ctest.IO{
{Addr: addr, W: []byte{0x54, 0x03}, R: nil},
{Addr: addr, W: nil, R: []byte{0x01, 0x23, 0xa0}},
},
}
scd30, _ := Open(bus)
o, err := scd30.GetTemperatureOffset()
assertNoError(t, err)
if o != 0x123 {
t.Fatalf("Got incorrect offset %v should be %v", o, 0x123)
}
}
func TestSetTemperatureOffset(t *testing.T) {
bus := &i2ctest.Playback{
Ops: []i2ctest.IO{
{Addr: addr, W: []byte{0x54, 0x03, 0x1, 0x23, 0xa0}, R: nil},
},
}
scd30, _ := Open(bus)
err := scd30.SetTemperatureOffset(0x123)
assertNoError(t, err)
}
func TestSetAutomaticSelfCalibration(t *testing.T) {
bus := &i2ctest.Playback{
Ops: []i2ctest.IO{
{Addr: addr, W: []byte{0x53, 0x06, 0x0, 0x0, 0x81}, R: nil},
},
}
scd30, _ := Open(bus)
err := scd30.SetAutomaticSelfCalibration(0)
assertNoError(t, err)
}
func TestHasMeasurement(t *testing.T) {
bus := &i2ctest.Playback{
Ops: []i2ctest.IO{
{Addr: addr, W: []byte{0x02, 0x02}, R: nil},
{Addr: addr, W: nil, R: []byte{0x00, 0x01, 0xb0}},
},
}
scd30, _ := Open(bus)
o, err := scd30.HasMeasurement()
assertNoError(t, err)
if !o {
t.Fatalf("expected true")
}
}
func TestGetMeasurement(t *testing.T) {
bus := &i2ctest.Playback{
Ops: []i2ctest.IO{
{Addr: addr, W: []byte{0x03, 0x00}, R: nil},
{Addr: addr, W: nil, R: []byte{0x3f, 0x8c, 0xad, 0xcc, 0xcd, 0x94, 0x40, 0xc, 0x75, 0xcc, 0xcd, 0x94, 0x40, 0x53, 0x25, 0x33, 0x33, 0x88}},
},
}
scd30, _ := Open(bus)
o, err := scd30.GetMeasurement()
assertNoError(t, err)
assertFloat(t, 1.1, o.CO2)
assertFloat(t, 2.2, o.Temperature)
assertFloat(t, 3.3, o.Humidity)
}
func TestStartMeasurements(t *testing.T) {
bus := &i2ctest.Playback{
Ops: []i2ctest.IO{
{Addr: addr, W: []byte{0x46, 0x00, 0x01, 0x23, 0xa0}, R: nil},
{Addr: addr, W: []byte{0x00, 0x10, 0x00, 0x00, 0x81}, R: nil},
},
}
scd30, _ := Open(bus)
err := scd30.StartMeasurements(0x123)
assertNoError(t, err)
}
func assertFloat(t *testing.T, expected float32, value float32) {
if expected != value {
t.Fatalf("Expected %f got %f", expected, value)
}
}
func assertNoError(t *testing.T, err error) {
if err != nil {
t.Fatalf("Got error %v", err)
}
}