http://mbed.org/users/shintamainjp/notebook/starboard_expbrd-one_ex1_en/

Dependencies:   mbed RemoteIR SuperTweet ConfigFile EthernetNetIf

main.cpp

Committer:
shintamainjp
Date:
2010-10-29
Revision:
0:db299c5a18ba
Child:
1:c4cfd136f9c7

File content as of revision 0:db299c5a18ba:

/**
 * StarBoard Orange Expansion Board - Example application No.1 (Version 0.0.1)
 * http://mbed.org/users/shintamainjp/notebook/starboard_expbrd-one_ex1_en/
 *
 * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
 * http://shinta.main.jp/
 */

#include "mbed.h"
#include "TextLCD.h"
#include "TextLCD_SR4.h"
#include "EthernetNetIf.h"
#include "SuperTweetV1XML.h"
#include "ConfigFile.h"
#include "MyHomeLight.h"
#include "StreamFilter.h"
#include "TransmitterIR.h"
#include "appconf.h"
#include <string.h>

extern "C" void mbed_reset();

/*
 * Definitions for a configuration file.
 */
#define CONFIG_FILENAME         "/local/SETUP.CFG"

/*
 * Variables.
 */
typedef struct {
    bool toggled;
    int fetch_count;
    int error_count;
    int toggle_count;
    int text_count;
    char txtcurr[1024];
    char txtprev[1024];
} work_t;
work_t work = {
    .toggled = false,
    .fetch_count = 0,
    .error_count = 0,
    .toggle_count = 0,
    .text_count = 0,
    .txtcurr = {0},
               .txtprev = {0}
                      };
static appconf_t appconf;
static const int CHANNEL = 2;

/*
 * Classes.
 */
TextLCD lcd(p24, p26, p27, p28, p29, p30);
// TextLCD_SR4 lcd(p29, p30, p28, p27);
LocalFileSystem fs_local("local");
EthernetNetIf netif;
MyHomeLight room_light(p21);
StreamFilter sf("<text>", "</text>");

void splash(void);
int main(void);

/**
 * Display a splash screen.
 */
void splash(void) {
    lcd.cls();
    lcd.locate(0, 0);
    lcd.printf("StarBoard Orange");
    lcd.locate(0, 1);
    lcd.printf("Expansion Board ");
    wait(2);

    lcd.cls();
    lcd.locate(0, 0);
    lcd.printf("Example app No.1");
    lcd.locate(0, 1);
    lcd.printf("                ");
    wait(2);
}

/**
 * A callback function with SuperTweet.
 *
 * @param buf A pointer to a buffer.
 * @param siz Size of the buffer.
 */
void cbfunc(char *buf, size_t siz) {
    /*
     * Push the stream data to the stream filter.
     */
    for (int i = 0; i < siz; i++) {
        sf.push(buf[i]);
    }

    /*
     * Check the state of the stream filter.
     */
    if (sf.done()) {
        if (sf.getContent(&work.txtcurr[0], sizeof(work.txtcurr))) {
            /*
             * Toggle my room light if there is a 'Light' text in twitter stream.
             */
            if (strstr(work.txtcurr, "Light") != NULL) {
                work.text_count++;
                if (work.text_count == 1) {
                    if (strcmp(work.txtcurr, work.txtprev) != 0) {
                        if (work.toggle_count > 0) {
                            if (room_light.toggle(CHANNEL)) {
                                printf("Controlled.\n");
                                work.toggled = true;
                            } else {
                                printf("Control failed.\n");
                            }
                        }
                        work.toggle_count++;
                    }
                    strcpy(work.txtprev, work.txtcurr);
                }
            }
        }
        sf.reset();
    }
}

/**
 * Entry point.
 */
int main(void) {

    /*
     * Splash.
     */
    splash();

    /*
     * Initialize ethernet interface.
     */
    lcd.cls();
    lcd.locate(0, 0);
    lcd.printf("Initializing.   ");
    lcd.locate(0, 1);
    lcd.printf("Ethernet:       ");
    EthernetErr ethErr = netif.setup();
    if (ethErr) {
        lcd.locate(0, 1);
        lcd.printf("Ethernet:NG     ");
        error("Network setup failed.\n");
    } else {
        lcd.locate(0, 1);
        lcd.printf("Ethernet:OK     ");
    }
    wait(2);

    /*
     * Read configuration variables from a file.
     */
    appconf_init(&appconf);
    if (appconf_read(CONFIG_FILENAME, &appconf) != 0) {
        lcd.cls();
        lcd.locate(0, 0);
        lcd.printf("ConfigFile");
        lcd.locate(0, 1);
        lcd.printf("Read error.");
        error("Failure to read a configuration file.\n");
    }

    /*
     * Setup http client object.
     *
     * Please replace this information by your account.
     */
    SuperTweetV1XML twitter(appconf.account, appconf.password);

    int count = 0;
    while (1) {
        /*
         * Fetching...
         */
        lcd.cls();
        lcd.locate(0, 0);
        lcd.printf("Fetching:%d", count++);
        lcd.locate(0, 1);
        lcd.printf("Toggled :%d", work.toggle_count);

        work.fetch_count++;
        work.text_count = 0;
        work.toggled = false;
        sf.reset();

        /*
         * getStatusesUserTimeline: Only You can control a device.
         * getStatusesHomeTimeline: Everybody can control a device.
         */
        // HTTPResult r = twitter.getStatusesUserTimeline(cbfunc);
        HTTPResult r = twitter.getStatusesHomeTimeline(cbfunc);

        /*
         * Report the toggled status to the twitter account.
         */
        if (work.toggled) {
            work.toggled = false;
            char buf[64];
            snprintf(buf, sizeof(buf), "Toggled (%d/%d)", work.toggle_count, count);
            if (twitter.postStatusesUpdate(buf) == 0) {
                printf("Post done.\n");
            } else {
                printf("Post failed.\n");
            }
        }

        /*
         * Note:
         * I don't know why sometime it get a protocol error number 5.
         * I think it a bug in a mbed library.
         */
        printf("No.%d:[%d]\n", work.fetch_count, (int)r);
        if ((int)r != 0) {
            work.error_count++;
            if (work.error_count > 2) {
                printf("\tResetting...\n");
                mbed_reset();
            }
        }

        /*
         * Waiting...
         */
        lcd.cls();
        lcd.locate(0, 0);
        lcd.printf("Waiting...");
        wait(2);
    }
}