Mark Gottscho / HardwareTimersLib

Fork of HardwareTimersLib by Mark Gottscho

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PreciseTime.h Source File

PreciseTime.h

00001 /* PreciseTime.h
00002  * Tested with mbed board: FRDM-KL46Z
00003  * Author: Mark Gottscho
00004  * mgottscho@ucla.edu
00005  */
00006 
00007 #ifndef PRECISETIME_H
00008 #define PRECISETIME_H
00009 
00010 #include "mbed.h"
00011 
00012 /**
00013  * This class provides a simple abstraction for time-keeping in a wall-clock sense.
00014  * It can store time by hour:min:sec:ms:us:ns, and convert time counts into a wall-clock
00015  * time and vice versa.
00016  */
00017 class PreciseTime {
00018     public:
00019         PreciseTime();
00020         
00021         /**
00022          * Prints an ASCII representation of this object.
00023          */
00024         void print();
00025         
00026         /**
00027          * Convert a PreciseTime object to hours.
00028          * @param obj the object to convert
00029          * @returns value of obj in hours
00030          */
00031         static uint32_t to_h(PreciseTime obj);
00032         
00033         /**
00034          * Convert a PreciseTime object to minutes.
00035          * @param obj the object to convert
00036          * @returns value of obj in minutes
00037          */
00038         static uint32_t to_m(PreciseTime obj);
00039         
00040         /**
00041          * Convert a PreciseTime object to seconds.
00042          * @param obj the object to convert
00043          * @returns value of obj in seconds
00044          */
00045         static uint32_t to_s(PreciseTime obj);
00046         
00047         /**
00048          * Convert a PreciseTime object to ms
00049          * @param obj the object to convert
00050          * @returns value of obj in ms
00051          */
00052         static uint32_t to_ms(PreciseTime obj);
00053         
00054         /**
00055          * Convert a PreciseTime object to us.
00056          * @param obj the object to convert
00057          * @returns value of obj in us
00058          */
00059         static uint32_t to_us(PreciseTime obj);
00060         
00061         /**
00062          * Convert a PreciseTime object to ns.
00063          * @param obj the object to convert
00064          * @returns value of obj in ns
00065          */
00066         static uint32_t to_ns(PreciseTime obj);
00067         
00068         /**
00069          * Convert an integer number of hours to a PreciseTime representation.
00070          * @param h number of hours
00071          * @returns the PreciseTime representation
00072          */
00073         static PreciseTime from_h(uint32_t h);
00074         
00075         /**
00076          * Convert an integer number of minutes to a PreciseTime representation.
00077          * @param m number of minutes
00078          * @returns the PreciseTime representation
00079          */
00080         static PreciseTime from_m(uint32_t m);
00081         
00082         /**
00083          * Convert an integer number of seconds to a PreciseTime representation.
00084          * @param s number of seconds
00085          * @returns the PreciseTime representation
00086          */
00087         static PreciseTime from_s(uint32_t s);
00088         
00089         /**
00090          * Convert an integer number of ms to a PreciseTime representation.
00091          * @param ms number of ms
00092          * @returns the PreciseTime representation
00093          */
00094         static PreciseTime from_ms(uint32_t ms);
00095         
00096         /**
00097          * Convert an integer number of us to a PreciseTime representation.
00098          * @param us number of us
00099          * @returns the PreciseTime representation
00100          */
00101         static PreciseTime from_us(uint32_t us);
00102         
00103         /**
00104          * Convert an integer number of ns to a PreciseTime representation.
00105          * @param ns number of ns
00106          * @returns the PreciseTime representation
00107          */
00108         static PreciseTime from_ns(uint32_t ns);
00109         
00110         uint32_t h;
00111         uint32_t m;
00112         uint32_t s;
00113         uint32_t ms;
00114         uint32_t us;
00115         uint32_t ns;
00116         
00117         //constants for time conversion since we do not have floating point division
00118         const static uint32_t NS_PER_US = 1000;
00119         const static uint32_t US_PER_MS = 1000;
00120         const static uint32_t MS_PER_SEC = 1000;
00121         const static uint32_t SEC_PER_MIN = 60;
00122         const static uint32_t MIN_PER_HOUR = 60;
00123         
00124         const static float US_PER_NS = 0.001;
00125         const static float MS_PER_US = 0.001;
00126         const static float SEC_PER_MS = 0.001;
00127         const static float MIN_PER_SEC = 0.016666666666;
00128         const static float HOUR_PER_MIN = 0.016666666666;
00129 };
00130 
00131 #endif