pamoja.sensors
Idiomatic sensor-driver facade.
These are the decode half of eleven parts a field node is likely to have wired to it, turning the register bytes a bus driver read into the physical reading the manufacturer's datasheet says they mean. Driving the bus is the caller's job; getting the arithmetic right is this layer's.
1"""Idiomatic sensor-driver facade. 2 3These are the decode half of eleven parts a field node is likely to have wired to 4it, turning the register bytes a bus driver read into the physical reading the 5manufacturer's datasheet says they mean. Driving the bus is the caller's job; 6getting the arithmetic right is this layer's. 7""" 8 9from __future__ import annotations 10 11import enum 12 13from pamoja._native import Ads1115Config, Bme280Calibration, Bme280Measurement, Ds18b20Reading 14from pamoja._native import ads1115_config_bits as _ads1115_config_bits 15from pamoja._native import ads1115_config_from_bits as _ads1115_config_from_bits 16from pamoja._native import ads1115_full_scale_microvolts as _ads1115_full_scale_microvolts 17from pamoja._native import ads1115_samples_per_second as _ads1115_samples_per_second 18from pamoja._native import ads1115_to_nanovolts as _ads1115_to_nanovolts 19from pamoja._native import ads1115_to_volts as _ads1115_to_volts 20from pamoja._native import ds18b20_build_scratchpad as _ds18b20_build_scratchpad 21from pamoja._native import ds18b20_celsius as _ds18b20_celsius 22from pamoja._native import ds18b20_config_byte as _ds18b20_config_byte 23from pamoja._native import ds18b20_crc8 as _ds18b20_crc8 24from pamoja._native import ds18b20_max_conversion_micros as _ds18b20_max_conversion_micros 25from pamoja._native import ds18b20_micro_celsius as _ds18b20_micro_celsius 26from pamoja._native import ds18b20_parse_scratchpad as _ds18b20_parse_scratchpad 27from pamoja._native import ds18b20_resolution_bits as _ds18b20_resolution_bits 28from pamoja._native import ds18b20_step_micro_celsius as _ds18b20_step_micro_celsius 29from pamoja._native import ina219_bus_register as _ina219_bus_register 30from pamoja._native import ina219_current_register as _ina219_current_register 31from pamoja._native import ina219_power_register as _ina219_power_register 32from pamoja._native import ina219_shunt_register as _ina219_shunt_register 33from pamoja._native import ina219_bus_millivolts as _ina219_bus_millivolts 34from pamoja._native import ina219_calibration as _ina219_calibration 35from pamoja._native import ina219_conversion_ready as _ina219_conversion_ready 36from pamoja._native import ina219_current_microamps as _ina219_current_microamps 37from pamoja._native import ina219_math_overflow as _ina219_math_overflow 38from pamoja._native import ( 39 ina219_minimum_current_lsb_microamps as _ina219_minimum_current_lsb_microamps, 40) 41from pamoja._native import ina219_power_microwatts as _ina219_power_microwatts 42from pamoja._native import ina219_shunt_microvolts as _ina219_shunt_microvolts 43from pamoja._native import ( 44 Bmp280Calibration, 45 Bmp280Coefficients, 46 Bmp280Config, 47 Bmp280CtrlMeas, 48 Bmp280RawMeasurement, 49 Bmp280Reading, 50 Hdc1080Config, 51 Hdc1080Measurement, 52 Ina226Config, 53 Ina226DieId, 54 Ina226MaskEnable, 55 Opt3001Config, 56 Scd4xMeasurement, 57 Sht3xMeasurement, 58 Sht3xStatus, 59 Tmp117Config, 60) 61from pamoja._native import bmp280_config_bits as _bmp280_config_bits 62from pamoja._native import bmp280_config_from_bits as _bmp280_config_from_bits 63from pamoja._native import bmp280_ctrl_meas_bits as _bmp280_ctrl_meas_bits 64from pamoja._native import bmp280_ctrl_meas_from_bits as _bmp280_ctrl_meas_from_bits 65from pamoja._native import bmp280_image_updating as _bmp280_image_updating 66from pamoja._native import bmp280_measurement_bytes as _bmp280_measurement_bytes 67from pamoja._native import bmp280_measuring as _bmp280_measuring 68from pamoja._native import bmp280_oversampling_factor as _bmp280_oversampling_factor 69from pamoja._native import bmp280_parse_measurement as _bmp280_parse_measurement 70from pamoja._native import bmp280_pressure_skipped as _bmp280_pressure_skipped 71from pamoja._native import bmp280_standby_micros as _bmp280_standby_micros 72from pamoja._native import bmp280_temperature_skipped as _bmp280_temperature_skipped 73from pamoja._native import hdc1080_celsius as _hdc1080_celsius 74from pamoja._native import hdc1080_config_from_register as _hdc1080_config_from_register 75from pamoja._native import hdc1080_config_to_register as _hdc1080_config_to_register 76from pamoja._native import hdc1080_conversion_time_micros as _hdc1080_conversion_time_micros 77from pamoja._native import ( 78 hdc1080_humidity_conversion_micros as _hdc1080_humidity_conversion_micros, 79) 80from pamoja._native import hdc1080_humidity_register as _hdc1080_humidity_register 81from pamoja._native import hdc1080_measurement_bytes as _hdc1080_measurement_bytes 82from pamoja._native import hdc1080_measurement_from_physical as _hdc1080_measurement_from_physical 83from pamoja._native import hdc1080_milli_celsius as _hdc1080_milli_celsius 84from pamoja._native import hdc1080_milli_percent as _hdc1080_milli_percent 85from pamoja._native import hdc1080_parse_measurement as _hdc1080_parse_measurement 86from pamoja._native import hdc1080_relative_humidity as _hdc1080_relative_humidity 87from pamoja._native import hdc1080_serial_id as _hdc1080_serial_id 88from pamoja._native import hdc1080_serial_id_registers as _hdc1080_serial_id_registers 89from pamoja._native import ( 90 hdc1080_temperature_conversion_micros as _hdc1080_temperature_conversion_micros, 91) 92from pamoja._native import hdc1080_temperature_register as _hdc1080_temperature_register 93from pamoja._native import ina226_active_alert_function as _ina226_active_alert_function 94from pamoja._native import ina226_address as _ina226_address 95from pamoja._native import ina226_averaging_samples as _ina226_averaging_samples 96from pamoja._native import ina226_bus_microvolts as _ina226_bus_microvolts 97from pamoja._native import ina226_bus_register as _ina226_bus_register 98from pamoja._native import ina226_bus_volts as _ina226_bus_volts 99from pamoja._native import ina226_calibration as _ina226_calibration 100from pamoja._native import ina226_config_from_register as _ina226_config_from_register 101from pamoja._native import ina226_config_to_register as _ina226_config_to_register 102from pamoja._native import ina226_conversion_micros as _ina226_conversion_micros 103from pamoja._native import ina226_current_amps as _ina226_current_amps 104from pamoja._native import ina226_current_microamps as _ina226_current_microamps 105from pamoja._native import ina226_current_register as _ina226_current_register 106from pamoja._native import ( 107 ina226_current_register_from_shunt as _ina226_current_register_from_shunt, 108) 109from pamoja._native import ina226_die_id as _ina226_die_id 110from pamoja._native import ina226_identify as _ina226_identify 111from pamoja._native import ina226_is_continuous as _ina226_is_continuous 112from pamoja._native import ina226_mask_enable_from_register as _ina226_mask_enable_from_register 113from pamoja._native import ina226_mask_enable_to_register as _ina226_mask_enable_to_register 114from pamoja._native import ina226_measures_bus as _ina226_measures_bus 115from pamoja._native import ina226_measures_shunt as _ina226_measures_shunt 116from pamoja._native import ( 117 ina226_minimum_current_lsb_microamps as _ina226_minimum_current_lsb_microamps, 118) 119from pamoja._native import ina226_power_microwatts as _ina226_power_microwatts 120from pamoja._native import ina226_power_register as _ina226_power_register 121from pamoja._native import ( 122 ina226_power_register_from_current as _ina226_power_register_from_current, 123) 124from pamoja._native import ina226_power_watts as _ina226_power_watts 125from pamoja._native import ina226_shunt_millivolts as _ina226_shunt_millivolts 126from pamoja._native import ina226_shunt_nanovolts as _ina226_shunt_nanovolts 127from pamoja._native import ina226_shunt_register as _ina226_shunt_register 128from pamoja._native import ina226_update_micros as _ina226_update_micros 129from pamoja._native import opt3001_config_bits as _opt3001_config_bits 130from pamoja._native import opt3001_config_from_bits as _opt3001_config_from_bits 131from pamoja._native import opt3001_conversion_millis as _opt3001_conversion_millis 132from pamoja._native import opt3001_fault_count as _opt3001_fault_count 133from pamoja._native import opt3001_full_scale_milli_lux as _opt3001_full_scale_milli_lux 134from pamoja._native import opt3001_is_automatic_range as _opt3001_is_automatic_range 135from pamoja._native import opt3001_lsb_milli_lux as _opt3001_lsb_milli_lux 136from pamoja._native import opt3001_lux as _opt3001_lux 137from pamoja._native import opt3001_milli_lux as _opt3001_milli_lux 138from pamoja._native import opt3001_raw_from_milli_lux as _opt3001_raw_from_milli_lux 139from pamoja._native import opt3001_word_from_bytes as _opt3001_word_from_bytes 140from pamoja._native import opt3001_word_to_bytes as _opt3001_word_to_bytes 141from pamoja._native import scd4x_allowed_during_measurement as _scd4x_allowed_during_measurement 142from pamoja._native import scd4x_ambient_pressure_pascals as _scd4x_ambient_pressure_pascals 143from pamoja._native import scd4x_ambient_pressure_word as _scd4x_ambient_pressure_word 144from pamoja._native import ( 145 scd4x_automatic_self_calibration_enabled as _scd4x_automatic_self_calibration_enabled, 146) 147from pamoja._native import ( 148 scd4x_automatic_self_calibration_word as _scd4x_automatic_self_calibration_word, 149) 150from pamoja._native import scd4x_celsius as _scd4x_celsius 151from pamoja._native import scd4x_command_frame as _scd4x_command_frame 152from pamoja._native import scd4x_crc as _scd4x_crc 153from pamoja._native import scd4x_data_ready as _scd4x_data_ready 154from pamoja._native import ( 155 scd4x_forced_recalibration_correction_ppm as _scd4x_forced_recalibration_correction_ppm, 156) 157from pamoja._native import scd4x_forced_recalibration_word as _scd4x_forced_recalibration_word 158from pamoja._native import scd4x_humidity_milli_percent as _scd4x_humidity_milli_percent 159from pamoja._native import scd4x_humidity_raw as _scd4x_humidity_raw 160from pamoja._native import scd4x_max_duration_ms as _scd4x_max_duration_ms 161from pamoja._native import scd4x_measurement_bytes as _scd4x_measurement_bytes 162from pamoja._native import scd4x_measurement_from_physical as _scd4x_measurement_from_physical 163from pamoja._native import scd4x_milli_celsius as _scd4x_milli_celsius 164from pamoja._native import scd4x_parse_measurement as _scd4x_parse_measurement 165from pamoja._native import scd4x_relative_humidity_percent as _scd4x_relative_humidity_percent 166from pamoja._native import scd4x_self_test_passed as _scd4x_self_test_passed 167from pamoja._native import scd4x_serial_number as _scd4x_serial_number 168from pamoja._native import scd4x_serial_number_frame as _scd4x_serial_number_frame 169from pamoja._native import ( 170 scd4x_temperature_offset_milli_celsius as _scd4x_temperature_offset_milli_celsius, 171) 172from pamoja._native import scd4x_temperature_offset_word as _scd4x_temperature_offset_word 173from pamoja._native import scd4x_temperature_raw as _scd4x_temperature_raw 174from pamoja._native import scd4x_word as _scd4x_word 175from pamoja._native import scd4x_word_frame as _scd4x_word_frame 176from pamoja._native import scd4x_write_frame as _scd4x_write_frame 177from pamoja._native import sht3x_celsius as _sht3x_celsius 178from pamoja._native import sht3x_crc as _sht3x_crc 179from pamoja._native import sht3x_fahrenheit as _sht3x_fahrenheit 180from pamoja._native import ( 181 sht3x_humidity_raw_from_milli_percent as _sht3x_humidity_raw_from_milli_percent, 182) 183from pamoja._native import ( 184 sht3x_humidity_raw_from_relative_humidity as _sht3x_humidity_raw_from_relative_humidity, 185) 186from pamoja._native import sht3x_interval_micros as _sht3x_interval_micros 187from pamoja._native import sht3x_max_measurement_micros as _sht3x_max_measurement_micros 188from pamoja._native import sht3x_measurement_bytes as _sht3x_measurement_bytes 189from pamoja._native import sht3x_milli_celsius as _sht3x_milli_celsius 190from pamoja._native import sht3x_milli_fahrenheit as _sht3x_milli_fahrenheit 191from pamoja._native import sht3x_milli_percent as _sht3x_milli_percent 192from pamoja._native import sht3x_parse_measurement as _sht3x_parse_measurement 193from pamoja._native import sht3x_parse_status as _sht3x_parse_status 194from pamoja._native import sht3x_periodic as _sht3x_periodic 195from pamoja._native import sht3x_relative_humidity as _sht3x_relative_humidity 196from pamoja._native import sht3x_single_shot as _sht3x_single_shot 197from pamoja._native import sht3x_status_bytes as _sht3x_status_bytes 198from pamoja._native import sht3x_status_from_bits as _sht3x_status_from_bits 199from pamoja._native import ( 200 sht3x_temperature_raw_from_celsius as _sht3x_temperature_raw_from_celsius, 201) 202from pamoja._native import ( 203 sht3x_temperature_raw_from_milli_celsius as _sht3x_temperature_raw_from_milli_celsius, 204) 205from pamoja._native import ( 206 sht3x_temperature_raw_from_milli_fahrenheit as _sht3x_temperature_raw_from_milli_fahrenheit, 207) 208from pamoja._native import sht3x_typical_measurement_micros as _sht3x_typical_measurement_micros 209from pamoja._native import sht3x_word as _sht3x_word 210from pamoja._native import sht3x_word_bytes as _sht3x_word_bytes 211from pamoja._native import tmp117_averaging_conversions as _tmp117_averaging_conversions 212from pamoja._native import tmp117_averaging_micros as _tmp117_averaging_micros 213from pamoja._native import tmp117_celsius as _tmp117_celsius 214from pamoja._native import tmp117_config_bits as _tmp117_config_bits 215from pamoja._native import tmp117_config_from_bits as _tmp117_config_from_bits 216from pamoja._native import tmp117_cycle_micros as _tmp117_cycle_micros 217from pamoja._native import tmp117_cycle_nominal_micros as _tmp117_cycle_nominal_micros 218from pamoja._native import tmp117_data_ready as _tmp117_data_ready 219from pamoja._native import tmp117_device_id as _tmp117_device_id 220from pamoja._native import tmp117_eeprom_busy as _tmp117_eeprom_busy 221from pamoja._native import tmp117_eeprom_unlock_busy as _tmp117_eeprom_unlock_busy 222from pamoja._native import tmp117_high_alert as _tmp117_high_alert 223from pamoja._native import tmp117_low_alert as _tmp117_low_alert 224from pamoja._native import tmp117_micro_celsius as _tmp117_micro_celsius 225from pamoja._native import tmp117_nano_celsius as _tmp117_nano_celsius 226from pamoja._native import tmp117_raw_from_celsius as _tmp117_raw_from_celsius 227from pamoja._native import tmp117_raw_from_micro_celsius as _tmp117_raw_from_micro_celsius 228from pamoja._native import tmp117_revision as _tmp117_revision 229from pamoja._native import tmp117_temperature_bytes as _tmp117_temperature_bytes 230from pamoja._native import tmp117_temperature_from_bytes as _tmp117_temperature_from_bytes 231 232__all__ = [ 233 "Ads1115Config", 234 "Bme280Calibration", 235 "Bme280Measurement", 236 "Bmp280Calibration", 237 "Bmp280Coefficients", 238 "Bmp280Config", 239 "Bmp280CtrlMeas", 240 "Bmp280RawMeasurement", 241 "Bmp280Reading", 242 "Ds18b20Reading", 243 "Hdc1080Config", 244 "Hdc1080Measurement", 245 "Ina226AlertFunction", 246 "Ina226Config", 247 "Ina226DieId", 248 "Ina226MaskEnable", 249 "Opt3001Config", 250 "Scd4xMeasurement", 251 "Sht3xMeasurement", 252 "Sht3xRate", 253 "Sht3xRepeatability", 254 "Sht3xStatus", 255 "Tmp117Config", 256 "ads1115", 257 "bme280", 258 "bmp280", 259 "ds18b20", 260 "hdc1080", 261 "ina219", 262 "ina226", 263 "opt3001", 264 "scd4x", 265 "sht3x", 266 "tmp117", 267] 268 269 270class _Bme280: 271 """A Bosch BME280 temperature, pressure, and humidity sensor.""" 272 273 __slots__ = () 274 275 #: The address a BME280 answers on with its SDO pin low. 276 ADDRESS_PRIMARY = 0x76 277 #: The address it answers on with SDO high. 278 ADDRESS_SECONDARY = 0x77 279 #: The value its chip-ID register reads, which confirms the part. 280 CHIP_ID = 0x60 281 282 def calibration(self, temp_press: bytes, humidity: bytes) -> Bme280Calibration: 283 """Read the factory calibration out of the registers, once at start-up. 284 285 :param temp_press: The 26-byte temperature and pressure calibration block. 286 :param humidity: The 7-byte humidity calibration block. 287 :returns: The calibration, to reuse for every measurement. 288 :raises ValueError: If either block is the wrong length. 289 """ 290 return Bme280Calibration(bytes(temp_press), bytes(humidity)) 291 292 293class _Ds18b20: 294 """A Maxim DS18B20 1-Wire thermometer.""" 295 296 __slots__ = () 297 298 #: The 1-Wire family code that identifies a DS18B20 on the bus. 299 FAMILY_CODE = 0x28 300 301 def parse_scratchpad(self, data: bytes) -> Ds18b20Reading: 302 """Parse and CRC-check a nine-byte scratchpad. 303 304 :param data: The scratchpad as the device sent it, the ninth byte its CRC. 305 :returns: The decoded reading. 306 :raises PamojaError: If the CRC does not match, which means the read was 307 corrupted on the bus and should be repeated. 308 """ 309 return _ds18b20_parse_scratchpad(bytes(data)) 310 311 def build_scratchpad( 312 self, celsius: float, resolution_bits: int, alarm_high: int, alarm_low: int 313 ) -> bytes: 314 """Build the nine bytes a part in the given state puts on the bus. 315 316 This is the inverse of :meth:`parse_scratchpad`, so a node can be written and 317 tested against what a thermometer sends without one attached. 318 319 :param celsius: The temperature the part is reading. 320 :param resolution_bits: The resolution it is configured for, 9 to 12. 321 :param alarm_high: The high alarm threshold in whole degrees Celsius. 322 :param alarm_low: The low alarm threshold in whole degrees Celsius. 323 :returns: The nine scratchpad bytes in transmission order, CRC last. 324 :raises PamojaError: If the resolution is not 9, 10, 11, or 12 bits. 325 """ 326 return bytes( 327 _ds18b20_build_scratchpad(celsius, resolution_bits, alarm_high, alarm_low) 328 ) 329 330 def crc8(self, data: bytes) -> int: 331 """Compute the Maxim CRC-8 a 1-Wire device checks its own bytes with. 332 333 :param data: The bytes the checksum covers. 334 :returns: The checksum. 335 """ 336 return _ds18b20_crc8(bytes(data)) 337 338 def micro_celsius(self, raw: int) -> int: 339 """Convert a raw temperature register to micro-degrees Celsius. 340 341 :param raw: The 16-bit two's-complement register. 342 :returns: The temperature, exact in integer arithmetic. 343 """ 344 return _ds18b20_micro_celsius(raw) 345 346 def celsius(self, raw: int) -> float: 347 """Convert a raw temperature register to degrees Celsius. 348 349 :param raw: The 16-bit two's-complement register. 350 :returns: The temperature. 351 """ 352 return _ds18b20_celsius(raw) 353 354 def config_byte(self, bits: int) -> int: 355 """Return the configuration byte that selects a resolution. 356 357 :param bits: The resolution in bits: 9, 10, 11, or 12. 358 :returns: The byte to write to the configuration register. 359 :raises ValueError: If the resolution is not one the part offers. 360 """ 361 return _ds18b20_config_byte(bits) 362 363 def resolution_bits(self, config_byte: int) -> int: 364 """Return the resolution a configuration byte selects. 365 366 :param config_byte: The byte read from the configuration register. 367 :returns: The resolution in bits. 368 """ 369 return _ds18b20_resolution_bits(config_byte) 370 371 def step_micro_celsius(self, bits: int) -> int: 372 """Return the temperature step a resolution resolves. 373 374 :param bits: The resolution in bits. 375 :returns: The step in micro-degrees Celsius. 376 :raises ValueError: If the resolution is not one the part offers. 377 """ 378 return _ds18b20_step_micro_celsius(bits) 379 380 def max_conversion_micros(self, bits: int) -> int: 381 """Return how long a conversion may take at a resolution. 382 383 :param bits: The resolution in bits. 384 :returns: The datasheet's worst case, in microseconds. 385 :raises ValueError: If the resolution is not one the part offers. 386 """ 387 return _ds18b20_max_conversion_micros(bits) 388 389 390class _Ina219: 391 """A TI INA219 current, voltage, and power monitor.""" 392 393 __slots__ = () 394 395 def calibration(self, current_lsb_microamps: int, shunt_milliohms: int) -> int: 396 """Compute the calibration register for a shunt and current resolution. 397 398 :param current_lsb_microamps: The microamps per count the current register 399 should carry. 400 :param shunt_milliohms: The shunt resistor value. 401 :returns: The register value to write. 402 """ 403 return _ina219_calibration(current_lsb_microamps, shunt_milliohms) 404 405 def minimum_current_lsb_microamps(self, max_expected_microamps: int) -> int: 406 """Return the smallest current resolution that still covers a maximum. 407 408 :param max_expected_microamps: The largest current the application measures. 409 :returns: The minimum current LSB in microamps. 410 """ 411 return _ina219_minimum_current_lsb_microamps(max_expected_microamps) 412 413 def shunt_register(self, microvolts: int) -> int: 414 """Build the shunt-voltage register a monitor reports for a shunt voltage. 415 416 The inverse of :meth:`shunt_microvolts`, so a node can be written and tested 417 against what a monitor sends without one attached. 418 419 :param microvolts: The shunt voltage in microvolts. 420 :returns: The signed register value, at 10 uV per count. 421 """ 422 return _ina219_shunt_register(microvolts) 423 424 def bus_register(self, millivolts: int) -> int: 425 """Build the bus-voltage register a monitor reports for a bus voltage. 426 427 :param millivolts: The bus voltage in millivolts. 428 :returns: The register value, with the conversion-ready flag set. 429 """ 430 return _ina219_bus_register(millivolts) 431 432 def current_register(self, microamps: int, current_lsb_microamps: int) -> int: 433 """Build the current register a monitor reports for a current. 434 435 :param microamps: The current in microamps. 436 :param current_lsb_microamps: The current LSB the calibration was set for. 437 :returns: The signed register value. 438 """ 439 return _ina219_current_register(microamps, current_lsb_microamps) 440 441 def power_register(self, microwatts: int, current_lsb_microamps: int) -> int: 442 """Build the power register a monitor reports for a power. 443 444 :param microwatts: The power in microwatts. 445 :param current_lsb_microamps: The current LSB the calibration was set for. 446 :returns: The register value. 447 """ 448 return _ina219_power_register(microwatts, current_lsb_microamps) 449 450 def shunt_microvolts(self, raw: int) -> int: 451 """Convert a raw shunt-voltage register to microvolts. 452 453 :param raw: The signed register value. 454 :returns: The shunt voltage. 455 """ 456 return _ina219_shunt_microvolts(raw) 457 458 def bus_millivolts(self, raw: int) -> int: 459 """Convert a raw bus-voltage register to millivolts. 460 461 :param raw: The register value. 462 :returns: The bus voltage. 463 """ 464 return _ina219_bus_millivolts(raw) 465 466 def conversion_ready(self, raw: int) -> bool: 467 """Report whether a bus-voltage register says a conversion is ready. 468 469 :param raw: The register value. 470 :returns: Whether the conversion-ready flag is set. 471 """ 472 return _ina219_conversion_ready(raw) 473 474 def math_overflow(self, raw: int) -> bool: 475 """Report whether a bus-voltage register flags a math overflow. 476 477 :param raw: The register value. 478 :returns: Whether the current and power readings are meaningless, which 479 means the calibration needs revisiting. 480 """ 481 return _ina219_math_overflow(raw) 482 483 def current_microamps(self, raw: int, current_lsb_microamps: int) -> int: 484 """Convert a raw current register to microamps. 485 486 :param raw: The signed register value. 487 :param current_lsb_microamps: The resolution the calibration selected. 488 :returns: The current. 489 """ 490 return _ina219_current_microamps(raw, current_lsb_microamps) 491 492 def power_microwatts(self, raw: int, current_lsb_microamps: int) -> int: 493 """Convert a raw power register to microwatts. 494 495 :param raw: The register value. 496 :param current_lsb_microamps: The resolution the calibration selected. 497 :returns: The power. The power LSB is fixed at twenty times the current LSB. 498 """ 499 return _ina219_power_microwatts(raw, current_lsb_microamps) 500 501 502class _Ads1115: 503 """A TI ADS1115 16-bit analogue-to-digital converter.""" 504 505 __slots__ = () 506 507 #: The value the configuration register reads after a reset. 508 CONFIG_RESET = 0x8583 509 510 def config_bits(self, config: Ads1115Config) -> int: 511 """Assemble the 16-bit configuration register value. 512 513 :param config: The settings to encode. 514 :returns: The register value to write, most significant bit first. 515 """ 516 return _ads1115_config_bits(config) 517 518 def config_from_bits(self, bits: int) -> Ads1115Config: 519 """Parse a 16-bit configuration register value. 520 521 :param bits: The register value, as read from the device. 522 :returns: The decoded settings. Every value decodes, so this never raises. 523 """ 524 return _ads1115_config_from_bits(bits) 525 526 def full_scale_microvolts(self, pga: int) -> int: 527 """Return the full-scale range a gain code selects. 528 529 :param pga: The gain code, 0 to 7. 530 :returns: The full scale in microvolts. 531 """ 532 return _ads1115_full_scale_microvolts(pga) 533 534 def samples_per_second(self, data_rate: int) -> int: 535 """Return the sample rate a data-rate code selects. 536 537 :param data_rate: The data-rate code, 0 to 7. 538 :returns: The rate in samples per second. 539 """ 540 return _ads1115_samples_per_second(data_rate) 541 542 def to_nanovolts(self, pga: int, raw: int) -> int: 543 """Convert a raw conversion result to nanovolts. 544 545 :param pga: The gain the conversion was taken at. 546 :param raw: The signed conversion register value. 547 :returns: The measured voltage, exact at every gain setting. 548 """ 549 return _ads1115_to_nanovolts(pga, raw) 550 551 def to_volts(self, pga: int, raw: int) -> float: 552 """Convert a raw conversion result to volts. 553 554 :param pga: The gain the conversion was taken at. 555 :param raw: The signed conversion register value. 556 :returns: The measured voltage. 557 """ 558 return _ads1115_to_volts(pga, raw) 559 560 561class Sht3xRepeatability(str, enum.Enum): 562 """How hard an SHT3x works at one measurement, traded against time and power.""" 563 564 #: The fastest and least precise setting. 565 LOW = "Low" 566 #: The middle setting. 567 MEDIUM = "Medium" 568 #: The slowest and most precise setting. 569 HIGH = "High" 570 571 572class Sht3xRate(str, enum.Enum): 573 """How often an SHT3x in periodic mode takes a measurement.""" 574 575 #: One measurement every two seconds. 576 HALF_MPS = "HalfMps" 577 #: One measurement per second. 578 ONE_MPS = "OneMps" 579 #: Two measurements per second. 580 TWO_MPS = "TwoMps" 581 #: Four measurements per second. 582 FOUR_MPS = "FourMps" 583 #: Ten measurements per second. 584 TEN_MPS = "TenMps" 585 586 587class Ina226AlertFunction(str, enum.Enum): 588 """The limit comparison an INA226 alert pin responds to.""" 589 590 #: The shunt voltage rose above the alert limit. 591 SHUNT_OVER_LIMIT = "ShuntOverLimit" 592 #: The shunt voltage fell below the alert limit. 593 SHUNT_UNDER_LIMIT = "ShuntUnderLimit" 594 #: The bus voltage rose above the alert limit. 595 BUS_OVER_LIMIT = "BusOverLimit" 596 #: The bus voltage fell below the alert limit. 597 BUS_UNDER_LIMIT = "BusUnderLimit" 598 #: The power rose above the alert limit. 599 POWER_OVER_LIMIT = "PowerOverLimit" 600 601 602class _Bmp280: 603 """A Bosch BMP280 pressure and temperature sensor, the BME280 without humidity.""" 604 605 __slots__ = () 606 607 #: The address a BMP280 answers on with its SDO pin low. 608 ADDRESS_PRIMARY = 0x76 609 #: The address it answers on with SDO high. 610 ADDRESS_SECONDARY = 0x77 611 #: The value its chip-ID register reads, which confirms the part. 612 CHIP_ID = 0x58 613 #: The word written to the reset register to restart the part. 614 RESET_WORD = 0xB6 615 #: The raw output a channel reports when its measurement is switched off. 616 SKIPPED_OUTPUT = 0x80000 617 #: How many bytes the calibration block holds. 618 CALIBRATION_LENGTH = 24 619 #: How many bytes one measurement burst holds. 620 DATA_LENGTH = 6 621 #: The first of the 24 calibration bytes. 622 REGISTER_CALIBRATION = 0x88 623 #: The chip-ID register. 624 REGISTER_CHIP_ID = 0xD0 625 #: The reset register. 626 REGISTER_RESET = 0xE0 627 #: The status register. 628 REGISTER_STATUS = 0xF3 629 #: The measurement-control register. 630 REGISTER_CTRL_MEAS = 0xF4 631 #: The configuration register. 632 REGISTER_CONFIG = 0xF5 633 #: The first of the six data bytes. 634 REGISTER_DATA = 0xF7 635 636 def calibration(self, data: bytes) -> Bmp280Calibration: 637 """Read the factory calibration out of the registers, once at start-up. 638 639 :param data: The 24-byte calibration block. 640 :returns: The calibration, to reuse for every measurement. 641 :raises ValueError: If the block is the wrong length. 642 """ 643 return Bmp280Calibration(bytes(data)) 644 645 def parse_measurement(self, data: bytes) -> Bmp280RawMeasurement: 646 """Split a six-byte data read into its two uncompensated outputs. 647 648 :param data: The six data registers in read order. 649 :returns: The raw pressure and temperature, before compensation. 650 :raises ValueError: If the read is the wrong length. 651 """ 652 return _bmp280_parse_measurement(bytes(data)) 653 654 def measurement_bytes(self, pressure: int, temperature: int) -> bytes: 655 """Build the six data-register bytes a part reports for two raw outputs. 656 657 The inverse of :meth:`parse_measurement`, so a node can be written and tested 658 against what a sensor sends without one attached. 659 660 :param pressure: The 20-bit uncompensated pressure. 661 :param temperature: The 20-bit uncompensated temperature. 662 :returns: The six bytes in read order. 663 """ 664 return bytes(_bmp280_measurement_bytes(pressure, temperature)) 665 666 def pressure_skipped(self, pressure: int) -> bool: 667 """Report whether a raw pressure says the measurement is switched off. 668 669 :param pressure: The 20-bit uncompensated pressure. 670 :returns: Whether the channel's oversampling is set to skip. 671 """ 672 return _bmp280_pressure_skipped(pressure) 673 674 def temperature_skipped(self, temperature: int) -> bool: 675 """Report whether a raw temperature says the measurement is switched off. 676 677 :param temperature: The 20-bit uncompensated temperature. 678 :returns: Whether the channel's oversampling is set to skip. 679 """ 680 return _bmp280_temperature_skipped(temperature) 681 682 def measuring(self, status: int) -> bool: 683 """Report whether a status register says a conversion is running. 684 685 :param status: The status register. 686 :returns: Whether a measurement is in progress. 687 """ 688 return _bmp280_measuring(status) 689 690 def image_updating(self, status: int) -> bool: 691 """Report whether a status register says the calibration image is loading. 692 693 :param status: The status register. 694 :returns: Whether the coefficients are still being copied from NVM. 695 """ 696 return _bmp280_image_updating(status) 697 698 def ctrl_meas_bits(self, ctrl: Bmp280CtrlMeas) -> int: 699 """Pack a measurement-control register value. 700 701 :param ctrl: The oversampling settings and power mode. 702 :returns: The byte to write. 703 """ 704 return _bmp280_ctrl_meas_bits(ctrl) 705 706 def ctrl_meas_from_bits(self, bits: int) -> Bmp280CtrlMeas: 707 """Parse a measurement-control register value. 708 709 :param bits: The byte read from the device. 710 :returns: The decoded settings. 711 """ 712 return _bmp280_ctrl_meas_from_bits(bits) 713 714 def config_bits(self, config: Bmp280Config) -> int: 715 """Pack a configuration register value. 716 717 :param config: The standby, filter, and interface settings. 718 :returns: The byte to write. 719 """ 720 return _bmp280_config_bits(config) 721 722 def config_from_bits(self, bits: int) -> Bmp280Config: 723 """Parse a configuration register value. 724 725 :param bits: The byte read from the device. 726 :returns: The decoded settings. 727 """ 728 return _bmp280_config_from_bits(bits) 729 730 def oversampling_factor(self, code: int) -> int: 731 """Return how many samples an oversampling code averages. 732 733 :param code: The oversampling code, 0 to 5. 734 :returns: The sample count, or 0 when the measurement is skipped. 735 """ 736 return _bmp280_oversampling_factor(code) 737 738 def standby_micros(self, code: int) -> int: 739 """Return the normal-mode standby period a code selects. 740 741 :param code: The standby code, 0 to 7. 742 :returns: The period in microseconds. 743 """ 744 return _bmp280_standby_micros(code) 745 746 747class _Sht3x: 748 """A Sensirion SHT30, SHT31, or SHT35 temperature and humidity sensor.""" 749 750 __slots__ = () 751 752 #: The address the part answers on with its ADDR pin low. 753 ADDRESS_A = 0x44 754 #: The address it answers on with ADDR high. 755 ADDRESS_B = 0x45 756 #: The shortest gap the datasheet allows between two commands, in microseconds. 757 MIN_COMMAND_GAP_MICROS = 1_000 758 #: The status word the part powers up with. 759 STATUS_DEFAULT = 0x8010 760 #: Single shot, high repeatability, holding the clock until the result is ready. 761 COMMAND_SINGLE_SHOT_HIGH_STRETCH = 0x2C06 762 #: Single shot, medium repeatability, with clock stretching. 763 COMMAND_SINGLE_SHOT_MEDIUM_STRETCH = 0x2C0D 764 #: Single shot, low repeatability, with clock stretching. 765 COMMAND_SINGLE_SHOT_LOW_STRETCH = 0x2C10 766 #: Single shot, high repeatability, polled rather than stretched. 767 COMMAND_SINGLE_SHOT_HIGH = 0x2400 768 #: Single shot, medium repeatability, polled. 769 COMMAND_SINGLE_SHOT_MEDIUM = 0x240B 770 #: Single shot, low repeatability, polled. 771 COMMAND_SINGLE_SHOT_LOW = 0x2416 772 #: Periodic at one measurement every two seconds, high repeatability. 773 COMMAND_PERIODIC_HALF_MPS_HIGH = 0x2032 774 #: Periodic at one measurement every two seconds, medium repeatability. 775 COMMAND_PERIODIC_HALF_MPS_MEDIUM = 0x2024 776 #: Periodic at one measurement every two seconds, low repeatability. 777 COMMAND_PERIODIC_HALF_MPS_LOW = 0x202F 778 #: Periodic at one measurement per second, high repeatability. 779 COMMAND_PERIODIC_ONE_MPS_HIGH = 0x2130 780 #: Periodic at one measurement per second, medium repeatability. 781 COMMAND_PERIODIC_ONE_MPS_MEDIUM = 0x2126 782 #: Periodic at one measurement per second, low repeatability. 783 COMMAND_PERIODIC_ONE_MPS_LOW = 0x212D 784 #: Periodic at two measurements per second, high repeatability. 785 COMMAND_PERIODIC_TWO_MPS_HIGH = 0x2236 786 #: Periodic at two measurements per second, medium repeatability. 787 COMMAND_PERIODIC_TWO_MPS_MEDIUM = 0x2220 788 #: Periodic at two measurements per second, low repeatability. 789 COMMAND_PERIODIC_TWO_MPS_LOW = 0x222B 790 #: Periodic at four measurements per second, high repeatability. 791 COMMAND_PERIODIC_FOUR_MPS_HIGH = 0x2334 792 #: Periodic at four measurements per second, medium repeatability. 793 COMMAND_PERIODIC_FOUR_MPS_MEDIUM = 0x2322 794 #: Periodic at four measurements per second, low repeatability. 795 COMMAND_PERIODIC_FOUR_MPS_LOW = 0x2329 796 #: Periodic at ten measurements per second, high repeatability. 797 COMMAND_PERIODIC_TEN_MPS_HIGH = 0x2737 798 #: Periodic at ten measurements per second, medium repeatability. 799 COMMAND_PERIODIC_TEN_MPS_MEDIUM = 0x2721 800 #: Periodic at ten measurements per second, low repeatability. 801 COMMAND_PERIODIC_TEN_MPS_LOW = 0x272A 802 #: Accelerated response time, four measurements per second. 803 COMMAND_PERIODIC_ART = 0x2B32 804 #: Fetch the latest periodic result. 805 COMMAND_FETCH_DATA = 0xE000 806 #: Leave periodic mode. 807 COMMAND_BREAK = 0x3093 808 #: Soft reset. 809 COMMAND_SOFT_RESET = 0x30A2 810 #: The general-call reset, addressed to 0x00. 811 COMMAND_GENERAL_CALL_RESET = 0x0006 812 #: Turn the on-die heater on. 813 COMMAND_HEATER_ENABLE = 0x306D 814 #: Turn the on-die heater off. 815 COMMAND_HEATER_DISABLE = 0x3066 816 #: Read the status register. 817 COMMAND_READ_STATUS = 0xF32D 818 #: Clear the status register. 819 COMMAND_CLEAR_STATUS = 0x3041 820 821 def crc(self, data: bytes) -> int: 822 """Compute the Sensirion CRC-8 the part appends to every word. 823 824 :param data: The bytes the checksum covers. 825 :returns: The checksum. 826 """ 827 return _sht3x_crc(bytes(data)) 828 829 def word(self, frame: bytes) -> int: 830 """Read a CRC-checked three-byte word frame. 831 832 :param frame: The two data bytes and their checksum. 833 :returns: The 16-bit word. 834 :raises ValueError: If the frame is not three bytes. 835 :raises PamojaError: If the checksum does not match, which means the read 836 was corrupted on the bus and should be repeated. 837 """ 838 return _sht3x_word(bytes(frame)) 839 840 def word_bytes(self, value: int) -> bytes: 841 """Build the three bytes the part sends for a word: the word then its CRC. 842 843 :param value: The 16-bit word. 844 :returns: The frame in transmission order. 845 """ 846 return bytes(_sht3x_word_bytes(value)) 847 848 def parse_measurement(self, frame: bytes) -> Sht3xMeasurement: 849 """Parse and CRC-check a six-byte measurement frame. 850 851 :param frame: The temperature word, humidity word, and their checksums. 852 :returns: The decoded reading, in raw words and physical units. 853 :raises ValueError: If the frame is not six bytes. 854 :raises PamojaError: If either checksum does not match. 855 """ 856 return _sht3x_parse_measurement(bytes(frame)) 857 858 def measurement_bytes(self, temperature_raw: int, humidity_raw: int) -> bytes: 859 """Build the six bytes the part sends for a pair of raw words. 860 861 The inverse of :meth:`parse_measurement`, so a node can be written and tested 862 against what a sensor sends without one attached. 863 864 :param temperature_raw: The raw temperature word. 865 :param humidity_raw: The raw humidity word. 866 :returns: The frame in transmission order, each word followed by its CRC. 867 """ 868 return bytes(_sht3x_measurement_bytes(temperature_raw, humidity_raw)) 869 870 def parse_status(self, frame: bytes) -> Sht3xStatus: 871 """Parse and CRC-check a three-byte status frame. 872 873 :param frame: The status word and its checksum. 874 :returns: The decoded status word and its flags. 875 :raises ValueError: If the frame is not three bytes. 876 :raises PamojaError: If the checksum does not match. 877 """ 878 return _sht3x_parse_status(bytes(frame)) 879 880 def status_from_bits(self, bits: int) -> Sht3xStatus: 881 """Split a status word into its flags. 882 883 :param bits: The 16-bit status register. 884 :returns: The decoded flags. Every value decodes, so this never raises. 885 """ 886 return _sht3x_status_from_bits(bits) 887 888 def status_bytes(self, bits: int) -> bytes: 889 """Build the three bytes the part sends for a status word, CRC last. 890 891 :param bits: The 16-bit status register. 892 :returns: The frame in transmission order. 893 """ 894 return bytes(_sht3x_status_bytes(bits)) 895 896 def milli_celsius(self, raw: int) -> int: 897 """Convert a raw temperature word to milli-degrees Celsius. 898 899 :param raw: The 16-bit temperature word. 900 :returns: The temperature, exact in integer arithmetic. 901 """ 902 return _sht3x_milli_celsius(raw) 903 904 def celsius(self, raw: int) -> float: 905 """Convert a raw temperature word to degrees Celsius. 906 907 :param raw: The 16-bit temperature word. 908 :returns: The temperature. 909 """ 910 return _sht3x_celsius(raw) 911 912 def milli_fahrenheit(self, raw: int) -> int: 913 """Convert a raw temperature word to milli-degrees Fahrenheit. 914 915 :param raw: The 16-bit temperature word. 916 :returns: The temperature, from the datasheet's own Fahrenheit formula. 917 """ 918 return _sht3x_milli_fahrenheit(raw) 919 920 def fahrenheit(self, raw: int) -> float: 921 """Convert a raw temperature word to degrees Fahrenheit. 922 923 :param raw: The 16-bit temperature word. 924 :returns: The temperature. 925 """ 926 return _sht3x_fahrenheit(raw) 927 928 def milli_percent(self, raw: int) -> int: 929 """Convert a raw humidity word to milli-percent. 930 931 :param raw: The 16-bit humidity word. 932 :returns: The relative humidity, exact in integer arithmetic. 933 """ 934 return _sht3x_milli_percent(raw) 935 936 def relative_humidity(self, raw: int) -> float: 937 """Convert a raw humidity word to a relative humidity percentage. 938 939 :param raw: The 16-bit humidity word. 940 :returns: The relative humidity. 941 """ 942 return _sht3x_relative_humidity(raw) 943 944 def temperature_raw_from_milli_celsius(self, milli_celsius: int) -> int: 945 """Build the temperature word that decodes to a temperature. 946 947 :param milli_celsius: The temperature in milli-degrees Celsius. 948 :returns: The 16-bit word, clamped to the part's range. 949 """ 950 return _sht3x_temperature_raw_from_milli_celsius(milli_celsius) 951 952 def temperature_raw_from_celsius(self, celsius: float) -> int: 953 """Build the temperature word that decodes to a temperature in Celsius. 954 955 :param celsius: The temperature. 956 :returns: The 16-bit word, clamped to the part's range. 957 """ 958 return _sht3x_temperature_raw_from_celsius(celsius) 959 960 def temperature_raw_from_milli_fahrenheit(self, milli_fahrenheit: int) -> int: 961 """Build the temperature word that decodes to a temperature in Fahrenheit. 962 963 :param milli_fahrenheit: The temperature in milli-degrees Fahrenheit. 964 :returns: The 16-bit word, clamped to the part's range. 965 """ 966 return _sht3x_temperature_raw_from_milli_fahrenheit(milli_fahrenheit) 967 968 def humidity_raw_from_milli_percent(self, milli_percent: int) -> int: 969 """Build the humidity word that decodes to a relative humidity. 970 971 :param milli_percent: The relative humidity in milli-percent. 972 :returns: The 16-bit word, clamped to full scale. 973 """ 974 return _sht3x_humidity_raw_from_milli_percent(milli_percent) 975 976 def humidity_raw_from_relative_humidity(self, percent: float) -> int: 977 """Build the humidity word that decodes to a relative humidity percentage. 978 979 :param percent: The relative humidity. 980 :returns: The 16-bit word, clamped to full scale. 981 """ 982 return _sht3x_humidity_raw_from_relative_humidity(percent) 983 984 def single_shot(self, repeatability: Sht3xRepeatability, clock_stretching: bool) -> int: 985 """Return the single-shot command for a repeatability and clock mode. 986 987 :param repeatability: How hard the part works at the measurement. 988 :param clock_stretching: Whether the part holds the clock until the result 989 is ready, rather than making the driver poll for it. 990 :returns: The 16-bit command word. 991 :raises ValueError: If the repeatability is not one the part offers. 992 """ 993 return _sht3x_single_shot(Sht3xRepeatability(repeatability).value, clock_stretching) 994 995 def periodic(self, repeatability: Sht3xRepeatability, rate: Sht3xRate) -> int: 996 """Return the periodic-mode command for a repeatability and rate. 997 998 :param repeatability: How hard the part works at each measurement. 999 :param rate: How often it measures. 1000 :returns: The 16-bit command word. 1001 :raises ValueError: If either setting is not one the part offers. 1002 """ 1003 return _sht3x_periodic( 1004 Sht3xRepeatability(repeatability).value, Sht3xRate(rate).value 1005 ) 1006 1007 def max_measurement_micros(self, repeatability: Sht3xRepeatability) -> int: 1008 """Return how long a measurement may take. 1009 1010 :param repeatability: The setting the measurement is taken at. 1011 :returns: The datasheet's worst case, in microseconds. 1012 :raises ValueError: If the repeatability is not one the part offers. 1013 """ 1014 return _sht3x_max_measurement_micros(Sht3xRepeatability(repeatability).value) 1015 1016 def typical_measurement_micros(self, repeatability: Sht3xRepeatability) -> int: 1017 """Return how long a measurement typically takes. 1018 1019 :param repeatability: The setting the measurement is taken at. 1020 :returns: The datasheet's typical figure, in microseconds. 1021 :raises ValueError: If the repeatability is not one the part offers. 1022 """ 1023 return _sht3x_typical_measurement_micros(Sht3xRepeatability(repeatability).value) 1024 1025 def interval_micros(self, rate: Sht3xRate) -> int: 1026 """Return the gap between periodic measurements. 1027 1028 :param rate: The periodic rate. 1029 :returns: The interval in microseconds. 1030 :raises ValueError: If the rate is not one the part offers. 1031 """ 1032 return _sht3x_interval_micros(Sht3xRate(rate).value) 1033 1034class _Scd4x: 1035 """A Sensirion SCD40 or SCD41 carbon dioxide, temperature, and humidity sensor.""" 1036 1037 __slots__ = () 1038 1039 #: The only address the part answers on. 1040 ADDRESS = 0x62 1041 #: The highest concentration the part reports, in parts per million. 1042 CO2_MAX_PPM = 40_000 1043 #: The temperature offset the part powers up with, in milli-degrees Celsius. 1044 DEFAULT_TEMPERATURE_OFFSET_MILLI_CELSIUS = 4_000 1045 #: How often periodic measurement produces a result, in milliseconds. 1046 PERIODIC_MEASUREMENT_INTERVAL_MS = 5_000 1047 #: How often low-power periodic measurement produces a result, in milliseconds. 1048 LOW_POWER_PERIODIC_MEASUREMENT_INTERVAL_MS = 30_000 1049 #: How long the part takes to become ready after power-up, in milliseconds. 1050 POWER_UP_TIME_MS = 1_000 1051 #: The word a forced recalibration returns when it did not take. 1052 FORCED_RECALIBRATION_FAILED = 0xFFFF 1053 #: Start periodic measurement. 1054 COMMAND_START_PERIODIC_MEASUREMENT = 0x21B1 1055 #: Read the latest result. 1056 COMMAND_READ_MEASUREMENT = 0xEC05 1057 #: Leave periodic measurement. 1058 COMMAND_STOP_PERIODIC_MEASUREMENT = 0x3F86 1059 #: Write the temperature offset. 1060 COMMAND_SET_TEMPERATURE_OFFSET = 0x241D 1061 #: Read the temperature offset. 1062 COMMAND_GET_TEMPERATURE_OFFSET = 0x2318 1063 #: Write the installation altitude. 1064 COMMAND_SET_SENSOR_ALTITUDE = 0x2427 1065 #: Read the installation altitude. 1066 COMMAND_GET_SENSOR_ALTITUDE = 0x2322 1067 #: Write the ambient pressure, which overrides the altitude. 1068 COMMAND_SET_AMBIENT_PRESSURE = 0xE000 1069 #: Run a forced recalibration against a known concentration. 1070 COMMAND_PERFORM_FORCED_RECALIBRATION = 0x362F 1071 #: Turn automatic self-calibration on or off. 1072 COMMAND_SET_AUTOMATIC_SELF_CALIBRATION_ENABLED = 0x2416 1073 #: Read whether automatic self-calibration is on. 1074 COMMAND_GET_AUTOMATIC_SELF_CALIBRATION_ENABLED = 0x2313 1075 #: Start low-power periodic measurement. 1076 COMMAND_START_LOW_POWER_PERIODIC_MEASUREMENT = 0x21AC 1077 #: Ask whether a result is waiting. 1078 COMMAND_GET_DATA_READY_STATUS = 0xE4B8 1079 #: Copy the settings to non-volatile memory. 1080 COMMAND_PERSIST_SETTINGS = 0x3615 1081 #: Read the serial number. 1082 COMMAND_GET_SERIAL_NUMBER = 0x3682 1083 #: Run the self test. 1084 COMMAND_PERFORM_SELF_TEST = 0x3639 1085 #: Restore the factory settings. 1086 COMMAND_PERFORM_FACTORY_RESET = 0x3632 1087 #: Reinitialise from the stored settings. 1088 COMMAND_REINIT = 0x3646 1089 #: Take one measurement and return to idle. 1090 COMMAND_MEASURE_SINGLE_SHOT = 0x219D 1091 #: Take one humidity and temperature measurement, without carbon dioxide. 1092 COMMAND_MEASURE_SINGLE_SHOT_RHT_ONLY = 0x2196 1093 #: Enter sleep, SCD41 only. 1094 COMMAND_POWER_DOWN = 0x36E0 1095 #: Leave sleep, SCD41 only. 1096 COMMAND_WAKE_UP = 0x36F6 1097 1098 def crc(self, data: bytes) -> int: 1099 """Compute the Sensirion CRC-8 the part appends to every word. 1100 1101 :param data: The bytes the checksum covers. 1102 :returns: The checksum. 1103 """ 1104 return _scd4x_crc(bytes(data)) 1105 1106 def word(self, frame: bytes) -> int: 1107 """Read a CRC-checked three-byte word frame. 1108 1109 :param frame: The two data bytes and their checksum. 1110 :returns: The 16-bit word. 1111 :raises ValueError: If the frame is not three bytes. 1112 :raises PamojaError: If the checksum does not match, which means the read 1113 was corrupted on the bus and should be repeated. 1114 """ 1115 return _scd4x_word(bytes(frame)) 1116 1117 def word_frame(self, value: int) -> bytes: 1118 """Build the three bytes the part sends for a word: the word then its CRC. 1119 1120 :param value: The 16-bit word. 1121 :returns: The frame in transmission order. 1122 """ 1123 return bytes(_scd4x_word_frame(value)) 1124 1125 def command_frame(self, command: int) -> bytes: 1126 """Build the two bytes that send a bare command. 1127 1128 :param command: The 16-bit command word. 1129 :returns: The bytes to write, most significant first. 1130 """ 1131 return bytes(_scd4x_command_frame(command)) 1132 1133 def write_frame(self, command: int, value: int) -> bytes: 1134 """Build the five bytes that send a command with an argument. 1135 1136 :param command: The 16-bit command word. 1137 :param value: The argument the command takes. 1138 :returns: The command, the argument, and the argument's CRC. 1139 """ 1140 return bytes(_scd4x_write_frame(command, value)) 1141 1142 def max_duration_ms(self, command: int) -> int | None: 1143 """Return how long a command may take. 1144 1145 :param command: The 16-bit command word. 1146 :returns: The datasheet's execution time in milliseconds, or ``None`` for a 1147 command that completes without one. 1148 """ 1149 return _scd4x_max_duration_ms(command) 1150 1151 def allowed_during_measurement(self, command: int) -> bool: 1152 """Report whether the part accepts a command while it is measuring. 1153 1154 :param command: The 16-bit command word. 1155 :returns: Whether the command can be sent without stopping measurement first. 1156 """ 1157 return _scd4x_allowed_during_measurement(command) 1158 1159 def parse_measurement(self, frame: bytes) -> Scd4xMeasurement: 1160 """Parse and CRC-check a nine-byte measurement frame. 1161 1162 :param frame: The three words and their checksums, as the device sent them. 1163 :returns: The decoded reading, in raw words and physical units. 1164 :raises ValueError: If the frame is not nine bytes. 1165 :raises PamojaError: If any checksum does not match. 1166 """ 1167 return _scd4x_parse_measurement(bytes(frame)) 1168 1169 def measurement_from_physical( 1170 self, co2_ppm: int, milli_celsius: int, humidity_milli_percent: int 1171 ) -> Scd4xMeasurement: 1172 """Build the measurement a sensor reporting these values would send. 1173 1174 :param co2_ppm: The carbon dioxide concentration in parts per million. 1175 :param milli_celsius: The temperature in milli-degrees Celsius. 1176 :param humidity_milli_percent: The relative humidity in milli-percent. 1177 :returns: The measurement, with the raw words the part would have reported. 1178 """ 1179 return _scd4x_measurement_from_physical( 1180 co2_ppm, milli_celsius, humidity_milli_percent 1181 ) 1182 1183 def measurement_bytes( 1184 self, co2_ppm: int, temperature_raw: int, humidity_raw: int 1185 ) -> bytes: 1186 """Build the nine bytes the part sends for a set of raw words. 1187 1188 The inverse of :meth:`parse_measurement`, so a node can be written and tested 1189 against what a sensor sends without one attached. 1190 1191 :param co2_ppm: The carbon dioxide concentration in parts per million. 1192 :param temperature_raw: The raw temperature word. 1193 :param humidity_raw: The raw humidity word. 1194 :returns: The frame in transmission order, each word followed by its CRC. 1195 """ 1196 return bytes(_scd4x_measurement_bytes(co2_ppm, temperature_raw, humidity_raw)) 1197 1198 def milli_celsius(self, raw: int) -> int: 1199 """Convert a raw temperature word to milli-degrees Celsius. 1200 1201 :param raw: The 16-bit temperature word. 1202 :returns: The temperature, exact in integer arithmetic. 1203 """ 1204 return _scd4x_milli_celsius(raw) 1205 1206 def celsius(self, raw: int) -> float: 1207 """Convert a raw temperature word to degrees Celsius. 1208 1209 :param raw: The 16-bit temperature word. 1210 :returns: The temperature. 1211 """ 1212 return _scd4x_celsius(raw) 1213 1214 def temperature_raw(self, milli_celsius: int) -> int: 1215 """Build the temperature word that decodes to a temperature. 1216 1217 :param milli_celsius: The temperature in milli-degrees Celsius. 1218 :returns: The 16-bit word, clamped to the part's range. 1219 """ 1220 return _scd4x_temperature_raw(milli_celsius) 1221 1222 def humidity_milli_percent(self, raw: int) -> int: 1223 """Convert a raw humidity word to milli-percent. 1224 1225 :param raw: The 16-bit humidity word. 1226 :returns: The relative humidity, exact in integer arithmetic. 1227 """ 1228 return _scd4x_humidity_milli_percent(raw) 1229 1230 def relative_humidity_percent(self, raw: int) -> float: 1231 """Convert a raw humidity word to a relative humidity percentage. 1232 1233 :param raw: The 16-bit humidity word. 1234 :returns: The relative humidity. 1235 """ 1236 return _scd4x_relative_humidity_percent(raw) 1237 1238 def humidity_raw(self, milli_percent: int) -> int: 1239 """Build the humidity word that decodes to a relative humidity. 1240 1241 :param milli_percent: The relative humidity in milli-percent. 1242 :returns: The 16-bit word, clamped to full scale. 1243 """ 1244 return _scd4x_humidity_raw(milli_percent) 1245 1246 def data_ready(self, word: int) -> bool: 1247 """Report whether a data-ready word says a measurement is waiting. 1248 1249 :param word: The word the data-ready command returned. 1250 :returns: Whether a result can be read. 1251 """ 1252 return _scd4x_data_ready(word) 1253 1254 def temperature_offset_word(self, milli_celsius: int) -> int: 1255 """Build the word that programs a temperature offset. 1256 1257 The offset scales by 2^16, not by the 2^16 - 1 a measurement uses. 1258 1259 :param milli_celsius: The offset in milli-degrees Celsius. 1260 :returns: The word to write. 1261 """ 1262 return _scd4x_temperature_offset_word(milli_celsius) 1263 1264 def temperature_offset_milli_celsius(self, word: int) -> int: 1265 """Convert a temperature-offset word back to milli-degrees Celsius. 1266 1267 :param word: The word the part returned. 1268 :returns: The offset in milli-degrees Celsius. 1269 """ 1270 return _scd4x_temperature_offset_milli_celsius(word) 1271 1272 def ambient_pressure_word(self, pascals: int) -> int: 1273 """Build the word that programs an ambient pressure. 1274 1275 :param pascals: The ambient pressure. 1276 :returns: The word to write, in hundreds of pascals. 1277 """ 1278 return _scd4x_ambient_pressure_word(pascals) 1279 1280 def ambient_pressure_pascals(self, word: int) -> int: 1281 """Convert an ambient-pressure word back to pascals. 1282 1283 :param word: The word the part holds. 1284 :returns: The ambient pressure. 1285 """ 1286 return _scd4x_ambient_pressure_pascals(word) 1287 1288 def forced_recalibration_correction_ppm(self, word: int) -> int | None: 1289 """Read the correction the part reports after a forced recalibration. 1290 1291 :param word: The word the recalibration command returned. 1292 :returns: The correction in parts per million, or ``None`` if the 1293 recalibration did not take. 1294 """ 1295 return _scd4x_forced_recalibration_correction_ppm(word) 1296 1297 def forced_recalibration_word(self, correction_ppm: int | None = None) -> int: 1298 """Build the word the part returns for a forced-recalibration outcome. 1299 1300 :param correction_ppm: The correction in parts per million, or ``None`` for 1301 a recalibration that did not take. 1302 :returns: The word. 1303 """ 1304 return _scd4x_forced_recalibration_word(correction_ppm) 1305 1306 def automatic_self_calibration_enabled(self, word: int) -> bool: 1307 """Report whether a word says automatic self-calibration is on. 1308 1309 :param word: The word the part returned. 1310 :returns: Whether the part recalibrates itself against clean air. 1311 """ 1312 return _scd4x_automatic_self_calibration_enabled(word) 1313 1314 def automatic_self_calibration_word(self, enabled: bool) -> int: 1315 """Build the word that turns automatic self-calibration on or off. 1316 1317 :param enabled: Whether to leave self-calibration running. 1318 :returns: The word to write. 1319 """ 1320 return _scd4x_automatic_self_calibration_word(enabled) 1321 1322 def self_test_passed(self, word: int) -> bool: 1323 """Report whether a self-test word says the part passed. 1324 1325 :param word: The word the self-test command returned. 1326 :returns: Whether the part reported no malfunction. 1327 """ 1328 return _scd4x_self_test_passed(word) 1329 1330 def serial_number(self, frame: bytes) -> int: 1331 """Read a CRC-checked nine-byte serial-number frame. 1332 1333 :param frame: The three words and their checksums. 1334 :returns: The 48-bit serial number. 1335 :raises ValueError: If the frame is not nine bytes. 1336 :raises PamojaError: If any checksum does not match. 1337 """ 1338 return _scd4x_serial_number(bytes(frame)) 1339 1340 def serial_number_frame(self, serial: int) -> bytes: 1341 """Build the nine bytes the part sends for a serial number. 1342 1343 :param serial: The 48-bit serial number. 1344 :returns: The frame in transmission order, each word followed by its CRC. 1345 """ 1346 return bytes(_scd4x_serial_number_frame(serial)) 1347 1348 1349class _Tmp117: 1350 """A TI TMP117 precision thermometer.""" 1351 1352 __slots__ = () 1353 1354 #: The value the device-ID register reads, which confirms the part. 1355 DEVICE_ID = 0x0117 1356 #: The value the configuration register reads after a reset. 1357 CONFIG_RESET = 0x0220 1358 #: The value the high-limit register holds after a reset. 1359 HIGH_LIMIT_RESET = 0x6000 1360 #: The value the low-limit register holds after a reset. 1361 LOW_LIMIT_RESET = 0x8000 1362 #: The value the result register holds before the first conversion. 1363 TEMP_RESULT_RESET = 0x8000 1364 #: The byte a general-call reset sends to address 0x00. 1365 GENERAL_CALL_RESET = 0x06 1366 #: The word written to the EEPROM-unlock register to allow a write. 1367 EEPROM_UNLOCK = 0x8000 1368 #: The address with ADD0 tied to GND. 1369 ADDRESS_ADD0_GND = 0x48 1370 #: The address with ADD0 tied to V+. 1371 ADDRESS_ADD0_VPLUS = 0x49 1372 #: The address with ADD0 tied to SDA. 1373 ADDRESS_ADD0_SDA = 0x4A 1374 #: The address with ADD0 tied to SCL. 1375 ADDRESS_ADD0_SCL = 0x4B 1376 #: The temperature result. 1377 REGISTER_TEMP_RESULT = 0x00 1378 #: The configuration register. 1379 REGISTER_CONFIGURATION = 0x01 1380 #: The high alert limit. 1381 REGISTER_THIGH_LIMIT = 0x02 1382 #: The low alert limit. 1383 REGISTER_TLOW_LIMIT = 0x03 1384 #: The EEPROM unlock register. 1385 REGISTER_EEPROM_UL = 0x04 1386 #: The first general-purpose EEPROM word. 1387 REGISTER_EEPROM1 = 0x05 1388 #: The second general-purpose EEPROM word. 1389 REGISTER_EEPROM2 = 0x06 1390 #: The temperature offset applied to every result. 1391 REGISTER_TEMP_OFFSET = 0x07 1392 #: The third general-purpose EEPROM word. 1393 REGISTER_EEPROM3 = 0x08 1394 #: The device-ID register. 1395 REGISTER_DEVICE_ID = 0x0F 1396 1397 def nano_celsius(self, raw: int) -> int: 1398 """Convert a raw temperature register to nano-degrees Celsius. 1399 1400 :param raw: The signed 16-bit register. 1401 :returns: The temperature, exact at the part's 7.8125 m°C resolution. 1402 """ 1403 return _tmp117_nano_celsius(raw) 1404 1405 def micro_celsius(self, raw: int) -> int: 1406 """Convert a raw temperature register to micro-degrees Celsius. 1407 1408 :param raw: The signed 16-bit register. 1409 :returns: The temperature, truncated toward zero. 1410 """ 1411 return _tmp117_micro_celsius(raw) 1412 1413 def celsius(self, raw: int) -> float: 1414 """Convert a raw temperature register to degrees Celsius. 1415 1416 :param raw: The signed 16-bit register. 1417 :returns: The temperature. 1418 """ 1419 return _tmp117_celsius(raw) 1420 1421 def raw_from_micro_celsius(self, micro_celsius: int) -> int: 1422 """Build the temperature register that decodes to a temperature. 1423 1424 :param micro_celsius: The temperature in micro-degrees Celsius. 1425 :returns: The signed register, rounded to nearest and saturating at the 1426 part's range. 1427 """ 1428 return _tmp117_raw_from_micro_celsius(micro_celsius) 1429 1430 def raw_from_celsius(self, celsius: float) -> int: 1431 """Build the temperature register that decodes to a temperature in Celsius. 1432 1433 :param celsius: The temperature. 1434 :returns: The signed register, saturating at the part's range. 1435 """ 1436 return _tmp117_raw_from_celsius(celsius) 1437 1438 def temperature_bytes(self, raw: int) -> bytes: 1439 """Build the two bytes the part sends for a temperature register. 1440 1441 :param raw: The signed 16-bit register. 1442 :returns: The bytes, most significant first. 1443 """ 1444 return bytes(_tmp117_temperature_bytes(raw)) 1445 1446 def temperature_from_bytes(self, data: bytes) -> int: 1447 """Read the two bytes the part sends for a temperature register. 1448 1449 :param data: The two register bytes, most significant first. 1450 :returns: The signed register value. 1451 :raises ValueError: If the read is not two bytes. 1452 """ 1453 return _tmp117_temperature_from_bytes(bytes(data)) 1454 1455 def device_id(self, raw: int) -> int: 1456 """Read the device identifier out of a device-ID register. 1457 1458 :param raw: The device-ID register. 1459 :returns: The 12-bit identifier, :attr:`DEVICE_ID` for a TMP117. 1460 """ 1461 return _tmp117_device_id(raw) 1462 1463 def revision(self, raw: int) -> int: 1464 """Read the die revision out of a device-ID register. 1465 1466 :param raw: The device-ID register. 1467 :returns: The 4-bit revision. 1468 """ 1469 return _tmp117_revision(raw) 1470 1471 def high_alert(self, config: int) -> bool: 1472 """Report whether a configuration register flags a high alert. 1473 1474 :param config: The configuration register. 1475 :returns: Whether a result went above the high limit. 1476 """ 1477 return _tmp117_high_alert(config) 1478 1479 def low_alert(self, config: int) -> bool: 1480 """Report whether a configuration register flags a low alert. 1481 1482 :param config: The configuration register. 1483 :returns: Whether a result went below the low limit. 1484 """ 1485 return _tmp117_low_alert(config) 1486 1487 def data_ready(self, config: int) -> bool: 1488 """Report whether a configuration register says a result is ready. 1489 1490 :param config: The configuration register. 1491 :returns: Whether a conversion completed since the register was last read. 1492 """ 1493 return _tmp117_data_ready(config) 1494 1495 def eeprom_busy(self, config: int) -> bool: 1496 """Report whether a configuration register says an EEPROM write is running. 1497 1498 :param config: The configuration register. 1499 :returns: Whether a programming cycle is still in progress. 1500 """ 1501 return _tmp117_eeprom_busy(config) 1502 1503 def eeprom_unlock_busy(self, unlock: int) -> bool: 1504 """Report whether an EEPROM unlock register says a write is running. 1505 1506 :param unlock: The EEPROM unlock register. 1507 :returns: Whether a programming cycle is still in progress. 1508 """ 1509 return _tmp117_eeprom_unlock_busy(unlock) 1510 1511 def config_bits(self, config: Tmp117Config) -> int: 1512 """Assemble the 16-bit configuration register value. 1513 1514 :param config: The settings to encode. 1515 :returns: The register value to write. 1516 """ 1517 return _tmp117_config_bits(config) 1518 1519 def config_from_bits(self, bits: int) -> Tmp117Config: 1520 """Parse a 16-bit configuration register value. 1521 1522 :param bits: The register value, as read from the device. 1523 :returns: The decoded settings. Every value decodes, so this never raises. 1524 """ 1525 return _tmp117_config_from_bits(bits) 1526 1527 def conversions(self, averaging: int) -> int: 1528 """Return how many conversions an averaging code folds into one result. 1529 1530 :param averaging: The averaging code, 0 to 3. 1531 :returns: The conversion count: 1, 8, 32, or 64. 1532 """ 1533 return _tmp117_averaging_conversions(averaging) 1534 1535 def conversion_micros(self, averaging: int) -> int: 1536 """Return how long an averaging code takes to convert. 1537 1538 :param averaging: The averaging code, 0 to 3. 1539 :returns: The conversion time in microseconds. 1540 """ 1541 return _tmp117_averaging_micros(averaging) 1542 1543 def nominal_micros(self, cycle: int) -> int: 1544 """Return the nominal cycle a conversion-cycle code selects. 1545 1546 :param cycle: The conversion-cycle code, 0 to 7. 1547 :returns: The nominal cycle in microseconds. 1548 """ 1549 return _tmp117_cycle_nominal_micros(cycle) 1550 1551 def cycle_micros(self, cycle: int, averaging: int) -> int: 1552 """Return the result-update interval for a cycle and averaging code. 1553 1554 A cycle shorter than the conversion it asks for stretches to the conversion. 1555 1556 :param cycle: The conversion-cycle code, 0 to 7. 1557 :param averaging: The averaging code, 0 to 3. 1558 :returns: The interval in microseconds. 1559 """ 1560 return _tmp117_cycle_micros(cycle, averaging) 1561 1562class _Hdc1080: 1563 """A TI HDC1080 temperature and humidity sensor.""" 1564 1565 __slots__ = () 1566 1567 #: The only address the part answers on. 1568 ADDRESS = 0x40 1569 #: The value the manufacturer-ID register reads. 1570 MANUFACTURER_ID = 0x5449 1571 #: The value the device-ID register reads, which confirms the part. 1572 DEVICE_ID = 0x1050 1573 #: The value the configuration register reads after a reset. 1574 CONFIGURATION_RESET = 0x1000 1575 #: The temperature result. 1576 REGISTER_TEMPERATURE = 0x00 1577 #: The humidity result. 1578 REGISTER_HUMIDITY = 0x01 1579 #: The configuration register. 1580 REGISTER_CONFIGURATION = 0x02 1581 #: The top word of the serial number. 1582 REGISTER_SERIAL_ID_HIGH = 0xFB 1583 #: The middle word of the serial number. 1584 REGISTER_SERIAL_ID_MID = 0xFC 1585 #: The bottom word of the serial number. 1586 REGISTER_SERIAL_ID_LOW = 0xFD 1587 #: The manufacturer-ID register. 1588 REGISTER_MANUFACTURER_ID = 0xFE 1589 #: The device-ID register. 1590 REGISTER_DEVICE_ID = 0xFF 1591 1592 def milli_celsius(self, raw: int) -> int: 1593 """Convert a raw temperature register to milli-degrees Celsius. 1594 1595 :param raw: The 16-bit register. 1596 :returns: The temperature, exact in integer arithmetic. 1597 """ 1598 return _hdc1080_milli_celsius(raw) 1599 1600 def celsius(self, raw: int) -> float: 1601 """Convert a raw temperature register to degrees Celsius. 1602 1603 :param raw: The 16-bit register. 1604 :returns: The temperature. 1605 """ 1606 return _hdc1080_celsius(raw) 1607 1608 def milli_percent(self, raw: int) -> int: 1609 """Convert a raw humidity register to milli-percent. 1610 1611 :param raw: The 16-bit register. 1612 :returns: The relative humidity, exact in integer arithmetic. 1613 """ 1614 return _hdc1080_milli_percent(raw) 1615 1616 def relative_humidity(self, raw: int) -> float: 1617 """Convert a raw humidity register to a relative humidity percentage. 1618 1619 :param raw: The 16-bit register. 1620 :returns: The relative humidity. 1621 """ 1622 return _hdc1080_relative_humidity(raw) 1623 1624 def temperature_register(self, milli_celsius: int) -> int: 1625 """Build the temperature register that decodes to a temperature. 1626 1627 :param milli_celsius: The temperature in milli-degrees Celsius. 1628 :returns: The 14-bit code in bits 15:2, clamped to the part's range. 1629 """ 1630 return _hdc1080_temperature_register(milli_celsius) 1631 1632 def humidity_register(self, milli_percent: int) -> int: 1633 """Build the humidity register that decodes to a relative humidity. 1634 1635 :param milli_percent: The relative humidity in milli-percent. 1636 :returns: The 14-bit code in bits 15:2, clamped to full scale. 1637 """ 1638 return _hdc1080_humidity_register(milli_percent) 1639 1640 def serial_id(self, high: int, mid: int, low: int) -> int: 1641 """Join the three serial-ID registers into the serial number. 1642 1643 :param high: The top serial-ID register. 1644 :param mid: The middle serial-ID register. 1645 :param low: The bottom serial-ID register. 1646 :returns: The 40-bit serial number. 1647 """ 1648 return _hdc1080_serial_id(high, mid, low) 1649 1650 def serial_id_registers(self, serial: int) -> tuple[int, int, int]: 1651 """Split a serial number back into the three serial-ID registers. 1652 1653 :param serial: The 40-bit serial number. 1654 :returns: The high, middle, and low registers. 1655 """ 1656 high, mid, low = _hdc1080_serial_id_registers(serial) 1657 return high, mid, low 1658 1659 def parse_measurement(self, data: bytes) -> Hdc1080Measurement: 1660 """Parse the four bytes a sequential read returns. 1661 1662 The part appends no checksum, so every read decodes. 1663 1664 :param data: The temperature and humidity registers, most significant first. 1665 :returns: The decoded reading, in raw registers and physical units. 1666 :raises ValueError: If the read is not four bytes. 1667 """ 1668 return _hdc1080_parse_measurement(bytes(data)) 1669 1670 def measurement_from_physical( 1671 self, milli_celsius: int, milli_percent: int 1672 ) -> Hdc1080Measurement: 1673 """Build the measurement a sensor reporting these values would send. 1674 1675 :param milli_celsius: The temperature in milli-degrees Celsius. 1676 :param milli_percent: The relative humidity in milli-percent. 1677 :returns: The measurement, with the raw registers the part would have held. 1678 """ 1679 return _hdc1080_measurement_from_physical(milli_celsius, milli_percent) 1680 1681 def measurement_bytes(self, temperature_raw: int, humidity_raw: int) -> bytes: 1682 """Build the four bytes the part sends for a pair of raw registers. 1683 1684 The inverse of :meth:`parse_measurement`, so a node can be written and tested 1685 against what a sensor sends without one attached. 1686 1687 :param temperature_raw: The raw temperature register. 1688 :param humidity_raw: The raw humidity register. 1689 :returns: The four bytes in read order. 1690 """ 1691 return bytes(_hdc1080_measurement_bytes(temperature_raw, humidity_raw)) 1692 1693 def config_from_register(self, raw: int) -> Hdc1080Config: 1694 """Parse a configuration register value. 1695 1696 :param raw: The register value, as read from the device. 1697 :returns: The decoded settings. 1698 :raises PamojaError: If the humidity-resolution field holds the code the 1699 datasheet leaves undefined, which no working part reports. 1700 """ 1701 return _hdc1080_config_from_register(raw) 1702 1703 def config_to_register(self, config: Hdc1080Config) -> int: 1704 """Assemble a configuration register value. 1705 1706 :param config: The settings to encode. 1707 :returns: The register value to write. 1708 :raises ValueError: If either resolution is not one the part offers. 1709 """ 1710 return _hdc1080_config_to_register(config) 1711 1712 def conversion_time_micros(self, config: Hdc1080Config) -> int: 1713 """Return how long to wait after triggering the part in this configuration. 1714 1715 :param config: The settings the part is running. 1716 :returns: The conversion time in microseconds, both channels in sequential 1717 mode. 1718 :raises ValueError: If either resolution is not one the part offers. 1719 """ 1720 return _hdc1080_conversion_time_micros(config) 1721 1722 def temperature_conversion_micros(self, bits: int) -> int: 1723 """Return how long a temperature conversion takes. 1724 1725 :param bits: The temperature resolution in bits: 14 or 11. 1726 :returns: The conversion time in microseconds. 1727 :raises ValueError: If the resolution is not one the part offers. 1728 """ 1729 return _hdc1080_temperature_conversion_micros(bits) 1730 1731 def humidity_conversion_micros(self, bits: int) -> int: 1732 """Return how long a humidity conversion takes. 1733 1734 :param bits: The humidity resolution in bits: 14, 11, or 8. 1735 :returns: The conversion time in microseconds. 1736 :raises ValueError: If the resolution is not one the part offers. 1737 """ 1738 return _hdc1080_humidity_conversion_micros(bits) 1739 1740 1741class _Opt3001: 1742 """A TI OPT3001 ambient light sensor.""" 1743 1744 __slots__ = () 1745 1746 #: The address the part answers on with ADDR tied to GND. 1747 ADDRESS_GND = 0x44 1748 #: The address it answers on with ADDR tied to VDD. 1749 ADDRESS_VDD = 0x45 1750 #: The address it answers on with ADDR tied to SDA. 1751 ADDRESS_SDA = 0x46 1752 #: The address it answers on with ADDR tied to SCL. 1753 ADDRESS_SCL = 0x47 1754 #: The value the manufacturer-ID register reads. 1755 MANUFACTURER_ID = 0x5449 1756 #: The value the device-ID register reads, which confirms the part. 1757 DEVICE_ID = 0x3001 1758 #: The value the configuration register reads after a reset. 1759 CONFIGURATION_RESET = 0xC810 1760 #: The value the low-limit register holds after a reset. 1761 LOW_LIMIT_RESET = 0x0000 1762 #: The value the high-limit register holds after a reset. 1763 HIGH_LIMIT_RESET = 0xBFFF 1764 #: The low-limit value that turns the INT pin into an end-of-conversion signal. 1765 LOW_LIMIT_END_OF_CONVERSION = 0xC000 1766 #: The range number that lets the part choose its own full scale. 1767 RANGE_AUTOMATIC = 0b1100 1768 #: The highest fixed range number. 1769 RANGE_MAX = 11 1770 #: The result register. 1771 REGISTER_RESULT = 0x00 1772 #: The configuration register. 1773 REGISTER_CONFIGURATION = 0x01 1774 #: The low limit. 1775 REGISTER_LOW_LIMIT = 0x02 1776 #: The high limit. 1777 REGISTER_HIGH_LIMIT = 0x03 1778 #: The manufacturer-ID register. 1779 REGISTER_MANUFACTURER_ID = 0x7E 1780 #: The device-ID register. 1781 REGISTER_DEVICE_ID = 0x7F 1782 1783 def lsb_milli_lux(self, exponent: int) -> int | None: 1784 """Return the illuminance one count carries at an exponent. 1785 1786 :param exponent: The result's exponent field, 0 to 11. 1787 :returns: The step in milli-lux, or ``None`` for a reserved exponent. 1788 """ 1789 return _opt3001_lsb_milli_lux(exponent) 1790 1791 def full_scale_milli_lux(self, range_number: int) -> int | None: 1792 """Return the full scale a range number covers. 1793 1794 :param range_number: The range number, 0 to 11. 1795 :returns: The full scale in milli-lux, or ``None`` for a reserved range, 1796 which includes the automatic-range code. 1797 """ 1798 return _opt3001_full_scale_milli_lux(range_number) 1799 1800 def milli_lux(self, raw: int) -> int: 1801 """Convert a raw result register to milli-lux. 1802 1803 :param raw: The register value, an exponent and a 12-bit mantissa. 1804 :returns: The illuminance, exact in integer arithmetic. 1805 """ 1806 return _opt3001_milli_lux(raw) 1807 1808 def lux(self, raw: int) -> float: 1809 """Convert a raw result register to lux. 1810 1811 :param raw: The register value. 1812 :returns: The illuminance. 1813 """ 1814 return _opt3001_lux(raw) 1815 1816 def raw_from_milli_lux(self, milli_lux: int) -> int: 1817 """Build the result register that decodes to an illuminance. 1818 1819 The smallest exponent that holds the value is used, so the mantissa keeps 1820 as much resolution as the format allows. 1821 1822 :param milli_lux: The illuminance in milli-lux. 1823 :returns: The register value, saturating at full scale. 1824 """ 1825 return _opt3001_raw_from_milli_lux(milli_lux) 1826 1827 def word_from_bytes(self, data: bytes) -> int: 1828 """Read the two bytes the part sends for a register. 1829 1830 :param data: The register bytes, most significant first. 1831 :returns: The register value. 1832 :raises ValueError: If the read is not two bytes. 1833 """ 1834 return _opt3001_word_from_bytes(bytes(data)) 1835 1836 def word_to_bytes(self, word: int) -> bytes: 1837 """Build the two bytes the part sends for a register. 1838 1839 :param word: The register value. 1840 :returns: The bytes, most significant first. 1841 """ 1842 return bytes(_opt3001_word_to_bytes(word)) 1843 1844 def config_bits(self, config: Opt3001Config) -> int: 1845 """Assemble the 16-bit configuration register value. 1846 1847 The read-only status bits are written as zero. 1848 1849 :param config: The settings to encode. 1850 :returns: The register value to write. 1851 """ 1852 return _opt3001_config_bits(config) 1853 1854 def config_from_bits(self, bits: int) -> Opt3001Config: 1855 """Parse a 16-bit configuration register value. 1856 1857 :param bits: The register value, as read from the device. 1858 :returns: The decoded settings. Every value decodes, so this never raises. 1859 """ 1860 return _opt3001_config_from_bits(bits) 1861 1862 def conversion_millis(self, long_conversion: bool) -> int: 1863 """Return the conversion time a setting selects. 1864 1865 :param long_conversion: Whether the 800 ms conversion is selected rather 1866 than the 100 ms one. 1867 :returns: The conversion time in milliseconds. 1868 """ 1869 return _opt3001_conversion_millis(long_conversion) 1870 1871 def fault_count(self, code: int) -> int: 1872 """Return how many consecutive faults a fault-count code requires. 1873 1874 :param code: The fault-count code, 0 to 3. 1875 :returns: The fault count: 1, 2, 4, or 8. 1876 """ 1877 return _opt3001_fault_count(code) 1878 1879 def is_automatic_range(self, range_number: int) -> bool: 1880 """Report whether a range number sets the full scale automatically. 1881 1882 :param range_number: The range number from a configuration register. 1883 :returns: Whether the part chooses its own full scale. 1884 """ 1885 return _opt3001_is_automatic_range(range_number) 1886 1887 1888class _Ina226: 1889 """A TI INA226 current, voltage, and power monitor.""" 1890 1891 __slots__ = () 1892 1893 #: The address with both A1 and A0 tied to GND. 1894 BASE_ADDRESS = 0x40 1895 #: The value the manufacturer-ID register reads. 1896 MANUFACTURER_ID = 0x5449 1897 #: The device identifier the die-ID register carries for an INA226. 1898 DEVICE_ID = 0x226 1899 #: The value the configuration register reads after a reset. 1900 CONFIG_RESET = 0x4127 1901 #: The shunt voltage one count carries, in nanovolts. 1902 SHUNT_LSB_NANOVOLTS = 2_500 1903 #: The bus voltage one count carries, in microvolts. 1904 BUS_LSB_MICROVOLTS = 1_250 1905 #: How many times the power LSB is the current LSB. 1906 POWER_LSB_RATIO = 25 1907 #: An address pin tied to GND. 1908 PIN_GROUND = 0 1909 #: An address pin tied to VS. 1910 PIN_SUPPLY = 1 1911 #: An address pin tied to SDA. 1912 PIN_SDA = 2 1913 #: An address pin tied to SCL. 1914 PIN_SCL = 3 1915 #: The configuration register. 1916 REGISTER_CONFIGURATION = 0x00 1917 #: The shunt voltage result. 1918 REGISTER_SHUNT_VOLTAGE = 0x01 1919 #: The bus voltage result. 1920 REGISTER_BUS_VOLTAGE = 0x02 1921 #: The power result. 1922 REGISTER_POWER = 0x03 1923 #: The current result. 1924 REGISTER_CURRENT = 0x04 1925 #: The calibration register. 1926 REGISTER_CALIBRATION = 0x05 1927 #: The Mask/Enable register. 1928 REGISTER_MASK_ENABLE = 0x06 1929 #: The alert limit. 1930 REGISTER_ALERT_LIMIT = 0x07 1931 #: The manufacturer-ID register. 1932 REGISTER_MANUFACTURER_ID = 0xFE 1933 #: The die-ID register. 1934 REGISTER_DIE_ID = 0xFF 1935 1936 def address(self, a1: int, a0: int) -> int: 1937 """Return the 7-bit address the A1 and A0 pins select. 1938 1939 :param a1: What the A1 pin is tied to, one of the ``PIN_`` codes. 1940 :param a0: What the A0 pin is tied to. 1941 :returns: The address, 0x40 to 0x4F. 1942 :raises ValueError: If either pin code is not 0, 1, 2, or 3. 1943 """ 1944 return _ina226_address(a1, a0) 1945 1946 def averaging_samples(self, averaging: int) -> int: 1947 """Return how many samples an averaging code folds into one result. 1948 1949 :param averaging: The averaging code, 0 to 7. 1950 :returns: The sample count, from 1 to 1024. 1951 """ 1952 return _ina226_averaging_samples(averaging) 1953 1954 def conversion_micros(self, conversion_time: int) -> int: 1955 """Return the conversion time a code selects. 1956 1957 :param conversion_time: The conversion-time code, 0 to 7. 1958 :returns: The typical conversion time in microseconds. 1959 """ 1960 return _ina226_conversion_micros(conversion_time) 1961 1962 def measures_shunt(self, mode: int) -> bool: 1963 """Report whether a mode code converts the shunt voltage. 1964 1965 :param mode: The operating-mode code, 0 to 7. 1966 :returns: Whether the shunt input is converted. 1967 """ 1968 return _ina226_measures_shunt(mode) 1969 1970 def measures_bus(self, mode: int) -> bool: 1971 """Report whether a mode code converts the bus voltage. 1972 1973 :param mode: The operating-mode code, 0 to 7. 1974 :returns: Whether the bus input is converted. 1975 """ 1976 return _ina226_measures_bus(mode) 1977 1978 def is_continuous(self, mode: int) -> bool: 1979 """Report whether a mode code keeps converting after the first result. 1980 1981 :param mode: The operating-mode code, 0 to 7. 1982 :returns: Whether conversions run back to back. 1983 """ 1984 return _ina226_is_continuous(mode) 1985 1986 def config_from_register(self, raw: int) -> Ina226Config: 1987 """Parse a configuration register value. 1988 1989 :param raw: The register value, as read from the device. 1990 :returns: The decoded settings. Every value decodes, so this never raises. 1991 """ 1992 return _ina226_config_from_register(raw) 1993 1994 def config_to_register(self, config: Ina226Config) -> int: 1995 """Assemble a configuration register value. 1996 1997 :param config: The settings to encode. 1998 :returns: The register value to write, with reserved bit 14 set as at reset. 1999 """ 2000 return _ina226_config_to_register(config) 2001 2002 def update_micros(self, config: Ina226Config) -> int: 2003 """Return how often the part in this configuration updates its results. 2004 2005 :param config: The settings the part is running. 2006 :returns: The update interval in microseconds, or 0 in power-down. 2007 """ 2008 return _ina226_update_micros(config) 2009 2010 def mask_enable_from_register(self, raw: int) -> Ina226MaskEnable: 2011 """Parse a Mask/Enable register value. 2012 2013 :param raw: The register value, as read from the device. 2014 :returns: The decoded enables and flags. 2015 """ 2016 return _ina226_mask_enable_from_register(raw) 2017 2018 def mask_enable_to_register(self, mask: Ina226MaskEnable) -> int: 2019 """Assemble a Mask/Enable register value. 2020 2021 :param mask: The enables and flags to encode. 2022 :returns: The register value to write. 2023 """ 2024 return _ina226_mask_enable_to_register(mask) 2025 2026 def active_alert_function( 2027 self, mask: Ina226MaskEnable 2028 ) -> Ina226AlertFunction | None: 2029 """Return the alert function the pin actually responds to. 2030 2031 Only one limit function drives the pin at a time; when several are enabled 2032 the part honours the most significant bit. 2033 2034 :param mask: The enables and flags the part is running. 2035 :returns: The function the pin follows, or ``None`` if no limit function is 2036 enabled. 2037 """ 2038 function = _ina226_active_alert_function(mask) 2039 return None if function is None else Ina226AlertFunction(function) 2040 2041 def die_id(self, raw: int) -> Ina226DieId: 2042 """Split a die-ID register into its device and revision fields. 2043 2044 :param raw: The die-ID register. 2045 :returns: The device identifier and die revision. 2046 """ 2047 return _ina226_die_id(raw) 2048 2049 def identify(self, manufacturer_id: int, die_id: int) -> Ina226DieId: 2050 """Check that a pair of identification registers belongs to an INA226. 2051 2052 :param manufacturer_id: The manufacturer-ID register. 2053 :param die_id: The die-ID register. 2054 :returns: The device identifier and die revision. 2055 :raises PamojaError: If either register does not carry the value the 2056 datasheet fixes, which means a different part answered at that address 2057 and its readings must not be trusted. 2058 """ 2059 return _ina226_identify(manufacturer_id, die_id) 2060 2061 def calibration(self, current_lsb_microamps: int, shunt_milliohms: int) -> int: 2062 """Compute the calibration register for a shunt and current resolution. 2063 2064 :param current_lsb_microamps: The microamps per count the current register 2065 should carry. 2066 :param shunt_milliohms: The shunt resistor value. 2067 :returns: The register value to write. 2068 """ 2069 return _ina226_calibration(current_lsb_microamps, shunt_milliohms) 2070 2071 def minimum_current_lsb_microamps(self, max_expected_microamps: int) -> int: 2072 """Return the smallest current resolution that still covers a maximum. 2073 2074 :param max_expected_microamps: The largest current the application measures. 2075 :returns: The minimum current LSB in microamps. 2076 """ 2077 return _ina226_minimum_current_lsb_microamps(max_expected_microamps) 2078 2079 def shunt_nanovolts(self, raw: int) -> int: 2080 """Convert a raw shunt-voltage register to nanovolts. 2081 2082 :param raw: The signed register value. 2083 :returns: The shunt voltage, at 2.5 uV per count. 2084 """ 2085 return _ina226_shunt_nanovolts(raw) 2086 2087 def shunt_millivolts(self, raw: int) -> float: 2088 """Convert a raw shunt-voltage register to millivolts. 2089 2090 :param raw: The signed register value. 2091 :returns: The shunt voltage. 2092 """ 2093 return _ina226_shunt_millivolts(raw) 2094 2095 def bus_microvolts(self, raw: int) -> int: 2096 """Convert a raw bus-voltage register to microvolts. 2097 2098 :param raw: The register value. 2099 :returns: The bus voltage, at 1.25 mV per count. 2100 """ 2101 return _ina226_bus_microvolts(raw) 2102 2103 def bus_volts(self, raw: int) -> float: 2104 """Convert a raw bus-voltage register to volts. 2105 2106 :param raw: The register value. 2107 :returns: The bus voltage. 2108 """ 2109 return _ina226_bus_volts(raw) 2110 2111 def current_microamps(self, raw: int, current_lsb_microamps: int) -> int: 2112 """Convert a raw current register to microamps. 2113 2114 :param raw: The signed register value. 2115 :param current_lsb_microamps: The resolution the calibration selected. 2116 :returns: The current. 2117 """ 2118 return _ina226_current_microamps(raw, current_lsb_microamps) 2119 2120 def current_amps(self, raw: int, current_lsb_microamps: int) -> float: 2121 """Convert a raw current register to amps. 2122 2123 :param raw: The signed register value. 2124 :param current_lsb_microamps: The resolution the calibration selected. 2125 :returns: The current. 2126 """ 2127 return _ina226_current_amps(raw, current_lsb_microamps) 2128 2129 def power_microwatts(self, raw: int, current_lsb_microamps: int) -> int: 2130 """Convert a raw power register to microwatts. 2131 2132 :param raw: The register value. 2133 :param current_lsb_microamps: The resolution the calibration selected. 2134 :returns: The power. The power LSB is fixed at 25 times the current LSB. 2135 """ 2136 return _ina226_power_microwatts(raw, current_lsb_microamps) 2137 2138 def power_watts(self, raw: int, current_lsb_microamps: int) -> float: 2139 """Convert a raw power register to watts. 2140 2141 :param raw: The register value. 2142 :param current_lsb_microamps: The resolution the calibration selected. 2143 :returns: The power. 2144 """ 2145 return _ina226_power_watts(raw, current_lsb_microamps) 2146 2147 def shunt_register(self, nanovolts: int) -> int: 2148 """Build the shunt-voltage register a monitor reports for a shunt voltage. 2149 2150 The inverse of :meth:`shunt_nanovolts`, so a node can be written and tested 2151 against what a monitor sends without one attached. 2152 2153 :param nanovolts: The shunt voltage in nanovolts. 2154 :returns: The signed register value. 2155 """ 2156 return _ina226_shunt_register(nanovolts) 2157 2158 def bus_register(self, microvolts: int) -> int: 2159 """Build the bus-voltage register a monitor reports for a bus voltage. 2160 2161 :param microvolts: The bus voltage in microvolts. 2162 :returns: The register value. 2163 """ 2164 return _ina226_bus_register(microvolts) 2165 2166 def current_register(self, microamps: int, current_lsb_microamps: int) -> int: 2167 """Build the current register a monitor reports for a current. 2168 2169 :param microamps: The current in microamps. 2170 :param current_lsb_microamps: The current LSB the calibration was set for. 2171 :returns: The signed register value. 2172 """ 2173 return _ina226_current_register(microamps, current_lsb_microamps) 2174 2175 def power_register(self, microwatts: int, current_lsb_microamps: int) -> int: 2176 """Build the power register a monitor reports for a power. 2177 2178 :param microwatts: The power in microwatts. 2179 :param current_lsb_microamps: The current LSB the calibration was set for. 2180 :returns: The register value. 2181 """ 2182 return _ina226_power_register(microwatts, current_lsb_microamps) 2183 2184 def current_register_from_shunt(self, shunt: int, calibration: int) -> int: 2185 """Compute the current register the chip derives from a shunt reading. 2186 2187 :param shunt: The signed shunt-voltage register. 2188 :param calibration: The calibration register the part is running. 2189 :returns: The signed current register. 2190 """ 2191 return _ina226_current_register_from_shunt(shunt, calibration) 2192 2193 def power_register_from_current(self, current: int, bus: int) -> int: 2194 """Compute the power register the chip derives from a current reading. 2195 2196 :param current: The signed current register. 2197 :param bus: The bus-voltage register. 2198 :returns: The power register. 2199 """ 2200 return _ina226_power_register_from_current(current, bus) 2201 2202 2203#: A Bosch BME280 temperature, pressure, and humidity sensor. 2204bme280 = _Bme280() 2205 2206#: A Maxim DS18B20 1-Wire thermometer. 2207ds18b20 = _Ds18b20() 2208 2209#: A TI INA219 current, voltage, and power monitor. 2210ina219 = _Ina219() 2211 2212#: A TI ADS1115 16-bit analogue-to-digital converter. 2213ads1115 = _Ads1115() 2214 2215#: A Bosch BMP280 pressure and temperature sensor. 2216bmp280 = _Bmp280() 2217 2218#: A Sensirion SHT30, SHT31, or SHT35 temperature and humidity sensor. 2219sht3x = _Sht3x() 2220 2221#: A Sensirion SCD40 or SCD41 carbon dioxide, temperature, and humidity sensor. 2222scd4x = _Scd4x() 2223 2224#: A TI TMP117 precision thermometer. 2225tmp117 = _Tmp117() 2226 2227#: A TI HDC1080 temperature and humidity sensor. 2228hdc1080 = _Hdc1080() 2229 2230#: A TI OPT3001 ambient light sensor. 2231opt3001 = _Opt3001() 2232 2233#: A TI INA226 current, voltage, and power monitor. 2234ina226 = _Ina226()
An ADS1115 configuration register, field by field.
A BME280's factory calibration, read once and reused for every measurement.
A compensated BME280 reading.
A BMP280's factory calibration, read once and reused for every measurement.
A BMP280's per-chip trimming coefficients, as they sit in its registers.
A BMP280 config register, field by field.
A BMP280 ctrl_meas register, field by field.
The uncompensated codes a BMP280 burst read carries.
A compensated BMP280 reading.
A decoded DS18B20 scratchpad.
An HDC1080 configuration register, field by field.
A decoded HDC1080 temperature and humidity pair.
588class Ina226AlertFunction(str, enum.Enum): 589 """The limit comparison an INA226 alert pin responds to.""" 590 591 #: The shunt voltage rose above the alert limit. 592 SHUNT_OVER_LIMIT = "ShuntOverLimit" 593 #: The shunt voltage fell below the alert limit. 594 SHUNT_UNDER_LIMIT = "ShuntUnderLimit" 595 #: The bus voltage rose above the alert limit. 596 BUS_OVER_LIMIT = "BusOverLimit" 597 #: The bus voltage fell below the alert limit. 598 BUS_UNDER_LIMIT = "BusUnderLimit" 599 #: The power rose above the alert limit. 600 POWER_OVER_LIMIT = "PowerOverLimit"
The limit comparison an INA226 alert pin responds to.
An INA226 configuration register, field by field.
A decoded INA226 die-ID register.
An INA226 Mask/Enable register, field by field.
An OPT3001 configuration register, field by field.
A decoded SCD4x measurement frame.
A decoded SHT3x temperature and humidity pair.
573class Sht3xRate(str, enum.Enum): 574 """How often an SHT3x in periodic mode takes a measurement.""" 575 576 #: One measurement every two seconds. 577 HALF_MPS = "HalfMps" 578 #: One measurement per second. 579 ONE_MPS = "OneMps" 580 #: Two measurements per second. 581 TWO_MPS = "TwoMps" 582 #: Four measurements per second. 583 FOUR_MPS = "FourMps" 584 #: Ten measurements per second. 585 TEN_MPS = "TenMps"
How often an SHT3x in periodic mode takes a measurement.
562class Sht3xRepeatability(str, enum.Enum): 563 """How hard an SHT3x works at one measurement, traded against time and power.""" 564 565 #: The fastest and least precise setting. 566 LOW = "Low" 567 #: The middle setting. 568 MEDIUM = "Medium" 569 #: The slowest and most precise setting. 570 HIGH = "High"
How hard an SHT3x works at one measurement, traded against time and power.
A decoded SHT3x status register.
A TMP117 configuration register, field by field.