From 2c2ac6e522d17ef653bea615ee0e024fcb09b617 Mon Sep 17 00:00:00 2001 From: Erik Corry Date: Thu, 25 Aug 2022 18:15:20 +0200 Subject: [PATCH] Allow continuous mode to be deactivated --- examples/room.toit | 2 ++ src/driver.toit | 47 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/examples/room.toit b/examples/room.toit index cac94b4..6198d31 100644 --- a/examples/room.toit +++ b/examples/room.toit @@ -27,6 +27,8 @@ main: device := bus.device Scd30.I2C_ADDRESS scd30 := Scd30 device + sleep --ms=5000 // In case continuous measurement mode was not activated. + while true: reading := scd30.read if reading.co2 > 2000: diff --git a/src/driver.toit b/src/driver.toit index 76eb248..7a612fe 100644 --- a/src/driver.toit +++ b/src/driver.toit @@ -25,10 +25,17 @@ class Scd30: device_/serial.Device pressure_/int := ? + continuous_mode_/bool := true /** - Creates a driver and initializes the ambient pressure (in millibar - or hectopascal). + Creates a driver and initializes the ambient pressure calibration + (in millibar or hectopascal). + The device is initially in continuous measurement mode, but this + can be switched with the setter `driver.continuous_mode = false`. + If continuous measurement was previously disabled there is a delay + of a few seconds after running this constructor before the device + is ready to provide measurements. Reading too early can throw + write exceptions. */ constructor device/serial.Device --pressure/int=1013: device_ = device @@ -51,9 +58,40 @@ class Scd30: if i == 100: throw "Device is not getting ready." sleep --ms=20 + /** + Gets ambient pressure calibration in millibar or hectopascal. + The device does not measure pressure. This getter merely + retrieves the value that is used to calibrate the other + measurements. + */ + pressure -> int: + return pressure_ + + /** + Sets the ambient pressure in millibar or hectopascal. + If the device is not in continuous measurement mode then the + change takes effect when continuous measurement mode is enabled. + */ pressure= value/int: pressure_ = value - set_pressure_ + if continuous_mode_: + set_pressure_ + + continuous_mode -> bool: + return continuous_mode_ + + /** + Activates or deactivates continuous mode. When continuous mode is + activated there is a delay of a few seconds before the device is + ready to provide measurements. Reading too early can throw write + exceptions. + */ + continuous_mode= value/bool: + continuous_mode_ = value + if value: + set_pressure_ + else: + device_.write #[0x01, 0x04] set_pressure_ -> none: // Set continuous mode with the given (or default) air pressure. @@ -62,9 +100,6 @@ class Scd30: command := COMMAND_SET_CONTINUOUS_AND_PRESSURE_ + pressure_bytes + #[compute_crc8_ pressure_bytes] device_.write command - pressure -> int: - return pressure_ - /** Reads the measurements from the sensor. Waits until data is available and returns an object containing the CO2, temperature and humidity.