データの保存、更新、取得ができるWebサービス「milkcocoa」に接続し、データのプッシュ、送信、取得ができるライブラリを使ったサンプルです。 EthernetIF版 https://mlkcca.com/

Dependencies:   EthernetInterface Milkcocoa_EthernetIF mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
jksoft
Date:
Tue Oct 31 09:26:44 2017 +0000
Parent:
8:25a9bc3ca875
Commit message:
??

Changed in this revision

ConfigFile/ConfigFile.cpp Show annotated file Show diff for this revision Revisions of this file
ConfigFile/ConfigFile.h Show annotated file Show diff for this revision Revisions of this file
LEDStrip.h Show annotated file Show diff for this revision Revisions of this file
LEDStrip_WS2812.cpp Show annotated file Show diff for this revision Revisions of this file
Milkcocoa.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
diff -r 25a9bc3ca875 -r 561182aac695 ConfigFile/ConfigFile.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ConfigFile/ConfigFile.cpp	Tue Oct 31 09:26:44 2017 +0000
@@ -0,0 +1,415 @@
+/**
+ * Configuration file interface class (Version 0.0.1)
+ *
+ * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
+ * http://shinta.main.jp/
+ */
+#include "ConfigFile.h"
+
+#define NEWLINE_UNIX "\n"
+#define NEWLINE_DOS "\r\n"
+#define NEWLINE_MAC "\r"
+
+/**
+ * Create a configuration file class.
+ */
+ConfigFile::ConfigFile() {
+    /*
+     * Allocation for a config_t list.
+     */
+    configlist = (config_t **)malloc(sizeof(config_t *) * MAXCONFIG);
+    for (int i = 0; i < MAXCONFIG; i++) {
+        configlist[i] = NULL;
+    }
+}
+
+/**
+ * Destroy a configuration file class.
+ */
+ConfigFile::~ConfigFile() {
+    /*
+     * Remove all storage and the contents.
+     */
+    for (int i = 0; i < MAXCONFIG; i++) {
+        config_t *cfg = configlist[i];
+        if (cfg != NULL) {
+            free(cfg->key);
+            free(cfg->value);
+            free(cfg);
+        }
+        configlist[i] = NULL;
+    }
+
+    /*
+     * Remove cnofig_t list.
+     */
+    free(configlist);
+    configlist = NULL;
+}
+
+/**
+ * Get a value for a key.
+ *
+ * @param key A target key name.
+ * @param value A pointer to a value storage.
+ * @param siz A size of a value storage.
+ * @return A value or NULL.
+ */
+bool ConfigFile::getValue(char *key, char *value, size_t siz) {
+    /*
+     * Null check.
+     */
+    if (key == NULL) {
+        return false;
+    }
+
+    /*
+     * Search a config_t object from the key.
+     */
+    config_t *p = search(key);
+    if (p == NULL) {
+        return false;
+    }
+
+    /*
+     * Check the storage size.
+     */
+    if (siz <= strlen(p->value)) {
+        return false;
+    }
+
+    /*
+     * Copy the value to the storage.
+     */
+    strcpy(value, p->value);
+    return true;
+}
+
+/**
+ * Set a set of a key and value.
+ *
+ * @param key A key.
+ * @param value A value.
+ *
+ * @return True if it succeed.
+ */
+bool ConfigFile::setValue(char *key, char *value) {
+    /*
+     * Null check.
+     */
+    if ((key == NULL) || (value == NULL)) {
+        return false;
+    }
+
+    /*
+     * Size check.
+     */
+    if ((MAXLEN_KEY < strlen(key)) || (MAXLEN_VALUE < strlen(value))) {
+        return false;
+    }
+
+    /*
+     * Search a config_t object from the key.
+     */
+    config_t *p = search(key);
+    if (p == NULL) {
+        /*
+         * Allocation a memory for a new key.
+         */
+        char *k = (char *)malloc(sizeof(char) * (strlen(key) + 1));
+        if (k == NULL) {
+            return false;
+        }
+        strcpy(k, key);
+
+        /*
+         * Allocation a memory for a new value.
+         */
+        char *v = (char *)malloc(sizeof(char) * (strlen(value) + 1));
+        if (v == NULL) {
+            free(k);
+            return false;
+        }
+        strcpy(v, value);
+
+        /*
+         * Allocation a memory for a new configuration.
+         */
+        config_t *cfg = (config_t *)malloc(sizeof(config_t) * 1);
+        if (cfg == NULL) {
+            free(k);
+            free(v);
+            return false;
+        }
+        cfg->key = k;
+        cfg->value = v;
+
+        /*
+         * Add the new configuration.
+         */
+        if (!add(cfg)) {
+            free(k);
+            free(v);
+            free(cfg);
+            return false;
+        }
+
+        return true;
+    } else {
+        /*
+         * The value is same.
+         */
+        if (strcmp(value, p->value) == 0) {
+            return true;
+        }
+
+        /*
+         * Free a memory for the value.
+         */
+        free(p->value);
+        p->value = NULL;
+
+        /*
+         * Allocation memory for the new value.
+         */
+        char *v = (char *)malloc(sizeof(char) * (strlen(value) + 1));
+        if (v == NULL) {
+            return false;
+        }
+
+        /*
+         * Store it.
+         */
+        strcpy(v, value);
+        p->value = v;
+
+        return true;
+    }
+}
+
+/**
+ * Remove a config.
+ *
+ * @param key A key.
+ *
+ * @return True if it succeed.
+ */
+bool ConfigFile::remove(char *key) {
+    if (key == NULL) {
+        return false;
+    }
+    for (int i = 0; i < MAXCONFIG; i++) {
+        config_t *cfg = configlist[i];
+        if (cfg != NULL) {
+            if (strcmp(cfg->key, key) == 0) {
+                free(cfg->key);
+                free(cfg->value);
+                free(cfg);
+                configlist[i] = NULL;
+                return true;
+            }
+        }
+    }
+    return false;
+}
+
+/**
+ * Remove all config.
+ *
+ * @return True if it succeed.
+ */
+bool ConfigFile::removeAll(void) {
+    for (int i = 0; i < MAXCONFIG; i++) {
+        config_t *p = configlist[i];
+        if (p != NULL) {
+            free(p->key);
+            free(p->value);
+        }
+        free(p);
+        configlist[i] = NULL;
+    }
+    return true;
+}
+
+/**
+ * Get a number of configuration sets.
+ *
+ * @return number of configuration sets.
+ */
+int ConfigFile::getCount() {
+    int cnt = 0;
+    for (int i = 0; i < MAXCONFIG; i++) {
+        config_t *p = configlist[i];
+        if (p != NULL) {
+            cnt++;
+        }
+    }
+    return cnt;
+}
+
+/**
+ * Get a key and a value.
+ *
+ * @param index Index number of this list.
+ * @param key A pointer to a buffer for key.
+ * @param keybufsiz A size of the key buffer.
+ * @param value A pointer to a buffer for value.
+ * @param valuebufsiz A size of the value buffer.
+ *
+ * @return true if it succeed.
+ */
+bool ConfigFile::getKeyAndValue(int index, char *key, size_t keybufsiz, char *value, size_t valuebufsiz) {
+    int cnt = 0;
+    for (int i = 0; i < MAXCONFIG; i++) {
+        config_t *p = configlist[i];
+        if (p != NULL) {
+            if (cnt == index) {
+                if ((strlen(p->key) < keybufsiz) && (strlen(p->value) < valuebufsiz)) {
+                    strcpy(key, p->key);
+                    strcpy(value, p->value);
+                    return true;
+                }
+                return false;
+            }
+            cnt++;
+        }
+    }
+    return false;
+}
+
+/**
+ * Read from the target file.
+ *
+ * @param file A target file name.
+ */
+bool ConfigFile::read(char *file) {
+    /*
+     * Open the target file.
+     */
+    FILE *fp = fopen(file, "r");
+    if (fp == NULL) {
+        return false;
+    }
+
+    /*
+     * Remove all configuration.
+     */
+    removeAll();
+
+    /*
+     * Read from a file.
+     */
+    char buf[MAXLEN_KEY + 8 + MAXLEN_VALUE];
+    while (fgets(buf, sizeof(buf), fp) != NULL) {
+        /*
+         * Ignore a comment.
+         */
+        if (buf[0] == '#') {
+            continue;
+        }
+
+        /*
+         * Trim a return.
+         */
+        const size_t len = strlen(buf);
+        for (int i = 0; i < len; i++) {
+            if ((buf[i] == '\r') || (buf[i] == '\n')) {
+                buf[i] = '\0';
+            }
+        }
+
+        /*
+         * Separate key and value.
+         */
+        char k[MAXLEN_KEY];
+        char v[MAXLEN_VALUE];
+        char *sp = strchr(buf, SEPARATOR);
+        if (sp != NULL) {
+            strcpy(v, sp + 1);
+            *sp = '\0';
+            strcpy(k, buf);
+            setValue(k, v);
+        }
+    }
+    fclose(fp);
+    return true;
+}
+
+/**
+ * Write from the target file.
+ *
+ * @param file A pointer to a file name.
+ * @param header A pointer to a header.
+ * @param ff File format.
+ */
+bool ConfigFile::write(char *file, char *header, FileFormat ff) {
+    /*
+     * Open the target file.
+     */
+    FILE *fp = fopen(file, "w");
+    if (fp == NULL) {
+        return false;
+    }
+
+    /*
+     * Set a type of new line.
+     */
+    char *newline = NEWLINE_UNIX;
+    switch (ff) {
+        case UNIX:
+            newline = NEWLINE_UNIX;
+            break;
+        case MAC:
+            newline = NEWLINE_MAC;
+            break;
+        case DOS:
+            newline = NEWLINE_DOS;
+            break;
+        default:
+            newline = NEWLINE_UNIX;
+            break;
+    }
+
+    /*
+     * Write the header.
+     */
+    if (header != NULL) {
+        fprintf(fp, "%s%s", header, newline);
+    }
+
+    /*
+     * Write the data.
+     */
+    for (int i = 0; i < MAXCONFIG; i++) {
+        config_t *cfg = configlist[i];
+        if (cfg != NULL) {
+            fprintf(fp, "%s=%s%s", cfg->key, cfg->value, newline);
+        }
+    }
+    fclose(fp);
+    return true;
+}
+
+ConfigFile::config_t *ConfigFile::search(char *key) {
+    if (key == NULL) {
+        return NULL;
+    }
+    for (int i = 0; i < MAXCONFIG; i++) {
+        if (configlist[i] != NULL) {
+            if (strcmp(configlist[i]->key, key) == 0) {
+                return configlist[i];
+            }
+        }
+    }
+    return NULL;
+}
+
+bool ConfigFile::add(config_t *cfg) {
+    for (int i = 0; i < MAXCONFIG; i++) {
+        if (configlist[i] == NULL) {
+            configlist[i] = cfg;
+            return true;
+        }
+    }
+    return false;
+}
\ No newline at end of file
diff -r 25a9bc3ca875 -r 561182aac695 ConfigFile/ConfigFile.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ConfigFile/ConfigFile.h	Tue Oct 31 09:26:44 2017 +0000
@@ -0,0 +1,121 @@
+/**
+ * Configuration file interface class (Version 0.0.1)
+ *
+ * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
+ * http://shinta.main.jp/
+ */
+#include "mbed.h"
+
+#ifndef _CONFIG_FILE_H_
+#define _CONFIG_FILE_H_
+
+/**
+ * Configuration File class.
+ */
+class ConfigFile {
+public:
+
+    /**
+     * Create a configuration file class.
+     */
+    ConfigFile();
+
+    /**
+     * Destroy a configuration file class.
+     */
+    ~ConfigFile();
+
+    /**
+     * Get a value for a key.
+     *
+     * @param key A target key name.
+     * @param value A pointer to a value storage.
+     * @param siz A size of a value storage.
+     * @return A value or NULL.
+     */
+    bool getValue(char *key, char *value, size_t siz);
+
+    /**
+     * Set a set of a key and value.
+     *
+     * @param key A key.
+     * @param value A value.
+     *
+     * @return True if it succeed.
+     */
+    bool setValue(char *key, char *value);
+
+    /**
+     * Remove a config.
+     *
+     * @param key A key.
+     *
+     * @return True if it succeed.
+     */
+    bool remove(char *key);
+
+    /**
+     * Remove all config.
+     *
+     * @return True if it succeed.
+     */
+    bool removeAll(void);
+
+    /**
+     * Get a number of configuration sets.
+     *
+     * @return number of configuration sets.
+     */
+    int getCount();
+
+    /**
+     * Get a key and a value.
+     *
+     * @param index Index number of this list.
+     * @param key A pointer to a buffer for key.
+     * @param keybufsiz A size of the key buffer.
+     * @param value A pointer to a buffer for value.
+     * @param valuebufsiz A size of the value buffer.
+     *
+     * @return true if it succeed.
+     */
+    bool getKeyAndValue(int index, char *key, size_t keybufsiz, char *value, size_t valuebufsiz);
+
+    /**
+     * Read from the target file.
+     *
+     * @param file A target file name.
+     */
+    bool read(char *file);
+
+    typedef enum {
+        UNIX,
+        MAC,
+        DOS
+    } FileFormat;
+
+    /**
+     * Write from the target file.
+     *
+     * @param file A pointer to a file name.
+     * @param header A pointer to a header.
+     * @param ff File format.
+     */
+    bool write(char *file, char *header = NULL, FileFormat ff = UNIX);
+
+private:
+    typedef struct {
+        char *key;
+        char *value;
+    } config_t;
+    config_t **configlist;
+    static const int MAXCONFIG = 64;
+    static const int MAXLEN_KEY = 64;
+    static const int MAXLEN_VALUE = 128;
+    static const char SEPARATOR = '=';
+
+    config_t *search(char *key);
+    bool add(config_t *cfg);
+};
+
+#endif
diff -r 25a9bc3ca875 -r 561182aac695 LEDStrip.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LEDStrip.h	Tue Oct 31 09:26:44 2017 +0000
@@ -0,0 +1,5 @@
+
+void tape_write (int d);
+void tapeInit (int speed, int n);
+void tapeSet (int n, int dat);
+void tapeSend ();
diff -r 25a9bc3ca875 -r 561182aac695 LEDStrip_WS2812.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LEDStrip_WS2812.cpp	Tue Oct 31 09:26:44 2017 +0000
@@ -0,0 +1,122 @@
+/*
+ * WS2812 tape led IC
+ *
+ *          0.35us   0.8us    (+-150ns)
+ *  0:     |^^^^^|__________|
+ *
+ *             0.7us   0.6us  (+-150ns)
+ *  1:     |^^^^^^^^^^|_____|
+ *
+ *               >50us
+ *  RESET: |________________|
+ */
+#include "mbed.h"
+#include "LEDStrip.h"
+
+SPI tape(p11, p12, p13);
+
+#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
+    LPC_SSP_TypeDef *_ssp = LPC_SSP0;
+#elif defined(TARGET_LPC11U24)
+    LPC_SSPx_Type *_ssp = LPC_SSP1;
+#endif
+
+int num = 100;
+int *data;
+volatile int busy = 0, wakeup = 0;
+
+
+extern "C"
+#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
+void SSP0_IRQHandler() {
+#elif defined(TARGET_LPC11U24)
+void SSP1_IRQHandler() {
+#endif
+    static int addr = 0, bit = 0x800000;
+repeat:
+    if (busy) {
+        // led data
+      while (_ssp->SR & (1<<1)) { // TNF
+        if (data[addr] & bit) {
+            // 1
+            _ssp->DR = 0x01f;
+        } else {
+            // 0
+            _ssp->DR = 0x07f;
+        }
+        bit = bit >> 1;
+        if (bit == 0) {
+            bit = 0x800000;
+            addr ++;
+            if (addr >= num) {
+                addr = 0;
+                busy = 0;
+                goto repeat;
+            }
+        }
+      }
+    } else {
+        // blank
+        while (_ssp->SR & (1<<1)) { // TNF
+            _ssp->DR = 0xfff;
+            if (addr < 50) {
+                addr ++;
+            } else {
+                addr = 0;
+                if (wakeup) {
+                    busy = 1;
+                    wakeup = 0;
+                    goto repeat;
+                }
+            }
+        }
+    }
+}
+
+void tapeInit (int freq, int n) {
+
+    num = n;
+//    data = new int(num);
+    data = (int*)malloc(sizeof(int) * num);
+    for (int i = 0; i < num; i ++) {
+        data[i] = 0;
+    }
+
+    tape.format(10, 1);
+    if (freq) {
+        tape.frequency(freq * 1000);
+    } else {
+        tape.frequency(8000000);
+    }
+#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
+    NVIC_SetVector(SSP0_IRQn, (uint32_t)SSP0_IRQHandler);
+    NVIC_SetPriority(SSP0_IRQn, 0);
+    NVIC_EnableIRQ(SSP0_IRQn);
+#elif defined(TARGET_LPC11U24)
+    NVIC_SetVector(SSP1_IRQn, (uint32_t)SSP1_IRQHandler);
+    NVIC_SetPriority(SSP1_IRQn, 0);
+    NVIC_EnableIRQ(SSP1_IRQn);
+#endif
+    _ssp->IMSC |= (1<<3); // TXIM
+}
+
+void tapeSet (int n, int dat) {
+    n+=2;
+    if (n >= 0 && n < num) {
+        // RGB -> GRB
+        data[n] = ((dat & 0xff0000) >> 8) | ((dat & 0xff00) << 8) | (dat & 0xff);
+    }
+}
+
+void tapeSend () {
+    if (busy) {
+        while (busy);
+        wait_us(50);
+    }
+    wakeup = 1;
+    while (wakeup);
+}
+
+int tapeGet (int n) {
+    return ((data[n] & 0xff0000) >> 8) | ((data[n] & 0xff00) << 8) | (data[n] & 0xff);
+}
diff -r 25a9bc3ca875 -r 561182aac695 Milkcocoa.lib
--- a/Milkcocoa.lib	Mon Jun 06 03:40:05 2016 +0000
+++ b/Milkcocoa.lib	Tue Oct 31 09:26:44 2017 +0000
@@ -1,1 +1,1 @@
-https://developer.mbed.org/users/jksoft/code/Milkcocoa_EthernetIF/#7b9d1fed76fe
+https://developer.mbed.org/users/jksoft/code/Milkcocoa_EthernetIF/#bf455cd5059f
diff -r 25a9bc3ca875 -r 561182aac695 main.cpp
--- a/main.cpp	Mon Jun 06 03:40:05 2016 +0000
+++ b/main.cpp	Tue Oct 31 09:26:44 2017 +0000
@@ -1,61 +1,618 @@
 #include "mbed.h"
+#include "LEDStrip.h"
+#include "ConfigFile.h"
 #include "MQTTEthernet.h"
 #include "MQTTClient.h"
 #include "Milkcocoa.h"
 #include "MClient.h"
 
-// The default setting is for the Simple IoT Board(mbed LPC1114FN28)
-// Please change to fit the platform
-Serial pc(USBTX, USBRX);
-DigitalOut myled(LED1);
+#define LED_NUM			60
+#define LED_DATA_NUM	3
+#define PI 3.14159265359f
+#define MAX_COUNT		60
+#define STAR_LINE_MAX	60
+
+#if 0
+#define DBG(x) x
+#else
+#define DBG(x)
+#endif
 
 /************************* Your Milkcocoa Setup *********************************/
-#define MILKCOCOA_APP_ID      "...YOUR_MILKCOCOA_APP_ID..."
-#define MILKCOCOA_DATASTORE   "mbed"
-
+#define MILKCOCOA_APP_ID      "juicej2vchzis"
+#define MILKCOCOA_DATASTORE   "halloween"
+ 
 /************* Milkcocoa Setup (you don't need to change this!) ******************/
-
+ 
 #define MILKCOCOA_SERVERPORT  1883
  
 /************ Global State (you don't need to change this!) ******************/
-
+ 
 const char MQTT_SERVER[]  = MILKCOCOA_APP_ID ".mlkcca.com";
 const char MQTT_CLIENTID[] = __TIME__ MILKCOCOA_APP_ID;
-
+ 
 extern void onpush(MQTT::MessageData& md);
 
+Serial pc(USBTX, USBRX);
+LocalFileSystem local("local");
+Ticker flipper;
+ConfigFile cfg;
+DigitalIn SW1(p25);
+DigitalIn SW2(p26);
+DigitalOut stat_led1(LED1);
+DigitalOut stat_led2(LED2);
+DigitalOut stat_led3(LED3);
+DigitalOut stat_led4(LED4);
+
+
+
+void flip();
+
+bool completed = false;
+int t_timer = 0;
+int like_count = 0;
+int max_count = 0;
+int demo_count = 0;
+
+const unsigned char _RED[3] = 	{ 0xFF, 0   , 0    };
+const unsigned char _GREEN[3] = 	{ 0   , 0xFF, 0    };
+const unsigned char _BLUE[3] = 	{ 0   , 0   , 0xFF };
+const unsigned char _YELLOW[3] = { 0xFF, 0xFF, 0    };
+const unsigned char _PURPLE[3] = { 0xFF, 0   , 0xFF };
+const unsigned char _AQUA[3] = 	{ 0   , 0xFF, 0xFF };
+const unsigned char _WHITE[3] = 	{ 0xFF, 0xFF, 0xFF };
+const unsigned char _GLAY[3] = 	{ 0x80, 0x80, 0x80 };
+const unsigned char _DARK_GLAY[3] = { 0x2, 0x2, 0x2 };
+const unsigned char _OFF[3] = 	{ 0, 0, 0 };
+
+unsigned char led_data[60][3];
+
+unsigned char base_led_data[60][3];
+
+unsigned char star_line[STAR_LINE_MAX] = { 
+	 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
+	 10,11,12,13,14,15,16,17,18,19,
+	 20,21,22,23,24,25,26,27,28,29,
+	 30,31,32,33,34,35,36,37,38,39,
+     40,41,42,43,44,45,46,47,48,49,
+     50,51,52,53,54,55,56,57,58,59
+};
+
+unsigned char color_line[STAR_LINE_MAX][3] =
+{
+	{ 0xFF, 0x00, 0x00 },{ 0xFF, 0x80, 0x00 },{ 0xFF, 0xFF, 0x00 },{ 0x80, 0xFF, 0x00 },
+	{ 0x00, 0xFF, 0x00 },{ 0x00, 0xFF, 0x80 },{ 0x00, 0xFF, 0xFF },{ 0x00, 0x80, 0xFF },
+	{ 0x00, 0x00, 0xFF },{ 0x80, 0x00, 0xFF },{ 0xFF, 0x00, 0xFF },{ 0xFF, 0x00, 0x80 },
+	{ 0xFF, 0x00, 0x00 },{ 0xFF, 0x80, 0x00 },{ 0xFF, 0xFF, 0x00 },{ 0x80, 0xFF, 0x00 },
+	{ 0x00, 0xFF, 0x00 },{ 0x00, 0xFF, 0x80 },{ 0x00, 0xFF, 0xFF },{ 0x00, 0x80, 0xFF },
+	{ 0x00, 0x00, 0xFF },{ 0x80, 0x00, 0xFF },{ 0xFF, 0x00, 0xFF },{ 0xFF, 0x00, 0x80 },
+	{ 0xFF, 0x00, 0x00 },{ 0xFF, 0x80, 0x00 },{ 0xFF, 0xFF, 0x00 },{ 0x80, 0xFF, 0x00 },
+	{ 0x00, 0xFF, 0x00 },{ 0x00, 0xFF, 0x80 },{ 0x00, 0xFF, 0xFF },{ 0x00, 0x80, 0xFF },
+	{ 0x00, 0x00, 0xFF },{ 0x80, 0x00, 0xFF },{ 0xFF, 0x00, 0xFF },{ 0xFF, 0x00, 0x80 },
+	{ 0xFF, 0x00, 0x00 },{ 0xFF, 0x80, 0x00 },{ 0xFF, 0xFF, 0x00 },{ 0x80, 0xFF, 0x00 },
+	{ 0x00, 0xFF, 0x00 },{ 0x00, 0xFF, 0x80 },{ 0x00, 0xFF, 0xFF },{ 0x00, 0x80, 0xFF },
+	{ 0x00, 0x00, 0xFF },{ 0x80, 0x00, 0xFF },{ 0xFF, 0x00, 0xFF },{ 0xFF, 0x00, 0x80 },
+	{ 0xFF, 0x00, 0x00 },{ 0xFF, 0x80, 0x00 },{ 0xFF, 0xFF, 0x00 },{ 0x80, 0xFF, 0x00 },
+	{ 0x00, 0xFF, 0x00 },{ 0x00, 0xFF, 0x80 },{ 0x00, 0xFF, 0xFF },{ 0x00, 0x80, 0xFF },
+	{ 0x00, 0x00, 0xFF },{ 0x80, 0x00, 0xFF },{ 0xFF, 0x00, 0xFF },{ 0xFF, 0x00, 0x80 }
+
+};
+
+extern void send_led();
+
+int DownLight(unsigned char *dat,int value)
+{
+	for(int i=0;i<LED_DATA_NUM;i++)
+	{
+		if( dat[i] > value )
+		{
+			dat[i] = dat[i] - value;
+		}
+	}
+	return(0);
+}
+
+int near(unsigned char *from_dat,const unsigned char *to_dat,int wide)
+{
+	int ret_code_l = 1;
+	
+	for( int i=0;i<LED_DATA_NUM;i++)
+	{
+		if( from_dat[i] < to_dat[i] )
+		{
+			if( (from_dat[i]+wide) < to_dat[i] )
+			{
+				from_dat[i] = from_dat[i] + wide;
+				ret_code_l = 0;
+			}
+			else
+			{
+				from_dat[i] = to_dat[i];
+
+			}
+		}
+		else if( from_dat[i] > to_dat[i] )
+		{
+			if( (from_dat[i]-wide) > to_dat[i])
+			{
+				from_dat[i] -= wide;
+				ret_code_l = 0;
+			}
+			else
+			{
+				from_dat[i] = to_dat[i];
+			}
+		}
+	}
+	
+	return(ret_code_l);
+}
+
+int flow_star(int from,int to,const unsigned char *dat,const unsigned char *base_dat,int speed)
+{
+	int i,j;
+	
+	if( from < to )
+	{
+		for(i=from;i<to;i++)
+		{
+			memcpy( led_data[star_line[i]] , base_dat , 3 );
+		}
+		
+		send_led();
+		wait_ms(speed);
+		
+		for(i=from;i<to;i++)
+		{
+			for(j=from;j<to;j++)
+			{
+				if(j==i)
+				{
+					memcpy( led_data[star_line[j]] , dat , 3 );
+				}
+				else
+				{
+					near( led_data[star_line[j]] , base_dat , 5 );
+				}
+			}
+			send_led();
+			wait_ms(speed);
+			
+		}
+		
+		i = 0;
+		j = 1;
+		while(i!=(abs(from-to)))
+		{
+			i = 0;
+			for(j=from;j<to;j++)
+			{
+				i += near( led_data[star_line[j]] , base_dat , 10 );
+			}
+			send_led();
+			wait_ms(speed);
+		}
+	}
+	else
+	{
+		for(i=from;i>to;i--)
+		{
+			memcpy( led_data[star_line[i]] , base_dat , 3 );
+		}
+		
+		send_led();
+		wait_ms(speed);
+		
+		for(i=from;i>to;i--)
+		{
+			for(j=from;j>to;j--)
+			{
+				if(j==i)
+				{
+					memcpy( led_data[star_line[j]] , dat , 3 );
+				}
+				else
+				{
+					near( led_data[star_line[j]] , base_dat , 5 );
+				}
+			}
+			send_led();
+			wait_ms(speed);
+		}
+		
+		i = 0;
+		j = 1;
+		while(i!=(abs(from-to)))
+		{
+			i = 0;
+			for(j=from;j>to;j--)
+			{
+				i += near( led_data[star_line[j]] , base_dat , 5 );
+			}
+			send_led();
+			wait_ms(speed);
+		}
+	}
+	
+	return(0);
+}
+
+int flow_star(int from,int to,const unsigned char *dat,int speed)
+{
+	int i,j;
+	
+	if( from < to )
+	{
+		for(i=from;i<to;i++)
+		{
+			memcpy( led_data[star_line[i]] , base_led_data[star_line[i]] , 3 );
+		}
+		
+		send_led();
+		wait_ms(speed);
+		
+		for(i=from;i<to;i++)
+		{
+			for(j=from;j<to;j++)
+			{
+				if(j==i)
+				{
+					memcpy( led_data[star_line[j]] , dat , 3 );
+				}
+				else
+				{
+					near( led_data[star_line[j]] , base_led_data[star_line[j]] , 5 );
+				}
+			}
+			send_led();
+			wait_ms(speed);
+			
+		}
+		
+		i = 0;
+		j = 1;
+		while(i!=(abs(from-to)))
+		{
+			i = 0;
+			for(j=from;j<to;j++)
+			{
+				i += near( led_data[star_line[j]] , base_led_data[star_line[j]] , 10 );
+			}
+			send_led();
+			wait_ms(speed);
+		}
+	}
+	else
+	{
+		for(i=from;i>to;i--)
+		{
+			memcpy( led_data[star_line[i]] , base_led_data[star_line[i]] , 3 );
+		}
+		
+		send_led();
+		wait_ms(speed);
+		
+		for(i=from;i>to;i--)
+		{
+			for(j=from;j>to;j--)
+			{
+				if(j==i)
+				{
+					memcpy( led_data[star_line[j]] , dat , 3 );
+				}
+				else
+				{
+					near( led_data[star_line[j]] , base_led_data[star_line[j]] , 5 );
+				}
+			}
+			send_led();
+			wait_ms(speed);
+		}
+		
+		i = 0;
+		j = 1;
+#if 0
+		while(i!=(abs(from-to)))
+		{
+			i = 0;
+			for(j=from;j>to;j--)
+			{
+				i += near( led_data[star_line[j]] , base_led_data[star_line[j]] , 5 );
+			}
+			send_led();
+			wait_ms(speed);
+		}
+#endif
+	}
+	
+	return(0);
+}
+
+void slow_light_all(const unsigned char *base_dat,int speed)
+{
+	int i,j;
+	while(i!=LED_NUM)
+	{
+		i = 0;
+		for(j=0;j<LED_NUM;j++)
+		{
+			i += near( led_data[j] , base_dat , 5 );
+		}
+		send_led();
+		wait_ms(speed);
+	}
+}
+
+void back_base(void)
+{
+	for(int i=0;i<LED_NUM;i++)
+	{
+		near( led_data[i] , base_led_data[i] , 5 );
+		send_led();
+	}
+}
+
+int glad(int i,unsigned char *dat)
+{
+	i = i % 0x2FE;
+	
+	if( i < 0xFF )
+	{
+		dat[0] = 0xFF * cos(PI/(510.0f/(double)i));
+		dat[1] = 0xFF * sin(PI/(510.0f/(double)i));
+		dat[2] = 0;
+	}
+	else if((i >= 0xFF)&&( i < 0x1FE ))
+	{
+		i -= 0xFF;
+		dat[0] = 0;
+		dat[1] = 0xFF * cos(PI/(510.0f/(double)i));
+		dat[2] = 0xFF * sin(PI/(510.0f/(double)i));
+	}
+	else if((i >= 0x1FE )&&( i < 0x2FD ))
+	{
+		i -= 0x1FE;
+		dat[0] = 0xFF * sin(PI/(510.0f/(double)i));
+		dat[1] = 0;
+		dat[2] = 0xFF * cos(PI/(510.0f/(double)i));
+	}
+	return(0);
+
+}
+
+void idle_cycle(int like) {
+	static int old_like_count_i = 0;
+	static int cycle = 0;
+	static int gorl_flag = 0;
+    static int gol_count = 0;
+	int point = STAR_LINE_MAX - like; //STAR_LINE_MAX - (int)((float)like / ((float)max_count / (float)STAR_LINE_MAX));
+	
+	cycle++;
+	
+	if( old_like_count_i != like ) {
+
+		flipper.detach();
+		
+		if( like < max_count ) {
+			for(int j=0;j<60;j++) {
+				memcpy( led_data[j] , base_led_data[j] , 3 );
+			}
+			printf("COUNT!! %d\r\n",point);
+			flow_star(0,point+1,_YELLOW,20);
+			flow_star(point,point+1,color_line[point],_WHITE,50);
+			flow_star(point,point+1,color_line[point],_WHITE,50);
+			flow_star(point,point+1,_WHITE,color_line[point],50);
+			memcpy(base_led_data[star_line[point]] , color_line[point] , 3 );
+		}
+		else if(gorl_flag == 0) {
+			gorl_flag = 1;
+			flow_star(0,STAR_LINE_MAX,_RED,10);
+			flow_star(0,STAR_LINE_MAX,_PURPLE,10);
+			flow_star(0,STAR_LINE_MAX,_BLUE,10);
+			flow_star(0,STAR_LINE_MAX,_AQUA,10);
+			flow_star(0,STAR_LINE_MAX,_RED,10);
+			flow_star(0,STAR_LINE_MAX,_GREEN,10);
+			flow_star(0,STAR_LINE_MAX,_YELLOW,_OFF,30);
+		}
+		else {
+			flow_star(0,STAR_LINE_MAX,_YELLOW,30);
+		}
+		
+		flipper.attach(&flip, 2.0);
+	}
+	old_like_count_i = like;
+	
+	if(gorl_flag == 1) {
+		for(int j=0;j<60;j++) {
+			glad(gol_count+(j*80),base_led_data[j]);
+			memcpy( led_data[j] , base_led_data[j] , 3 );
+		}
+	}
+	else {
+		for(int i=0;i<STAR_LINE_MAX;i++) {
+			if( i >= point ) {
+				memcpy(base_led_data[star_line[i]] , color_line[i] , 3 );
+			}
+		}
+	}
+	
+	back_base();
+	
+	gol_count += 30 ;
+	if( gol_count >= 0x2FD ) {
+		gol_count = 0;
+	}
+
+}
+
+void like_change(int like)
+{
+
+}
+
+void send_led()
+{
+	for(int i=0;i<LED_NUM;i++)
+	{
+		tapeSet(i,led_data[i][0] << 16 | led_data[i][1] << 8 | led_data[i][2]);
+	}
+	tapeSend();
+}
+
+
+void flip() {
+   // like_count += 1;
+	led_data[star_line[demo_count]][0] /= 2;
+	led_data[star_line[demo_count]][1] /= 2;
+	led_data[star_line[demo_count]][2] /= 2;
+	
+	demo_count += 1;
+	
+	if( demo_count >= STAR_LINE_MAX )
+	{
+		demo_count = 0;
+	}
+}
+
 int main() {
-// void setup() {
-    pc.baud(9600);
+    int old_like_count = 1;
+    char tag[64];
+    char tmp[5];
+    char tmp2[5];
+    
+    SW1.mode(PullUp);
+    SW2.mode(PullUp);
+    
+    if( SW1 == 0 ) { // DEMO MODE
+    	tapeInit(0, LED_NUM+2);
+
+    	while(1) {
+			flow_star(0,STAR_LINE_MAX,_RED   ,_OFF,30);
+			wait(0.5);
+			flow_star(0,STAR_LINE_MAX,_PURPLE,_OFF,30);
+			wait(0.5);
+			flow_star(0,STAR_LINE_MAX,_BLUE  ,_OFF,30);
+			wait(0.5);
+			flow_star(0,STAR_LINE_MAX,_AQUA  ,_OFF,30);
+			wait(0.5);
+			flow_star(0,STAR_LINE_MAX,_GREEN ,_OFF,30);
+			wait(0.5);
+			flow_star(0,STAR_LINE_MAX,_YELLOW,_OFF,30);
+			wait(0.5);
+    	}
+    }
+#if 0
+	cfg.read("/local/env.ini");
+	
+    if (cfg.getValue("TAG", &tag[0], sizeof(tag))) {
+        printf("'%s'='%s'\n\r", "TAG", tag);
+    }
+    else
+    {
+    	sprintf(tag,"ma9");
+    }
+    if (cfg.getValue("COUNT", &tmp[0], sizeof(tmp))) {
+        printf("'%s'='%s'\n\r", "COUNT", tmp);
+        like_count = atoi(tmp);
+    }
+    else
+    {
+    	like_count = 0;
+    }
+    if (cfg.getValue("MAX", &tmp2[0], sizeof(tmp2))) {
+        printf("'%s'='%s'\n\r", "MAX", tmp2);
+        max_count = atoi(tmp2);
+    }
+    else
+    {
+    	max_count = 0;
+    }
+#endif
+	like_count = 0;
+	max_count = 60;
+    
+    printf("ini file read\r\n");
+    printf("HashTag:%s\r\n",tag);
+    printf("Now Count:%d\r\n",like_count);
+    printf("Max Count:%d\r\n",max_count);
+    
+    stat_led1 = 1;
+    	
+	for(int i=0;i<LED_NUM;i++) {
+		memcpy( led_data[i] , _OFF , 3 );
+		memcpy( base_led_data[i] , _DARK_GLAY , 3 );
+	}
+	
+	tapeInit(0, LED_NUM+2);
+
+	send_led();
+		
+	slow_light_all(_DARK_GLAY , 50);
+	
 
     MQTTEthernet *ipstack = new MQTTEthernet();
 	MClient *client = new MClient(ipstack);
 	Milkcocoa *milkcocoa = new Milkcocoa(client, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID);
 
     pc.printf("Milkcocoa mbed ver demo\n\r\n\r\n\r");
+    
+    
+    wait(2);
 	
 	milkcocoa->connect();
-	pc.printf("\n\rEther connected\n\r");
+	while(milkcocoa->getConnectStatus()!=1) {
+		pc.printf("\n\rEther connected error\n\r");
+		pc.printf("\n\rEther re connect\n\r");
+		ipstack->reconnect();
+		wait(2);
+		milkcocoa->connect();
+	}
+	
+    stat_led2 = 1;
 	
-	pc.printf("%d\n\r",milkcocoa->on(MILKCOCOA_DATASTORE, "push", onpush));
+	int ret = milkcocoa->on(MILKCOCOA_DATASTORE, "push", onpush);
+		pc.printf("%d\n\r",ret);
+	while(ret != 1){
+		wait(1);
+		pc.printf("%d\n\r",ret);
+		ret = milkcocoa->on(MILKCOCOA_DATASTORE, "push", onpush);
+	}
+	
+    stat_led3 = 1;
 	
-// }
-	while(1) {
-// void loop() {
-		milkcocoa->loop();
+	while(1)
+	{
+        milkcocoa->loop();
+        
+        if(like_count != old_like_count)
+        {
+			printf("%04d\r\n",like_count);
+        	sprintf(&tmp[0],"%d",like_count);
+        	//cfg.setValue("COUNT", tmp);
+        	if(cfg.write("/local/env.ini"))
+        	{
+        		printf("SET COUNT\r\n");
+        	}
+        	else
+        	{
+        		printf("SET ERR\r\n");
+        	}
+        	old_like_count = like_count;
+        }
+ 		
+		idle_cycle(like_count);
 		
-		DataElement elem = DataElement();
-		elem.setValue("v", 1);
-		
-		milkcocoa->push(MILKCOCOA_DATASTORE, elem);
-		wait(7.0);
+	    stat_led4 = !stat_led4;
+
 	}
 }
 
 void onpush(MQTT::MessageData& md)
 {
     MQTT::Message &message = md.message;
-	DataElement de = DataElement((char*)message.payload);
-	pc.printf("onpush\n\r");
-	pc.printf("%d\n\r",de.getInt("v"));
-}
\ No newline at end of file
+    DataElement de = DataElement((char*)message.payload);
+    printf("onpush\n\r");
+    like_count++;
+}
+
+