BresserWeatherSensorReceiver
Bresser 5-in-1/6-in-1/7-in-1 868 MHz Weather Sensor Radio Receiver for Arduino based on CC1101 or SX1276/RFM95W
Loading...
Searching...
No Matches
RollingCounter.h
1
2// RollingCounter.h
3//
4// Base class for rolling counter implementations (RainGauge, Lightning, etc.)
5// Provides common functionality for history buffer management and calculations
6//
7// https://github.com/matthias-bs/BresserWeatherSensorReceiver
8//
9//
10// created: 02/2026
11//
12//
13// MIT License
14//
15// Copyright (c) 2026 Matthias Prinke
16//
17// Permission is hereby granted, free of charge, to any person obtaining a copy
18// of this software and associated documentation files (the "Software"), to deal
19// in the Software without restriction, including without limitation the rights
20// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21// copies of the Software, and to permit persons to whom the Software is
22// furnished to do so, subject to the following conditions:
23//
24// The above copyright notice and this permission notice shall be included in all
25// copies or substantial portions of the Software.
26//
27// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33// SOFTWARE.
34//
35// History:
36//
37// 20260211 Created from common code in RainGauge and Lightning
38// 20260221 Improved generalization, documentation, and code deduplication
39//
40// ToDo:
41// -
42//
44#ifndef _ROLLINGCOUNTER_H
45#define _ROLLINGCOUNTER_H
46
47#if defined(ESP32) || defined(ESP8266)
48#include <sys/time.h>
49#else
50#include <time.h>
51#include <stdint.h>
52#include <stddef.h>
53#endif
54
60#define ROLLING_COUNTER_UPD_RATE 6
61
67#define DEFAULT_QUALITY_THRESHOLD 0.8
68
78{
79protected:
80 float qualityThreshold;
81 time_t lastUpdate;
82 uint8_t updateRate;
83
89 typedef void (*HistInitCallback)();
90
96 enum UpdateResult
97 {
98 UPDATE_SUCCESS,
99 UPDATE_EXPIRED
100 };
101
107 typedef struct
108 {
109 int16_t *hist; // pointer to buffer
110 size_t size; // number of bins
111 uint8_t updateRate; // minutes per bin
112 } History;
113
122 int calculateIndex(const struct tm &tm, uint8_t rate) const;
123
133 void markMissedEntries(int16_t *hist, size_t size, time_t lastUpdate,
134 time_t timestamp, uint8_t rate);
135
155 UpdateResult updateHistoryBufferCore(int16_t *hist, size_t size, int idx, int16_t delta,
156 time_t t_delta, time_t timestamp, time_t lastUpdate,
157 uint8_t updateRate);
158
176 void updateHistoryBuffer(int16_t *hist, size_t size, int idx, int16_t delta,
177 time_t t_delta, time_t timestamp, time_t lastUpdate,
178 uint8_t updateRate);
179
187 virtual void hist_init(int16_t value = -1) = 0;
188
200 float sumHistory(const History &h, bool *valid = nullptr, int *nbins = nullptr,
201 float *quality = nullptr, float scale = 1.0);
202
203public:
209 RollingCounter(const float quality_threshold = DEFAULT_QUALITY_THRESHOLD) : qualityThreshold(quality_threshold),
210 lastUpdate(0),
211 updateRate(ROLLING_COUNTER_UPD_RATE) {};
212
216 virtual ~RollingCounter() = default;
217
223 time_t getLastUpdate() const { return lastUpdate; }
224
230 uint8_t getUpdateRate() const { return updateRate; }
231};
232
233#endif // _ROLLINGCOUNTER_H
Base class for rolling counter implementations.
Definition RollingCounter.h:78
time_t getLastUpdate() const
Definition RollingCounter.h:223
uint8_t getUpdateRate() const
Definition RollingCounter.h:230
RollingCounter(const float quality_threshold=DEFAULT_QUALITY_THRESHOLD)
Definition RollingCounter.h:209
virtual ~RollingCounter()=default