Skip to content

Commit 61cf4ff

Browse files
committed
Make SHTSensor a digital-sensor interface
SHT3xAnalog is now a standalone class split out from SHTSensor since it's the only analog driver currently supported. Support for SHTSensor integration was removed to simplify the interface. SHTSensor::SHTSensor and SHTSensor::init now do not take an optional external SHTSensorDriver* anymore making internal memory management simpler. Other minor other code changes Tested with SHT3x and SHTC1. SHT3xAnalog tested without attached sensor.
1 parent 5b827b5 commit 61cf4ff

File tree

3 files changed

+27
-70
lines changed

3 files changed

+27
-70
lines changed

SHTSensor.cpp

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (c) 2016, Sensirion AG <[email protected]>
3-
* Copyright (c) 2015, Johannes Winkelmann <[email protected]>
3+
* Copyright (c) 2015-2016, Johannes Winkelmann <[email protected]>
44
* All rights reserved.
55
*
66
* Redistribution and use in source and binary forms, with or without
@@ -170,7 +170,7 @@ class SHT3xSensor : public SHTI2cSensor
170170

171171
SHT3xSensor()
172172
: SHTI2cSensor(SHT3x_I2C_ADDRESS_44, SHT3x_ACCURACY_HIGH,
173-
-45, 175, 65535, 100, 65535)
173+
-45, 175, 65535, 100, 65535)
174174
{
175175
}
176176

@@ -212,26 +212,16 @@ class SHT3xAltSensor : public SHT3xSensor
212212
// class SHT3xAnalogSensor
213213
//
214214

215-
bool SHT3xAnalogSensor::readSample()
216-
{
217-
readTemperature();
218-
readHumidity();
219-
return true;
220-
}
221-
222215
float SHT3xAnalogSensor::readHumidity()
223216
{
224217
float max_adc = (float)((1 << mReadResolutionBits) - 1);
225-
mHumidity = -12.5f + 125 * (analogRead(mHumidityAdcPin) / max_adc);
226-
return mHumidity;
218+
return -12.5f + 125 * (analogRead(mHumidityAdcPin) / max_adc);
227219
}
228220

229221
float SHT3xAnalogSensor::readTemperature()
230222
{
231223
float max_adc = (float)((1 << mReadResolutionBits) - 1);
232-
mTemperature = -66.875f + 218.75f *
233-
(analogRead(mTemperatureAdcPin) / max_adc);
234-
return mTemperature;
224+
return -66.875f + 218.75f * (analogRead(mTemperatureAdcPin) / max_adc);
235225
}
236226

237227

@@ -247,36 +237,26 @@ const SHTSensor::SHTSensorType SHTSensor::AUTO_DETECT_SENSORS[] = {
247237
const float SHTSensor::TEMPERATURE_INVALID = NAN;
248238
const float SHTSensor::HUMIDITY_INVALID = NAN;
249239

250-
bool SHTSensor::init(SHTSensorDriver *sensor)
240+
bool SHTSensor::init()
251241
{
252242
if (mSensor != NULL) {
253243
cleanup();
254244
}
255-
mOwnSensor = (sensor == NULL);
256-
if (sensor) {
257-
mSensor = sensor;
258-
return true;
259-
}
260245

261246
switch(mSensorType) {
262247
case SHT3X:
263-
mSensor = new SHT3xSensor();
264-
break;
248+
mSensor = new SHT3xSensor();
249+
break;
265250

266251
case SHT3X_ALT:
267-
mSensor = new SHT3xAltSensor();
268-
break;
252+
mSensor = new SHT3xAltSensor();
253+
break;
269254

270255
case SHTW1:
271256
case SHTW2:
272257
case SHTC1:
273-
mSensor = new SHTC1Sensor();
274-
break;
275-
276-
case SHT3X_ANALOG:
277-
// There are no default parameters for the analog sensor.
278-
// Driver instantiation must happen explicitly and be passed to init().
279-
break;
258+
mSensor = new SHTC1Sensor();
259+
break;
280260

281261
case AUTO_DETECT:
282262
{
@@ -290,7 +270,6 @@ bool SHTSensor::init(SHTSensorDriver *sensor)
290270
break;
291271
}
292272
}
293-
// No sensor was auto detected
294273
if (!detected) {
295274
cleanup();
296275
}
@@ -325,7 +304,7 @@ bool SHTSensor::setI2cAddress(uint8_t newAddress)
325304

326305
void SHTSensor::cleanup()
327306
{
328-
if (mOwnSensor && mSensor) {
307+
if (mSensor) {
329308
delete mSensor;
330309
mSensor = NULL;
331310
}

SHTSensor.h

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (c) 2016, Sensirion AG <[email protected]>
3-
* Copyright (c) 2015, Johannes Winkelmann <[email protected]>
3+
* Copyright (c) 2015-2016, Johannes Winkelmann <[email protected]>
44
* All rights reserved.
55
*
66
* Redistribution and use in source and binary forms, with or without
@@ -41,8 +41,9 @@ class SHTSensor
4141
{
4242
public:
4343
/**
44-
* Enum of the supported Sensirion SHT Sensors.
45-
* Using the special AutoDetect sensor causes all i2c sensors to be
44+
* Enum of the supported Digital Sensirion SHT Sensors.
45+
* For analog sensors, see SHT3xAnalogSensor.
46+
* Using the special AUTO_DETECT sensor causes all i2c sensors to be
4647
* probed. The first matching sensor will then be used.
4748
*/
4849
enum SHTSensorType {
@@ -55,9 +56,7 @@ class SHTSensor
5556
SHT3X_ALT,
5657
SHTC1,
5758
SHTW1,
58-
SHTW2,
59-
// Analog sensors:
60-
SHT3X_ANALOG
59+
SHTW2
6160
};
6261

6362
/**
@@ -88,20 +87,10 @@ class SHTSensor
8887
* Instantiate a new SHTSensor
8988
* By default, the i2c bus is queried for known SHT Sensors. To address
9089
* a specific sensor, set the `sensorType'.
91-
*
92-
* Some sensor drivers require a specific parameter set and cannot be
93-
* auto-detected. Those must be instantiated separately and be passed to
94-
* `sensor'. In any other case sensor must be NULL.
95-
*
96-
* `ownSensor' describes whether the SHTSensor is responsible for freeing the
97-
* memory pointed to by `sensor'. This should never need to be set explicitly.
9890
*/
99-
SHTSensor(SHTSensorType sensorType = AUTO_DETECT,
100-
SHTSensorDriver *sensor = NULL,
101-
bool ownSensor = false)
91+
SHTSensor(SHTSensorType sensorType = AUTO_DETECT)
10292
: mSensorType(sensorType),
103-
mSensor(sensor),
104-
mOwnSensor(ownSensor),
93+
mSensor(NULL),
10594
mTemperature(SHTSensor::TEMPERATURE_INVALID),
10695
mHumidity(SHTSensor::HUMIDITY_INVALID)
10796
{
@@ -112,11 +101,11 @@ class SHTSensor
112101
}
113102

114103
/**
115-
* Initialize a new Temperature and Humidity sensor driver
116-
* To read out the sensor use readSample(), then use getTemperature() and
104+
* Initialize the sensor driver
105+
* To read out the sensor use readSample(), followed by getTemperature() and
117106
* getHumidity() to retrieve the values from the sample
118107
*/
119-
bool init(SHTSensorDriver *driver = NULL);
108+
bool init();
120109

121110
/**
122111
* Read new values from the sensor
@@ -162,7 +151,6 @@ class SHTSensor
162151

163152
SHTSensorType mSensorType;
164153
SHTSensorDriver *mSensor;
165-
bool mOwnSensor;
166154
float mTemperature;
167155
float mHumidity;
168156
};
@@ -171,7 +159,7 @@ class SHTSensor
171159
const uint8_t SHT3x_I2C_ADDRESS_44 = 0x44;
172160
const uint8_t SHT3x_I2C_ADDRESS_45 = 0x45;
173161

174-
/** Abstract class for an SHT Sensor driver */
162+
/** Abstract class for a digital SHT Sensor driver */
175163
class SHTSensorDriver
176164
{
177165
public:
@@ -271,7 +259,7 @@ class SHTI2cSensor : public SHTSensorDriver {
271259
uint8_t dataLength);
272260
};
273261

274-
class SHT3xAnalogSensor : public SHTSensorDriver
262+
class SHT3xAnalogSensor
275263
{
276264
public:
277265

@@ -281,19 +269,11 @@ class SHT3xAnalogSensor : public SHTSensorDriver
281269
* An optional `readResolutionBits' can be set since the Arduino/Genuino Zero
282270
* support 12bit precision analog readings. By default, 10 bit precision is
283271
* used.
284-
* The analog instance can then (a) be used directly or (b) be passed to the
285-
* SHTDriver instance for transparent usage accross SHT drivers:
286272
*
287-
* Example (a):
273+
* Example usage:
288274
* SHT3xAnalogSensor sht3xAnalog(HUMIDITY_PIN, TEMPERATURE_PIN);
289275
* float humidity = sht.readHumidity();
290-
*
291-
* Example (b):
292-
* SHT3xAnalogSensor shtDriver(HUMIDITY_PIN, TEMPERATURE_PIN);
293-
* SHTDriver sht(SHTDriver::SHT3xAnalog);
294-
* sht.init(&shtDriver)
295-
* sht.readSample();
296-
* float temperature = sht.getTemperature();
276+
* float temperature = sht.readTemperature();
297277
*/
298278
SHT3xAnalogSensor(uint8_t humidityPin, uint8_t temperaturePin,
299279
uint8_t readResolutionBits = 10)
@@ -306,8 +286,6 @@ class SHT3xAnalogSensor : public SHTSensorDriver
306286
{
307287
}
308288

309-
virtual bool readSample();
310-
311289
float readHumidity();
312290
float readTemperature();
313291

examples/sht-autodetect/sht-autodetect.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void loop() {
2626
// put your main code here, to run repeatedly:
2727

2828
if (sht.readSample()) {
29-
Serial.print("SHT:");
29+
Serial.print("SHT:\n");
3030
Serial.print(" RH: ");
3131
Serial.print(sht.getHumidity(), 2);
3232
Serial.print("\n");

0 commit comments

Comments
 (0)