Commit 6b079f75 authored by Eric Duminil's avatar Eric Duminil
Browse files

SCD30 lib -> 1.0.17

parent 8e15b6a8
Pipeline #5752 canceled with stage
...@@ -14,25 +14,34 @@ SCD30 KEYWORD1 ...@@ -14,25 +14,34 @@ SCD30 KEYWORD1
SCD30 KEYWORD2 SCD30 KEYWORD2
begin KEYWORD2 begin KEYWORD2
isConnected KEYWORD2
enableDebugging KEYWORD2 enableDebugging KEYWORD2
beginMeasuring KEYWORD2 beginMeasuring KEYWORD2
StopMeasurement KEYWORD2 StopMeasurement KEYWORD2
setAmbientPressure KEYWORD2
getSettingValue KEYWORD2 getSettingValue KEYWORD2
getForcedRecalibration KEYWORD2
getMeasurementInterval KEYWORD2
getTemperatureOffset KEYWORD2
getAltitudeCompensation KEYWORD2
getFirmwareVersion KEYWORD2 getFirmwareVersion KEYWORD2
getCO2 KEYWORD2 getCO2 KEYWORD2
getHumidity KEYWORD2 getHumidity KEYWORD2
getTemperature KEYWORD2 getTemperature KEYWORD2
getMeasurementInterval KEYWORD2
setMeasurementInterval KEYWORD2 setMeasurementInterval KEYWORD2
setAmbientPressure KEYWORD2
getAltitudeCompensation KEYWORD2
setAltitudeCompensation KEYWORD2 setAltitudeCompensation KEYWORD2
getAutoSelfCalibration KEYWORD2
setAutoSelfCalibration KEYWORD2 setAutoSelfCalibration KEYWORD2
getForcedRecalibration KEYWORD2
setForcedRecalibrationFactor KEYWORD2 setForcedRecalibrationFactor KEYWORD2
getTemperatureOffset KEYWORD2
setTemperatureOffset KEYWORD2 setTemperatureOffset KEYWORD2
getAutoSelfCalibration KEYWORD2
dataAvailable KEYWORD2 dataAvailable KEYWORD2
readMeasurement KEYWORD2 readMeasurement KEYWORD2
reset KEYWORD2 reset KEYWORD2
......
name=SparkFun SCD30 Arduino Library name=SparkFun SCD30 Arduino Library
version=1.0.13 version=1.0.17
author=SparkFun Electronics author=SparkFun Electronics
maintainer=SparkFun Electronics <sparkfun.com> maintainer=SparkFun Electronics <sparkfun.com>
sentence=Library for the Sensirion SCD30 CO2 Sensor sentence=Library for the Sensirion SCD30 CO2 Sensor
......
...@@ -7,9 +7,9 @@ ...@@ -7,9 +7,9 @@
Written by Nathan Seidle @ SparkFun Electronics, May 22nd, 2018 Written by Nathan Seidle @ SparkFun Electronics, May 22nd, 2018
Updated February 1st 2021 to include some of the features of paulvha's version of the library Updated February 1st 2021 to include some of the features of paulvha's version of the library
(while maintaining backward-compatibility): (while maintaining backward-compatibility):
https://github.com/paulvha/scd30 https://github.com/paulvha/scd30
Thank you Paul! Thank you Paul!
The SCD30 measures CO2 with accuracy of +/- 30ppm. The SCD30 measures CO2 with accuracy of +/- 30ppm.
...@@ -32,14 +32,14 @@ SCD30::SCD30(void) ...@@ -32,14 +32,14 @@ SCD30::SCD30(void)
// Constructor // Constructor
} }
//Initialize the Serial port // Initialize the Serial port
#ifdef USE_TEENSY3_I2C_LIB #ifdef USE_TEENSY3_I2C_LIB
bool SCD30::begin(i2c_t3 &wirePort, bool autoCalibrate, bool measBegin) bool SCD30::begin(i2c_t3 &wirePort, bool autoCalibrate, bool measBegin)
#else #else
bool SCD30::begin(TwoWire &wirePort, bool autoCalibrate, bool measBegin) bool SCD30::begin(TwoWire &wirePort, bool autoCalibrate, bool measBegin)
#endif #endif
{ {
_i2cPort = &wirePort; //Grab which port the user wants us to use _i2cPort = &wirePort; // Grab which port the user wants us to use
/* Especially during obtaining the ACK BIT after a byte sent the SCD30 is using clock stretching (but NOT only there)! /* Especially during obtaining the ACK BIT after a byte sent the SCD30 is using clock stretching (but NOT only there)!
* The need for clock stretching is described in the Sensirion_CO2_Sensors_SCD30_Interface_Description.pdf * The need for clock stretching is described in the Sensirion_CO2_Sensors_SCD30_Interface_Description.pdf
...@@ -58,128 +58,153 @@ bool SCD30::begin(TwoWire &wirePort, bool autoCalibrate, bool measBegin) ...@@ -58,128 +58,153 @@ bool SCD30::begin(TwoWire &wirePort, bool autoCalibrate, bool measBegin)
_i2cPort->setClockStretchLimit(200000); _i2cPort->setClockStretchLimit(200000);
#endif #endif
uint16_t fwVer; if (isConnected() == false)
if (getFirmwareVersion(&fwVer) == false) // Read the firmware version. Return false if the CRC check fails.
return (false); return (false);
if (_printDebug == true)
{
_debugPort->print(F("SCD30 begin: got firmware version 0x"));
_debugPort->println(fwVer, HEX);
}
if (measBegin == false) // Exit now if measBegin is false if (measBegin == false) // Exit now if measBegin is false
return (true); return (true);
//Check for device to respond correctly // Check for device to respond correctly
if (beginMeasuring() == true) //Start continuous measurements if (beginMeasuring() == true) // Start continuous measurements
{ {
setMeasurementInterval(2); //2 seconds between measurements setMeasurementInterval(2); // 2 seconds between measurements
setAutoSelfCalibration(autoCalibrate); //Enable auto-self-calibration setAutoSelfCalibration(autoCalibrate); // Enable auto-self-calibration
return (true); return (true);
} }
return (false); //Something went wrong return (false); // Something went wrong
}
// Returns true if device responds to a firmware request
bool SCD30::isConnected()
{
uint16_t fwVer;
if (getFirmwareVersion(&fwVer) == false) // Read the firmware version. Return false if the CRC check fails.
return (false);
if (_printDebug == true)
{
_debugPort->print(F("Firmware version 0x"));
_debugPort->println(fwVer, HEX);
}
return (true);
} }
//Calling this function with nothing sets the debug port to Serial // Calling this function with nothing sets the debug port to Serial
//You can also call it with other streams like Serial1, SerialUSB, etc. // You can also call it with other streams like Serial1, SerialUSB, etc.
void SCD30::enableDebugging(Stream &debugPort) void SCD30::enableDebugging(Stream &debugPort)
{ {
_debugPort = &debugPort; _debugPort = &debugPort;
_printDebug = true; _printDebug = true;
} }
//Returns the latest available CO2 level // Returns the latest available CO2 level
//If the current level has already been reported, trigger a new read // If the current level has already been reported, trigger a new read
uint16_t SCD30::getCO2(void) uint16_t SCD30::getCO2(void)
{ {
if (co2HasBeenReported == true) //Trigger a new read if (co2HasBeenReported == true) // Trigger a new read
readMeasurement(); //Pull in new co2, humidity, and temp into global vars {
if (readMeasurement() == false) // Pull in new co2, humidity, and temp into global vars
co2 = 0; // Failed to read sensor
}
co2HasBeenReported = true; co2HasBeenReported = true;
return (uint16_t)co2; //Cut off decimal as co2 is 0 to 10,000 return (uint16_t)co2; // Cut off decimal as co2 is 0 to 10,000
} }
//Returns the latest available humidity // Returns the latest available humidity
//If the current level has already been reported, trigger a new read // If the current level has already been reported, trigger a new read
float SCD30::getHumidity(void) float SCD30::getHumidity(void)
{ {
if (humidityHasBeenReported == true) //Trigger a new read if (humidityHasBeenReported == true) // Trigger a new read
readMeasurement(); //Pull in new co2, humidity, and temp into global vars if (readMeasurement() == false) // Pull in new co2, humidity, and temp into global vars
humidity = 0; // Failed to read sensor
humidityHasBeenReported = true; humidityHasBeenReported = true;
return humidity; return humidity;
} }
//Returns the latest available temperature // Returns the latest available temperature
//If the current level has already been reported, trigger a new read // If the current level has already been reported, trigger a new read
float SCD30::getTemperature(void) float SCD30::getTemperature(void)
{ {
if (temperatureHasBeenReported == true) //Trigger a new read if (temperatureHasBeenReported == true) // Trigger a new read
readMeasurement(); //Pull in new co2, humidity, and temp into global vars if (readMeasurement() == false) // Pull in new co2, humidity, and temp into global vars
temperature = 0; // Failed to read sensor
temperatureHasBeenReported = true; temperatureHasBeenReported = true;
return temperature; return temperature;
} }
//Enables or disables the ASC // Enables or disables the ASC
bool SCD30::setAutoSelfCalibration(bool enable) bool SCD30::setAutoSelfCalibration(bool enable)
{ {
if (enable) if (enable)
return sendCommand(COMMAND_AUTOMATIC_SELF_CALIBRATION, 1); //Activate continuous ASC return sendCommand(COMMAND_AUTOMATIC_SELF_CALIBRATION, 1); // Activate continuous ASC
else else
return sendCommand(COMMAND_AUTOMATIC_SELF_CALIBRATION, 0); //Deactivate continuous ASC return sendCommand(COMMAND_AUTOMATIC_SELF_CALIBRATION, 0); // Deactivate continuous ASC
} }
//Set the forced recalibration factor. See 1.3.7. // Set the forced recalibration factor. See 1.3.7.
//The reference CO2 concentration has to be within the range 400 ppm ≤ cref(CO2) ≤ 2000 ppm. // The reference CO2 concentration has to be within the range 400 ppm ≤ cref(CO2) ≤ 2000 ppm.
bool SCD30::setForcedRecalibrationFactor(uint16_t concentration) bool SCD30::setForcedRecalibrationFactor(uint16_t concentration)
{ {
if (concentration < 400 || concentration > 2000) if (concentration < 400 || concentration > 2000)
{ {
return false; //Error check. return false; // Error check.
} }
return sendCommand(COMMAND_SET_FORCED_RECALIBRATION_FACTOR, concentration); return sendCommand(COMMAND_SET_FORCED_RECALIBRATION_FACTOR, concentration);
} }
//Get the temperature offset. See 1.3.8. // Get the temperature offset. See 1.3.8.
float SCD30::getTemperatureOffset(void) float SCD30::getTemperatureOffset(void)
{ {
uint16_t response = readRegister(COMMAND_SET_TEMPERATURE_OFFSET); uint16_t response = readRegister(COMMAND_SET_TEMPERATURE_OFFSET);
return (((float)response) / 100.0);
}
//Set the temperature offset. See 1.3.8.
bool SCD30::setTemperatureOffset(float tempOffset)
{
union union
{ {
int16_t signed16; int16_t signed16;
uint16_t unsigned16; uint16_t unsigned16;
} signedUnsigned; // Avoid any ambiguity casting int16_t to uint16_t } signedUnsigned; // Avoid any ambiguity casting int16_t to uint16_t
signedUnsigned.signed16 = tempOffset * 100; signedUnsigned.signed16 = response;
return sendCommand(COMMAND_SET_TEMPERATURE_OFFSET, signedUnsigned.unsigned16);
return (((float)signedUnsigned.signed16) / 100.0);
} }
//Get the altitude compenstation. See 1.3.9. // Set the temperature offset to remove module heating from temp reading
bool SCD30::setTemperatureOffset(float tempOffset)
{
// Temp offset is only positive. See: https://github.com/sparkfun/SparkFun_SCD30_Arduino_Library/issues/27#issuecomment-971986826
//"The SCD30 offset temperature is obtained by subtracting the reference temperature from the SCD30 output temperature"
// https://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/9.5_CO2/Sensirion_CO2_Sensors_SCD30_Low_Power_Mode.pdf
if (tempOffset < 0.0)
return (false);
uint16_t value = tempOffset * 100;
return sendCommand(COMMAND_SET_TEMPERATURE_OFFSET, value);
}
// Get the altitude compenstation. See 1.3.9.
uint16_t SCD30::getAltitudeCompensation(void) uint16_t SCD30::getAltitudeCompensation(void)
{ {
return readRegister(COMMAND_SET_ALTITUDE_COMPENSATION); return readRegister(COMMAND_SET_ALTITUDE_COMPENSATION);
} }
//Set the altitude compenstation. See 1.3.9. // Set the altitude compenstation. See 1.3.9.
bool SCD30::setAltitudeCompensation(uint16_t altitude) bool SCD30::setAltitudeCompensation(uint16_t altitude)
{ {
return sendCommand(COMMAND_SET_ALTITUDE_COMPENSATION, altitude); return sendCommand(COMMAND_SET_ALTITUDE_COMPENSATION, altitude);
} }
//Set the pressure compenstation. This is passed during measurement startup. // Set the pressure compenstation. This is passed during measurement startup.
//mbar can be 700 to 1200 // mbar can be 700 to 1200
bool SCD30::setAmbientPressure(uint16_t pressure_mbar) bool SCD30::setAmbientPressure(uint16_t pressure_mbar)
{ {
if (pressure_mbar < 700 || pressure_mbar > 1200) if (pressure_mbar < 700 || pressure_mbar > 1200)
...@@ -192,33 +217,34 @@ bool SCD30::setAmbientPressure(uint16_t pressure_mbar) ...@@ -192,33 +217,34 @@ bool SCD30::setAmbientPressure(uint16_t pressure_mbar)
// SCD30 soft reset // SCD30 soft reset
void SCD30::reset() void SCD30::reset()
{ {
sendCommand(COMMAND_RESET); sendCommand(COMMAND_RESET);
} }
// Get the current ASC setting // Get the current ASC setting
bool SCD30::getAutoSelfCalibration() bool SCD30::getAutoSelfCalibration()
{ {
uint16_t response = readRegister(COMMAND_AUTOMATIC_SELF_CALIBRATION); uint16_t response = readRegister(COMMAND_AUTOMATIC_SELF_CALIBRATION);
if (response == 1) { if (response == 1)
{
return true; return true;
} }
else { else
return false; {
return false;
} }
} }
//Begins continuous measurements // Begins continuous measurements
//Continuous measurement status is saved in non-volatile memory. When the sensor // Continuous measurement status is saved in non-volatile memory. When the sensor
//is powered down while continuous measurement mode is active SCD30 will measure // is powered down while continuous measurement mode is active SCD30 will measure
//continuously after repowering without sending the measurement command. // continuously after repowering without sending the measurement command.
//Returns true if successful // Returns true if successful
bool SCD30::beginMeasuring(uint16_t pressureOffset) bool SCD30::beginMeasuring(uint16_t pressureOffset)
{ {
return (sendCommand(COMMAND_CONTINUOUS_MEASUREMENT, pressureOffset)); return (sendCommand(COMMAND_CONTINUOUS_MEASUREMENT, pressureOffset));
} }
//Overload - no pressureOffset // Overload - no pressureOffset
bool SCD30::beginMeasuring(void) bool SCD30::beginMeasuring(void)
{ {
return (beginMeasuring(0)); return (beginMeasuring(0));
...@@ -227,17 +253,26 @@ bool SCD30::beginMeasuring(void) ...@@ -227,17 +253,26 @@ bool SCD30::beginMeasuring(void)
// Stop continuous measurement // Stop continuous measurement
bool SCD30::StopMeasurement(void) bool SCD30::StopMeasurement(void)
{ {
return(sendCommand(COMMAND_STOP_MEAS)); return (sendCommand(COMMAND_STOP_MEAS));
} }
//Sets interval between measurements // Sets interval between measurements
//2 seconds to 1800 seconds (30 minutes) // 2 seconds to 1800 seconds (30 minutes)
bool SCD30::setMeasurementInterval(uint16_t interval) bool SCD30::setMeasurementInterval(uint16_t interval)
{ {
return sendCommand(COMMAND_SET_MEASUREMENT_INTERVAL, interval); return sendCommand(COMMAND_SET_MEASUREMENT_INTERVAL, interval);
} }
//Returns true when data is available // Gets interval between measurements
// 2 seconds to 1800 seconds (30 minutes)
uint16_t SCD30::getMeasurementInterval(void)
{
uint16_t interval = 0;
getSettingValue(COMMAND_SET_MEASUREMENT_INTERVAL, &interval);
return (interval);
}
// Returns true when data is available
bool SCD30::dataAvailable() bool SCD30::dataAvailable()
{ {
uint16_t response = readRegister(COMMAND_GET_DATA_READY); uint16_t response = readRegister(COMMAND_GET_DATA_READY);
...@@ -247,24 +282,27 @@ bool SCD30::dataAvailable() ...@@ -247,24 +282,27 @@ bool SCD30::dataAvailable()
return (false); return (false);
} }
//Get 18 bytes from SCD30 // Get 18 bytes from SCD30
//Updates global variables with floats // Updates global variables with floats
//Returns true if success // Returns true if success
bool SCD30::readMeasurement() bool SCD30::readMeasurement()
{ {
//Verify we have data from the sensor // Verify we have data from the sensor
if (dataAvailable() == false) if (dataAvailable() == false)
return (false); return (false);
ByteToFl tempCO2; tempCO2.value = 0; ByteToFl tempCO2;
ByteToFl tempHumidity; tempHumidity.value = 0; tempCO2.value = 0;
ByteToFl tempTemperature; tempTemperature.value = 0; ByteToFl tempHumidity;
tempHumidity.value = 0;
ByteToFl tempTemperature;
tempTemperature.value = 0;
_i2cPort->beginTransmission(SCD30_ADDRESS); _i2cPort->beginTransmission(SCD30_ADDRESS);
_i2cPort->write(COMMAND_READ_MEASUREMENT >> 8); //MSB _i2cPort->write(COMMAND_READ_MEASUREMENT >> 8); // MSB
_i2cPort->write(COMMAND_READ_MEASUREMENT & 0xFF); //LSB _i2cPort->write(COMMAND_READ_MEASUREMENT & 0xFF); // LSB
if (_i2cPort->endTransmission() != 0) if (_i2cPort->endTransmission() != 0)
return (0); //Sensor did not ACK return (0); // Sensor did not ACK
delay(3); delay(3);
...@@ -283,25 +321,25 @@ bool SCD30::readMeasurement() ...@@ -283,25 +321,25 @@ bool SCD30::readMeasurement()
case 1: case 1:
case 3: case 3:
case 4: case 4:
tempCO2.array[x < 3 ? 3-x : 4-x] = incoming; tempCO2.array[x < 3 ? 3 - x : 4 - x] = incoming;
bytesToCrc[x % 3] = incoming; bytesToCrc[x % 3] = incoming;
break; break;
case 6: case 6:
case 7: case 7:
case 9: case 9:
case 10: case 10:
tempTemperature.array[x < 9 ? 9-x : 10-x] = incoming; tempTemperature.array[x < 9 ? 9 - x : 10 - x] = incoming;
bytesToCrc[x % 3] = incoming; bytesToCrc[x % 3] = incoming;
break; break;
case 12: case 12:
case 13: case 13:
case 15: case 15:
case 16: case 16:
tempHumidity.array[x < 15 ? 15-x : 16-x] = incoming; tempHumidity.array[x < 15 ? 15 - x : 16 - x] = incoming;
bytesToCrc[x % 3] = incoming; bytesToCrc[x % 3] = incoming;
break; break;
default: default:
//Validate CRC // Validate CRC
uint8_t foundCrc = computeCRC8(bytesToCrc, 2); uint8_t foundCrc = computeCRC8(bytesToCrc, 2);
if (foundCrc != incoming) if (foundCrc != incoming)
{ {
...@@ -337,28 +375,28 @@ bool SCD30::readMeasurement() ...@@ -337,28 +375,28 @@ bool SCD30::readMeasurement()
_debugPort->println(F("readMeasurement: encountered error reading SCD30 data.")); _debugPort->println(F("readMeasurement: encountered error reading SCD30 data."));
return false; return false;
} }
//Now copy the uint32s into their associated floats // Now copy the uint32s into their associated floats
co2 = tempCO2.value; co2 = tempCO2.value;
temperature = tempTemperature.value; temperature = tempTemperature.value;
humidity = tempHumidity.value; humidity = tempHumidity.value;
//Mark our global variables as fresh // Mark our global variables as fresh
co2HasBeenReported = false; co2HasBeenReported = false;
humidityHasBeenReported = false; humidityHasBeenReported = false;
temperatureHasBeenReported = false; temperatureHasBeenReported = false;
return (true); //Success! New data available in globals. return (true); // Success! New data available in globals.
} }
//Gets a setting by reading the appropriate register. // Gets a setting by reading the appropriate register.
//Returns true if the CRC is valid. // Returns true if the CRC is valid.
bool SCD30::getSettingValue(uint16_t registerAddress, uint16_t *val) bool SCD30::getSettingValue(uint16_t registerAddress, uint16_t *val)
{ {
_i2cPort->beginTransmission(SCD30_ADDRESS); _i2cPort->beginTransmission(SCD30_ADDRESS);
_i2cPort->write(registerAddress >> 8); //MSB _i2cPort->write(registerAddress >> 8); // MSB
_i2cPort->write(registerAddress & 0xFF); //LSB _i2cPort->write(registerAddress & 0xFF); // LSB
if (_i2cPort->endTransmission() != 0) if (_i2cPort->endTransmission() != 0)
return (false); //Sensor did not ACK return (false); // Sensor did not ACK
delay(3); delay(3);
...@@ -384,14 +422,14 @@ bool SCD30::getSettingValue(uint16_t registerAddress, uint16_t *val) ...@@ -384,14 +422,14 @@ bool SCD30::getSettingValue(uint16_t registerAddress, uint16_t *val)
return (false); return (false);
} }
//Gets two bytes from SCD30 // Gets two bytes from SCD30
uint16_t SCD30::readRegister(uint16_t registerAddress) uint16_t SCD30::readRegister(uint16_t registerAddress)
{ {
_i2cPort->beginTransmission(SCD30_ADDRESS); _i2cPort->beginTransmission(SCD30_ADDRESS);
_i2cPort->write(registerAddress >> 8); //MSB _i2cPort->write(registerAddress >> 8); // MSB
_i2cPort->write(registerAddress & 0xFF); //LSB _i2cPort->write(registerAddress & 0xFF); // LSB
if (_i2cPort->endTransmission() != 0) if (_i2cPort->endTransmission() != 0)
return (0); //Sensor did not ACK return (0); // Sensor did not ACK
delay(3); delay(3);
...@@ -402,49 +440,49 @@ uint16_t SCD30::readRegister(uint16_t registerAddress) ...@@ -402,49 +440,49 @@ uint16_t SCD30::readRegister(uint16_t registerAddress)
uint8_t lsb = _i2cPort->read(); uint8_t lsb = _i2cPort->read();
return ((uint16_t)msb << 8 | lsb); return ((uint16_t)msb << 8 | lsb);
} }
return (0); //Sensor did not respond return (0); // Sensor did not respond
} }
//Sends a command along with arguments and CRC // Sends a command along with arguments and CRC
bool SCD30::sendCommand(uint16_t command, uint16_t arguments) bool SCD30::sendCommand(uint16_t command, uint16_t arguments)
{ {
uint8_t data[2]; uint8_t data[2];
data[0] = arguments >> 8; data[0] = arguments >> 8;
data[1] = arguments & 0xFF; data[1] = arguments & 0xFF;
uint8_t crc = computeCRC8(data, 2); //Calc CRC on the arguments only, not the command uint8_t crc = computeCRC8(data, 2); // Calc CRC on the arguments only, not the command
_i2cPort->beginTransmission(SCD30_ADDRESS); _i2cPort->beginTransmission(SCD30_ADDRESS);
_i2cPort->write(command >> 8); //MSB _i2cPort->write(command >> 8); // MSB
_i2cPort->write(command & 0xFF); //LSB _i2cPort->write(command & 0xFF); // LSB
_i2cPort->write(arguments >> 8); //MSB _i2cPort->write(arguments >> 8); // MSB
_i2cPort->write(arguments & 0xFF); //LSB _i2cPort->write(arguments & 0xFF); // LSB
_i2cPort->write(crc); _i2cPort->write(crc);
if (_i2cPort->endTransmission() != 0) if (_i2cPort->endTransmission() != 0)
return (false); //Sensor did not ACK return (false); // Sensor did not ACK
return (true); return (true);
} }
//Sends just a command, no arguments, no CRC // Sends just a command, no arguments, no CRC
bool SCD30::sendCommand(uint16_t command) bool SCD30::sendCommand(uint16_t command)
{ {
_i2cPort->beginTransmission(SCD30_ADDRESS); _i2cPort->beginTransmission(SCD30_ADDRESS);
_i2cPort->write(command >> 8); //MSB _i2cPort->write(command >> 8); // MSB
_i2cPort->write(command & 0xFF); //LSB _i2cPort->write(command & 0xFF); // LSB
if (_i2cPort->endTransmission() != 0) if (_i2cPort->endTransmission() != 0)
return (false); //Sensor did not ACK return (false); // Sensor did not ACK
return (true); return (true);
} }
//Given an array and a number of bytes, this calculate CRC8 for those bytes // Given an array and a number of bytes, this calculate CRC8 for those bytes
//CRC is only calc'd on the data portion (two bytes) of the four bytes being sent // CRC is only calc'd on the data portion (two bytes) of the four bytes being sent
//From: http://www.sunshine2k.de/articles/coding/crc/understanding_crc.html // From: http://www.sunshine2k.de/articles/coding/crc/understanding_crc.html
//Tested with: http://www.sunshine2k.de/coding/javascript/crc/crc_js.html // Tested with: http://www.sunshine2k.de/coding/javascript/crc/crc_js.html
//x^8+x^5+x^4+1 = 0x31 // x^8+x^5+x^4+1 = 0x31
uint8_t SCD30::computeCRC8(uint8_t data[], uint8_t len) uint8_t SCD30::computeCRC8(uint8_t data[], uint8_t len)
{ {
uint8_t crc = 0xFF; //Init with 0xFF uint8_t crc = 0xFF; // Init with 0xFF
for (uint8_t x = 0; x < len; x++) for (uint8_t x = 0; x < len; x++)
{ {
...@@ -459,5 +497,5 @@ uint8_t SCD30::computeCRC8(uint8_t data[], uint8_t len) ...@@ -459,5 +497,5 @@ uint8_t SCD30::computeCRC8(uint8_t data[], uint8_t len)
} }
} }
return crc; //No output reflection return crc; // No output reflection
} }
...@@ -40,10 +40,10 @@ ...@@ -40,10 +40,10 @@
#include <Wire.h> #include <Wire.h>
#endif #endif
//The default I2C address for the SCD30 is 0x61. // The default I2C address for the SCD30 is 0x61.
#define SCD30_ADDRESS 0x61 #define SCD30_ADDRESS 0x61
//Available commands // Available commands
#define COMMAND_CONTINUOUS_MEASUREMENT 0x0010 #define COMMAND_CONTINUOUS_MEASUREMENT 0x0010
#define COMMAND_SET_MEASUREMENT_INTERVAL 0x4600 #define COMMAND_SET_MEASUREMENT_INTERVAL 0x4600
...@@ -70,38 +70,43 @@ public: ...@@ -70,38 +70,43 @@ public:
bool begin(bool autoCalibrate) { return begin(Wire, autoCalibrate); } bool begin(bool autoCalibrate) { return begin(Wire, autoCalibrate); }
#ifdef USE_TEENSY3_I2C_LIB #ifdef USE_TEENSY3_I2C_LIB
bool begin(i2c_t3 &wirePort = Wire, bool autoCalibrate = false, bool measBegin = true); //By default use Wire port bool begin(i2c_t3 &wirePort = Wire, bool autoCalibrate = false, bool measBegin = true); // By default use Wire port
#else #else
bool begin(TwoWire &wirePort = Wire, bool autoCalibrate = false, bool measBegin = true); //By default use Wire port bool begin(TwoWire &wirePort = Wire, bool autoCalibrate = false, bool measBegin = true); // By default use Wire port
#endif #endif
void enableDebugging(Stream &debugPort = Serial); //Turn on debug printing. If user doesn't specify then Serial will be used. bool isConnected();
void enableDebugging(Stream &debugPort = Serial); // Turn on debug printing. If user doesn't specify then Serial will be used.
bool beginMeasuring(uint16_t pressureOffset); bool beginMeasuring(uint16_t pressureOffset);
bool beginMeasuring(void); bool beginMeasuring(void);
bool StopMeasurement(void); // paulvha bool StopMeasurement(void); // paulvha
// based on paulvha bool setAmbientPressure(uint16_t pressure_mbar);
bool getSettingValue(uint16_t registerAddress, uint16_t *val); bool getSettingValue(uint16_t registerAddress, uint16_t *val);
bool getForcedRecalibration(uint16_t *val) { return (getSettingValue(COMMAND_SET_FORCED_RECALIBRATION_FACTOR, val)); }
bool getMeasurementInterval(uint16_t *val) { return (getSettingValue(COMMAND_SET_MEASUREMENT_INTERVAL, val)); }
bool getTemperatureOffset(uint16_t *val) { return (getSettingValue(COMMAND_SET_TEMPERATURE_OFFSET, val)); }
bool getAltitudeCompensation(uint16_t *val) { return (getSettingValue(COMMAND_SET_ALTITUDE_COMPENSATION, val)); }
bool getFirmwareVersion(uint16_t *val) { return (getSettingValue(COMMAND_READ_FW_VER, val)); } bool getFirmwareVersion(uint16_t *val) { return (getSettingValue(COMMAND_READ_FW_VER, val)); }
uint16_t getCO2(void); uint16_t getCO2(void);
float getHumidity(void); float getHumidity(void);
float getTemperature(void); float getTemperature(void);
float getTemperatureOffset(void);
uint16_t getAltitudeCompensation(void);
uint16_t getMeasurementInterval(void);
bool getMeasurementInterval(uint16_t *val) { return (getSettingValue(COMMAND_SET_MEASUREMENT_INTERVAL, val)); }
bool setMeasurementInterval(uint16_t interval); bool setMeasurementInterval(uint16_t interval);
bool setAmbientPressure(uint16_t pressure_mbar);
uint16_t getAltitudeCompensation(void);
bool getAltitudeCompensation(uint16_t *val) { return (getSettingValue(COMMAND_SET_ALTITUDE_COMPENSATION, val)); }
bool setAltitudeCompensation(uint16_t altitude); bool setAltitudeCompensation(uint16_t altitude);
bool getAutoSelfCalibration(void);
bool setAutoSelfCalibration(bool enable); bool setAutoSelfCalibration(bool enable);
bool getForcedRecalibration(uint16_t *val) { return (getSettingValue(COMMAND_SET_FORCED_RECALIBRATION_FACTOR, val)); }
bool setForcedRecalibrationFactor(uint16_t concentration); bool setForcedRecalibrationFactor(uint16_t concentration);
float getTemperatureOffset(void);
bool getTemperatureOffset(uint16_t *val) { return (getSettingValue(COMMAND_SET_TEMPERATURE_OFFSET, val)); }
bool setTemperatureOffset(float tempOffset); bool setTemperatureOffset(float tempOffset);
bool getAutoSelfCalibration(void);
bool dataAvailable(); bool dataAvailable();
bool readMeasurement(); bool readMeasurement();
...@@ -116,25 +121,25 @@ public: ...@@ -116,25 +121,25 @@ public:
uint8_t computeCRC8(uint8_t data[], uint8_t len); uint8_t computeCRC8(uint8_t data[], uint8_t len);
private: private:
//Variables // Variables
#ifdef USE_TEENSY3_I2C_LIB #ifdef USE_TEENSY3_I2C_LIB
i2c_t3 *_i2cPort; //The generic connection to user's chosen I2C hardware i2c_t3 *_i2cPort; // The generic connection to user's chosen I2C hardware
#else #else
TwoWire *_i2cPort; //The generic connection to user's chosen I2C hardware TwoWire *_i2cPort; // The generic connection to user's chosen I2C hardware
#endif #endif
//Global main datums // Global main datums
float co2 = 0; float co2 = 0;
float temperature = 0; float temperature = 0;
float humidity = 0; float humidity = 0;
//These track the staleness of the current data // These track the staleness of the current data
//This allows us to avoid calling readMeasurement() every time individual datums are requested // This allows us to avoid calling readMeasurement() every time individual datums are requested
bool co2HasBeenReported = true; bool co2HasBeenReported = true;
bool humidityHasBeenReported = true; bool humidityHasBeenReported = true;
bool temperatureHasBeenReported = true; bool temperatureHasBeenReported = true;
//Debug // Debug
Stream *_debugPort; //The stream to send debug messages to if enabled. Usually Serial. Stream *_debugPort; // The stream to send debug messages to if enabled. Usually Serial.
boolean _printDebug = false; //Flag to print debugging variables boolean _printDebug = false; // Flag to print debugging variables
}; };
#endif #endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment