From 39a9fddc6218e7bceb95b2a164a6329434043d58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Mon, 9 Apr 2018 15:42:25 +0200 Subject: [PATCH] Support for TMP35 & TMP37 temperature sensors --- code/espurna/config/sensors.h | 22 ++++++---- code/espurna/sensor.ino | 5 ++- .../sensors/{TMP36Sensor.h => TMP3XSensor.h} | 44 +++++++++++++++---- 3 files changed, 51 insertions(+), 20 deletions(-) rename code/espurna/sensors/{TMP36Sensor.h => TMP3XSensor.h} (55%) diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index 89d649c3..28e6f873 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -109,7 +109,7 @@ #define SENSOR_AM2320_ID 0x19 #define SENSOR_GUVAS12SD_ID 0x20 #define SENSOR_CSE7766_ID 0x21 -#define SENSOR_TMP36_ID 0x22 +#define SENSOR_TMP3X_ID 0x22 //-------------------------------------------------------------------------------- // Magnitudes @@ -516,15 +516,19 @@ #endif //------------------------------------------------------------------------------ -// TMP36 analog temperature sensor -// Enable support by passing TMP36_SUPPORT=1 build flag +// TMP3X analog temperature sensor +// Enable support by passing TMP3X_SUPPORT=1 build flag //------------------------------------------------------------------------------ -#ifndef TMP36_SUPPORT -#define TMP36_SUPPORT 1 +#ifndef TMP3X_SUPPORT +#define TMP3X_SUPPORT 1 #endif -#if TMP36_SUPPORT +#ifndef TMP3X_TYPE +#define TMP3X_TYPE TMP3X_TMP35 +#endif + +#if TMP3X_SUPPORT #undef ADC_VCC_ENABLED #define ADC_VCC_ENABLED 0 #endif @@ -597,7 +601,7 @@ || EMON_ANALOG_SUPPORT || EVENTS_SUPPORT || HLW8012_SUPPORT \ || MHZ19_SUPPORT || PMSX003_SUPPORT || SHT3X_I2C_SUPPORT \ || SI7021_SUPPORT || V9261F_SUPPORT || AM2320_SUPPORT \ - || GUVAS12SD_SUPPORT || CSE7766_SUPPORT || TMP36_SUPPORT + || GUVAS12SD_SUPPORT || CSE7766_SUPPORT || TMP3X_SUPPORT #define SENSOR_SUPPORT 1 #else #define SENSOR_SUPPORT 0 @@ -803,8 +807,8 @@ PROGMEM const char* const magnitude_units[] = { #include "../sensors/SHT3XI2CSensor.h" #endif -#if TMP36_SUPPORT - #include "../sensors/TMP36Sensor.h" +#if TMP3X_SUPPORT + #include "../sensors/TMP3XSensor.h" #endif #if V9261F_SUPPORT diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index f27d2717..99e93ee0 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -484,9 +484,10 @@ void _sensorLoad() { } #endif - #if TMP36_SUPPORT + #if TMP3X_SUPPORT { - TMP36Sensor * sensor = new TMP36Sensor(); + TMP3XSensor * sensor = new TMP3XSensor(); + sensor->setType(TMP3X_TYPE); _sensors.push_back(sensor); } #endif diff --git a/code/espurna/sensors/TMP36Sensor.h b/code/espurna/sensors/TMP3XSensor.h similarity index 55% rename from code/espurna/sensors/TMP36Sensor.h rename to code/espurna/sensors/TMP3XSensor.h index ba7fe19e..bd34ec11 100644 --- a/code/espurna/sensors/TMP36Sensor.h +++ b/code/espurna/sensors/TMP3XSensor.h @@ -1,16 +1,20 @@ // ----------------------------------------------------------------------------- -// Analog Sensor (maps to an analogRead) -// Copyright (C) 2017-2018 by Xose Pérez +// TMP3X Temperature Analog Sensor +// Copyright (C) 2018 by Xose Pérez // ----------------------------------------------------------------------------- -#if SENSOR_SUPPORT && TMP36_SUPPORT +#if SENSOR_SUPPORT && TMP3X_SUPPORT #pragma once #include "Arduino.h" #include "BaseSensor.h" -class TMP36Sensor : public BaseSensor { +#define TMP3X_TMP35 35 +#define TMP3X_TMP36 36 +#define TMP3X_TMP37 37 + +class TMP3XSensor : public BaseSensor { public: @@ -18,9 +22,21 @@ class TMP36Sensor : public BaseSensor { // Public // --------------------------------------------------------------------- - TMP36Sensor(): BaseSensor() { + TMP3XSensor(): BaseSensor() { _count = 1; - _sensor_id = SENSOR_TMP36_ID; + _sensor_id = SENSOR_TMP3X_ID; + } + + void setType(unsigned char type) { + if (type == _type) return; + if (35 <= type && type <= 37) { + _type = type; + _dirty = true; + } + } + + unsigned char getType() { + return _type; } // --------------------------------------------------------------------- @@ -35,7 +51,9 @@ class TMP36Sensor : public BaseSensor { // Descriptive name of the sensor String description() { - return String("TMP36 @ TOUT"); + char buffer[14]; + snprintf(buffer, sizeof(buffer), "TMP%d @ TOUT", _type); + return String(buffer); } // Descriptive name of the slot # index @@ -56,11 +74,19 @@ class TMP36Sensor : public BaseSensor { // Current value for slot # index double value(unsigned char index) { - if (index == 0) return (double) analogRead(0) * 100 * (3.3 / 1024) - 50; + if (index == 0) { + double mV = 3300.0 * analogRead(0) / 1024.0; + if (_type == TMP3X_TMP35) return mV / 10.0; + if (_type == TMP3X_TMP36) return mV / 10.0 - 50.0; + if (_type == TMP3X_TMP37) return mV / 20.0; + } return 0; } + private: + + unsigned char _type = TMP3X_TMP35; }; -#endif // SENSOR_SUPPORT && TMP36_SUPPORT +#endif // SENSOR_SUPPORT && TMP3X_SUPPORT