Skip to content

Commit c682c6c

Browse files
committed
Fix sensor determination.
Add command to return device type. Remove determineSensorType function. Replace with getFeatureSetVersion. Move setSensorType to public. Remove utility functions convertASCIIToHex and extractMaskedSensorType. Adapt begin function for new sensor function. Rev library version. Revise keywords.txt for the above changes.
1 parent 67f3677 commit c682c6c

File tree

5 files changed

+57
-128
lines changed

5 files changed

+57
-128
lines changed

examples/Example9_SensorType/Example9_SensorType.ino

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,10 @@ void setup()
4747
;
4848
}
4949

50-
char serialNumber[13];
51-
bool success = mySensor.getSerialNumber(serialNumber);
52-
Serial.print(F("Serial Number is: "));
53-
Serial.println(serialNumber);
54-
55-
Serial.print(F("Getting the sensor type: SCD4"));
56-
Serial.println(mySensor.getSensorType());
50+
scd4x_sensor_type_e sensor;
51+
bool success = mySensor.getFeatureSetVersion(&sensor);
52+
Serial.print(F("Sensor determined to by of type: SCD4"));
53+
Serial.println(sensor);
5754

5855
}
5956

keywords.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ measureSingleShotRHTOnly KEYWORD2
3838
sendCommand KEYWORD2
3939
readRegister KEYWORD2
4040
computeCRC8 KEYWORD2
41-
determineSensorType KEYWORD2
41+
getFeatureSetVersion KEYWORD2
4242
getSensorType KEYWORD2
43+
setSensorType KEYWORD2
4344

4445
#######################################
4546
# Constants (LITERAL1)
@@ -67,5 +68,6 @@ SCD4x_COMMAND_GET_SERIAL_NUMBER LITERAL1
6768
SCD4x_COMMAND_PERFORM_SELF_TEST LITERAL1
6869
SCD4x_COMMAND_PERFORM_FACTORY_RESET LITERAL1
6970
SCD4x_COMMAND_REINIT LITERAL1
71+
SCD4x_COMMAND_GET_FEATURE_SET_VERSION LITERAL1
7072
SCD4x_COMMAND_MEASURE_SINGLE_SHOT LITERAL1
7173
SCD4x_COMMAND_MEASURE_SINGLE_SHOT_RHT_ONLY LITERAL1

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SparkFun SCD4x Arduino Library
2-
version=1.1.1
2+
version=1.1.2
33
author=SparkFun Electronics
44
maintainer=SparkFun Electronics <sparkfun.com>
55
sentence=Library for the Sensirion SCD4x family of CO2 Sensors (SCD40 and SCD41)

src/SparkFun_SCD4x_Arduino_Library.cpp

Lines changed: 45 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ bool SCD4x::begin(TwoWire &wirePort, bool measBegin, bool autoCalibrate, bool sk
5959
if (pollAndSetDeviceType == true)
6060
{
6161
scd4x_sensor_type_e sensorType;
62-
success &= determineSensorType(&sensorType, serialNumber);
62+
success &= getFeatureSetVersion(&sensorType);
6363

6464
setSensorType(sensorType);
6565

@@ -870,27 +870,6 @@ char SCD4x::convertHexToASCII(uint8_t digit)
870870
return (char(digit + 0x41 - 10)); // Use upper case for A-F
871871
}
872872

873-
void SCD4x::convertASCIIToHex(const char *hexstr, uint16_t *integers)
874-
{
875-
for (int i = 0; i < 3; i++ ) {
876-
uint16_t val = 0;
877-
for (int j = 0; j < 4; j++ ) {
878-
char c = hexstr[i * 4 + j];
879-
val <<= 4;
880-
if (c >= '0' && c <= '9') {
881-
val += c - '0';
882-
}
883-
else if (c >= 'A' && c <= 'F') {
884-
val += c - 'A' + 10;
885-
}
886-
else if (c >= 'a' && c <= 'f') {
887-
val += c - 'a' + 10;
888-
}
889-
}
890-
integers[i] = val;
891-
}
892-
}
893-
894873
//Perform self test. Takes 10 seconds to complete. See 3.9.3
895874
//The perform_self_test feature can be used as an end-of-line test to check sensor functionality
896875
//and the customer power supply to the sensor.
@@ -1061,59 +1040,7 @@ bool SCD4x::measureSingleShotRHTOnly(void)
10611040
return (success);
10621041
}
10631042

1064-
// Determine Sensor type from the serial number
1065-
bool SCD4x::determineSensorType(scd4x_sensor_type_e *sensorType, char *serialNumber)
1066-
{
1067-
bool success = true;
1068-
1069-
if(strlen(serialNumber) != 12)
1070-
{
1071-
#if SCD4x_ENABLE_DEBUGLOG
1072-
if (_printDebug == true)
1073-
{
1074-
_debugPort->println(F("SCD40::getSensorType: S/N is not 13 bytes."));
1075-
_debugPort->print(F("SCD40::getSensorType: Num Bytes: "));
1076-
_debugPort->println(strlen(serialNumber));
1077-
}
1078-
#endif // if SCD4x_ENABLE_DEBUGLOG
1079-
success = false;
1080-
return (success);
1081-
}
1082-
1083-
uint16_t serNumInt[3];
1084-
convertASCIIToHex(serialNumber, serNumInt);
1085-
1086-
#if SCD4x_ENABLE_DEBUGLOG
1087-
if (_printDebug == true)
1088-
{
1089-
_debugPort->print("SCD40::getSensorType: Word 1: ");
1090-
_debugPort->println(serNumInt[0], HEX);
1091-
_debugPort->print("SCD40::getSensorType: Word 2: ");
1092-
_debugPort->println(serNumInt[1], HEX);
1093-
_debugPort->print("SCD40::getSensorType: Word 3: ");
1094-
_debugPort->println(serNumInt[2], HEX);
1095-
}
1096-
#endif // if SCD4x_ENABLE_DEBUGLOG
1097-
1098-
*sensorType = extractMaskedSensorType(serNumInt);
1099-
1100-
if (*sensorType == SCD4x_SENSOR_INVALID)
1101-
{
1102-
#if SCD4x_ENABLE_DEBUGLOG
1103-
if (_printDebug == true)
1104-
{
1105-
_debugPort->println("SCD40::getSensorType: Invalid Sensor Type Determined.");
1106-
}
1107-
#endif // if SCD4x_ENABLE_DEBUGLOG
1108-
*sensorType = SCD4x_SENSOR_SCD40; // Pick a default so we don't break things.
1109-
success = false;
1110-
return (success);
1111-
}
1112-
1113-
return (success);
1114-
}
1115-
1116-
scd4x_sensor_type_e SCD4x::getSensorType()
1043+
scd4x_sensor_type_e SCD4x::getSensorType(void)
11171044
{
11181045
return _sensorType;
11191046
}
@@ -1123,71 +1050,80 @@ void SCD4x::setSensorType(scd4x_sensor_type_e sensorType)
11231050
_sensorType = sensorType;
11241051
}
11251052

1126-
// Extract the Sensor type (SCD40 vs SCD41) from the Serial Number of the connected device.
1127-
scd4x_sensor_type_e SCD4x::extractMaskedSensorType(uint16_t *serialNumberArray)
1053+
bool SCD4x::getFeatureSetVersion(scd4x_sensor_type_e* sensorType)
11281054
{
1129-
// Of the 48-bit serial number, the Type is identified by bits 24-37
1130-
// 0bxxxx'xxxx'xx11'1111'1111'1111'xxxx'xxxx'xxxx'xxxx'xxxx'xxxx
1131-
uint16_t masks[] = {0x003F, 0xFF00};
1132-
1133-
uint16_t maskedValues[2];
1134-
uint16_t combinedValue = 0;
1135-
1136-
for (int i = 0; i < 2; i++) {
1137-
maskedValues[i] = serialNumberArray[i] & masks[i];
1055+
if (periodicMeasurementsAreRunning)
1056+
{
11381057
#if SCD4x_ENABLE_DEBUGLOG
1139-
if(_printDebug == true) {
1140-
_debugPort->print("SCD40::extractMaskedSensorType: i: ");
1141-
_debugPort->println(i);
1142-
_debugPort->print("SCD40::extractMaskedSensorType: serialNumberArray[i]: ");
1143-
_debugPort->println(serialNumberArray[i], HEX);
1144-
_debugPort->print("SCD40::extractMaskedSensorType: masks[i]: ");
1145-
_debugPort->println(masks[i], HEX);
1146-
_debugPort->print("SCD40::extractMaskedSensorType: maskedValues[i]: ");
1147-
_debugPort->println(maskedValues[i], HEX);
1058+
if (_printDebug == true)
1059+
{
1060+
_debugPort->println(F("SCD4x::getFeatureSetVersion: periodic measurements are running. Aborting..."));
11481061
}
1149-
#endif // if SCD4x_ENABLE_DEBUGLOG
1062+
#endif // SCD4x_ENABLE_DEBUGLOG
1063+
return (false);
11501064
}
11511065

1152-
combinedValue = (maskedValues[0] << 8) | (maskedValues[1] >> 8);
1066+
uint16_t featureSet;
1067+
1068+
bool success = readRegister(SCD4x_COMMAND_GET_FEATURE_SET_VERSION, &featureSet, 1);
11531069

11541070
#if SCD4x_ENABLE_DEBUGLOG
1155-
if (_printDebug == true)
1071+
if (_printDebug == true)
11561072
{
1157-
_debugPort->print("SCD40::extractMaskedSensorType: Combined Value: ");
1158-
_debugPort->println(combinedValue, HEX);
1073+
_debugPort->print(F("SCD4x::getFeatureSetVersion: Read value: 0x"));
1074+
_debugPort->println(featureSet, HEX);
11591075
}
1160-
#endif // if SCD4x_ENABLE_DEBUGLOG
1076+
#endif // SCD4x_ENABLE_DEBUGLOG
1077+
1078+
uint8_t typeOfSensor = ((featureSet & 0x1000) >> 12);
1079+
1080+
#if SCD4x_ENABLE_DEBUGLOG
1081+
if (_printDebug == true)
1082+
{
1083+
_debugPort->print(F("SCD4x::getFeatureSetVersion: Type read: 0x"));
1084+
_debugPort->println(typeOfSensor, HEX);
1085+
}
1086+
#endif // SCD4x_ENABLE_DEBUGLOG
11611087

1162-
if (combinedValue == 0x376F)
1088+
if (typeOfSensor == 0)
11631089
{
11641090
#if SCD4x_ENABLE_DEBUGLOG
11651091
if (_printDebug == true)
11661092
{
1167-
_debugPort->println("SCD40::extractMaskedSensorType: Picked SCD41");
1093+
_debugPort->println(F("SCD4x::getFeatureSetVersion: Picked SCD40"));
11681094
}
11691095
#endif // if SCD4x_ENABLE_DEBUGLOG
1170-
return SCD4x_SENSOR_SCD41;
1096+
*sensorType = SCD4x_SENSOR_SCD40;
11711097
}
1172-
else if (combinedValue == 0x2397)
1098+
else if (typeOfSensor == 1)
11731099
{
11741100
#if SCD4x_ENABLE_DEBUGLOG
11751101
if (_printDebug == true)
11761102
{
1177-
_debugPort->println("SCD40::extractMaskedSensorType: Picked SCD40");
1103+
_debugPort->println(F("SCD4x::getFeatureSetVersion: Picked SCD41"));
11781104
}
11791105
#endif // if SCD4x_ENABLE_DEBUGLOG
1180-
return SCD4x_SENSOR_SCD40;
1106+
*sensorType = SCD4x_SENSOR_SCD41;
11811107
}
11821108
else
11831109
{
11841110
#if SCD4x_ENABLE_DEBUGLOG
11851111
if (_printDebug == true) {
1186-
_debugPort->println("SCD40::extractMaskedSensorType: Something's seriously wrong here...");
1112+
if(typeOfSensor == 2)
1113+
{
1114+
_debugPort->println(F("SCD4x::getFeatureSetVersion: SCD42 is not supported by this library."));
1115+
}
1116+
else
1117+
{
1118+
_debugPort->println(F("SCD4x::getFeatureSetVersion: Unknown device type."));
1119+
}
11871120
}
11881121
#endif // if SCD4x_ENABLE_DEBUGLOG
1189-
return SCD4x_SENSOR_INVALID;
1122+
*sensorType = SCD4x_SENSOR_INVALID;
1123+
success = false;
11901124
}
1125+
1126+
return (success);
11911127
}
11921128

11931129
//Sends a command along with arguments and CRC

src/SparkFun_SCD4x_Arduino_Library.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
#define SCD4x_COMMAND_PERFORM_SELF_TEST 0x3639 // execution time: 10000ms
8282
#define SCD4x_COMMAND_PERFORM_FACTORY_RESET 0x3632 // execution time: 1200ms
8383
#define SCD4x_COMMAND_REINIT 0x3646 // execution time: 20ms
84+
#define SCD4x_COMMAND_GET_FEATURE_SET_VERSION 0x202F // execution time: 1ms
8485

8586
//Low power single shot - SCD41 only
8687
#define SCD4x_COMMAND_MEASURE_SINGLE_SHOT 0x219d // execution time: 5000ms
@@ -184,8 +185,9 @@ class SCD4x
184185

185186
uint8_t computeCRC8(uint8_t data[], uint8_t len);
186187

187-
bool determineSensorType(scd4x_sensor_type_e *sensorType, char *serialNumber); // Determine sensor type from serial number.
188-
scd4x_sensor_type_e getSensorType(); // Get the sensor type stored in the struct.
188+
bool getFeatureSetVersion(scd4x_sensor_type_e* sensorType);
189+
scd4x_sensor_type_e getSensorType(void); // Get the sensor type stored in the struct.
190+
void setSensorType(scd4x_sensor_type_e sensorType); // Set the sensor type for the device.
189191

190192
private:
191193
//Variables
@@ -215,14 +217,6 @@ class SCD4x
215217
//Convert serial number digit to ASCII
216218
char convertHexToASCII(uint8_t digit);
217219

218-
// Convert serial number string to hex digits.
219-
void convertASCIIToHex(const char *hexstr, uint16_t *integers);
220-
221-
// Helper function to compare the serial number to known IDs.
222-
scd4x_sensor_type_e extractMaskedSensorType(uint16_t *serialNumberArray);
223-
224-
void setSensorType(scd4x_sensor_type_e sensorType); // Set the sensor type for the device.
225-
226220
#if SCD4x_ENABLE_DEBUGLOG
227221
//Debug
228222
Stream *_debugPort; //The stream to send debug messages to if enabled. Usually Serial.

0 commit comments

Comments
 (0)