Mark Gottscho / HardwareTimersLib

Fork of HardwareTimersLib by Mark Gottscho

Committer:
mgottscho
Date:
Sat Mar 08 20:31:49 2014 +0000
Revision:
0:47acc8320421
Fixes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mgottscho 0:47acc8320421 1 /* PreciseTime.h
mgottscho 0:47acc8320421 2 * Tested with mbed board: FRDM-KL46Z
mgottscho 0:47acc8320421 3 * Author: Mark Gottscho
mgottscho 0:47acc8320421 4 * mgottscho@ucla.edu
mgottscho 0:47acc8320421 5 */
mgottscho 0:47acc8320421 6
mgottscho 0:47acc8320421 7 #ifndef PRECISETIME_H
mgottscho 0:47acc8320421 8 #define PRECISETIME_H
mgottscho 0:47acc8320421 9
mgottscho 0:47acc8320421 10 #include "mbed.h"
mgottscho 0:47acc8320421 11
mgottscho 0:47acc8320421 12 /**
mgottscho 0:47acc8320421 13 * This class provides a simple abstraction for time-keeping in a wall-clock sense.
mgottscho 0:47acc8320421 14 * It can store time by hour:min:sec:ms:us:ns, and convert time counts into a wall-clock
mgottscho 0:47acc8320421 15 * time and vice versa.
mgottscho 0:47acc8320421 16 */
mgottscho 0:47acc8320421 17 class PreciseTime {
mgottscho 0:47acc8320421 18 public:
mgottscho 0:47acc8320421 19 PreciseTime();
mgottscho 0:47acc8320421 20
mgottscho 0:47acc8320421 21 /**
mgottscho 0:47acc8320421 22 * Prints an ASCII representation of this object.
mgottscho 0:47acc8320421 23 */
mgottscho 0:47acc8320421 24 void print();
mgottscho 0:47acc8320421 25
mgottscho 0:47acc8320421 26 /**
mgottscho 0:47acc8320421 27 * Convert a PreciseTime object to hours.
mgottscho 0:47acc8320421 28 * @param obj the object to convert
mgottscho 0:47acc8320421 29 * @returns value of obj in hours
mgottscho 0:47acc8320421 30 */
mgottscho 0:47acc8320421 31 static uint32_t to_h(PreciseTime obj);
mgottscho 0:47acc8320421 32
mgottscho 0:47acc8320421 33 /**
mgottscho 0:47acc8320421 34 * Convert a PreciseTime object to minutes.
mgottscho 0:47acc8320421 35 * @param obj the object to convert
mgottscho 0:47acc8320421 36 * @returns value of obj in minutes
mgottscho 0:47acc8320421 37 */
mgottscho 0:47acc8320421 38 static uint32_t to_m(PreciseTime obj);
mgottscho 0:47acc8320421 39
mgottscho 0:47acc8320421 40 /**
mgottscho 0:47acc8320421 41 * Convert a PreciseTime object to seconds.
mgottscho 0:47acc8320421 42 * @param obj the object to convert
mgottscho 0:47acc8320421 43 * @returns value of obj in seconds
mgottscho 0:47acc8320421 44 */
mgottscho 0:47acc8320421 45 static uint32_t to_s(PreciseTime obj);
mgottscho 0:47acc8320421 46
mgottscho 0:47acc8320421 47 /**
mgottscho 0:47acc8320421 48 * Convert a PreciseTime object to ms
mgottscho 0:47acc8320421 49 * @param obj the object to convert
mgottscho 0:47acc8320421 50 * @returns value of obj in ms
mgottscho 0:47acc8320421 51 */
mgottscho 0:47acc8320421 52 static uint32_t to_ms(PreciseTime obj);
mgottscho 0:47acc8320421 53
mgottscho 0:47acc8320421 54 /**
mgottscho 0:47acc8320421 55 * Convert a PreciseTime object to us.
mgottscho 0:47acc8320421 56 * @param obj the object to convert
mgottscho 0:47acc8320421 57 * @returns value of obj in us
mgottscho 0:47acc8320421 58 */
mgottscho 0:47acc8320421 59 static uint32_t to_us(PreciseTime obj);
mgottscho 0:47acc8320421 60
mgottscho 0:47acc8320421 61 /**
mgottscho 0:47acc8320421 62 * Convert a PreciseTime object to ns.
mgottscho 0:47acc8320421 63 * @param obj the object to convert
mgottscho 0:47acc8320421 64 * @returns value of obj in ns
mgottscho 0:47acc8320421 65 */
mgottscho 0:47acc8320421 66 static uint32_t to_ns(PreciseTime obj);
mgottscho 0:47acc8320421 67
mgottscho 0:47acc8320421 68 /**
mgottscho 0:47acc8320421 69 * Convert an integer number of hours to a PreciseTime representation.
mgottscho 0:47acc8320421 70 * @param h number of hours
mgottscho 0:47acc8320421 71 * @returns the PreciseTime representation
mgottscho 0:47acc8320421 72 */
mgottscho 0:47acc8320421 73 static PreciseTime from_h(uint32_t h);
mgottscho 0:47acc8320421 74
mgottscho 0:47acc8320421 75 /**
mgottscho 0:47acc8320421 76 * Convert an integer number of minutes to a PreciseTime representation.
mgottscho 0:47acc8320421 77 * @param m number of minutes
mgottscho 0:47acc8320421 78 * @returns the PreciseTime representation
mgottscho 0:47acc8320421 79 */
mgottscho 0:47acc8320421 80 static PreciseTime from_m(uint32_t m);
mgottscho 0:47acc8320421 81
mgottscho 0:47acc8320421 82 /**
mgottscho 0:47acc8320421 83 * Convert an integer number of seconds to a PreciseTime representation.
mgottscho 0:47acc8320421 84 * @param s number of seconds
mgottscho 0:47acc8320421 85 * @returns the PreciseTime representation
mgottscho 0:47acc8320421 86 */
mgottscho 0:47acc8320421 87 static PreciseTime from_s(uint32_t s);
mgottscho 0:47acc8320421 88
mgottscho 0:47acc8320421 89 /**
mgottscho 0:47acc8320421 90 * Convert an integer number of ms to a PreciseTime representation.
mgottscho 0:47acc8320421 91 * @param ms number of ms
mgottscho 0:47acc8320421 92 * @returns the PreciseTime representation
mgottscho 0:47acc8320421 93 */
mgottscho 0:47acc8320421 94 static PreciseTime from_ms(uint32_t ms);
mgottscho 0:47acc8320421 95
mgottscho 0:47acc8320421 96 /**
mgottscho 0:47acc8320421 97 * Convert an integer number of us to a PreciseTime representation.
mgottscho 0:47acc8320421 98 * @param us number of us
mgottscho 0:47acc8320421 99 * @returns the PreciseTime representation
mgottscho 0:47acc8320421 100 */
mgottscho 0:47acc8320421 101 static PreciseTime from_us(uint32_t us);
mgottscho 0:47acc8320421 102
mgottscho 0:47acc8320421 103 /**
mgottscho 0:47acc8320421 104 * Convert an integer number of ns to a PreciseTime representation.
mgottscho 0:47acc8320421 105 * @param ns number of ns
mgottscho 0:47acc8320421 106 * @returns the PreciseTime representation
mgottscho 0:47acc8320421 107 */
mgottscho 0:47acc8320421 108 static PreciseTime from_ns(uint32_t ns);
mgottscho 0:47acc8320421 109
mgottscho 0:47acc8320421 110 uint32_t h;
mgottscho 0:47acc8320421 111 uint32_t m;
mgottscho 0:47acc8320421 112 uint32_t s;
mgottscho 0:47acc8320421 113 uint32_t ms;
mgottscho 0:47acc8320421 114 uint32_t us;
mgottscho 0:47acc8320421 115 uint32_t ns;
mgottscho 0:47acc8320421 116
mgottscho 0:47acc8320421 117 //constants for time conversion since we do not have floating point division
mgottscho 0:47acc8320421 118 const static uint32_t NS_PER_US = 1000;
mgottscho 0:47acc8320421 119 const static uint32_t US_PER_MS = 1000;
mgottscho 0:47acc8320421 120 const static uint32_t MS_PER_SEC = 1000;
mgottscho 0:47acc8320421 121 const static uint32_t SEC_PER_MIN = 60;
mgottscho 0:47acc8320421 122 const static uint32_t MIN_PER_HOUR = 60;
mgottscho 0:47acc8320421 123
mgottscho 0:47acc8320421 124 const static float US_PER_NS = 0.001;
mgottscho 0:47acc8320421 125 const static float MS_PER_US = 0.001;
mgottscho 0:47acc8320421 126 const static float SEC_PER_MS = 0.001;
mgottscho 0:47acc8320421 127 const static float MIN_PER_SEC = 0.016666666666;
mgottscho 0:47acc8320421 128 const static float HOUR_PER_MIN = 0.016666666666;
mgottscho 0:47acc8320421 129 };
mgottscho 0:47acc8320421 130
mgottscho 0:47acc8320421 131 #endif