Smart Clock

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

Revision:
5:e4891827f7b1
Parent:
4:8a2d1544d6e0
Child:
7:53dd54df1441
--- a/main.cpp	Sun Jun 28 00:17:22 2015 +0000
+++ b/main.cpp	Fri Oct 23 05:52:37 2015 +0000
@@ -1,4 +1,5 @@
 #include "mbed.h"
+#include <ctype.h>
 
 #include "EthernetInterface.h"
 #include "HTTPClient.h"
@@ -6,16 +7,32 @@
 #include "AlarmClock.h"
 #include "TrainStat.h"
 #include "WeatherInfo.h"
+#include "matchLine.h"
 
-AlarmClock alarmclock (                          
-    /* Segment 0 - 6, Dot */    D11,D6, D3, D1, D0, D10,D4, D2 , 
-    /* Digit 1 - 4        */    D5, D7, D9, D12, 
-    /* Alarm, Hour, Min, Tone */D13, D14, D15, PTC11
+#if 0
+//Enable debug
+#define DBG(x, ...) std::printf("[Main : DBG]"x"\r\n", ##__VA_ARGS__);
+#define WARN(x, ...) std::printf("[Main : WARN]"x"\r\n", ##__VA_ARGS__);
+#else
+//Disable debug
+#define DBG(x, ...)
+#define WARN(x, ...)
+#endif
+
+#define ERR(x, ...) std::printf("[Main : ERR]"x"\r\n", ##__VA_ARGS__);
+
+AlarmClock alarmclock (
+    /* Segment 0 - 6, Dot */    //D11,D6, D3, D1, D0, D10,D4, D2 ,
+                                  PTB19,PTC0, D3, D1, D0, PTC1, D4, D2,
+    /* Digit 1 - 4        */    //D5, D7, D9, D12,
+                                  D5, PTC9, PTC8, PTB18,
+    /* Alarm, Hour, Min, Tone */D13, D15, D14, PTC11
 )  ;
- 
-TrainStat   trainstat(YAHOO_TRAIN) ;
+
+TrainStat   trainstat(/* YAHOO_TRAIN*/ TOKYO_METRO) ;
 WeatherInfo weatherinfo(WEATHER_HACKS) ;
 HTTPClient httpClient;
+matchLine   watchList ;
 
 static void ntp(char *site)
 {
@@ -34,47 +51,215 @@
 void clock_main(void const *av)
 {
 
-    alarmclock.start() ; 
-    while(1){        
+    alarmclock.start() ;
+    while(1) {
         alarmclock.poll() ;
-        Thread::wait(100);
-    } 
-} 
+    }
+    printf("clock_main\n") ;
+}
+
+#define FRONT0 0x06
+#define FRONT1 0x79 
+#define FRONT2 0x49
+#define MIDDLE 0x49
+#define TAIL0  0x49
+#define TAIL1  0x4f
+#define TAIL2  0x30
+#define ROCK   0x5c
+#define BLANK  0x00
+
+static unsigned int delayPtn[14][4] = {
+    { BLANK,  BLANK, BLANK,  BLANK } ,
+    { FRONT1, BLANK, BLANK, ROCK } ,
+    { FRONT2, FRONT0,BLANK, ROCK } ,
+    { TAIL0, FRONT1,BLANK, ROCK } ,
+    { TAIL1, FRONT2, FRONT0,ROCK } ,
+    { TAIL2, MIDDLE, FRONT1,ROCK } ,
+    { TAIL2, MIDDLE, FRONT1,ROCK } ,
+    { TAIL2, MIDDLE, FRONT1,ROCK } ,
+    { TAIL2, MIDDLE, FRONT1,ROCK } ,
+    { TAIL2, MIDDLE, FRONT1,ROCK } ,
+    { TAIL2, MIDDLE, FRONT1,ROCK } ,
+    { TAIL2, MIDDLE, FRONT1,ROCK } ,
+    { TAIL2, MIDDLE, FRONT1,ROCK } ,
+    { BLANK,  BLANK, BLANK,  BLANK } 
+} ;
+
+static unsigned int noDelayPtn[14][4] = {
+    
+//    { MIDDLE, FRONT1,BLANK,  BLANK } ,
+//    { MIDDLE, FRONT2,FRONT0, BLANK } ,
+    { BLANK,  BLANK, BLANK,  BLANK } ,
+    { MIDDLE, FRONT1,BLANK,  BLANK } ,
+    { TAIL0,  FRONT2,FRONT0, BLANK } ,
+    { TAIL1,  MIDDLE,FRONT1, BLANK } ,
+    { TAIL2,  TAIL0, FRONT2, FRONT0 } ,
+    { BLANK,  TAIL1, MIDDLE, FRONT1 } ,
+    { BLANK,  TAIL2, TAIL0,  FRONT2 } ,
+    { BLANK,  BLANK, TAIL1,  MIDDLE  } ,
+    { BLANK,  BLANK, TAIL2,  TAIL0 } ,
+    { BLANK,  BLANK, BLANK,  TAIL1 } ,
+    { BLANK,  BLANK, BLANK,  TAIL2 } ,
+    { BLANK,  BLANK, BLANK,  BLANK } ,
+    { BLANK,  BLANK, BLANK,  BLANK } ,
+    { BLANK,  BLANK, BLANK,  BLANK } 
+} ;
+       
+static void setLED_Tdelay(bool delay)
+{
+/* Delay/No Delay */
+//    const unsigned int d[4] = { 0x6e, 0x77, 0x38, 0x5e} ;
+//    const unsigned int n[4] = { 0x5c, 0x37, 0x38, 0x5e,} ;
+/*const unsigned int n[4] = { 0x09, 0x09, 0x29, 0x58} ;
+const unsigned int d[4] = { 0x09, 0x29, 0x58, 0x14} ;
+    int index ;
+
+    if(delay){
+         DBG("Delay\n") ;
+         for(index=8*3; index<8*4; index++)
+             alarmclock.setOptVal(index, d, 8) ;
+    } else {
+         DBG("No Delay\n") ;
+         for(index=8*3; index<8*4; index++)
+             alarmclock.setOptVal(index, n, 8) ;
+    }
+*/
+    #define TRAIN_INDEX (8*4)
+    for(int i=0; i<14; i++)
+        alarmclock.setOptVal(i+TRAIN_INDEX, delay ? delayPtn[i] : noDelayPtn[i], 1) ;
+}
+ 
+#define DUSH 0x40
+static void char2LED(const char *str, unsigned int *ptn) {
+    int i ;
+    
+    if(isdigit(str[0]) && isdigit(str[1])){
+        i = atoi(str) ; 
+        ptn[0] = alarmclock.getPtn(i%10) ;
+        ptn[1] = alarmclock.getPtn(i/10) ;
+    } else {
+        ptn[0] = DUSH ;
+        ptn[1] = DUSH ;
+    }
+}
+
+static unsigned int precPtn[8][2] = {
+    { 0x20, 0x00 } ,    { 0x30, 0x20 } ,    { 0x12, 0x30 } ,    { 0x06, 0x12 } ,
+    { 0x24, 0x06 } ,    { 0x30, 0x24 } ,    { 0x10, 0x30 } ,    { 0x00, 0x10 } 
+
+} ;
+
+#define PREC(j, led0, led1) { led0 = precPtn[j][0]; led1 = precPtn[j][1]; }
+ 
+static void setLED_WInfo(const char *hi, const char *lo, const char *prec)
+{
+    unsigned int hi_ptn[4] ;
+    unsigned int lo_ptn[4] ; 
+    unsigned int prec_ptn[4] ;
+    int index ;
+
+    DBG("hi=%s, lo=%s, prec=%s\n", hi, lo, prec) ;
+
+    char2LED(hi, hi_ptn) ;
+    hi_ptn[2] = 0x10 ;
+    hi_ptn[3] = 0x76 ; 
+    for(index=0; index<8; index++)
+        alarmclock.setOptVal(index, hi_ptn, 8) ;
+      
+    char2LED(lo, lo_ptn) ;
+    lo_ptn[2] = 0x5c ;
+    lo_ptn[3] = 0x38 ; 
+    for( ; index<16; index++)
+    alarmclock.setOptVal(index, lo_ptn, 8) ;
+
+    #define PREC_INDEX (8*2)
+    for(int i=0; i<16; i++) {
+        char2LED(prec, prec_ptn) ;
+        PREC(i%8, prec_ptn[3], prec_ptn[2]) ;
+        alarmclock.setOptVal(i+PREC_INDEX, prec_ptn, 1) ;
+    }
+}
+
+static     EthernetInterface eth ;
+#if 0
+static bool restartEth(void)
+{
+    int ret ;
+    printf("==== RESTARTING Ethernet ===\n") ;
+    eth->disconnect();
+    printf("eth.disconnect\n") ;
+    /*free(eth) ;
+    printf("free(eth)\n") ;
+    eth = new EthernetInterface ;
+    if(eth == NULL) {
+        ERR("Ethernet can not Restart") ;
+        return false ;
+    }
+    printf("new EthernetInterface|n") ;
+    alarmclock.setLED(11, 11) ;
+    ret = eth->init();*/
+    alarmclock.setLED(22, 22) ;
+    while(1) {
+        ret = eth->connect();
+        if(ret == 0)break ;
+        Thread::wait(10);
+    }
+    return true ;
+}
+#endif
+
 #define BUFF_SIZE 1024*64
 static char recvBuff[BUFF_SIZE] ;
 void net_main(void const *av)
 {
-    bool sw ;
     int count = 0 ;
-    
+    time_t t ; 
+    char *p[1000] ; int i ;
+#define TEMP_SIZE 5
+    char hiTemp[TEMP_SIZE], loTemp[TEMP_SIZE], prec[TEMP_SIZE] ;
+
+    watchList.getList("/sd/watchingLines.txt") ;
     trainstat.setBuff(recvBuff, BUFF_SIZE) ;
     weatherinfo.setBuff(recvBuff, BUFF_SIZE) ;
-    
-    trainstat.setLine("千代田線") ;
+
     while(1) {
-
-        sw = !sw ;
+        setLED_Tdelay(trainstat.getStat()) ;
+        if(count%1 ==0) {
+            DBG("weatherinfo.getInfo") ;
+            weatherinfo.getInfo(hiTemp, loTemp, prec) ;
+            setLED_WInfo(hiTemp, loTemp, prec) ;
+        }
+        t = time(NULL);
+        for( i=0; i<sizeof(p) ; i++) {
+            p[i] = (char *)malloc(1000) ;
+            if(p[i]==NULL) {
+                printf("\nHeap=%d\n", (unsigned int)i) ;
+                break ;
+            } else printf("+") ;
+        }
+        for(i=0; p[i] != NULL; i++){ printf("-") ; free(p[i]) ;}
+        printf("\n") ;
+        Thread::wait(60) ;
+        printf("wait=%d\n", time(NULL) - t ) ;
+        count++ ;
         /*
-        if(!trainstat.getStat())
-        {    printf("遅れあり\n") ; alarmclock.setBlink(true) ; }
-        else
-        {    printf("遅れなし\n") ; alarmclock.setBlink(false) ; }
+        if((count % 1) == 0)
+            if(restartEth() == false)break ;
         */
-        trainstat.getStat() ;
-        if(count%1 ==0)
-            weatherinfo.getInfo() ;
-        
-        wait(60.0) ;
-        count++ ;
     }
+    printf("net_main\n") ;
 }
+ 
+int main()
+{
 
-int main() {
-    EthernetInterface eth;
     int ret ;
-    
+
+#define RESTART_TIMES 3
+
+    //eth =  new EthernetInterface ;
     alarmclock.setLED(11, 11) ;
-    ret = eth.init(); 
+    ret = eth.init();
     alarmclock.setLED(22, 22) ;
     while(1) {
         ret = eth.connect();
@@ -83,8 +268,9 @@
     }
     ntp("ntp.jst.mfeed.ad.jp") ;
 
-    #define NET_STACK   16000
-    Thread t_clock(clock_main, NULL, osPriorityNormal);
+#define NET_STACK   24000
+    Thread t_clock(clock_main, NULL, osPriorityHigh);
     Thread t_net  (net_main,   NULL, osPriorityNormal, NET_STACK  );
+
     while(1)Thread::wait(1000);
 }