afLib 1.3 which is supporting both SPI and UART
Dependencies: vt100 mbed afLib_1_3
edge_time.cpp
00001 #include "mbed.h" 00002 #include "edge_mgr.h" 00003 #include "edge_time.h" 00004 00005 static const uint8_t daysInMonth[12] = { 00006 31, 28, 31, 30, 00007 31, 30, 31, 31, 00008 30, 31, 30, 31 00009 } ; 00010 00011 const char *nameOfDay[7] = { 00012 "Sunday", "Monday", "Tuesday", "Wednesday", 00013 "Thursday", "Friday", "Saturday" 00014 } ; 00015 00016 uint32_t edge_time = 0 ; 00017 uint32_t utc_offset = 9 * 60 * 60 ; 00018 tm current_time ; 00019 Ticker *tokei = 0 ; 00020 00021 void inc_sec(void) 00022 { 00023 __disable_irq() ; // Disable Interrupts 00024 edge_time++ ; 00025 __enable_irq() ; // Enable Interrupts 00026 } 00027 00028 void init_timer(void) 00029 { 00030 tokei = new Ticker() ; 00031 tokei->attach(inc_sec, 1.0) ; 00032 } 00033 00034 void set_time(const uint16_t valueLen, const uint8_t *value) 00035 { 00036 uint32_t tmp_timestamp = 0 ; 00037 for (int i = 0 ; i < valueLen ; i++ ) { 00038 tmp_timestamp |= (value[i] & 0xFF) << (i * 8) ; 00039 } 00040 edge_time = tmp_timestamp ; 00041 ts2tm(edge_time, ¤t_time) ; 00042 // ts2time(edge_time, ¤t_time) ; 00043 } 00044 00045 void ts2time(uint32_t timestamp, struct tm *tm) 00046 { 00047 uint32_t seconds, minutes, hours, days, month, year ; 00048 uint32_t dayOfWeek ; 00049 00050 // timestamp += (3600 * 9) ; /* +9 hours for JST */ 00051 timestamp += utc_offset ; 00052 00053 seconds = timestamp % 60 ; 00054 minutes = timestamp / 60 ; 00055 hours = minutes / 60 ; /* +9 for JST */ 00056 minutes -= hours * 60 ; 00057 days = hours / 24 ; 00058 hours -= days * 24 ; 00059 00060 tm->tm_sec = seconds ; 00061 tm->tm_min = minutes ; 00062 tm->tm_hour = hours ; 00063 tm->tm_mday = days + 1 ; 00064 // tm->tm_mon = month ; 00065 // tm->tm_year = year ; 00066 // tm->tm_wday = dayOfWeek ; 00067 } 00068 00069 void ts2tm(uint32_t timestamp, struct tm *tm) 00070 { 00071 uint32_t seconds, minutes, hours, days, month, year ; 00072 uint32_t dayOfWeek ; 00073 00074 // timestamp += (3600 * 9) ; /* +9 hours for JST */ 00075 timestamp += utc_offset ; 00076 00077 seconds = timestamp % 60 ; 00078 minutes = timestamp / 60 ; 00079 hours = minutes / 60 ; /* +9 for JST */ 00080 minutes -= hours * 60 ; 00081 days = hours / 24 ; 00082 hours -= days * 24 ; 00083 00084 /* Unix timestamp start 1-Jan-1970 Thursday */ 00085 year = 1970 ; 00086 dayOfWeek = 4 ; /* Thursday */ 00087 00088 while(1) { 00089 bool isLeapYear = 00090 (((year % 4) == 0) 00091 &&(((year % 100) != 0) 00092 || ((year % 400) == 0))) ; 00093 uint16_t daysInYear = isLeapYear ? 366 : 365 ; 00094 if (days >= daysInYear) { 00095 dayOfWeek += isLeapYear ? 2 : 1 ; 00096 days -= daysInYear ; 00097 if (dayOfWeek >= 7) { 00098 dayOfWeek -= 7 ; 00099 } 00100 year++ ; 00101 } else { 00102 tm->tm_yday = days ; 00103 dayOfWeek += days ; 00104 dayOfWeek %= 7 ; 00105 00106 /* calc the month and the day */ 00107 for (month = 0 ; month < 12 ; month++) { 00108 uint8_t dim = daysInMonth[month] ; 00109 00110 /* add a day to feburary if this is a leap year */ 00111 if ((month == 1) && (isLeapYear)) { 00112 dim++ ; 00113 } 00114 00115 if (days >= dim) { 00116 days -= dim ; 00117 } else { 00118 break ; 00119 } 00120 } 00121 break ; 00122 } 00123 } 00124 tm->tm_sec = seconds ; 00125 tm->tm_min = minutes ; 00126 tm->tm_hour = hours ; 00127 tm->tm_mday = days + 1 ; 00128 tm->tm_mon = month ; 00129 tm->tm_year = year ; 00130 tm->tm_wday = dayOfWeek ; 00131 } 00132 00133 void print_time(struct tm *tm) 00134 { 00135 tty->printf("%02d:%02d:%02d", 00136 tm->tm_hour, 00137 tm->tm_min, 00138 tm->tm_sec ) ; 00139 } 00140 00141 void print_time(uint32_t thetime) 00142 { 00143 struct tm timestruct ; 00144 ts2time(thetime, ×truct) ; 00145 print_time(×truct) ; 00146 } 00147 00148 void print_time(void) 00149 { 00150 struct tm timestruct ; 00151 ts2time(edge_time, ×truct) ; 00152 print_time(×truct) ; 00153 } 00154 00155 void print_date(struct tm *tm) 00156 { 00157 tty->printf("%d/%d/%d %02d:%02d:%02d", 00158 tm->tm_year, 00159 tm->tm_mon + 1, 00160 tm->tm_mday, 00161 tm->tm_hour, 00162 tm->tm_min, 00163 tm->tm_sec 00164 ) ; 00165 } 00166 00167 void print_date_wd(struct tm *tm) 00168 { 00169 tty->printf("%d/%d/%d %02d:%02d:%02d (%s)", 00170 tm->tm_year, 00171 tm->tm_mon + 1, 00172 tm->tm_mday, 00173 tm->tm_hour, 00174 tm->tm_min, 00175 tm->tm_sec, 00176 nameOfDay[tm->tm_wday] 00177 ) ; 00178 } 00179 00180 void time2str(struct tm *tm, char *timestr) 00181 { 00182 sprintf(timestr, "%02d:%02d:%02d", 00183 tm->tm_hour, 00184 tm->tm_min, 00185 tm->tm_sec ) ; 00186 } 00187 00188 void time2str(char *timestr) 00189 { 00190 struct tm timestruct ; 00191 ts2time(edge_time, ×truct) ; 00192 time2str(×truct, timestr) ; 00193 } 00194 00195 int32_t time2seq(uint32_t timestamp) 00196 { 00197 struct tm timestruct ; 00198 int32_t result ; 00199 ts2time(timestamp, ×truct) ; 00200 result = timestruct.tm_hour * 10000 00201 + timestruct.tm_min * 100 00202 + timestruct.tm_sec ; 00203 return(result) ; 00204 } 00205 00206 void time2seq(uint32_t timestamp, char *timestr) 00207 { 00208 struct tm timestruct ; 00209 ts2tm(timestamp, ×truct) ; 00210 sprintf(timestr, "%d%02d%02d%02d%02d%02d", 00211 timestruct.tm_year, 00212 timestruct.tm_mon + 1, 00213 timestruct.tm_mday, 00214 timestruct.tm_hour, 00215 timestruct.tm_min, 00216 timestruct.tm_sec 00217 ) ; 00218 } 00219 00220 void time2date(struct tm *tm, char *datestr) 00221 { 00222 sprintf(datestr, "%d/%d/%d %02d:%02d:%02d (%s)", 00223 tm->tm_year, 00224 tm->tm_mon + 1, 00225 tm->tm_mday, 00226 tm->tm_hour, 00227 tm->tm_min, 00228 tm->tm_sec, 00229 nameOfDay[tm->tm_wday] 00230 ) ; 00231 }
Generated on Wed Jul 20 2022 21:24:28 by
1.7.2