esp8266_web_settings beta
AbstractAnalog.h
1/*
2 * Copyright (c) 2021, 2022 G. R. McDorman
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23#pragma once
24
25#include <Arduino.h>
26#include <Ticker.h>
27
28#include "grmcdorman/device/Device.h"
29#include "grmcdorman/device/Accumulator.h"
30
31namespace grmcdorman::device
32{
45 class AbstractAnalog: public Device
46 {
47 public:
57 explicit AbstractAnalog(const __FlashStringHelper *device_name, const __FlashStringHelper *device_identifier,
58 float defaultScale = 1.0f, float defaultOffset = 0.0f, bool invert = false);
59
60 void setup() override;
61 void loop() override;
62 bool publish(DynamicJsonDocument &json) const override;
63
69 float get_last_reading() const
70 {
71 return sensor_reading.get_last_reading();
72 }
73
79 float raw_value() const
80 {
81 return last_raw_value;
82 }
83
92 float get_current_average() const
93 {
94 return sensor_reading.get_current_average();
95 }
96
97 DynamicJsonDocument as_json() const override;
98 protected:
110 virtual float transform_raw_reading(int reading) = 0;
111 FloatSetting scale;
112 FloatSetting offset;
113 ToggleSetting invertReading;
114 UnsignedIntegerSetting readInterval;
115 uint32_t last_read_millis = 0;
116 constexpr static uint32_t statusReadInterval = (30 / 5) * 1000;
118 private:
119 void set_timer();
120 Ticker ticker;
121 uint32_t current_polling_seconds = 0;
122 float last_raw_value = 0;
123 };
124}
Abstract analog device.
Definition: AbstractAnalog.h:46
void setup() override
Setup the device.
Definition: AbstractAnalog.cpp:44
bool publish(DynamicJsonDocument &json) const override
Publish the value and attributes.
Definition: AbstractAnalog.cpp:67
FloatSetting offset
Scaling.
Definition: AbstractAnalog.h:112
AbstractAnalog(const __FlashStringHelper *device_name, const __FlashStringHelper *device_identifier, float defaultScale=1.0f, float defaultOffset=0.0f, bool invert=false)
Construct a new Basic Analog object.
Definition: AbstractAnalog.cpp:29
void loop() override
Main loop.
Definition: AbstractAnalog.cpp:54
uint32_t last_read_millis
Timestamp of last read.
Definition: AbstractAnalog.h:115
ToggleSetting invertReading
Whether to invert the reading.
Definition: AbstractAnalog.h:113
float get_last_reading() const
Last computed reading.
Definition: AbstractAnalog.h:69
float get_current_average() const
Get the last average reading.
Definition: AbstractAnalog.h:92
constexpr static uint32_t statusReadInterval
Default read interval. Chosen such that there should be 5 readings per 30 seconds.
Definition: AbstractAnalog.h:116
Accumulator< float, 5 > sensor_reading
Reading.
Definition: AbstractAnalog.h:117
virtual float transform_raw_reading(int reading)=0
Transform the raw reading into the reported value.
FloatSetting scale
Offset.
Definition: AbstractAnalog.h:111
UnsignedIntegerSetting readInterval
How often to request a reading.
Definition: AbstractAnalog.h:114
float raw_value() const
Last raw value (no scale or offset applied).
Definition: AbstractAnalog.h:79
DynamicJsonDocument as_json() const override
Get the values, as a JSON document.
Definition: AbstractAnalog.cpp:79
A class to handle accumulating values.
Definition: Accumulator.h:54
The generic device interface.
Definition: Device.h:46