supla-device
Loading...
Searching...
No Matches
TMP102.h
1/*
2 Copyright (C) AC SOFTWARE SP. Z O.O.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17*/
18
19#pragma once
20
21/*
22Dependency: https://github.com/sparkfun/SparkFun_TMP102_Arduino_Library
23Use library manager to install it
24*/
25
26#include <SparkFunTMP102.h>
27#include <supla/log_wrapper.h>
28#include "thermometer.h"
29
30namespace Supla {
31namespace Sensor {
32
33class TMP102 : public Thermometer {
34 public:
35 explicit TMP102(uint8_t address = 0x48,
36 TwoWire *wire = &Wire,
37 float hTemp = 70.0,
38 float lTemp = 65.0,
39 bool extMode = false,
40 bool alertPolarity = false,
41 uint8_t fault = 0,
42 bool alertMode = false) : alertMode_(alertMode) {
43 if (!tmp102_.begin(address, *wire)) {
44 SUPLA_LOG_ERROR("Unable to find TMP102 at address: 0x%x", address);
45 } else {
46 SUPLA_LOG_DEBUG("TMP102 is connected at address: 0x%x", address);
47 tmp102_.wakeup();
48 tmp102_.setHighTempC(hTemp);
49 tmp102_.setLowTempC(lTemp);
50 tmp102_.setExtendedMode(extMode);
51 tmp102_.setAlertPolarity(alertPolarity);
52 tmp102_.setFault(fault);
53 tmp102_.setAlertMode(alertMode);
54 isConnected_ = true;
55 }
56 }
57
58 void onInit() override {
59 channel.setNewValue(getTemp());
60 }
61
62 double getTemp() override {
63 if (isConnected_) {
64 double temp = tmp102_.readTempC();
65 if (temp >= -55.0 && temp <= 128.0) {
66 return round(temp * 100) / 100.0;
67 } else {
68 SUPLA_LOG_WARNING("[TMP102] invalid temperature reading: %f", temp);
69 return TEMPERATURE_NOT_AVAILABLE;
70 }
71 } else {
72 return TEMPERATURE_NOT_AVAILABLE;
73 }
74 }
75
76 bool getAlertState() {
77 bool raw = tmp102_.alert();
78 return alertMode_ ? raw : !raw;
79 }
80
81 protected:
82 ::TMP102 tmp102_;
83 bool alertMode_ - false;
84 bool isConnected_ = false;
85};
86
87}; // namespace Sensor
88}; // namespace Supla
void onInit() override
Third method called on element in SuplaDevice.begin()
Definition TMP102.h:58