Smart Clock

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

Committer:
takashikojo
Date:
Sun Nov 22 08:52:05 2015 +0000
Revision:
7:53dd54df1441
Parent:
5:e4891827f7b1
Cleaned up for publishing

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 7:53dd54df1441 25 /* Segment 0 - 6, Dot */ PTB19,PTC0, D3, D1, D0, PTC1, D4, D2,
takashikojo 7:53dd54df1441 26 /* Digit 1 - 4 */ D5, PTC9, PTC8, PTB18,
takashikojo 7:53dd54df1441 27 /* Alarm, Hour, Min, Tone */ D13, D15, D14, PTC11
takashikojo 0:0689619f2486 28 ) ;
takashikojo 5:e4891827f7b1 29
takashikojo 5:e4891827f7b1 30 TrainStat trainstat(/* YAHOO_TRAIN*/ TOKYO_METRO) ;
takashikojo 4:8a2d1544d6e0 31 WeatherInfo weatherinfo(WEATHER_HACKS) ;
takashikojo 4:8a2d1544d6e0 32 HTTPClient httpClient;
takashikojo 5:e4891827f7b1 33 matchLine watchList ;
takashikojo 2:bcc1a40ffa04 34
takashikojo 1:1a28aaca601d 35 static void ntp(char *site)
takashikojo 1:1a28aaca601d 36 {
takashikojo 1:1a28aaca601d 37 NTPClient ntp;
takashikojo 1:1a28aaca601d 38
takashikojo 1:1a28aaca601d 39 alarmclock.setLED(33, 33) ;
takashikojo 1:1a28aaca601d 40 if (ntp.setTime(site) == 0) {
takashikojo 1:1a28aaca601d 41 alarmclock.flashLED() ;
takashikojo 1:1a28aaca601d 42 } else {
takashikojo 1:1a28aaca601d 43 alarmclock.setLED(0, 0) ;
takashikojo 1:1a28aaca601d 44 wait(30.0) ;
takashikojo 1:1a28aaca601d 45 return ;
takashikojo 1:1a28aaca601d 46 }
takashikojo 1:1a28aaca601d 47 }
takashikojo 0:0689619f2486 48
takashikojo 0:0689619f2486 49 void clock_main(void const *av)
takashikojo 0:0689619f2486 50 {
takashikojo 3:2cb03c287c22 51
takashikojo 5:e4891827f7b1 52 alarmclock.start() ;
takashikojo 5:e4891827f7b1 53 while(1) {
takashikojo 1:1a28aaca601d 54 alarmclock.poll() ;
takashikojo 5:e4891827f7b1 55 }
takashikojo 7:53dd54df1441 56
takashikojo 5:e4891827f7b1 57 }
takashikojo 5:e4891827f7b1 58
takashikojo 7:53dd54df1441 59 #if defined(ANIMATION)
takashikojo 5:e4891827f7b1 60 #define FRONT0 0x06
takashikojo 5:e4891827f7b1 61 #define FRONT1 0x79
takashikojo 5:e4891827f7b1 62 #define FRONT2 0x49
takashikojo 5:e4891827f7b1 63 #define MIDDLE 0x49
takashikojo 5:e4891827f7b1 64 #define TAIL0 0x49
takashikojo 7:53dd54df1441 65
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 { BLANK, BLANK, BLANK, BLANK } ,
takashikojo 5:e4891827f7b1 90 { MIDDLE, FRONT1,BLANK, BLANK } ,
takashikojo 5:e4891827f7b1 91 { TAIL0, FRONT2,FRONT0, BLANK } ,
takashikojo 5:e4891827f7b1 92 { TAIL1, MIDDLE,FRONT1, BLANK } ,
takashikojo 5:e4891827f7b1 93 { TAIL2, TAIL0, FRONT2, FRONT0 } ,
takashikojo 5:e4891827f7b1 94 { BLANK, TAIL1, MIDDLE, FRONT1 } ,
takashikojo 5:e4891827f7b1 95 { BLANK, TAIL2, TAIL0, FRONT2 } ,
takashikojo 5:e4891827f7b1 96 { BLANK, BLANK, TAIL1, MIDDLE } ,
takashikojo 5:e4891827f7b1 97 { BLANK, BLANK, TAIL2, TAIL0 } ,
takashikojo 5:e4891827f7b1 98 { BLANK, BLANK, BLANK, TAIL1 } ,
takashikojo 5:e4891827f7b1 99 { BLANK, BLANK, BLANK, TAIL2 } ,
takashikojo 5:e4891827f7b1 100 { BLANK, BLANK, BLANK, BLANK } ,
takashikojo 5:e4891827f7b1 101 { BLANK, BLANK, BLANK, BLANK } ,
takashikojo 5:e4891827f7b1 102 { BLANK, BLANK, BLANK, BLANK }
takashikojo 5:e4891827f7b1 103 } ;
takashikojo 7:53dd54df1441 104 #endif
takashikojo 7:53dd54df1441 105
takashikojo 5:e4891827f7b1 106 static void setLED_Tdelay(bool delay)
takashikojo 5:e4891827f7b1 107 {
takashikojo 7:53dd54df1441 108 #if !defined(ANIMATION)
takashikojo 5:e4891827f7b1 109 /* Delay/No Delay */
takashikojo 7:53dd54df1441 110 const unsigned int d[4] = { 0x6e, 0x77, 0x38, 0x5e} ;
takashikojo 7:53dd54df1441 111 const unsigned int n[4] = { 0x38, 0x5e, 0x5c, 0x37} ;
takashikojo 7:53dd54df1441 112
takashikojo 5:e4891827f7b1 113 int index ;
takashikojo 5:e4891827f7b1 114
takashikojo 5:e4891827f7b1 115 if(delay){
takashikojo 5:e4891827f7b1 116 DBG("Delay\n") ;
takashikojo 7:53dd54df1441 117 for(index=8*4; index<8*5; index++)
takashikojo 5:e4891827f7b1 118 alarmclock.setOptVal(index, d, 8) ;
takashikojo 5:e4891827f7b1 119 } else {
takashikojo 5:e4891827f7b1 120 DBG("No Delay\n") ;
takashikojo 7:53dd54df1441 121 for(index=8*4; index<8*5; index++)
takashikojo 5:e4891827f7b1 122 alarmclock.setOptVal(index, n, 8) ;
takashikojo 5:e4891827f7b1 123 }
takashikojo 7:53dd54df1441 124 #else
takashikojo 5:e4891827f7b1 125 #define TRAIN_INDEX (8*4)
takashikojo 5:e4891827f7b1 126 for(int i=0; i<14; i++)
takashikojo 5:e4891827f7b1 127 alarmclock.setOptVal(i+TRAIN_INDEX, delay ? delayPtn[i] : noDelayPtn[i], 1) ;
takashikojo 7:53dd54df1441 128 #endif
takashikojo 7:53dd54df1441 129
takashikojo 5:e4891827f7b1 130 }
takashikojo 5:e4891827f7b1 131
takashikojo 7:53dd54df1441 132 #define DUSH 0x40
takashikojo 7:53dd54df1441 133 #define BLANK 0x00
takashikojo 5:e4891827f7b1 134 static void char2LED(const char *str, unsigned int *ptn) {
takashikojo 5:e4891827f7b1 135 int i ;
takashikojo 5:e4891827f7b1 136
takashikojo 5:e4891827f7b1 137 if(isdigit(str[0]) && isdigit(str[1])){
takashikojo 5:e4891827f7b1 138 i = atoi(str) ;
takashikojo 5:e4891827f7b1 139 ptn[0] = alarmclock.getPtn(i%10) ;
takashikojo 5:e4891827f7b1 140 ptn[1] = alarmclock.getPtn(i/10) ;
takashikojo 7:53dd54df1441 141 } else if((str[0] == ' ')&&(str[1] == '0')) {
takashikojo 7:53dd54df1441 142 ptn[0] = alarmclock.getPtn(0) ;
takashikojo 7:53dd54df1441 143 ptn[1] = BLANK ;
takashikojo 5:e4891827f7b1 144 } else {
takashikojo 5:e4891827f7b1 145 ptn[0] = DUSH ;
takashikojo 5:e4891827f7b1 146 ptn[1] = DUSH ;
takashikojo 5:e4891827f7b1 147 }
takashikojo 5:e4891827f7b1 148 }
takashikojo 5:e4891827f7b1 149
takashikojo 5:e4891827f7b1 150 static unsigned int precPtn[8][2] = {
takashikojo 5:e4891827f7b1 151 { 0x20, 0x00 } , { 0x30, 0x20 } , { 0x12, 0x30 } , { 0x06, 0x12 } ,
takashikojo 5:e4891827f7b1 152 { 0x24, 0x06 } , { 0x30, 0x24 } , { 0x10, 0x30 } , { 0x00, 0x10 }
takashikojo 5:e4891827f7b1 153
takashikojo 5:e4891827f7b1 154 } ;
takashikojo 5:e4891827f7b1 155
takashikojo 5:e4891827f7b1 156 #define PREC(j, led0, led1) { led0 = precPtn[j][0]; led1 = precPtn[j][1]; }
takashikojo 5:e4891827f7b1 157
takashikojo 5:e4891827f7b1 158 static void setLED_WInfo(const char *hi, const char *lo, const char *prec)
takashikojo 5:e4891827f7b1 159 {
takashikojo 5:e4891827f7b1 160 unsigned int hi_ptn[4] ;
takashikojo 5:e4891827f7b1 161 unsigned int lo_ptn[4] ;
takashikojo 5:e4891827f7b1 162 unsigned int prec_ptn[4] ;
takashikojo 5:e4891827f7b1 163 int index ;
takashikojo 5:e4891827f7b1 164
takashikojo 5:e4891827f7b1 165 DBG("hi=%s, lo=%s, prec=%s\n", hi, lo, prec) ;
takashikojo 5:e4891827f7b1 166
takashikojo 5:e4891827f7b1 167 char2LED(hi, hi_ptn) ;
takashikojo 5:e4891827f7b1 168 hi_ptn[2] = 0x10 ;
takashikojo 5:e4891827f7b1 169 hi_ptn[3] = 0x76 ;
takashikojo 5:e4891827f7b1 170 for(index=0; index<8; index++)
takashikojo 5:e4891827f7b1 171 alarmclock.setOptVal(index, hi_ptn, 8) ;
takashikojo 5:e4891827f7b1 172
takashikojo 5:e4891827f7b1 173 char2LED(lo, lo_ptn) ;
takashikojo 5:e4891827f7b1 174 lo_ptn[2] = 0x5c ;
takashikojo 5:e4891827f7b1 175 lo_ptn[3] = 0x38 ;
takashikojo 5:e4891827f7b1 176 for( ; index<16; index++)
takashikojo 5:e4891827f7b1 177 alarmclock.setOptVal(index, lo_ptn, 8) ;
takashikojo 5:e4891827f7b1 178
takashikojo 5:e4891827f7b1 179 #define PREC_INDEX (8*2)
takashikojo 5:e4891827f7b1 180 for(int i=0; i<16; i++) {
takashikojo 5:e4891827f7b1 181 char2LED(prec, prec_ptn) ;
takashikojo 5:e4891827f7b1 182 PREC(i%8, prec_ptn[3], prec_ptn[2]) ;
takashikojo 5:e4891827f7b1 183 alarmclock.setOptVal(i+PREC_INDEX, prec_ptn, 1) ;
takashikojo 5:e4891827f7b1 184 }
takashikojo 5:e4891827f7b1 185 }
takashikojo 5:e4891827f7b1 186
takashikojo 4:8a2d1544d6e0 187 #define BUFF_SIZE 1024*64
takashikojo 4:8a2d1544d6e0 188 static char recvBuff[BUFF_SIZE] ;
takashikojo 2:bcc1a40ffa04 189 void net_main(void const *av)
takashikojo 2:bcc1a40ffa04 190 {
takashikojo 4:8a2d1544d6e0 191 int count = 0 ;
takashikojo 5:e4891827f7b1 192 #define TEMP_SIZE 5
takashikojo 5:e4891827f7b1 193 char hiTemp[TEMP_SIZE], loTemp[TEMP_SIZE], prec[TEMP_SIZE] ;
takashikojo 5:e4891827f7b1 194
takashikojo 5:e4891827f7b1 195 watchList.getList("/sd/watchingLines.txt") ;
takashikojo 4:8a2d1544d6e0 196 trainstat.setBuff(recvBuff, BUFF_SIZE) ;
takashikojo 4:8a2d1544d6e0 197 weatherinfo.setBuff(recvBuff, BUFF_SIZE) ;
takashikojo 5:e4891827f7b1 198
takashikojo 2:bcc1a40ffa04 199 while(1) {
takashikojo 7:53dd54df1441 200 if(count%60 ==0){
takashikojo 7:53dd54df1441 201 int stat = trainstat.getStat() ;
takashikojo 7:53dd54df1441 202 setLED_Tdelay(stat) ;
takashikojo 7:53dd54df1441 203 if(stat)alarmclock.alarmAhead(1*60*60) ;
takashikojo 7:53dd54df1441 204 else alarmclock.alarmAhead(0) ;
takashikojo 7:53dd54df1441 205
takashikojo 7:53dd54df1441 206 }
takashikojo 7:53dd54df1441 207 if(count%60 ==0) {
takashikojo 5:e4891827f7b1 208 DBG("weatherinfo.getInfo") ;
takashikojo 5:e4891827f7b1 209 weatherinfo.getInfo(hiTemp, loTemp, prec) ;
takashikojo 5:e4891827f7b1 210 setLED_WInfo(hiTemp, loTemp, prec) ;
takashikojo 5:e4891827f7b1 211 }
takashikojo 7:53dd54df1441 212 if(count%60*60*24)
takashikojo 7:53dd54df1441 213 ntp("ntp.jst.mfeed.ad.jp") ;
takashikojo 5:e4891827f7b1 214 count++ ;
takashikojo 7:53dd54df1441 215 wait(1.0) ;
takashikojo 2:bcc1a40ffa04 216 }
takashikojo 2:bcc1a40ffa04 217 }
takashikojo 5:e4891827f7b1 218
takashikojo 5:e4891827f7b1 219 int main()
takashikojo 5:e4891827f7b1 220 {
takashikojo 2:bcc1a40ffa04 221
takashikojo 0:0689619f2486 222 int ret ;
takashikojo 7:53dd54df1441 223 EthernetInterface eth ;
takashikojo 5:e4891827f7b1 224 #define RESTART_TIMES 3
takashikojo 5:e4891827f7b1 225
takashikojo 0:0689619f2486 226 alarmclock.setLED(11, 11) ;
takashikojo 5:e4891827f7b1 227 ret = eth.init();
takashikojo 0:0689619f2486 228 alarmclock.setLED(22, 22) ;
takashikojo 0:0689619f2486 229 while(1) {
takashikojo 0:0689619f2486 230 ret = eth.connect();
takashikojo 0:0689619f2486 231 if(ret == 0)break ;
takashikojo 0:0689619f2486 232 Thread::wait(10);
takashikojo 0:0689619f2486 233 }
takashikojo 3:2cb03c287c22 234 ntp("ntp.jst.mfeed.ad.jp") ;
takashikojo 3:2cb03c287c22 235
takashikojo 5:e4891827f7b1 236 #define NET_STACK 24000
takashikojo 5:e4891827f7b1 237 Thread t_clock(clock_main, NULL, osPriorityHigh);
takashikojo 1:1a28aaca601d 238 Thread t_net (net_main, NULL, osPriorityNormal, NET_STACK );
takashikojo 5:e4891827f7b1 239
takashikojo 0:0689619f2486 240 while(1)Thread::wait(1000);
takashikojo 0:0689619f2486 241 }