Smart Clock

Dependencies:   AlarmClock DigitalClock EthernetInterface FourDigitLED HTTPClient NTPClient SDFileSystem TrainStat WeatherInfo XML_aide mbed-rtos mbed picojson wolfSSL

Committer:
takashikojo
Date:
Fri Oct 23 05:52:37 2015 +0000
Revision:
5:e4891827f7b1
Parent:
4:8a2d1544d6e0
Child:
7:53dd54df1441
Initial public version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
takashikojo 0:0689619f2486 1 #include "mbed.h"
takashikojo 5:e4891827f7b1 2 #include <ctype.h>
takashikojo 0:0689619f2486 3
takashikojo 0:0689619f2486 4 #include "EthernetInterface.h"
takashikojo 4:8a2d1544d6e0 5 #include "HTTPClient.h"
takashikojo 0:0689619f2486 6 #include "NTPClient.h"
takashikojo 0:0689619f2486 7 #include "AlarmClock.h"
takashikojo 2:bcc1a40ffa04 8 #include "TrainStat.h"
takashikojo 4:8a2d1544d6e0 9 #include "WeatherInfo.h"
takashikojo 5:e4891827f7b1 10 #include "matchLine.h"
takashikojo 1:1a28aaca601d 11
takashikojo 5:e4891827f7b1 12 #if 0
takashikojo 5:e4891827f7b1 13 //Enable debug
takashikojo 5:e4891827f7b1 14 #define DBG(x, ...) std::printf("[Main : DBG]"x"\r\n", ##__VA_ARGS__);
takashikojo 5:e4891827f7b1 15 #define WARN(x, ...) std::printf("[Main : WARN]"x"\r\n", ##__VA_ARGS__);
takashikojo 5:e4891827f7b1 16 #else
takashikojo 5:e4891827f7b1 17 //Disable debug
takashikojo 5:e4891827f7b1 18 #define DBG(x, ...)
takashikojo 5:e4891827f7b1 19 #define WARN(x, ...)
takashikojo 5:e4891827f7b1 20 #endif
takashikojo 5:e4891827f7b1 21
takashikojo 5:e4891827f7b1 22 #define ERR(x, ...) std::printf("[Main : ERR]"x"\r\n", ##__VA_ARGS__);
takashikojo 5:e4891827f7b1 23
takashikojo 5:e4891827f7b1 24 AlarmClock alarmclock (
takashikojo 5:e4891827f7b1 25 /* Segment 0 - 6, Dot */ //D11,D6, D3, D1, D0, D10,D4, D2 ,
takashikojo 5:e4891827f7b1 26 PTB19,PTC0, D3, D1, D0, PTC1, D4, D2,
takashikojo 5:e4891827f7b1 27 /* Digit 1 - 4 */ //D5, D7, D9, D12,
takashikojo 5:e4891827f7b1 28 D5, PTC9, PTC8, PTB18,
takashikojo 5:e4891827f7b1 29 /* Alarm, Hour, Min, Tone */D13, D15, D14, PTC11
takashikojo 0:0689619f2486 30 ) ;
takashikojo 5:e4891827f7b1 31
takashikojo 5:e4891827f7b1 32 TrainStat trainstat(/* YAHOO_TRAIN*/ TOKYO_METRO) ;
takashikojo 4:8a2d1544d6e0 33 WeatherInfo weatherinfo(WEATHER_HACKS) ;
takashikojo 4:8a2d1544d6e0 34 HTTPClient httpClient;
takashikojo 5:e4891827f7b1 35 matchLine watchList ;
takashikojo 2:bcc1a40ffa04 36
takashikojo 1:1a28aaca601d 37 static void ntp(char *site)
takashikojo 1:1a28aaca601d 38 {
takashikojo 1:1a28aaca601d 39 NTPClient ntp;
takashikojo 1:1a28aaca601d 40
takashikojo 1:1a28aaca601d 41 alarmclock.setLED(33, 33) ;
takashikojo 1:1a28aaca601d 42 if (ntp.setTime(site) == 0) {
takashikojo 1:1a28aaca601d 43 alarmclock.flashLED() ;
takashikojo 1:1a28aaca601d 44 } else {
takashikojo 1:1a28aaca601d 45 alarmclock.setLED(0, 0) ;
takashikojo 1:1a28aaca601d 46 wait(30.0) ;
takashikojo 1:1a28aaca601d 47 return ;
takashikojo 1:1a28aaca601d 48 }
takashikojo 1:1a28aaca601d 49 }
takashikojo 0:0689619f2486 50
takashikojo 0:0689619f2486 51 void clock_main(void const *av)
takashikojo 0:0689619f2486 52 {
takashikojo 3:2cb03c287c22 53
takashikojo 5:e4891827f7b1 54 alarmclock.start() ;
takashikojo 5:e4891827f7b1 55 while(1) {
takashikojo 1:1a28aaca601d 56 alarmclock.poll() ;
takashikojo 5:e4891827f7b1 57 }
takashikojo 5:e4891827f7b1 58 printf("clock_main\n") ;
takashikojo 5:e4891827f7b1 59 }
takashikojo 5:e4891827f7b1 60
takashikojo 5:e4891827f7b1 61 #define FRONT0 0x06
takashikojo 5:e4891827f7b1 62 #define FRONT1 0x79
takashikojo 5:e4891827f7b1 63 #define FRONT2 0x49
takashikojo 5:e4891827f7b1 64 #define MIDDLE 0x49
takashikojo 5:e4891827f7b1 65 #define TAIL0 0x49
takashikojo 5:e4891827f7b1 66 #define TAIL1 0x4f
takashikojo 5:e4891827f7b1 67 #define TAIL2 0x30
takashikojo 5:e4891827f7b1 68 #define ROCK 0x5c
takashikojo 5:e4891827f7b1 69 #define BLANK 0x00
takashikojo 5:e4891827f7b1 70
takashikojo 5:e4891827f7b1 71 static unsigned int delayPtn[14][4] = {
takashikojo 5:e4891827f7b1 72 { BLANK, BLANK, BLANK, BLANK } ,
takashikojo 5:e4891827f7b1 73 { FRONT1, BLANK, BLANK, ROCK } ,
takashikojo 5:e4891827f7b1 74 { FRONT2, FRONT0,BLANK, ROCK } ,
takashikojo 5:e4891827f7b1 75 { TAIL0, FRONT1,BLANK, ROCK } ,
takashikojo 5:e4891827f7b1 76 { TAIL1, FRONT2, FRONT0,ROCK } ,
takashikojo 5:e4891827f7b1 77 { TAIL2, MIDDLE, FRONT1,ROCK } ,
takashikojo 5:e4891827f7b1 78 { TAIL2, MIDDLE, FRONT1,ROCK } ,
takashikojo 5:e4891827f7b1 79 { TAIL2, MIDDLE, FRONT1,ROCK } ,
takashikojo 5:e4891827f7b1 80 { TAIL2, MIDDLE, FRONT1,ROCK } ,
takashikojo 5:e4891827f7b1 81 { TAIL2, MIDDLE, FRONT1,ROCK } ,
takashikojo 5:e4891827f7b1 82 { TAIL2, MIDDLE, FRONT1,ROCK } ,
takashikojo 5:e4891827f7b1 83 { TAIL2, MIDDLE, FRONT1,ROCK } ,
takashikojo 5:e4891827f7b1 84 { TAIL2, MIDDLE, FRONT1,ROCK } ,
takashikojo 5:e4891827f7b1 85 { BLANK, BLANK, BLANK, BLANK }
takashikojo 5:e4891827f7b1 86 } ;
takashikojo 5:e4891827f7b1 87
takashikojo 5:e4891827f7b1 88 static unsigned int noDelayPtn[14][4] = {
takashikojo 5:e4891827f7b1 89
takashikojo 5:e4891827f7b1 90 // { MIDDLE, FRONT1,BLANK, BLANK } ,
takashikojo 5:e4891827f7b1 91 // { MIDDLE, FRONT2,FRONT0, BLANK } ,
takashikojo 5:e4891827f7b1 92 { BLANK, BLANK, BLANK, BLANK } ,
takashikojo 5:e4891827f7b1 93 { MIDDLE, FRONT1,BLANK, BLANK } ,
takashikojo 5:e4891827f7b1 94 { TAIL0, FRONT2,FRONT0, BLANK } ,
takashikojo 5:e4891827f7b1 95 { TAIL1, MIDDLE,FRONT1, BLANK } ,
takashikojo 5:e4891827f7b1 96 { TAIL2, TAIL0, FRONT2, FRONT0 } ,
takashikojo 5:e4891827f7b1 97 { BLANK, TAIL1, MIDDLE, FRONT1 } ,
takashikojo 5:e4891827f7b1 98 { BLANK, TAIL2, TAIL0, FRONT2 } ,
takashikojo 5:e4891827f7b1 99 { BLANK, BLANK, TAIL1, MIDDLE } ,
takashikojo 5:e4891827f7b1 100 { BLANK, BLANK, TAIL2, TAIL0 } ,
takashikojo 5:e4891827f7b1 101 { BLANK, BLANK, BLANK, TAIL1 } ,
takashikojo 5:e4891827f7b1 102 { BLANK, BLANK, BLANK, TAIL2 } ,
takashikojo 5:e4891827f7b1 103 { BLANK, BLANK, BLANK, BLANK } ,
takashikojo 5:e4891827f7b1 104 { BLANK, BLANK, BLANK, BLANK } ,
takashikojo 5:e4891827f7b1 105 { BLANK, BLANK, BLANK, BLANK }
takashikojo 5:e4891827f7b1 106 } ;
takashikojo 5:e4891827f7b1 107
takashikojo 5:e4891827f7b1 108 static void setLED_Tdelay(bool delay)
takashikojo 5:e4891827f7b1 109 {
takashikojo 5:e4891827f7b1 110 /* Delay/No Delay */
takashikojo 5:e4891827f7b1 111 // const unsigned int d[4] = { 0x6e, 0x77, 0x38, 0x5e} ;
takashikojo 5:e4891827f7b1 112 // const unsigned int n[4] = { 0x5c, 0x37, 0x38, 0x5e,} ;
takashikojo 5:e4891827f7b1 113 /*const unsigned int n[4] = { 0x09, 0x09, 0x29, 0x58} ;
takashikojo 5:e4891827f7b1 114 const unsigned int d[4] = { 0x09, 0x29, 0x58, 0x14} ;
takashikojo 5:e4891827f7b1 115 int index ;
takashikojo 5:e4891827f7b1 116
takashikojo 5:e4891827f7b1 117 if(delay){
takashikojo 5:e4891827f7b1 118 DBG("Delay\n") ;
takashikojo 5:e4891827f7b1 119 for(index=8*3; index<8*4; index++)
takashikojo 5:e4891827f7b1 120 alarmclock.setOptVal(index, d, 8) ;
takashikojo 5:e4891827f7b1 121 } else {
takashikojo 5:e4891827f7b1 122 DBG("No Delay\n") ;
takashikojo 5:e4891827f7b1 123 for(index=8*3; index<8*4; index++)
takashikojo 5:e4891827f7b1 124 alarmclock.setOptVal(index, n, 8) ;
takashikojo 5:e4891827f7b1 125 }
takashikojo 5:e4891827f7b1 126 */
takashikojo 5:e4891827f7b1 127 #define TRAIN_INDEX (8*4)
takashikojo 5:e4891827f7b1 128 for(int i=0; i<14; i++)
takashikojo 5:e4891827f7b1 129 alarmclock.setOptVal(i+TRAIN_INDEX, delay ? delayPtn[i] : noDelayPtn[i], 1) ;
takashikojo 5:e4891827f7b1 130 }
takashikojo 5:e4891827f7b1 131
takashikojo 5:e4891827f7b1 132 #define DUSH 0x40
takashikojo 5:e4891827f7b1 133 static void char2LED(const char *str, unsigned int *ptn) {
takashikojo 5:e4891827f7b1 134 int i ;
takashikojo 5:e4891827f7b1 135
takashikojo 5:e4891827f7b1 136 if(isdigit(str[0]) && isdigit(str[1])){
takashikojo 5:e4891827f7b1 137 i = atoi(str) ;
takashikojo 5:e4891827f7b1 138 ptn[0] = alarmclock.getPtn(i%10) ;
takashikojo 5:e4891827f7b1 139 ptn[1] = alarmclock.getPtn(i/10) ;
takashikojo 5:e4891827f7b1 140 } else {
takashikojo 5:e4891827f7b1 141 ptn[0] = DUSH ;
takashikojo 5:e4891827f7b1 142 ptn[1] = DUSH ;
takashikojo 5:e4891827f7b1 143 }
takashikojo 5:e4891827f7b1 144 }
takashikojo 5:e4891827f7b1 145
takashikojo 5:e4891827f7b1 146 static unsigned int precPtn[8][2] = {
takashikojo 5:e4891827f7b1 147 { 0x20, 0x00 } , { 0x30, 0x20 } , { 0x12, 0x30 } , { 0x06, 0x12 } ,
takashikojo 5:e4891827f7b1 148 { 0x24, 0x06 } , { 0x30, 0x24 } , { 0x10, 0x30 } , { 0x00, 0x10 }
takashikojo 5:e4891827f7b1 149
takashikojo 5:e4891827f7b1 150 } ;
takashikojo 5:e4891827f7b1 151
takashikojo 5:e4891827f7b1 152 #define PREC(j, led0, led1) { led0 = precPtn[j][0]; led1 = precPtn[j][1]; }
takashikojo 5:e4891827f7b1 153
takashikojo 5:e4891827f7b1 154 static void setLED_WInfo(const char *hi, const char *lo, const char *prec)
takashikojo 5:e4891827f7b1 155 {
takashikojo 5:e4891827f7b1 156 unsigned int hi_ptn[4] ;
takashikojo 5:e4891827f7b1 157 unsigned int lo_ptn[4] ;
takashikojo 5:e4891827f7b1 158 unsigned int prec_ptn[4] ;
takashikojo 5:e4891827f7b1 159 int index ;
takashikojo 5:e4891827f7b1 160
takashikojo 5:e4891827f7b1 161 DBG("hi=%s, lo=%s, prec=%s\n", hi, lo, prec) ;
takashikojo 5:e4891827f7b1 162
takashikojo 5:e4891827f7b1 163 char2LED(hi, hi_ptn) ;
takashikojo 5:e4891827f7b1 164 hi_ptn[2] = 0x10 ;
takashikojo 5:e4891827f7b1 165 hi_ptn[3] = 0x76 ;
takashikojo 5:e4891827f7b1 166 for(index=0; index<8; index++)
takashikojo 5:e4891827f7b1 167 alarmclock.setOptVal(index, hi_ptn, 8) ;
takashikojo 5:e4891827f7b1 168
takashikojo 5:e4891827f7b1 169 char2LED(lo, lo_ptn) ;
takashikojo 5:e4891827f7b1 170 lo_ptn[2] = 0x5c ;
takashikojo 5:e4891827f7b1 171 lo_ptn[3] = 0x38 ;
takashikojo 5:e4891827f7b1 172 for( ; index<16; index++)
takashikojo 5:e4891827f7b1 173 alarmclock.setOptVal(index, lo_ptn, 8) ;
takashikojo 5:e4891827f7b1 174
takashikojo 5:e4891827f7b1 175 #define PREC_INDEX (8*2)
takashikojo 5:e4891827f7b1 176 for(int i=0; i<16; i++) {
takashikojo 5:e4891827f7b1 177 char2LED(prec, prec_ptn) ;
takashikojo 5:e4891827f7b1 178 PREC(i%8, prec_ptn[3], prec_ptn[2]) ;
takashikojo 5:e4891827f7b1 179 alarmclock.setOptVal(i+PREC_INDEX, prec_ptn, 1) ;
takashikojo 5:e4891827f7b1 180 }
takashikojo 5:e4891827f7b1 181 }
takashikojo 5:e4891827f7b1 182
takashikojo 5:e4891827f7b1 183 static EthernetInterface eth ;
takashikojo 5:e4891827f7b1 184 #if 0
takashikojo 5:e4891827f7b1 185 static bool restartEth(void)
takashikojo 5:e4891827f7b1 186 {
takashikojo 5:e4891827f7b1 187 int ret ;
takashikojo 5:e4891827f7b1 188 printf("==== RESTARTING Ethernet ===\n") ;
takashikojo 5:e4891827f7b1 189 eth->disconnect();
takashikojo 5:e4891827f7b1 190 printf("eth.disconnect\n") ;
takashikojo 5:e4891827f7b1 191 /*free(eth) ;
takashikojo 5:e4891827f7b1 192 printf("free(eth)\n") ;
takashikojo 5:e4891827f7b1 193 eth = new EthernetInterface ;
takashikojo 5:e4891827f7b1 194 if(eth == NULL) {
takashikojo 5:e4891827f7b1 195 ERR("Ethernet can not Restart") ;
takashikojo 5:e4891827f7b1 196 return false ;
takashikojo 5:e4891827f7b1 197 }
takashikojo 5:e4891827f7b1 198 printf("new EthernetInterface|n") ;
takashikojo 5:e4891827f7b1 199 alarmclock.setLED(11, 11) ;
takashikojo 5:e4891827f7b1 200 ret = eth->init();*/
takashikojo 5:e4891827f7b1 201 alarmclock.setLED(22, 22) ;
takashikojo 5:e4891827f7b1 202 while(1) {
takashikojo 5:e4891827f7b1 203 ret = eth->connect();
takashikojo 5:e4891827f7b1 204 if(ret == 0)break ;
takashikojo 5:e4891827f7b1 205 Thread::wait(10);
takashikojo 5:e4891827f7b1 206 }
takashikojo 5:e4891827f7b1 207 return true ;
takashikojo 5:e4891827f7b1 208 }
takashikojo 5:e4891827f7b1 209 #endif
takashikojo 5:e4891827f7b1 210
takashikojo 4:8a2d1544d6e0 211 #define BUFF_SIZE 1024*64
takashikojo 4:8a2d1544d6e0 212 static char recvBuff[BUFF_SIZE] ;
takashikojo 2:bcc1a40ffa04 213 void net_main(void const *av)
takashikojo 2:bcc1a40ffa04 214 {
takashikojo 4:8a2d1544d6e0 215 int count = 0 ;
takashikojo 5:e4891827f7b1 216 time_t t ;
takashikojo 5:e4891827f7b1 217 char *p[1000] ; int i ;
takashikojo 5:e4891827f7b1 218 #define TEMP_SIZE 5
takashikojo 5:e4891827f7b1 219 char hiTemp[TEMP_SIZE], loTemp[TEMP_SIZE], prec[TEMP_SIZE] ;
takashikojo 5:e4891827f7b1 220
takashikojo 5:e4891827f7b1 221 watchList.getList("/sd/watchingLines.txt") ;
takashikojo 4:8a2d1544d6e0 222 trainstat.setBuff(recvBuff, BUFF_SIZE) ;
takashikojo 4:8a2d1544d6e0 223 weatherinfo.setBuff(recvBuff, BUFF_SIZE) ;
takashikojo 5:e4891827f7b1 224
takashikojo 2:bcc1a40ffa04 225 while(1) {
takashikojo 5:e4891827f7b1 226 setLED_Tdelay(trainstat.getStat()) ;
takashikojo 5:e4891827f7b1 227 if(count%1 ==0) {
takashikojo 5:e4891827f7b1 228 DBG("weatherinfo.getInfo") ;
takashikojo 5:e4891827f7b1 229 weatherinfo.getInfo(hiTemp, loTemp, prec) ;
takashikojo 5:e4891827f7b1 230 setLED_WInfo(hiTemp, loTemp, prec) ;
takashikojo 5:e4891827f7b1 231 }
takashikojo 5:e4891827f7b1 232 t = time(NULL);
takashikojo 5:e4891827f7b1 233 for( i=0; i<sizeof(p) ; i++) {
takashikojo 5:e4891827f7b1 234 p[i] = (char *)malloc(1000) ;
takashikojo 5:e4891827f7b1 235 if(p[i]==NULL) {
takashikojo 5:e4891827f7b1 236 printf("\nHeap=%d\n", (unsigned int)i) ;
takashikojo 5:e4891827f7b1 237 break ;
takashikojo 5:e4891827f7b1 238 } else printf("+") ;
takashikojo 5:e4891827f7b1 239 }
takashikojo 5:e4891827f7b1 240 for(i=0; p[i] != NULL; i++){ printf("-") ; free(p[i]) ;}
takashikojo 5:e4891827f7b1 241 printf("\n") ;
takashikojo 5:e4891827f7b1 242 Thread::wait(60) ;
takashikojo 5:e4891827f7b1 243 printf("wait=%d\n", time(NULL) - t ) ;
takashikojo 5:e4891827f7b1 244 count++ ;
takashikojo 4:8a2d1544d6e0 245 /*
takashikojo 5:e4891827f7b1 246 if((count % 1) == 0)
takashikojo 5:e4891827f7b1 247 if(restartEth() == false)break ;
takashikojo 4:8a2d1544d6e0 248 */
takashikojo 2:bcc1a40ffa04 249 }
takashikojo 5:e4891827f7b1 250 printf("net_main\n") ;
takashikojo 2:bcc1a40ffa04 251 }
takashikojo 5:e4891827f7b1 252
takashikojo 5:e4891827f7b1 253 int main()
takashikojo 5:e4891827f7b1 254 {
takashikojo 2:bcc1a40ffa04 255
takashikojo 0:0689619f2486 256 int ret ;
takashikojo 5:e4891827f7b1 257
takashikojo 5:e4891827f7b1 258 #define RESTART_TIMES 3
takashikojo 5:e4891827f7b1 259
takashikojo 5:e4891827f7b1 260 //eth = new EthernetInterface ;
takashikojo 0:0689619f2486 261 alarmclock.setLED(11, 11) ;
takashikojo 5:e4891827f7b1 262 ret = eth.init();
takashikojo 0:0689619f2486 263 alarmclock.setLED(22, 22) ;
takashikojo 0:0689619f2486 264 while(1) {
takashikojo 0:0689619f2486 265 ret = eth.connect();
takashikojo 0:0689619f2486 266 if(ret == 0)break ;
takashikojo 0:0689619f2486 267 Thread::wait(10);
takashikojo 0:0689619f2486 268 }
takashikojo 3:2cb03c287c22 269 ntp("ntp.jst.mfeed.ad.jp") ;
takashikojo 3:2cb03c287c22 270
takashikojo 5:e4891827f7b1 271 #define NET_STACK 24000
takashikojo 5:e4891827f7b1 272 Thread t_clock(clock_main, NULL, osPriorityHigh);
takashikojo 1:1a28aaca601d 273 Thread t_net (net_main, NULL, osPriorityNormal, NET_STACK );
takashikojo 5:e4891827f7b1 274
takashikojo 0:0689619f2486 275 while(1)Thread::wait(1000);
takashikojo 0:0689619f2486 276 }