soft_timer.hpp
Go to the documentation of this file.
1 /*
2  * SocialLedge.com - Copyright (C) 2013
3  *
4  * This file is part of free software framework for embedded processors.
5  * You can use it and/or distribute it as long as this copyright header
6  * remains unmodified. The code is free for personal use and requires
7  * permission to use in a commercial product.
8  *
9  * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
10  * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
12  * I SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
13  * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
14  *
15  * You can reach the author of this software at :
16  * p r e e t . w i k i @ g m a i l . c o m
17  */
18 
26 #ifndef SOFT_TIMER_H__
27 #define SOFT_TIMER_H__
28 
29 #include <stdint.h>
30 #include "lpc_sys.h"
31 
32 
33 
43 class SoftTimer
44 {
45  public:
48 
50  SoftTimer(uint32_t ms) : mTargetMs(0), mIntervalMs(0) { reset(ms); }
51 
53  inline bool expired(void) const
54  { return (0 == mIntervalMs) ? false : ( getCurrentTimeMs() >= mTargetMs); }
55 
71  inline void restart(void) { mTargetMs += mIntervalMs; };
72 
77  inline void reset(uint64_t ms) { mIntervalMs = ms; mTargetMs = getCurrentTimeMs() + ms; }
78 
80  inline void reset(void) { mTargetMs = getCurrentTimeMs() + mIntervalMs; }
81 
83  inline void stop(void) { mIntervalMs = mTargetMs = 0; }
84 
86  inline bool isRunning(void) const { return (mIntervalMs > 0); }
87 
89  inline uint64_t getTimerValueMs(void) const { return mIntervalMs; }
90 
92  inline uint64_t getTargetTimerValueMs(void) const { return mTargetMs; }
93 
98  inline uint64_t getTimeToExpirationMs(void) const
99  {
100  uint64_t now = getCurrentTimeMs();
101  const bool isExpired = (now >= mTargetMs);
102  return isExpired ? 0 : (mTargetMs - now);
103  }
104 
109  inline uint64_t getTimeSinceExpirationMs(void) const
110  {
111  uint64_t now = getCurrentTimeMs();
112  const bool isExpired = (now >= mTargetMs);
113  return isExpired ? (now - mTargetMs) : 0;
114  }
115 
127  static inline uint64_t getCurrentTimeMs(void) { return sys_get_uptime_ms(); }
128 
129  protected:
131  uint64_t mTargetMs;
132  uint64_t mIntervalMs;
133 
134 };
135 
136 
137 
138 
139 #ifdef TESTING
140 static inline void test_soft_timer_file(void)
141 {
142  SoftTimer t;
143  assert(!t.expired());
144  assert(!t.isRunning());
145 
146  t.reset(); assert(!t.expired());
147  t.reset(10); assert(!t.expired());
148  delay_ms(8); assert(!t.expired());
149  delay_ms(3); assert(t.expired());
150  t.reset(); assert(!t.expired());
151  delay_ms(8); assert(!t.expired());
152  delay_ms(3); assert(t.expired());
153  assert(t.isRunning());
154  assert(10 == t.getTimerValueMs());
155 
156  t.reset(5); assert(5 == t.getTimeToExpirationMs());
157  delay_ms(10); assert(5 == t.getTimeSinceExpirationMs());
158 }
159 #endif /* #ifdef TESTING */
160 
161 
162 
163 #endif /* SOFT_TIMER_H__ */
uint64_t mIntervalMs
Timer interval.
Definition: soft_timer.hpp:132
SoftTimer(uint32_t ms)
Constructor to set timer while instantiating this object.
Definition: soft_timer.hpp:50
uint64_t getTimeToExpirationMs(void) const
Definition: soft_timer.hpp:98
static uint64_t getCurrentTimeMs(void)
Definition: soft_timer.hpp:127
uint64_t getTimerValueMs(void) const
Definition: soft_timer.hpp:89
uint64_t getTargetTimerValueMs(void) const
Definition: soft_timer.hpp:92
void restart(void)
Definition: soft_timer.hpp:71
uint64_t getTimeSinceExpirationMs(void) const
Definition: soft_timer.hpp:109
Definition: soft_timer.hpp:43
void stop(void)
Stops the timer.
Definition: soft_timer.hpp:83
bool expired(void) const
Definition: soft_timer.hpp:53
Provides the following system services :
void delay_ms(unsigned int millisec)
Definition: utilities.c:49
uint64_t mTargetMs
Expire time with respect to OS tick.
Definition: soft_timer.hpp:131
SoftTimer()
Default constructor.
Definition: soft_timer.hpp:47
void reset(void)
Resets the timer from this point of time using the previous timeout interval.
Definition: soft_timer.hpp:80
bool isRunning(void) const
Definition: soft_timer.hpp:86
void reset(uint64_t ms)
Definition: soft_timer.hpp:77