forked from robotics-4-all/tektrain-robot-sw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_bme680.py
116 lines (100 loc) · 4.32 KB
/
test_bme680.py
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
import unittest
import time
from pidevices.sensors.bme680 import BME680
class TestBME680(unittest.TestCase):
def test_set_bits(self):
sensor = BME680(1, 0)
register = 0b11111111
value = 0
shift = 3
bits = 3
register = sensor._set_bits(register, value, bits, shift)
#print(bin(register))
def test_get_bits(self):
sensor = BME680(1, 0)
register = 0b00011100
shift = 4
bits = 3
register = sensor._get_bits(register, bits, shift)
#print(bin(register))
def test_mode(self):
pass
def test_reset(self):
t_over = 16
sensor = BME680(1, 0, t_oversample=t_over)
val = sensor._get_register(sensor.CTRL_MEAS,
sensor.OSRS_T_BITS,
sensor.OSRS_T)
sensor._reset()
val = sensor._get_register(sensor.CTRL_MEAS,
sensor.OSRS_T_BITS,
sensor.OSRS_T)
self.assertEqual(val, 0, "Should be zero.")
def test_init(self):
t_over = 16
h_over = 1
p_over = 0
iir_coef = 3
sensor = BME680(1, 0,
t_oversample=t_over,
h_oversample=h_over,
p_oversample=p_over,
iir_coef=iir_coef)
t_over = sensor.OVERSAMPLING[t_over]
p_over = sensor.OVERSAMPLING[p_over]
h_over = sensor.OVERSAMPLING[h_over]
iir_coef = sensor.IIR[iir_coef]
ctrl_meas = \
sensor.hardware_interfaces[sensor._i2c].read(sensor.BME_ADDRESS,
sensor.CTRL_MEAS)
ctrl_hum = \
sensor.hardware_interfaces[sensor._i2c].read(sensor.BME_ADDRESS,
sensor.CTRL_HUM)
config = sensor.hardware_interfaces[sensor._i2c].read(sensor.BME_ADDRESS,
sensor.CONFIG)
t = sensor._get_bits(ctrl_meas, sensor.OSRS_T_BITS, sensor.OSRS_T)
p = sensor._get_bits(ctrl_meas, sensor.OSRS_P_BITS, sensor.OSRS_P)
h = sensor._get_bits(ctrl_hum, sensor.OSRS_H_BITS, sensor.OSRS_H)
iir = sensor._get_bits(config, sensor.FILTER_BITS, sensor.FILTER)
self.assertEqual(t, t_over, "Should be {}".format(t_over))
self.assertEqual(h, h_over, "Should be {}".format(h_over))
self.assertEqual(p, p_over, "Should be {}".format(p_over))
self.assertEqual(iir, iir_coef, "Should be {}".format(iir_coef))
def test_read(self):
t_over = 8
h_over = 2
p_over = 4
iir_coef = 3
g_status = 1
sensor = BME680(5, 0,
t_oversample=t_over,
h_oversample=h_over,
p_oversample=p_over,
iir_coef=iir_coef,
gas_status=g_status)
sensor.set_heating_temp([0], [320])
sensor.set_heating_time([0], [100])
sensor.set_nb_conv(0)
for i in range(10):
data = sensor.read()
print("Temp: {}\tPres: {}\tHumi: {}\tGas: {}".format(data.temp,
data.pres,
data.hum,
data.gas))
time.sleep(1)
data = sensor.read(hum=False, gas=False, temp=False)
print("Temp: {}\tPres: {}\tHumi: {}\tGas: {}".format(data.temp,
data.pres,
data.hum,
data.gas))
data = sensor.read()
print("Temp: {}\tPres: {}\tHumi: {}\tGas: {}".format(data.temp,
data.pres,
data.hum,
data.gas))
def test_get_bytes(self):
sensor = BME680(1, 0)
sensor._get_bytes(sensor.PAR_T1_l, 2)
sensor._get_bytes(sensor.PAR_T1_l, 1)
if __name__ == "__main__":
unittest.main()