Color sensor reset at the end of calibration added. sensor id auto assignment was changed to be a fixed value assignment to avoid sensor id shift when some sensor is absent.

Dependencies:   UniGraphic mbed vt100

Committer:
Rhyme
Date:
Fri Feb 23 05:40:22 2018 +0000
Revision:
0:ce97f6d34336
color sensor reset was added at the end of calibration

Who changed what in which revision?

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