平成24年中国四国技術職員研修用

Dependencies:   MSCUsbHost NetServices RPCInterface TextLCD mbed

main.cpp

Committer:
yueee_yt
Date:
2012-08-22
Revision:
1:f2088fbce73f
Parent:
0:fd83f3540b1a

File content as of revision 1:f2088fbce73f:

#include "mbed.h"
#include "MSCFileSystem.h"
#include "TextLCD.h"
#include "EthernetNetIf.h"
#include "NTPClient.h"
#include "HTTPServer.h"
#include "RPCVariable.h"
#include "RPCFunction.h"

#define NTPServer "ntp.cc.yamaguchi-u.ac.jp"

#if 1
// Use DHCP
EthernetNetIf ethif;
#else
// Use "static IP address" (Parameters:IP, Subnet mask, Gateway, DNS)
EthernetNetIf ethif(IpAddr(xx,xx,xx,xx), IpAddr(xx,xx,xx,xx), IpAddr(xx,xx,xx,xx), IpAddr(xx,xx,xx,xx));
#endif

TextLCD lcd(p24, p26, p27, p28, p29, p30);
MSCFileSystem msc("usb");
LocalFileSystem local("local");
DigitalOut led1(LED1,"led1");
DigitalOut led2(LED2,"led2");
DigitalOut led3(LED3,"led3");
DigitalOut led4(LED4,"led4");
AnalogIn ain(p15,"ain");
float temp;
time_t ctTime;

void lastdate(char *input,char *output);
NTPClient ntp;
HTTPServer svr;

RPCVariable<float> rpcv(&temp,"Temp");
RPCFunction rpcf(&lastdate,"LastDate");

void lastdate(char *input,char *output)
{
    if (input[0]=='0')led4=0;
    if (input[0]=='1')led4=1;
    char ldate[32];
    strftime(ldate,32,"%x %X",localtime(&ctTime));
    strcpy(output,ldate);
}

void mesure_temp()
{
    char buffer[9];
    temp=ain*3.3*100.0;

    ctTime = time(NULL);
    strftime(buffer,9,"%X",localtime(&ctTime));
    lcd.locate(0,1);
    lcd.printf("%s %4.1fDeg",buffer,temp);

    char filename[23];
    strftime(filename,23,"/usb/data/%Y%m%d.dat",localtime(&ctTime));
    FILE *fp= fopen(filename, "a");
    if ( fp == NULL ) {
        printf("Could not open file for write\n");
    } else {
        fprintf(fp, "%s , %4.1f \r\n",buffer,temp);
    }
    fclose(fp);
}

int main()
{
    time_t ctTime;
    char buffer[32];

    lcd.cls();
    lcd.locate(0,0);
    lcd.printf("Program Start");

    if (ethif.setup()) {
        error("Ethernet setup failed.");
        return 1;
    }

    IpAddr ethIp=ethif.getIp();
    lcd.locate(0,0);
    lcd.printf("%3d.%3d.%3d.%3d", ethIp[0], ethIp[1], ethIp[2], ethIp[3]);
    Host server(IpAddr(), 123, NTPServer);
    ntp.setTime(server);
    //UTC-->JST +9Hour(32400Sec)
    ctTime = time(NULL);
    ctTime+=32400;
    set_time(ctTime);
    ctTime = time(NULL);
    strftime(buffer,32,"%x %X",localtime(&ctTime));
    printf("%s \n",buffer);
    printf("finish \n");

    Base::add_rpc_class<DigitalOut>();
    Base::add_rpc_class<AnalogIn>();
    FSHandler::mount("/local", "/" );
    svr.addHandler<RPCHandler>("/rpc");
    svr.addHandler<FSHandler>("/");
    svr.addHandler<SimpleHandler>("/hello");
    svr.bind(80);

    Timer tm;
    Timer tm2;
    tm.start();
    tm2.start();
    while (true) {
        Net::poll();
        if (tm.read()>.5) {
            led1=!led1; //Show that we are alive
            tm.start();
        }
        if (tm2.read()>5) {
            led2=!led2;
            tm2.start();
            mesure_temp();
        }
    }
}