stop_watch.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 STOP_WATCH_H_
27 #define STOP_WATCH_H_
28 
29 #include <stdint.h>
30 #include "lpc_sys.h"
31 
32 
33 
38 {
39  public:
41  MicroSecondStopWatch() : mStartValue(0), mStopValue(0) { start(); }
42 
44  inline void start(void) { mStartValue = mStopValue = getTimerValue(); }
45 
47  inline void stop(void) { mStopValue = getTimerValue(); }
48 
53  inline uint64_t getCapturedTime(void) const { return (mStopValue - mStartValue); }
54 
56  inline uint64_t getElapsedTime (void) const { return (getTimerValue() - mStartValue); }
57 
58  private:
60  inline uint64_t getTimerValue(void) const { return sys_get_uptime_us(); }
61 
62  uint64_t mStartValue;
63  uint64_t mStopValue;
64 };
65 
66 
67 
68 #ifdef TESTING
69 static inline void test_stop_watch_file(void)
70 {
72  assert(0 == w.getCapturedTime());
73  assert(0 == w.getElapsedTime());
74  w.start();
75  delay_ms(5);
76  assert(0 == w.getCapturedTime());
77  w.stop();
78  assert(w.getCapturedTime() >= 5000 && w.getCapturedTime() <= 6000);
79 }
80 #endif /* #ifdef TESTING */
81 
82 
83 
84 #endif /* STOP_WATCH_H_ */
MicroSecondStopWatch()
Default constructor that starts the stopwatch.
Definition: stop_watch.hpp:41
uint64_t sys_get_uptime_us(void)
Definition: lpc_sys.cpp:103
void start(void)
Starts the stopwatch operation (can be used to restart the stopwatch too)
Definition: stop_watch.hpp:44
Provides the following system services :
void delay_ms(unsigned int millisec)
Definition: utilities.c:49
uint64_t getElapsedTime(void) const
Get the elapsed time since the stopwatch was started.
Definition: stop_watch.hpp:56
uint64_t getCapturedTime(void) const
Definition: stop_watch.hpp:53
Definition: stop_watch.hpp:37
void stop(void)
Stops the stopwatch operation enabling the captured value to be obtained.
Definition: stop_watch.hpp:47