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
Lightning Class Reference

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 &timestamp, int &events, uint8_t &distance)
 

Detailed Description

Calculation number of lightning events during last sensor update cycle and during last hour (past 60 minutes); storing timestamp and distance of last event.

Constructor & Destructor Documentation

◆ Lightning()

Lightning::Lightning ( const float  quality_threshold = DEFAULT_QUALITY_THRESHOLD)
inline

Constructor

Parameters
quality_thresholdfraction of valid hist entries required for valid pastHour() result

Member Function Documentation

◆ hist_init()

void Lightning::hist_init ( int16_t  count = -1)

Initialize histogram of hourly (past 60 minutes) events

Parameters
countnumber of events

◆ pastHour()

Lightning::pastHour ( bool *  valid = nullptr,
int *  nbins = nullptr,
float *  quality = nullptr 
)

Get number of lightning events during past 60 minutes.

Parameters
validnumber of valid entries in hist >= qualityThreshold * 60 / updateRate
nbinsnumber of valid entries in hist
qualityfraction of valid entries in hist (0..1); quality = nbins / (60 / updateRate)
Returns
number of events during past 60 minutes

◆ reset()

void Lightning::reset ( void  )

Initialize/reset non-volatile data

◆ setUpdateRate()

void Lightning::setUpdateRate ( uint8_t  rate = LIGHTNING_UPD_RATE)
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:

  1. updateRate = 6 -> 60 / 6 = 10 entries
  2. updateRate = 12 -> 60 / 12 = 5 entries

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.

Parameters
rateupdate rate in minutes (default: 6)

◆ update()

Lightning::update ( time_t  timestamp,
int16_t  count,
uint8_t  distance,
bool  startup = false 
)

Update lightning data.

Parameters
timestamptimestamp (epoch)
countaccumulated number of events
startupsensor startup flag
lightningCountMaxoverflow 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
*
* 

The documentation for this class was generated from the following files: