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
|
Calculation number of lightning events during last sensor update cycle and during last hour (past 60 minutes); storing timestamp and distance of last event. More...
#include <Lightning.h>
Public Member Functions | |
Lightning (const float quality_threshold=DEFAULT_QUALITY_THRESHOLD) | |
void | setUpdateRate (uint8_t rate=LIGHTNING_UPD_RATE) |
Set expected update rate for pastHour() calculation. | |
void | reset (void) |
void | hist_init (int16_t count=-1) |
void | prefs_load (void) |
void | prefs_save (void) |
void | update (time_t timestamp, int16_t count, uint8_t distance, bool startup=false) |
Update lightning data. | |
int | pastHour (bool *valid=nullptr, int *nbins=nullptr, float *quality=nullptr) |
Get number of lightning events during past 60 minutes. | |
int | lastCycle (void) |
bool | lastEvent (time_t ×tamp, int &events, uint8_t &distance) |
Calculation number of lightning events during last sensor update cycle and during last hour (past 60 minutes); storing timestamp and distance of last event.
|
inline |
Constructor
quality_threshold | fraction of valid hist entries required for valid pastHour() result |
void Lightning::hist_init | ( | int16_t | count = -1 | ) |
Initialize histogram of hourly (past 60 minutes) events
count | number of events |
Lightning::pastHour | ( | bool * | valid = nullptr , |
int * | nbins = nullptr , |
||
float * | quality = nullptr |
||
) |
Get number of lightning events during past 60 minutes.
valid | number of valid entries in hist >= qualityThreshold * 60 / updateRate |
nbins | number of valid entries in hist |
quality | fraction of valid entries in hist (0..1); quality = nbins / (60 / updateRate) |
void Lightning::reset | ( | void | ) |
Initialize/reset non-volatile data
|
inline |
Set expected update rate for pastHour() calculation.
LIGHTNING_HIST_SIZE: number of entries in hist[] updateRate: update rate in minutes
60 minutes / updateRate = no_of_hist_bins The resulting number of history bins must be an integer value which does not exceed LIGHTNING_HIST_SIZE.
Examples:
Changing the update rate will reset the history buffer, therefore the caller should avoid frequent changes.
Actual update intervals shorter than updateRate will lead to a reduced resolution of the pastHour() result and a higher risk of an invalid result if a bin in the history buffer was missed.
Actual update intervals longer than updateRate will lead to an invalid result, because bins in the history buffer will be missed.
rate | update rate in minutes (default: 6) |
Lightning::update | ( | time_t | timestamp, |
int16_t | count, | ||
uint8_t | distance, | ||
bool | startup = false |
||
) |
Update lightning data.
timestamp | timestamp (epoch) |
count | accumulated number of events |
startup | sensor startup flag |
lightningCountMax | overflow value; when reached, the sensor's counter is reset to zero |
* Total number of events during past 60 minutes * ---------------------------------------------- * * In each update(): * - timestamp (time_t) -> t (localtime, struct tm) * - calculate index into hist[]: idx = t.tm_min / updateRate * - expired time since last update: t_delta = timestamp - nvLightning.lastUpdate * - number of events since last update: delta = currCount - nvLightning.prevCount * - t_delta * < 0: something is wrong, e.g. RTC was not set correctly -> ignore, return * t_delta < expected update rate: * idx same as in previous cycle: hist[idx] += delta * idx changed by 1: hist[idx] = delta * t_delta >= history size: mark all history entries as invalid * else (index changed > 1): mark all history entries in interval [expected_index, current_index) as invalid * hist[idx] = delta * * --------------- ----------- * | | | | |...| | | | hist[LIGHTNING_HIST_SIZE] * --------------- ----------- * ^ * | * idx = t.tm_min / updateRate * * - Calculate hourly rate: * pastHour = sum of all valid hist[] entries * *