Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding isEnabled as public method #40

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 22 additions & 27 deletions TimeAlarms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ void TimeAlarmsClass::disable(AlarmID_t ID)
}
}

bool TimeAlarmsClass::isEnabled(AlarmID_t ID)
{
if (isAllocated(ID)) {
return Alarm[ID].Mode.isEnabled;
}
}

// write the given value to the given alarm
void TimeAlarmsClass::write(AlarmID_t ID, time_t value)
{
Expand All @@ -122,7 +129,7 @@ void TimeAlarmsClass::write(AlarmID_t ID, time_t value)
}

// return the value for the given alarm ID
time_t TimeAlarmsClass::read(AlarmID_t ID) const
time_t TimeAlarmsClass::read(AlarmID_t ID)
{
if (isAllocated(ID)) {
return Alarm[ID].value ;
Expand All @@ -132,7 +139,7 @@ time_t TimeAlarmsClass::read(AlarmID_t ID) const
}

// return the alarm type for the given alarm ID
dtAlarmPeriod_t TimeAlarmsClass::readType(AlarmID_t ID) const
dtAlarmPeriod_t TimeAlarmsClass::readType(AlarmID_t ID)
{
if (isAllocated(ID)) {
return (dtAlarmPeriod_t)Alarm[ID].Mode.alarmType ;
Expand All @@ -153,7 +160,7 @@ void TimeAlarmsClass::free(AlarmID_t ID)
}

// returns the number of allocated timers
uint8_t TimeAlarmsClass::count() const
uint8_t TimeAlarmsClass::count()
{
uint8_t c = 0;
for(uint8_t id = 0; id < dtNBR_ALARMS; id++) {
Expand All @@ -163,20 +170,20 @@ uint8_t TimeAlarmsClass::count() const
}

// 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) const
bool TimeAlarmsClass::isAlarm(AlarmID_t ID)
{
return( isAllocated(ID) && dtIsAlarm(Alarm[ID].Mode.alarmType) );
}

// returns true if this id is allocated
bool TimeAlarmsClass::isAllocated(AlarmID_t ID) const
bool TimeAlarmsClass::isAllocated(AlarmID_t ID)
{
return (ID < dtNBR_ALARMS && Alarm[ID].Mode.alarmType != dtNotAllocated);
}

// returns the currently triggered alarm id
// returns dtINVALID_ALARM_ID if not invoked from within an alarm handler
AlarmID_t TimeAlarmsClass::getTriggeredAlarmId() const
AlarmID_t TimeAlarmsClass::getTriggeredAlarmId()
{
if (isServicing) {
return servicedAlarmId; // new private data member used instead of local loop variable i in serviceAlarms();
Expand All @@ -189,10 +196,9 @@ AlarmID_t TimeAlarmsClass::getTriggeredAlarmId() const
void TimeAlarmsClass::delay(unsigned long ms)
{
unsigned long start = millis();
do {
while (millis() - start <= ms) {
serviceAlarms();
yield();
} while (millis() - start <= ms);
}
}

void TimeAlarmsClass::waitForDigits( uint8_t Digits, dtUnits_t Units)
Expand All @@ -211,7 +217,7 @@ void TimeAlarmsClass::waitForRollover( dtUnits_t Units)
waitForDigits(0, Units);
}

uint8_t TimeAlarmsClass::getDigitsNow( dtUnits_t Units) const
uint8_t TimeAlarmsClass::getDigitsNow( dtUnits_t Units)
{
time_t time = now();
if (Units == dtSecond) return numberOfSeconds(time);
Expand All @@ -222,7 +228,7 @@ uint8_t TimeAlarmsClass::getDigitsNow( dtUnits_t Units) const
}

//returns isServicing
bool TimeAlarmsClass::getIsServicing() const
bool TimeAlarmsClass::getIsServicing()
{
return isServicing;
}
Expand Down Expand Up @@ -252,30 +258,18 @@ void TimeAlarmsClass::serviceAlarms()
}

// returns the absolute time of the next scheduled alarm, or 0 if none
time_t TimeAlarmsClass::getNextTrigger() const
time_t TimeAlarmsClass::getNextTrigger()
{
time_t nextTrigger = 0;
time_t nextTrigger = (time_t)0xffffffff; // the max time value

for (uint8_t id = 0; id < dtNBR_ALARMS; id++) {
if (isAllocated(id)) {
if (nextTrigger == 0) {
nextTrigger = Alarm[id].nextTrigger;
}
else if (Alarm[id].nextTrigger < nextTrigger) {
if (Alarm[id].nextTrigger < nextTrigger) {
nextTrigger = Alarm[id].nextTrigger;
}
}
}
return nextTrigger;
}

time_t TimeAlarmsClass::getNextTrigger(AlarmID_t ID) const
{
if (isAllocated(ID)) {
return Alarm[ID].nextTrigger;
} else {
return 0;
}
return nextTrigger == (time_t)0xffffffff ? 0 : nextTrigger;
}

// attempt to create an alarm and return true if successful
Expand All @@ -300,3 +294,4 @@ AlarmID_t TimeAlarmsClass::create(time_t value, OnTick_t onTickHandler, uint8_t

// make one instance for the user to use
TimeAlarmsClass Alarm = TimeAlarmsClass() ;

24 changes: 10 additions & 14 deletions TimeAlarms.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@
#include <Arduino.h>
#include "TimeLib.h"

#if !defined(dtNBR_ALARMS )
#if defined(__AVR__)
#define dtNBR_ALARMS 6 // max is 255
#elif defined(ESP8266)
#define dtNBR_ALARMS 20 // for esp8266 chip - max is 255
#else
#define dtNBR_ALARMS 12 // assume non-AVR has more memory
#endif
#endif

#define USE_SPECIALIST_METHODS // define this for testing

Expand Down Expand Up @@ -143,29 +139,29 @@ class TimeAlarmsClass
void delay(unsigned long ms);

// utility methods
uint8_t getDigitsNow( dtUnits_t Units) const; // returns the current digit value for the given time unit
uint8_t getDigitsNow( dtUnits_t Units); // returns the current digit value for the given time unit
void waitForDigits( uint8_t Digits, dtUnits_t Units);
void waitForRollover(dtUnits_t Units);

// low level methods
void enable(AlarmID_t ID); // enable the alarm to trigger
void disable(AlarmID_t ID); // prevent the alarm from triggering
AlarmID_t getTriggeredAlarmId() const; // returns the currently triggered alarm id
bool getIsServicing() const; // returns isServicing
bool isEnabled(AlarmID_t ID); // returns true if the given alarm is enabled
AlarmID_t getTriggeredAlarmId(); // returns the currently triggered alarm id
bool getIsServicing(); // returns isServicing
void write(AlarmID_t ID, time_t value); // write the value (and enable) the alarm with the given ID
time_t read(AlarmID_t ID) const; // return the value for the given timer
dtAlarmPeriod_t readType(AlarmID_t ID) const; // return the alarm type for the given alarm ID
time_t read(AlarmID_t ID); // return the value for the given timer
dtAlarmPeriod_t readType(AlarmID_t ID); // return the alarm type for the given alarm ID

void free(AlarmID_t ID); // free the id to allow its reuse

#ifndef USE_SPECIALIST_METHODS
private: // the following methods are for testing and are not documented as part of the standard library
#endif
uint8_t count() const; // returns the number of allocated timers
time_t getNextTrigger() const; // returns the time of the next scheduled alarm
time_t getNextTrigger(AlarmID_t ID) const; // returns the time of scheduled alarm
bool isAllocated(AlarmID_t ID) const; // returns true if this id is allocated
bool isAlarm(AlarmID_t ID) const; // returns true if id is for a time based alarm, false if its a timer or not allocated
uint8_t count(); // returns the number of allocated timers
time_t getNextTrigger(); // returns the time of the next scheduled alarm
bool isAllocated(AlarmID_t ID); // returns true if this id is allocated
bool isAlarm(AlarmID_t ID); // returns true if id is for a time based alarm, false if its a timer or not allocated
};

extern TimeAlarmsClass Alarm; // make an instance for the user
Expand Down