supla-device
Loading...
Searching...
No Matches
max44009.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// Dependencies:
20// https://github.com/RobTillaart/Max44009
21
22// Defualt I2C address: 0x4A
23// Alternative I2C address: 0x4B
24
25#ifndef SRC_SUPLA_SENSOR_MAX44009_H_
26#define SRC_SUPLA_SENSOR_MAX44009_H_
27
28#include <supla/sensor/general_purpose_measurement.h>
29
30#include <math.h>
31#include <Wire.h>
32#include <Max44009.h>
33#include <supla/log_wrapper.h>
34
35namespace Supla {
36namespace Sensor {
37class Max44009 : public GeneralPurposeMeasurement {
38 public:
39 explicit Max44009(int i2cAddress = 0x4A, TwoWire *wire = &Wire)
40 : GeneralPurposeMeasurement(nullptr, false), sensor(i2cAddress, wire) {
42 getChannel()->setDefaultIcon(14);
43 }
44
45 void onInit() override {
46 channel.setNewValue(getValue());
47 }
48
49 protected:
50 double getValue() override {
51 double value = sensor.getLux();
52 if (isnan(value) || value <= 0) {
53 if (invalidCounter < 3) {
54 invalidCounter++;
55 return lastValue;
56 }
57 lastValue = NAN;
58 return lastValue;
59 }
60 invalidCounter = 0;
61 lastValue = value;
62 return value;
63 }
64
65 ::Max44009 sensor;
66
67 double lastValue = NAN;
68 int invalidCounter = 0;
69};
70
71} // namespace Sensor
72} // namespace Supla
73
74#endif // SRC_SUPLA_SENSOR_MAX44009_H_
void setDefaultUnitAfterValue(const char *unit)
Sets default unit which is displayed after value.
Definition general_purpose_channel_base.cpp:208
Definition max44009.h:37
void onInit() override
Supla::Element::onInit() - called by SuplaDeviceClass::begin() during initialization.
Definition max44009.h:45
double getValue() override
Method used to obtain new value for channel.
Definition max44009.h:50