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

Dependencies:   mbed RemoteIR SuperTweet ConfigFile EthernetNetIf

Files at this revision

API Documentation at this revision

Comitter:
shintamainjp
Date:
Fri Oct 29 23:17:01 2010 +0000
Child:
1:c4cfd136f9c7
Commit message:
Initial version.

Changed in this revision

extlib/EthernetNetIf.lib Show annotated file Show diff for this revision Revisions of this file
extlib/HTTPClient.lib Show annotated file Show diff for this revision Revisions of this file
extlib/TextLCD.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
mylib/ConfigFile.lib Show annotated file Show diff for this revision Revisions of this file
mylib/MyHomeLight/MyHomeLight.cpp Show annotated file Show diff for this revision Revisions of this file
mylib/MyHomeLight/MyHomeLight.h Show annotated file Show diff for this revision Revisions of this file
mylib/RemoteIR.lib Show annotated file Show diff for this revision Revisions of this file
mylib/StreamFilter/StreamFilter.cpp Show annotated file Show diff for this revision Revisions of this file
mylib/StreamFilter/StreamFilter.h Show annotated file Show diff for this revision Revisions of this file
mylib/SuperTweet.lib Show annotated file Show diff for this revision Revisions of this file
mylib/TextLCD_SR4.lib Show annotated file Show diff for this revision Revisions of this file
mylib/appconf/appconf.cpp Show annotated file Show diff for this revision Revisions of this file
mylib/appconf/appconf.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extlib/EthernetNetIf.lib	Fri Oct 29 23:17:01 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/donatien/code/EthernetNetIf/#bc7df6da7589
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extlib/HTTPClient.lib	Fri Oct 29 23:17:01 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/donatien/code/HTTPClient/#d0be6af2d1db
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extlib/TextLCD.lib	Fri Oct 29 23:17:01 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/simon/code/TextLCD/#a53b3e2d6f1e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Oct 29 23:17:01 2010 +0000
@@ -0,0 +1,236 @@
+/**
+ * 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);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri Oct 29 23:17:01 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/e2ac27c8e93e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mylib/ConfigFile.lib	Fri Oct 29 23:17:01 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/shintamainjp/code/ConfigFile/#f6ceafabe9f8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mylib/MyHomeLight/MyHomeLight.cpp	Fri Oct 29 23:17:01 2010 +0000
@@ -0,0 +1,64 @@
+#include "MyHomeLight.h"
+#include "RemoteIR.h"
+
+const MyHomeLight::light_signal_t MyHomeLight::lights[8] = {
+    {0, "\x2C\x52\x09\x02\x08\x82"},
+    {1, "\x2C\x52\x09\x02\x0A\xA2"},
+    {2, "\x2C\x52\x09\x02\x0C\xC2"},
+    {3, "\x2C\x52\x09\x02\x0E\xE2"},
+    {4, "\x2C\x52\x09\x42\x08\xC2"},
+    {5, "\x2C\x52\x09\x42\x0A\xE2"},
+    {6, "\x2C\x52\x09\x42\x0C\x82"},
+    {7, "\x2C\x52\x09\x42\x0E\xA2"},
+};
+
+/**
+ * Create.
+ *
+ * @param tx_pin Pin of IR transmitter.
+ */
+MyHomeLight::MyHomeLight(PinName tx_pin) : tx(tx_pin) {
+}
+
+/**
+ * Dispose.
+ */
+MyHomeLight::~MyHomeLight() {
+}
+
+/**
+ * Toggle state.
+ *
+ * @param channel Target channel number.
+ * @return true if it succeed.
+ */
+bool MyHomeLight::toggle(const int channel) {
+    RemoteIR::Format fmt = RemoteIR::AEHA;
+    uint8_t *sig = getLightSignal(channel);
+    if (sig != NULL) {
+        for (int i = 0; i < 2; i++) {
+            while (tx.getState() != TransmitterIR::Idle) {
+                wait_us(100);
+            }
+            tx.setData(fmt, sig, 48);
+            wait_ms(120);
+        }
+        return true;
+    }
+    return false;
+}
+
+/**
+ * Get a signal for a light.
+ *
+ * @param channel Channel of a light.
+ *
+ * @return A pointer to a signal.
+ */
+uint8_t *MyHomeLight::getLightSignal(int channel) {
+    const int n = sizeof(lights) / sizeof(lights[0]);
+    if ((0 <= channel) && (channel <= n - 1)) {
+        return (uint8_t *)lights[channel].signal;
+    }
+    return NULL;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mylib/MyHomeLight/MyHomeLight.h	Fri Oct 29 23:17:01 2010 +0000
@@ -0,0 +1,48 @@
+#include "mbed.h"
+#include "TransmitterIR.h"
+
+class MyHomeLight {
+public:
+
+    /**
+     * Create.
+     *
+     * @param tx_pin Pin of IR transmitter.
+     */
+    explicit MyHomeLight(PinName tx_pin);
+
+    /**
+     * Dispose.
+     */
+    ~MyHomeLight();
+
+    /**
+     * Toggle state.
+     *
+     * @param channel Target channel number.
+     * @return true if it succeed.
+     */
+    bool toggle(const int channel);
+
+private:
+    TransmitterIR tx;
+
+    typedef struct {
+        int channel;    /**< Channel number. */
+        char *signal;   /**< Signal data. */
+    } light_signal_t;
+
+    /**
+     * Signal for a light.
+     */
+    static const light_signal_t lights[8];
+
+    /**
+     * Get a signal for a light.
+     *
+     * @param channel Channel of a light.
+     *
+     * @return A pointer to a signal.
+     */
+    uint8_t *getLightSignal(int channel);
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mylib/RemoteIR.lib	Fri Oct 29 23:17:01 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/shintamainjp/code/RemoteIR/#268cc2ab63bd
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mylib/StreamFilter/StreamFilter.cpp	Fri Oct 29 23:17:01 2010 +0000
@@ -0,0 +1,97 @@
+
+#include "StreamFilter.h"
+
+/**
+ * Create.
+ *
+ * @param headtxt Head of a target.
+ * @param tailtxt Tail of a target.
+ */
+StreamFilter::StreamFilter(const char *headtxt, const char *tailtxt)
+        : headcnt(0), tailcnt(0), head(headtxt), tail(tailtxt), content("") {
+}
+
+/**
+ * Dispose.
+ */
+StreamFilter::~StreamFilter() {
+}
+
+/**
+ * Check state of done.
+ *
+ * @return Return true if it done.
+ */
+bool StreamFilter::done() {
+    if ((headcnt == strlen(head)) && (tailcnt == strlen(tail))) {
+        return true;
+    }
+    return false;
+}
+
+/**
+ * Reset state.
+ */
+void StreamFilter::reset() {
+    headcnt = 0;
+    tailcnt = 0;
+    memset(content, 0, sizeof(content));
+}
+
+/**
+ * Push a data to a internal buffer.
+ *
+ * @param c character.
+ */
+void StreamFilter::push(char c) {
+    if (done()) {
+        return;
+    }
+
+    /*
+     * Check a text for the header.
+     */
+    if (headcnt < strlen(head)) {
+        if (c == head[headcnt]) {
+            headcnt++;
+        } else {
+            headcnt = 0;
+        }
+        return;
+    }
+    /*
+     * Check a text for the tailer.
+     */
+    if (tailcnt < strlen(tail)) {
+        if (c == tail[tailcnt]) {
+            tailcnt++;
+            return;
+        } else {
+            tailcnt = 0;
+        }
+    }
+    /*
+     * It's a content.
+     */
+    const int n = strlen(content);
+    if (n < sizeof(content) - 1) {
+        content[n + 0] = c;
+        content[n + 1] = '\0';
+    }
+}
+
+/**
+ * Get a content text.
+ *
+ * @param buf A pointer to a buffer.
+ * @param bufsiz Size of the buffer.
+ *
+ * @return true if it succeed.
+ */
+bool StreamFilter::getContent(char *buf, size_t bufsiz) {
+    if (strlen(content) < bufsiz) {
+        strcpy(buf, content);
+        return true;
+    }
+    return false;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mylib/StreamFilter/StreamFilter.h	Fri Oct 29 23:17:01 2010 +0000
@@ -0,0 +1,57 @@
+
+#include "mbed.h"
+
+/**
+ * Stream filter.
+ */
+class StreamFilter {
+public:
+
+    /**
+     * Create.
+     *
+     * @param headtxt Head of a target.
+     * @param tailtxt Tail of a target.
+     */
+    StreamFilter(const char *headtxt, const char *tailtxt);
+
+    /**
+     * Dispose.
+     */
+    ~StreamFilter();
+
+    /**
+     * Check state of done.
+     *
+     * @return Return true if it done.
+     */
+    bool done();
+
+    /**
+     * Reset state.
+     */
+    void reset();
+
+    /**
+     * Push a data to a internal buffer.
+     *
+     * @param c character.
+     */
+    void push(char c);
+
+    /**
+     * Get a content text.
+     *
+     * @param buf A pointer to a buffer.
+     * @param bufsiz Size of the buffer.
+     *
+     * @return true if it succeed.
+     */
+    bool getContent(char *buf, size_t bufsiz);
+private:
+    int headcnt;
+    int tailcnt;
+    const char * head;
+    const char * tail;
+    char content[1024];
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mylib/SuperTweet.lib	Fri Oct 29 23:17:01 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/shintamainjp/code/SuperTweet/#d463d3fc4f81
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mylib/TextLCD_SR4.lib	Fri Oct 29 23:17:01 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/shintamainjp/code/TextLCD_SR4/#e768257aba09
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mylib/appconf/appconf.cpp	Fri Oct 29 23:17:01 2010 +0000
@@ -0,0 +1,57 @@
+#include "appconf.h"
+#include "ConfigFile.h"
+
+#define KEY_SUPERTWEET_ACCOUNT  "SUPERTWEET_ACCOUNT"
+#define KEY_SUPERTWEET_PASSWORD "SUPERTWEET_PASSWORD"
+
+/**
+ * Initialize configuration.
+ *
+ * @param p A pointer to a application config.
+ */
+void appconf_init(appconf_t *p) {
+    memset(p->account, 0, sizeof(p->account));
+    memset(p->password, 0, sizeof(p->password));
+}
+
+/**
+ * Read configuration.
+ *
+ * @param filename Filename.
+ * @param p A pointer to a application config.
+ * @return Return zero if it succeed.
+ */
+int appconf_read(char *filename, appconf_t *p) {
+    ConfigFile cf;
+    if (!cf.read(filename)) {
+        return -1;
+    }
+    if (!cf.getValue(KEY_SUPERTWEET_ACCOUNT, p->account, sizeof(p->account))) {
+        return -2;
+    }    
+    if (!cf.getValue(KEY_SUPERTWEET_PASSWORD, p->password, sizeof(p->password))) {
+        return -3;
+    }
+    return 0;
+}
+
+/**
+ * Write configuration.
+ *
+ * @param filename Filename.
+ * @param p A pointer to a application config.
+ * @return Return zero if it succeed.
+ */
+int appconf_write(char *filename, appconf_t *p) {
+    ConfigFile cf;
+    if (!cf.setValue(KEY_SUPERTWEET_ACCOUNT, p->account)) {
+        return -1;
+    }
+    if (!cf.setValue(KEY_SUPERTWEET_PASSWORD, p->password)) {
+        return -2;
+    }
+    if (!cf.write(filename)) {
+        return -3;
+    }
+    return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mylib/appconf/appconf.h	Fri Oct 29 23:17:01 2010 +0000
@@ -0,0 +1,30 @@
+
+typedef struct {
+    char account[64];
+    char password[64];
+} appconf_t;
+
+/**
+ * Initialize configuration.
+ *
+ * @param p A pointer to a application config.
+ */
+void appconf_init(appconf_t *p);
+
+/**
+ * Read configuration.
+ *
+ * @param filename Filename.
+ * @param p A pointer to a application config.
+ * @return Return zero if it succeed.
+ */
+int appconf_read(char *filename, appconf_t *p);
+
+/**
+ * Write configuration.
+ *
+ * @param filename Filename.
+ * @param p A pointer to a application config.
+ * @return Return zero if it succeed.
+ */
+int appconf_write(char *filename, appconf_t *p);