From ec9331f6595622ff7678d5db6e0b815916af00aa Mon Sep 17 00:00:00 2001 From: Erik Corry Date: Wed, 24 Aug 2022 16:32:22 +0200 Subject: [PATCH] Set the device in continuous mode --- README.md | 2 +- examples/EXAMPLES_LICENSE | 14 ++++++++++++++ examples/package.lock | 5 +++++ examples/package.yaml | 3 +++ examples/room.toit | 22 ++++++++++++---------- src/driver.toit | 29 ++++++++++++++++++++++++++--- 6 files changed, 61 insertions(+), 14 deletions(-) create mode 100644 examples/EXAMPLES_LICENSE create mode 100644 examples/package.lock create mode 100644 examples/package.yaml diff --git a/README.md b/README.md index 7332426..d78bbeb 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Driver for the Sensirion SCD30 sensor module, for monitoring CO2, temperature, a ## Usage -Installation: `toit pkg install github.com/qvisten999/scd30` +Installation: `toit.pkg install github.com/qvisten999/scd30` ``` import scd30 diff --git a/examples/EXAMPLES_LICENSE b/examples/EXAMPLES_LICENSE new file mode 100644 index 0000000..67136d0 --- /dev/null +++ b/examples/EXAMPLES_LICENSE @@ -0,0 +1,14 @@ +Zero-Clause BSD License + +Copyright (C) 2021 Toitware ApS + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR +OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +PERFORMANCE OF THIS SOFTWARE. diff --git a/examples/package.lock b/examples/package.lock new file mode 100644 index 0000000..65fa86b --- /dev/null +++ b/examples/package.lock @@ -0,0 +1,5 @@ +prefixes: + scd30: .. +packages: + ..: + path: .. diff --git a/examples/package.yaml b/examples/package.yaml new file mode 100644 index 0000000..71d9d4f --- /dev/null +++ b/examples/package.yaml @@ -0,0 +1,3 @@ +dependencies: + scd30: + path: .. diff --git a/examples/room.toit b/examples/room.toit index 4950c6f..cac94b4 100644 --- a/examples/room.toit +++ b/examples/room.toit @@ -1,6 +1,6 @@ -// Copyright (C) 2021 Toitware ApS. All rights reserved. -// Use of this source code is governed by a MIT-style license that can be found -// in the LICENSE file. +// Copyright (C) 2022 Toitware ApS. All rights reserved. +// Use of this source code is governed by a Zero-Clause BSD license that can +// be found in the EXAMPLES_LICENSE file. /** Small example to show the use of the CO2 sensor. @@ -15,9 +15,9 @@ The ESP32 pin connection to the SCD30 sensor is as follows: import gpio import i2c -import ..src.scd30 +import scd30 show Scd30 -co2_level_ := 0.0 +co2_level := 0.0 main: bus := i2c.Bus @@ -28,9 +28,11 @@ main: scd30 := Scd30 device while true: - co2_level_ = scd30.read.co2 - if co2_level_ > 2000: - print "Open your window" + reading := scd30.read + if reading.co2 > 2000: + print "Open your window: $(reading.co2.to_int)ppm" else: - print "CO2 level is healthy" - sleep --ms=60000 + print "CO2 level is healthy: $(reading.co2.to_int)ppm" + print "Temperature: $(%.1f reading.temperature)ÂșC" + print "Humidity: $(%.1f reading.humidity)%" + sleep --ms=6000 diff --git a/src/driver.toit b/src/driver.toit index 0b86219..76eb248 100644 --- a/src/driver.toit +++ b/src/driver.toit @@ -1,4 +1,4 @@ -// Copyright (C) 2021 Toitware ApS. All rights reserved. +// Copyright (C) 2022 Toitware ApS. All rights reserved. // Use of this source code is governed by an MIT-style license that can be // found in the LICENSE file. @@ -21,11 +21,20 @@ class Scd30: // Available commands. static COMMAND_GET_DATA_READY_ ::= #[0x02, 0x02] static COMMAND_READ_MEASUREMENT_ ::= #[0x03, 0x00] + static COMMAND_SET_CONTINUOUS_AND_PRESSURE_ ::= #[0x00, 0x10] - device_/serial.Device ::= ? + device_/serial.Device + pressure_/int := ? - constructor device/serial.Device: + /** + Creates a driver and initializes the ambient pressure (in millibar + or hectopascal). + */ + constructor device/serial.Device --pressure/int=1013: device_ = device + if not 0 < pressure < 0x10000: throw "OUT_OF_RANGE" + pressure_ = pressure + set_pressure_ /** Returns whether data is ready. @@ -42,6 +51,20 @@ class Scd30: if i == 100: throw "Device is not getting ready." sleep --ms=20 + pressure= value/int: + pressure_ = value + set_pressure_ + + set_pressure_ -> none: + // Set continuous mode with the given (or default) air pressure. + pressure_bytes := ByteArray 2 + binary.BIG_ENDIAN.put_int16 pressure_bytes 0 pressure_ + 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.