Smart Clock

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

main.txt

Committer:
takashikojo
Date:
2015-10-23
Revision:
5:e4891827f7b1

File content as of revision 5:e4891827f7b1:

#include "mbed.h"
#include <ctype.h>

#include "EthernetInterface.h"
#include "HTTPClient.h"
#include "NTPClient.h"
#include "AlarmClock.h"
#include "TrainStat.h"
#include "WeatherInfo.h"
#include "matchLine.h"
  
#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 , 
    /* Digit 1 - 4        */    D5, D7, D9, D12, 
    /* Alarm, Hour, Min, Tone */D13, D14, D15, PTC11
)  ;

TrainStat   trainstat(TOKYO_METRO /*YAHOO_TRAIN*/) ;
WeatherInfo weatherinfo(WEATHER_HACKS) ;
HTTPClient  httpClient;
matchLine   watchList ;

static void ntp(char *site)
{
    NTPClient ntp;

    alarmclock.setLED(33, 33) ;
    if (ntp.setTime(site) == 0) {
        alarmclock.flashLED() ;
    } else {
        alarmclock.setLED(0, 0) ;
        wait(30.0) ;
        return ;
    }
}

void clock_main(void const *av)
{

    alarmclock.start() ; 
    while(1){        
        alarmclock.poll() ;
        Thread::wait(100);
    } 
} 

#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) ;
    }
}

#define BUFF_SIZE 1024*64
static char recvBuff[BUFF_SIZE] ;
void net_main(void const *av)
{
    int count = 0 ;
    time_t t ; 
    bool sw = true ;
    #define TEMP_SIZE 5
    char hiTemp[TEMP_SIZE], loTemp[TEMP_SIZE], prec[TEMP_SIZE] ;
    char *p ;
    
    watchList.getList("/sd/watchingLines.txt") ;
    trainstat.setBuff(recvBuff, BUFF_SIZE) ;
    weatherinfo.setBuff(recvBuff, BUFF_SIZE) ;

    while(1) {
        sw = !sw ;
        trainstat.getStat() ;
        setLED_Tdelay(sw) ;
        if(count%1 ==0){
            DBG("weatherinfo.getInfo") ;
            weatherinfo.getInfo(hiTemp, loTemp, prec) ;
            setLED_WInfo(hiTemp, loTemp, prec) ;
        }
        p = (char *)malloc(10000) ;
        printf("Heap=%x\n", (unsigned int)p) ;
        free(p) ;
        t = time(NULL); 
        wait(60.0) ;
        printf("wait=%d\n", time(NULL) - t ) ;
        count++ ;
    }
}
 
int main() {
    EthernetInterface eth;
    int ret ;
    
    alarmclock.setLED(11, 11) ;
    ret = eth.init(); 
    alarmclock.setLED(22, 22) ;
    while(1) {
        ret = eth.connect();
        if(ret == 0)break ;
        Thread::wait(10);
    }
    ntp("ntp.jst.mfeed.ad.jp") ;

    #define NET_STACK   24000
    Thread t_clock(clock_main, NULL, osPriorityNormal);
    Thread t_net  (net_main,   NULL, osPriorityNormal, NET_STACK  );
    while(1)Thread::wait(1000);
}