void AlarmClass::updateNextTrigger() { if( (value != 0) && Mode.isEnabled ) { time_t time = now(); if( dtIsAlarm(Mode.alarmType) && nextTrigger <= time ) // update alarm if next trigger is not yet in the future { if(Mode.alarmType == dtExplicitAlarm ) // is the value a specific date and time in the future { nextTrigger = value; // yes, trigger on this value } else if(Mode.alarmType == dtDailyAlarm) //if this is a daily alarm { if( value + previousMidnight(now()) <= time) { nextTrigger = value + nextMidnight(time); // if time has passed then set for tomorrow } else { nextTrigger = value + previousMidnight(time); // set the date to today and add the time given in value } } else if(Mode.alarmType == dtWeeklyAlarm) // if this is a weekly alarm { if( (value + previousSunday(now())) <= time) { nextTrigger = value + nextSunday(time); // if day has passed then set for the next week. } else { nextTrigger = value + previousSunday(time); // set the date to this week today and add the time given in value } } else // its not a recognized alarm type - this should not happen { Mode.isEnabled = 0; // Disable the alarm } } if( Mode.alarmType == dtTimer) { // its a timer nextTrigger = time + value; // add the value to previous time (this ensures delay always at least Value seconds) } } else { Mode.isEnabled = 0; // Disable if the value is 0 } }
// attempt to create an alarm and return true if successful AlarmID_t TimeAlarmsClass::create( time_t value, OnTick_t onTickHandler, uint8_t isOneShot, dtAlarmPeriod_t alarmType, uint8_t isEnabled) { if( ! (dtIsAlarm(alarmType) && now() < SECS_PER_YEAR)) // only create alarm ids if the time is at least Jan 1 1971 { for(uint8_t id = 0; id < dtNBR_ALARMS; id++) { if( Alarm[id].Mode.alarmType == dtNotAllocated ) { // here if there is an Alarm id that is not allocated Alarm[id].onTickHandler = onTickHandler; Alarm[id].Mode.isOneShot = isOneShot; Alarm[id].Mode.alarmType = alarmType; Alarm[id].value = value; isEnabled ? enable(id) : disable(id); return id; // alarm created ok } } } return dtINVALID_ALARM_ID; // no IDs available or time is invalid }
// returns true only if id is allocated and the type is a time based alarm, returns false if not allocated or if its a timer bool TimeAlarmsClass::isAlarm(AlarmID_t ID) { return( isAllocated(ID) && dtIsAlarm(Alarm[ID].Mode.alarmType) ); }
return create( value, onTickHandler, IS_ONESHOT, dtTimer );