Added a GPIO to power on/off for external I2C sensor(s) (with LEDs)

Dependencies:   UniGraphic mbed vt100

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers edge_time.cpp Source File

edge_time.cpp

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