Skip to content

Commit

Permalink
Set the device in continuous mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Corry committed Aug 24, 2022
1 parent b1b3dfc commit ec9331f
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions examples/EXAMPLES_LICENSE
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions examples/package.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
prefixes:
scd30: ..
packages:
..:
path: ..
3 changes: 3 additions & 0 deletions examples/package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dependencies:
scd30:
path: ..
22 changes: 12 additions & 10 deletions examples/room.toit
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand All @@ -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
29 changes: 26 additions & 3 deletions src/driver.toit
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down

0 comments on commit ec9331f

Please sign in to comment.