@@ -80,12 +80,14 @@ def reset(self):
80
80
81
81
@property
82
82
def measurement_interval (self ):
83
- """Sets the interval between readings"""
84
- # raise RuntimeError("NOT WORKING")
83
+ """Sets the interval between readings in seconds. The interval value must be from 2-1800 """
84
+
85
85
return self ._read_register (_CMD_SET_MEASUREMENT_INTERVAL )
86
86
87
87
@measurement_interval .setter
88
88
def measurement_interval (self , value ):
89
+ if value < 2 or value > 1800 :
90
+ raise AttributeError ("measurement_interval must be from 2-1800 seconds" )
89
91
self ._send_command (_CMD_SET_MEASUREMENT_INTERVAL , value )
90
92
91
93
@property
@@ -102,30 +104,9 @@ def self_calibration_enabled(self):
102
104
def self_calibration_enabled (self , enabled ):
103
105
self ._send_command (_CMD_AUTOMATIC_SELF_CALIBRATION , enabled )
104
106
105
- def _send_command (self , command , arguments = None ):
106
- if arguments is not None :
107
- self ._crc_buffer [0 ] = arguments >> 8
108
- self ._crc_buffer [1 ] = arguments & 0xFF
109
- self ._buffer [2 ] = arguments >> 8
110
- self ._buffer [3 ] = arguments & 0xFF
111
- crc = self ._crc8 (self ._crc_buffer )
112
- self ._buffer [4 ] = crc
113
- end_byte = 5
114
- else :
115
- end_byte = 2
116
-
117
- self ._buffer [0 ] = command >> 8
118
- self ._buffer [1 ] = command & 0xFF
119
-
120
- with self .i2c_device as i2c :
121
- i2c .write (self ._buffer , end = end_byte )
122
-
123
107
@property
124
- def _data_available (self ):
125
- """
126
- Check the sensor to see if new data is available
127
- """
128
-
108
+ def data_available (self ):
109
+ """Check the sensor to see if new data is available"""
129
110
return self ._read_register (_CMD_GET_DATA_READY )
130
111
131
112
@property
@@ -155,13 +136,17 @@ def altitude(self, altitude):
155
136
@property
156
137
def temperature_offset (self ):
157
138
"""Specifies the offset to be added to the reported measurements to account for a bias in
158
- the measured signal."""
139
+ the measured signal. Value is in degrees C with a resolution of 0.01 degrees and a maximum
140
+ value of 655.35"""
159
141
raw_offset = self ._read_register (_CMD_SET_TEMPERATURE_OFFSET )
160
142
return raw_offset / 100.0
161
143
162
144
@temperature_offset .setter
163
145
def temperature_offset (self , offset ):
164
- # TODO: check value before set
146
+ if offset > 655.35 :
147
+ raise AttributeError (
148
+ "Offset value must be less than or equal to 655.35 degrees C"
149
+ )
165
150
166
151
self ._send_command (_CMD_SET_TEMPERATURE_OFFSET , int (offset * 100 ))
167
152
@@ -178,20 +163,59 @@ def forced_recalibration_reference(self):
178
163
def forced_recalibration_reference (self , reference_value ):
179
164
self ._send_command (_CMD_SET_FORCED_RECALIBRATION_FACTOR , reference_value )
180
165
166
+ @property
167
+ def co2 (self ):
168
+ """Returns the CO2 concentration in PPM (parts per million)
169
+
170
+ Between measurements, the most recent reading will be cached and returned."""
171
+ if self .data_available :
172
+ self ._read_data ()
173
+ return self ._co2
174
+
175
+ @property
176
+ def temperature (self ):
177
+ """Returns the current temperature in degrees celcius
178
+
179
+ Between measurements, the most recent reading will be cached and returned."""
180
+ if self .data_available :
181
+ self ._read_data ()
182
+ return self ._temperature
183
+
184
+ @property
185
+ def relative_humidity (self ):
186
+ """Returns the current relative humidity in %rH.
187
+
188
+ Between measurements, the most recent reading will be cached and returned. """
189
+ if self .data_available :
190
+ self ._read_data ()
191
+ return self ._relative_humitidy
192
+
193
+ def _send_command (self , command , arguments = None ):
194
+ # if there is an argument, calculate the CRC and include it as well.
195
+ if arguments is not None :
196
+ self ._crc_buffer [0 ] = arguments >> 8
197
+ self ._crc_buffer [1 ] = arguments & 0xFF
198
+ self ._buffer [2 ] = arguments >> 8
199
+ self ._buffer [3 ] = arguments & 0xFF
200
+ crc = self ._crc8 (self ._crc_buffer )
201
+ self ._buffer [4 ] = crc
202
+ end_byte = 5
203
+ else :
204
+ end_byte = 2
205
+
206
+ self ._buffer [0 ] = command >> 8
207
+ self ._buffer [1 ] = command & 0xFF
208
+
209
+ with self .i2c_device as i2c :
210
+ i2c .write (self ._buffer , end = end_byte )
211
+
181
212
def _read_register (self , reg_addr ):
182
213
self ._buffer [0 ] = reg_addr >> 8
183
214
self ._buffer [1 ] = reg_addr & 0xFF
184
215
with self .i2c_device as i2c :
185
216
i2c .write_then_readinto (self ._buffer , self ._buffer , out_end = 2 , in_end = 2 )
186
217
return unpack_from (">H" , self ._buffer )[0 ]
187
218
188
- @property
189
- def co2 (self ):
190
- """Returns the CO2 concentration in PPM (parts per million)"""
191
- if self ._data_available :
192
- self ._read_data ()
193
- return self ._co2
194
-
195
219
def _read_data (self ):
196
220
self ._send_command (_CMD_READ_MEASUREMENT )
197
221
with self .i2c_device as i2c :
0 commit comments