POC1.5 prototype 2 x color sensor 2 x LM75B 3 x AnalogIn 1 x accel

Dependencies:   mbed vt100

Committer:
Rhyme
Date:
Thu Dec 07 10:13:13 2017 +0000
Revision:
8:5590f55bdf41
Parent:
0:f0de320e23ac
commit at the end of 7-Dec-2017

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rhyme 0:f0de320e23ac 1 #include "mbed.h"
Rhyme 0:f0de320e23ac 2 #include "edge_time.h"
Rhyme 0:f0de320e23ac 3
Rhyme 0:f0de320e23ac 4 static const uint8_t daysInMonth[12] = {
Rhyme 0:f0de320e23ac 5 31, 28, 31, 30,
Rhyme 0:f0de320e23ac 6 31, 30, 31, 31,
Rhyme 0:f0de320e23ac 7 30, 31, 30, 31
Rhyme 0:f0de320e23ac 8 } ;
Rhyme 0:f0de320e23ac 9
Rhyme 0:f0de320e23ac 10 const char *nameOfDay[7] = {
Rhyme 0:f0de320e23ac 11 "Sunday", "Monday", "Tuesday", "Wednesday",
Rhyme 0:f0de320e23ac 12 "Thursday", "Friday", "Saturday"
Rhyme 0:f0de320e23ac 13 } ;
Rhyme 0:f0de320e23ac 14
Rhyme 0:f0de320e23ac 15 uint32_t edge_time = 0 ;
Rhyme 0:f0de320e23ac 16 uint32_t utc_offset = 9 * 60 * 60 ;
Rhyme 0:f0de320e23ac 17 tm current_time ;
Rhyme 0:f0de320e23ac 18 Ticker *tokei = 0 ;
Rhyme 0:f0de320e23ac 19
Rhyme 0:f0de320e23ac 20 void inc_sec(void)
Rhyme 0:f0de320e23ac 21 {
Rhyme 0:f0de320e23ac 22 edge_time++ ;
Rhyme 0:f0de320e23ac 23 }
Rhyme 0:f0de320e23ac 24
Rhyme 0:f0de320e23ac 25 void init_timer(void)
Rhyme 0:f0de320e23ac 26 {
Rhyme 0:f0de320e23ac 27 tokei = new Ticker() ;
Rhyme 0:f0de320e23ac 28 tokei->attach(inc_sec, 1.0) ;
Rhyme 0:f0de320e23ac 29 }
Rhyme 0:f0de320e23ac 30
Rhyme 0:f0de320e23ac 31 void set_time(const uint16_t valueLen, const uint8_t *value)
Rhyme 0:f0de320e23ac 32 {
Rhyme 0:f0de320e23ac 33 uint32_t tmp_timestamp = 0 ;
Rhyme 0:f0de320e23ac 34 for (int i = 0 ; i < valueLen ; i++ ) {
Rhyme 0:f0de320e23ac 35 tmp_timestamp |= (value[i] & 0xFF) << (i * 8) ;
Rhyme 0:f0de320e23ac 36 }
Rhyme 0:f0de320e23ac 37 edge_time = tmp_timestamp ;
Rhyme 0:f0de320e23ac 38 ts2tm(edge_time, &current_time) ;
Rhyme 0:f0de320e23ac 39 // ts2time(edge_time, &current_time) ;
Rhyme 0:f0de320e23ac 40 }
Rhyme 0:f0de320e23ac 41
Rhyme 0:f0de320e23ac 42 void ts2time(uint32_t timestamp, struct tm *tm)
Rhyme 0:f0de320e23ac 43 {
Rhyme 0:f0de320e23ac 44 uint32_t seconds, minutes, hours, days, month, year ;
Rhyme 0:f0de320e23ac 45 uint32_t dayOfWeek ;
Rhyme 0:f0de320e23ac 46
Rhyme 0:f0de320e23ac 47 // timestamp += (3600 * 9) ; /* +9 hours for JST */
Rhyme 0:f0de320e23ac 48 timestamp += utc_offset ;
Rhyme 0:f0de320e23ac 49
Rhyme 0:f0de320e23ac 50 seconds = timestamp % 60 ;
Rhyme 0:f0de320e23ac 51 minutes = timestamp / 60 ;
Rhyme 0:f0de320e23ac 52 hours = minutes / 60 ; /* +9 for JST */
Rhyme 0:f0de320e23ac 53 minutes -= hours * 60 ;
Rhyme 0:f0de320e23ac 54 days = hours / 24 ;
Rhyme 0:f0de320e23ac 55 hours -= days * 24 ;
Rhyme 0:f0de320e23ac 56
Rhyme 0:f0de320e23ac 57 tm->tm_sec = seconds ;
Rhyme 0:f0de320e23ac 58 tm->tm_min = minutes ;
Rhyme 0:f0de320e23ac 59 tm->tm_hour = hours ;
Rhyme 0:f0de320e23ac 60 tm->tm_mday = days + 1 ;
Rhyme 0:f0de320e23ac 61 // tm->tm_mon = month ;
Rhyme 0:f0de320e23ac 62 // tm->tm_year = year ;
Rhyme 0:f0de320e23ac 63 // tm->tm_wday = dayOfWeek ;
Rhyme 0:f0de320e23ac 64 }
Rhyme 0:f0de320e23ac 65
Rhyme 0:f0de320e23ac 66 void ts2tm(uint32_t timestamp, struct tm *tm)
Rhyme 0:f0de320e23ac 67 {
Rhyme 0:f0de320e23ac 68 uint32_t seconds, minutes, hours, days, month, year ;
Rhyme 0:f0de320e23ac 69 uint32_t dayOfWeek ;
Rhyme 0:f0de320e23ac 70
Rhyme 0:f0de320e23ac 71 // timestamp += (3600 * 9) ; /* +9 hours for JST */
Rhyme 0:f0de320e23ac 72 timestamp += utc_offset ;
Rhyme 0:f0de320e23ac 73
Rhyme 0:f0de320e23ac 74 seconds = timestamp % 60 ;
Rhyme 0:f0de320e23ac 75 minutes = timestamp / 60 ;
Rhyme 0:f0de320e23ac 76 hours = minutes / 60 ; /* +9 for JST */
Rhyme 0:f0de320e23ac 77 minutes -= hours * 60 ;
Rhyme 0:f0de320e23ac 78 days = hours / 24 ;
Rhyme 0:f0de320e23ac 79 hours -= days * 24 ;
Rhyme 0:f0de320e23ac 80
Rhyme 0:f0de320e23ac 81 /* Unix timestamp start 1-Jan-1970 Thursday */
Rhyme 0:f0de320e23ac 82 year = 1970 ;
Rhyme 0:f0de320e23ac 83 dayOfWeek = 4 ; /* Thursday */
Rhyme 0:f0de320e23ac 84
Rhyme 0:f0de320e23ac 85 while(1) {
Rhyme 0:f0de320e23ac 86 bool isLeapYear =
Rhyme 0:f0de320e23ac 87 (((year % 4) == 0)
Rhyme 0:f0de320e23ac 88 &&(((year % 100) != 0)
Rhyme 0:f0de320e23ac 89 || ((year % 400) == 0))) ;
Rhyme 0:f0de320e23ac 90 uint16_t daysInYear = isLeapYear ? 366 : 365 ;
Rhyme 0:f0de320e23ac 91 if (days >= daysInYear) {
Rhyme 0:f0de320e23ac 92 dayOfWeek += isLeapYear ? 2 : 1 ;
Rhyme 0:f0de320e23ac 93 days -= daysInYear ;
Rhyme 0:f0de320e23ac 94 if (dayOfWeek >= 7) {
Rhyme 0:f0de320e23ac 95 dayOfWeek -= 7 ;
Rhyme 0:f0de320e23ac 96 }
Rhyme 0:f0de320e23ac 97 year++ ;
Rhyme 0:f0de320e23ac 98 } else {
Rhyme 0:f0de320e23ac 99 tm->tm_yday = days ;
Rhyme 0:f0de320e23ac 100 dayOfWeek += days ;
Rhyme 0:f0de320e23ac 101 dayOfWeek %= 7 ;
Rhyme 0:f0de320e23ac 102
Rhyme 0:f0de320e23ac 103 /* calc the month and the day */
Rhyme 0:f0de320e23ac 104 for (month = 0 ; month < 12 ; month++) {
Rhyme 0:f0de320e23ac 105 uint8_t dim = daysInMonth[month] ;
Rhyme 0:f0de320e23ac 106
Rhyme 0:f0de320e23ac 107 /* add a day to feburary if this is a leap year */
Rhyme 0:f0de320e23ac 108 if ((month == 1) && (isLeapYear)) {
Rhyme 0:f0de320e23ac 109 dim++ ;
Rhyme 0:f0de320e23ac 110 }
Rhyme 0:f0de320e23ac 111
Rhyme 0:f0de320e23ac 112 if (days >= dim) {
Rhyme 0:f0de320e23ac 113 days -= dim ;
Rhyme 0:f0de320e23ac 114 } else {
Rhyme 0:f0de320e23ac 115 break ;
Rhyme 0:f0de320e23ac 116 }
Rhyme 0:f0de320e23ac 117 }
Rhyme 0:f0de320e23ac 118 break ;
Rhyme 0:f0de320e23ac 119 }
Rhyme 0:f0de320e23ac 120 }
Rhyme 0:f0de320e23ac 121 tm->tm_sec = seconds ;
Rhyme 0:f0de320e23ac 122 tm->tm_min = minutes ;
Rhyme 0:f0de320e23ac 123 tm->tm_hour = hours ;
Rhyme 0:f0de320e23ac 124 tm->tm_mday = days + 1 ;
Rhyme 0:f0de320e23ac 125 tm->tm_mon = month ;
Rhyme 0:f0de320e23ac 126 tm->tm_year = year ;
Rhyme 0:f0de320e23ac 127 tm->tm_wday = dayOfWeek ;
Rhyme 0:f0de320e23ac 128 }
Rhyme 0:f0de320e23ac 129
Rhyme 0:f0de320e23ac 130 void print_time(struct tm *tm)
Rhyme 0:f0de320e23ac 131 {
Rhyme 0:f0de320e23ac 132 printf("%02d:%02d:%02d",
Rhyme 0:f0de320e23ac 133 tm->tm_hour,
Rhyme 0:f0de320e23ac 134 tm->tm_min,
Rhyme 0:f0de320e23ac 135 tm->tm_sec ) ;
Rhyme 0:f0de320e23ac 136 }
Rhyme 0:f0de320e23ac 137
Rhyme 0:f0de320e23ac 138 void print_time(uint32_t thetime)
Rhyme 0:f0de320e23ac 139 {
Rhyme 0:f0de320e23ac 140 struct tm timestruct ;
Rhyme 0:f0de320e23ac 141 ts2time(thetime, &timestruct) ;
Rhyme 0:f0de320e23ac 142 print_time(&timestruct) ;
Rhyme 0:f0de320e23ac 143 }
Rhyme 0:f0de320e23ac 144
Rhyme 0:f0de320e23ac 145 void print_time(void)
Rhyme 0:f0de320e23ac 146 {
Rhyme 0:f0de320e23ac 147 struct tm timestruct ;
Rhyme 0:f0de320e23ac 148 ts2time(edge_time, &timestruct) ;
Rhyme 0:f0de320e23ac 149 print_time(&timestruct) ;
Rhyme 0:f0de320e23ac 150 }
Rhyme 0:f0de320e23ac 151
Rhyme 0:f0de320e23ac 152 void print_date(struct tm *tm)
Rhyme 0:f0de320e23ac 153 {
Rhyme 8:5590f55bdf41 154 printf("%d/%d/%d %02d:%02d:%02d",
Rhyme 8:5590f55bdf41 155 tm->tm_year,
Rhyme 8:5590f55bdf41 156 tm->tm_mon + 1,
Rhyme 8:5590f55bdf41 157 tm->tm_mday,
Rhyme 8:5590f55bdf41 158 tm->tm_hour,
Rhyme 8:5590f55bdf41 159 tm->tm_min,
Rhyme 8:5590f55bdf41 160 tm->tm_sec
Rhyme 8:5590f55bdf41 161 ) ;
Rhyme 8:5590f55bdf41 162 }
Rhyme 8:5590f55bdf41 163
Rhyme 8:5590f55bdf41 164 void print_date_wd(struct tm *tm)
Rhyme 8:5590f55bdf41 165 {
Rhyme 0:f0de320e23ac 166 printf("%d/%d/%d %02d:%02d:%02d (%s)",
Rhyme 0:f0de320e23ac 167 tm->tm_year,
Rhyme 0:f0de320e23ac 168 tm->tm_mon + 1,
Rhyme 0:f0de320e23ac 169 tm->tm_mday,
Rhyme 0:f0de320e23ac 170 tm->tm_hour,
Rhyme 0:f0de320e23ac 171 tm->tm_min,
Rhyme 0:f0de320e23ac 172 tm->tm_sec,
Rhyme 0:f0de320e23ac 173 nameOfDay[tm->tm_wday]
Rhyme 0:f0de320e23ac 174 ) ;
Rhyme 0:f0de320e23ac 175 }
Rhyme 0:f0de320e23ac 176
Rhyme 0:f0de320e23ac 177 void time2str(struct tm *tm, char *timestr)
Rhyme 0:f0de320e23ac 178 {
Rhyme 0:f0de320e23ac 179 sprintf(timestr, "%02d:%02d:%02d",
Rhyme 0:f0de320e23ac 180 tm->tm_hour,
Rhyme 0:f0de320e23ac 181 tm->tm_min,
Rhyme 0:f0de320e23ac 182 tm->tm_sec ) ;
Rhyme 0:f0de320e23ac 183 }
Rhyme 0:f0de320e23ac 184
Rhyme 0:f0de320e23ac 185 void time2str(char *timestr)
Rhyme 0:f0de320e23ac 186 {
Rhyme 0:f0de320e23ac 187 struct tm timestruct ;
Rhyme 0:f0de320e23ac 188 ts2time(edge_time, &timestruct) ;
Rhyme 0:f0de320e23ac 189 time2str(&timestruct, timestr) ;
Rhyme 0:f0de320e23ac 190 }
Rhyme 0:f0de320e23ac 191
Rhyme 0:f0de320e23ac 192 int32_t time2seq(uint32_t timestamp)
Rhyme 0:f0de320e23ac 193 {
Rhyme 0:f0de320e23ac 194 struct tm timestruct ;
Rhyme 0:f0de320e23ac 195 int32_t result ;
Rhyme 0:f0de320e23ac 196 ts2time(timestamp, &timestruct) ;
Rhyme 0:f0de320e23ac 197 result = timestruct.tm_hour * 10000
Rhyme 0:f0de320e23ac 198 + timestruct.tm_min * 100
Rhyme 0:f0de320e23ac 199 + timestruct.tm_sec ;
Rhyme 0:f0de320e23ac 200 return(result) ;
Rhyme 0:f0de320e23ac 201 }
Rhyme 0:f0de320e23ac 202
Rhyme 8:5590f55bdf41 203 void time2seq(uint32_t timestamp, char *timestr)
Rhyme 8:5590f55bdf41 204 {
Rhyme 8:5590f55bdf41 205 struct tm timestruct ;
Rhyme 8:5590f55bdf41 206 ts2tm(timestamp, &timestruct) ;
Rhyme 8:5590f55bdf41 207 sprintf(timestr, "%d%02d%02d%02d%02d%02d",
Rhyme 8:5590f55bdf41 208 timestruct.tm_year,
Rhyme 8:5590f55bdf41 209 timestruct.tm_mon + 1,
Rhyme 8:5590f55bdf41 210 timestruct.tm_mday,
Rhyme 8:5590f55bdf41 211 timestruct.tm_hour,
Rhyme 8:5590f55bdf41 212 timestruct.tm_min,
Rhyme 8:5590f55bdf41 213 timestruct.tm_sec
Rhyme 8:5590f55bdf41 214 ) ;
Rhyme 8:5590f55bdf41 215 }
Rhyme 8:5590f55bdf41 216
Rhyme 0:f0de320e23ac 217 void time2date(struct tm *tm, char *datestr)
Rhyme 0:f0de320e23ac 218 {
Rhyme 0:f0de320e23ac 219 sprintf(datestr, "%d/%d/%d %02d:%02d:%02d (%s)",
Rhyme 0:f0de320e23ac 220 tm->tm_year,
Rhyme 0:f0de320e23ac 221 tm->tm_mon + 1,
Rhyme 0:f0de320e23ac 222 tm->tm_mday,
Rhyme 0:f0de320e23ac 223 tm->tm_hour,
Rhyme 0:f0de320e23ac 224 tm->tm_min,
Rhyme 0:f0de320e23ac 225 tm->tm_sec,
Rhyme 0:f0de320e23ac 226 nameOfDay[tm->tm_wday]
Rhyme 0:f0de320e23ac 227 ) ;
Rhyme 0:f0de320e23ac 228 }