跳转至

基础框架


概述

简单的一个框架,新建程序参考。


代码


var server = "iot.easelive.cn";
var port = 10808;

function netCB(m)
{
    switch(m.event)
    {
        case Net.READY:
            Net.syncTime();
            Net.tcp(0, server, port);
            break;
        case Net.CONN_OK:
            print("net", m.id, "connected");
            break;
        case Net.CONN_FAIL:
            print("net", m.id, "connect fail");
            break;  
        case Net.CONN_CLOSE:
            print("net", m.id, "closed");
            break;
        case Net.RECV:
            print("net", m.id, "receive:", m.data.hex());
            break;
    }
}

function timerCB(id)
{
    switch(id)
    {
        case 1:
            print("timer 1 fired");
            break;
        case 2: 
            print("timer 2 fired");
            break;
    }
}

function uartCB(port, data)
{
    if(port == 1)
    {
        print(data.hex());
    }
    if(port == 2)
    {
        print(data.hex());
    }
}

function ioCB(port, level)
{
    print(port, level);
}

GPIO.set(GPIO.POWER, 1);
Net.init();
Timer.start(1, 1000);

while(true)
{
    var m = Event.get();
    switch(m.msg)
    {
        case Event.NET_EVENT:
            netCB(m);
            break;
        case Event.TIMER_OUT:
            timerCB(m.id);
            break;
        case Event.UART_RECV:
            uartCB(m.port, m.data);  
            break;
        case Event.PIO_CHANGE:
            ioCB(m.port, m.level);
            break;
    }
}


下载代码(右键另存为):base.js