@ -12,6 +12,8 @@ Copyright (C) 2018 by Xose Pérez <xose dot perez at gmail dot com>
unsigned long _loop_delay = 0 ;
bool _system_send_heartbeat = false ;
unsigned char _heartbeat_mode = HEARTBEAT_MODE ;
unsigned long _heartbeat_interval = HEARTBEAT_INTERVAL ;
// Calculated load average 0 to 100;
unsigned short int _load_average = 100 ;
@ -66,6 +68,10 @@ void systemSendHeartbeat() {
_system_send_heartbeat = true ;
}
bool systemGetHeartbeat ( ) {
return _system_send_heartbeat ;
}
unsigned long systemLoopDelay ( ) {
return _loop_delay ;
}
@ -75,6 +81,11 @@ unsigned long systemLoadAverage() {
return _load_average ;
}
void _systemSetupHeartbeat ( ) {
_heartbeat_mode = getSetting ( " hbMode " , HEARTBEAT_MODE ) . toInt ( ) ;
_heartbeat_interval = getSetting ( " hbInterval " , HEARTBEAT_INTERVAL ) . toInt ( ) ;
}
void systemLoop ( ) {
// -------------------------------------------------------------------------
@ -97,19 +108,21 @@ void systemLoop() {
// Heartbeat
// -------------------------------------------------------------------------
# if HEARTBEAT_MODE == HEARTBEAT_ONCE
if ( _system_send_heartbeat ) {
_system_send_heartbeat = false ;
heartbeat ( ) ;
}
# elif HEARTBEAT_MODE == HEARTBEAT_REPEAT
if ( _system_send_heartbeat & & _heartbeat_mode = = HEARTBEAT_ONCE ) {
heartbeat ( ) ;
_system_send_heartbeat = false ;
} else if ( _heartbeat_mode = = HEARTBEAT_REPEAT | | _heartbeat_mode = = HEARTBEAT_REPEAT_STATUS ) {
static unsigned long last_hbeat = 0 ;
if ( _system_send_heartbeat | | ( last_hbeat = = 0 ) | | ( millis ( ) - last_hbeat > HEARTBEAT_INTERVAL ) ) {
_system_send_heartbeat = false ;
# if NTP_SUPPORT
if ( ( _system_send_heartbeat & & ntpSynced ( ) ) | | ( millis ( ) - last_hbeat > _heartbeat_interval * 1000 ) ) {
# else
if ( _system_send_heartbeat | | ( millis ( ) - last_hbeat > _heartbeat_interval * 1000 ) ) {
# endif
last_hbeat = millis ( ) ;
heartbeat ( ) ;
_system_send_heartbeat = false ;
}
# endif // HEARTBEAT_MODE == HEARTBEAT_REPEAT
}
// -------------------------------------------------------------------------
// Load Average calculation
@ -179,4 +192,7 @@ void systemSetup() {
// Register Loop
espurnaRegisterLoop ( systemLoop ) ;
// Cache Heartbeat values
_systemSetupHeartbeat ( ) ;
}