Timer is now functional.

This commit is contained in:
2016-05-20 22:44:03 +02:00
parent c0f600653f
commit 4b6c8187a9
4 changed files with 66 additions and 14 deletions

View File

@@ -25,7 +25,6 @@
/*------------------------------- INCLUDES ----------------------------------*/ /*------------------------------- INCLUDES ----------------------------------*/
#include <cstdio> #include <cstdio>
#include <ctime>
#include "timers/Clock.h" #include "timers/Clock.h"
@@ -53,22 +52,34 @@ Clock::~Clock (void)
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn isEqual * @fn isEqual
* *
* @brief Constructor of the Clock Object. * @brief Set an Event to the Clock.
*/ */
bool Clock::isCurrentTimeEqualTo (const Event &anEvent) int Clock::set (const Event &anEvent)
{
mEvent = anEvent;
mNow = time (NULL);
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn isEqualToCurrentTime
*
* @brief return true if the clock is equal to current time.
*/
bool Clock::isEqualToCurrentTime (void)
{ {
time_t theNow;
struct tm theComparedTime; struct tm theComparedTime;
double theDiff; double theDiff;
theNow = time (NULL); theComparedTime = *localtime (&mNow);
theComparedTime = *localtime (&theNow); theComparedTime.tm_hour = mEvent.getHour();
theComparedTime.tm_min = mEvent.getMinute();
theComparedTime.tm_hour = anEvent.getHour(); theDiff = difftime (mNow, mktime (&theComparedTime));
theComparedTime.tm_min = anEvent.getMinute();
theDiff = difftime (theNow, mktime (&theComparedTime));
//printf ("%.f seconds of diff between current time and Timer time.\n", theDiff); //printf ("%.f seconds of diff between current time and Timer time.\n", theDiff);
@@ -77,3 +88,32 @@ bool Clock::isCurrentTimeEqualTo (const Event &anEvent)
return false; return false;
} }
/*! ----------------------------------------------------------------------------
* @fn isExpired
*
* @brief Return true if the curent time has expired.
*/
bool Clock::isExpired (void)
{
time_t theExpired;
struct tm theComparedTime;
double theDiff;
theComparedTime = *localtime (&mNow);
theComparedTime.tm_hour = mEvent.getHour();
theComparedTime.tm_min = mEvent.getMinute();
theExpired = mktime (&theComparedTime) + (mEvent.getDuration() * 60);
theDiff = difftime (mNow, theExpired);
//printf ("%.f seconds of diff between %lld and %lld.\n", theDiff, mNow, theExpired);
if (theDiff == 0)
return true;
return false;
}

View File

@@ -28,6 +28,8 @@
/*------------------------------- INCLUDES ----------------------------------*/ /*------------------------------- INCLUDES ----------------------------------*/
#include <ctime>
#include "timers/Event.h" #include "timers/Event.h"
/*---------------------------------- Deps -----------------------------------*/ /*---------------------------------- Deps -----------------------------------*/
@@ -39,7 +41,14 @@ class Clock {
public: public:
Clock (void); Clock (void);
~Clock (void); ~Clock (void);
bool isCurrentTimeEqualTo (const Event &anEvent);
int set (const Event &anEvent);
bool isEqualToCurrentTime (void);
bool isExpired (void);
private:
Event mEvent;
time_t mNow;
}; };
#endif /* _CLOCK_H */ #endif /* _CLOCK_H */

View File

@@ -234,7 +234,7 @@ bool Event::isInProgress (void) const
* *
* @brief Set the InProgress to a new state. * @brief Set the InProgress to a new state.
*/ */
void setInProgress (bool aState) void Event::setInProgress (bool aState)
{ {
mInProgress = aState; mInProgress = aState;
} }

View File

@@ -142,16 +142,19 @@ int Timers::Expire (void)
theClock.set ((*timer_evt)); theClock.set ((*timer_evt));
if ((*timer_evt).isInProgress()) { if ((*timer_evt).isInProgress()) {
if (theClock.IsExpired()) { if (theClock.isExpired()) {
// //
(*timer_evt).setInProgress (false); (*timer_evt).setInProgress (false);
//TODO Action Stop. //TODO Action Stop.
fprintf (stdout, "Timer Stop\n"); fprintf (stdout, "Timer Stop\n");
} }
else {
fprintf (stdout, "Not expired yet.\n");
}
} }
else { else {
if (theClock.isEqualToCurrentTime () { if (theClock.isEqualToCurrentTime ()) {
(*timer_evt).setInProgress (true); (*timer_evt).setInProgress (true);
// Action START. // Action START.