TeensyNanoExoCode
Loading...
Searching...
No Matches
Time_Helper.h
Go to the documentation of this file.
1#ifndef TIME_HELPER_H
2#define TIME_HELPER_H
3
4#define MAX_TICKERS 15
5
6#include <vector>
7
8/* Class to help track the time of code execution. The class uses a singleton design pattern.
9 * Example usage:
10 * Time_Helper* my_time_helper_singleton = get_instance();
11 * static const float my_context = my_time_helper_singleton->generate_new_context();
12 * static my_delta_time;
13 * my_delta_time = my_time_helper_singleton->tick(my_context);
14 *
15 * The above code gets a reference to the singleton and uses it to generate a persisent context (see below).
16 * 'my_delta_time' will be filled with the time between the calls to tick in millis (first call will return 0).
17 *
18 * When you are done with a context, clean it up using 'destroy_context'. If you would like to use microseconds,
19 * the default value of 'use_micros' in the constructor should be true.
20 *
21 * If 'tick()' is continuosly returning 0, you are passing an invalid context.
22 *
23 *
24 */
25
26typedef struct {
27 float context;
28 float old_time = -1;
30} ticker_t;
31
33{
34 public:
35 Time_Helper(bool use_micros=true);
36 static Time_Helper* get_instance();
37
38 float peek(float context);
39 float tick(float context);
40
42 void destroy_context(float context);
43 private:
44 bool _context_conflicts(float context);
45 ticker_t* _ticker_from_context(float context);
46
47 int ticker_count = 0;
48 std::vector<ticker_t> tickers;
49
50 bool _k_use_micros;
51};
52
53#endif
Definition Time_Helper.h:33
static Time_Helper * get_instance()
Definition Time_Helper.cpp:12
void destroy_context(float context)
Definition Time_Helper.cpp:80
float generate_new_context()
Definition Time_Helper.cpp:54
float peek(float context)
Definition Time_Helper.cpp:18
float tick(float context)
Definition Time_Helper.cpp:31
Definition Time_Helper.h:26
float context
Definition Time_Helper.h:27
int k_index
Definition Time_Helper.h:29