Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of HardwareTimersLib by
PreciseTime.cpp
00001 /* PreciseTime.cpp 00002 * Tested with mbed board: FRDM-KL46Z 00003 * Author: Mark Gottscho 00004 * mgottscho@ucla.edu 00005 */ 00006 00007 #include "mbed.h" 00008 #include "PreciseTime.h" 00009 00010 PreciseTime::PreciseTime() : 00011 h(0), 00012 m(0), 00013 s(0), 00014 ms(0), 00015 us(0), 00016 ns(0) 00017 {} 00018 00019 00020 void PreciseTime::print() { 00021 // printf("HH:MM:SS:ms:us:ns\r\n"); 00022 printf("%02u:%02u:%02u:%03u:%03u:%03u", h, m, s, ms, us, ns); 00023 } 00024 00025 uint32_t PreciseTime::to_h(PreciseTime obj) { 00026 return obj.h; 00027 } 00028 00029 uint32_t PreciseTime::to_m(PreciseTime obj) { 00030 return to_h(obj)*MIN_PER_HOUR + obj.m; 00031 } 00032 00033 uint32_t PreciseTime::to_s(PreciseTime obj) { 00034 return to_m(obj)*SEC_PER_MIN + obj.s; 00035 } 00036 00037 uint32_t PreciseTime::to_ms(PreciseTime obj) { 00038 return to_s(obj)*MS_PER_SEC + obj.ms; 00039 } 00040 00041 uint32_t PreciseTime::to_us(PreciseTime obj) { 00042 return to_ms(obj)*US_PER_MS + obj.us; 00043 } 00044 00045 uint32_t PreciseTime::to_ns(PreciseTime obj) { 00046 return to_us(obj)*NS_PER_US + obj.ns; 00047 } 00048 00049 PreciseTime PreciseTime::from_h(uint32_t h) { 00050 PreciseTime obj; 00051 obj.h = h; 00052 return obj; 00053 } 00054 00055 PreciseTime PreciseTime::from_m(uint32_t m) { 00056 PreciseTime obj; 00057 obj.h = m * HOUR_PER_MIN; 00058 obj.m = m % MIN_PER_HOUR; 00059 return obj; 00060 } 00061 00062 PreciseTime PreciseTime::from_s(uint32_t s) { 00063 PreciseTime obj; 00064 obj.m = s * MIN_PER_SEC; 00065 obj.s = s % SEC_PER_MIN; 00066 00067 obj.h = obj.m * HOUR_PER_MIN; 00068 obj.m = obj.m % MIN_PER_HOUR; 00069 return obj; 00070 } 00071 00072 PreciseTime PreciseTime::from_ms(uint32_t ms) { 00073 PreciseTime obj; 00074 obj.s = ms * SEC_PER_MS; 00075 obj.ms = ms % MS_PER_SEC; 00076 00077 obj.m = obj.s * MIN_PER_SEC; 00078 obj.s = obj.s % SEC_PER_MIN; 00079 00080 obj.h = obj.m * HOUR_PER_MIN; 00081 obj.m = obj.m % MIN_PER_HOUR; 00082 return obj; 00083 } 00084 00085 PreciseTime PreciseTime::from_us(uint32_t us) { 00086 PreciseTime obj; 00087 obj.ms = us * MS_PER_US; 00088 obj.us = us % US_PER_MS; 00089 00090 obj.s = obj.ms * SEC_PER_MS; 00091 obj.ms = obj.ms % MS_PER_SEC; 00092 00093 obj.m = obj.s * MIN_PER_SEC; 00094 obj.s = obj.s % SEC_PER_MIN; 00095 00096 obj.h = obj.m * HOUR_PER_MIN; 00097 obj.m = obj.m % MIN_PER_HOUR; 00098 return obj; 00099 } 00100 00101 PreciseTime PreciseTime::from_ns(uint32_t ns) { 00102 PreciseTime obj; 00103 obj.us = ns * US_PER_NS; 00104 obj.ns = ns % NS_PER_US; 00105 00106 obj.ms = obj.us * MS_PER_US; 00107 obj.us = obj.us % US_PER_MS; 00108 00109 obj.s = obj.ms * SEC_PER_MS; 00110 obj.ms = obj.ms % MS_PER_SEC; 00111 00112 obj.m = obj.s * MIN_PER_SEC; 00113 obj.s = obj.s % SEC_PER_MIN; 00114 00115 obj.h = obj.m * HOUR_PER_MIN; 00116 obj.m = obj.m % MIN_PER_HOUR; 00117 return obj; 00118 }
Generated on Tue Jul 12 2022 20:37:48 by
1.7.2
