Junichi Katsu / Mbed 2 deprecated MilkcocoaSample_Eth

Dependencies:   EthernetInterface Milkcocoa_EthernetIF mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "LEDStrip.h"
00003 #include "ConfigFile.h"
00004 #include "MQTTEthernet.h"
00005 #include "MQTTClient.h"
00006 #include "Milkcocoa.h"
00007 #include "MClient.h"
00008 
00009 #define LED_NUM         60
00010 #define LED_DATA_NUM    3
00011 #define PI 3.14159265359f
00012 #define MAX_COUNT       60
00013 #define STAR_LINE_MAX   60
00014 
00015 #if 0
00016 #define DBG(x) x
00017 #else
00018 #define DBG(x)
00019 #endif
00020 
00021 /************************* Your Milkcocoa Setup *********************************/
00022 #define MILKCOCOA_APP_ID      "juicej2vchzis"
00023 #define MILKCOCOA_DATASTORE   "halloween"
00024  
00025 /************* Milkcocoa Setup (you don't need to change this!) ******************/
00026  
00027 #define MILKCOCOA_SERVERPORT  1883
00028  
00029 /************ Global State (you don't need to change this!) ******************/
00030  
00031 const char MQTT_SERVER[]  = MILKCOCOA_APP_ID ".mlkcca.com";
00032 const char MQTT_CLIENTID[] = __TIME__ MILKCOCOA_APP_ID;
00033  
00034 extern void onpush(MQTT::MessageData& md);
00035 
00036 Serial pc(USBTX, USBRX);
00037 LocalFileSystem local("local");
00038 Ticker flipper;
00039 ConfigFile cfg;
00040 DigitalIn SW1(p25);
00041 DigitalIn SW2(p26);
00042 DigitalOut stat_led1(LED1);
00043 DigitalOut stat_led2(LED2);
00044 DigitalOut stat_led3(LED3);
00045 DigitalOut stat_led4(LED4);
00046 
00047 
00048 
00049 void flip();
00050 
00051 bool completed = false;
00052 int t_timer = 0;
00053 int like_count = 0;
00054 int max_count = 0;
00055 int demo_count = 0;
00056 
00057 const unsigned char _RED[3] =   { 0xFF, 0   , 0    };
00058 const unsigned char _GREEN[3] =     { 0   , 0xFF, 0    };
00059 const unsigned char _BLUE[3] =  { 0   , 0   , 0xFF };
00060 const unsigned char _YELLOW[3] = { 0xFF, 0xFF, 0    };
00061 const unsigned char _PURPLE[3] = { 0xFF, 0   , 0xFF };
00062 const unsigned char _AQUA[3] =  { 0   , 0xFF, 0xFF };
00063 const unsigned char _WHITE[3] =     { 0xFF, 0xFF, 0xFF };
00064 const unsigned char _GLAY[3] =  { 0x80, 0x80, 0x80 };
00065 const unsigned char _DARK_GLAY[3] = { 0x2, 0x2, 0x2 };
00066 const unsigned char _OFF[3] =   { 0, 0, 0 };
00067 
00068 unsigned char led_data[60][3];
00069 
00070 unsigned char base_led_data[60][3];
00071 
00072 unsigned char star_line[STAR_LINE_MAX] = { 
00073      0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
00074      10,11,12,13,14,15,16,17,18,19,
00075      20,21,22,23,24,25,26,27,28,29,
00076      30,31,32,33,34,35,36,37,38,39,
00077      40,41,42,43,44,45,46,47,48,49,
00078      50,51,52,53,54,55,56,57,58,59
00079 };
00080 
00081 unsigned char color_line[STAR_LINE_MAX][3] =
00082 {
00083     { 0xFF, 0x00, 0x00 },{ 0xFF, 0x80, 0x00 },{ 0xFF, 0xFF, 0x00 },{ 0x80, 0xFF, 0x00 },
00084     { 0x00, 0xFF, 0x00 },{ 0x00, 0xFF, 0x80 },{ 0x00, 0xFF, 0xFF },{ 0x00, 0x80, 0xFF },
00085     { 0x00, 0x00, 0xFF },{ 0x80, 0x00, 0xFF },{ 0xFF, 0x00, 0xFF },{ 0xFF, 0x00, 0x80 },
00086     { 0xFF, 0x00, 0x00 },{ 0xFF, 0x80, 0x00 },{ 0xFF, 0xFF, 0x00 },{ 0x80, 0xFF, 0x00 },
00087     { 0x00, 0xFF, 0x00 },{ 0x00, 0xFF, 0x80 },{ 0x00, 0xFF, 0xFF },{ 0x00, 0x80, 0xFF },
00088     { 0x00, 0x00, 0xFF },{ 0x80, 0x00, 0xFF },{ 0xFF, 0x00, 0xFF },{ 0xFF, 0x00, 0x80 },
00089     { 0xFF, 0x00, 0x00 },{ 0xFF, 0x80, 0x00 },{ 0xFF, 0xFF, 0x00 },{ 0x80, 0xFF, 0x00 },
00090     { 0x00, 0xFF, 0x00 },{ 0x00, 0xFF, 0x80 },{ 0x00, 0xFF, 0xFF },{ 0x00, 0x80, 0xFF },
00091     { 0x00, 0x00, 0xFF },{ 0x80, 0x00, 0xFF },{ 0xFF, 0x00, 0xFF },{ 0xFF, 0x00, 0x80 },
00092     { 0xFF, 0x00, 0x00 },{ 0xFF, 0x80, 0x00 },{ 0xFF, 0xFF, 0x00 },{ 0x80, 0xFF, 0x00 },
00093     { 0x00, 0xFF, 0x00 },{ 0x00, 0xFF, 0x80 },{ 0x00, 0xFF, 0xFF },{ 0x00, 0x80, 0xFF },
00094     { 0x00, 0x00, 0xFF },{ 0x80, 0x00, 0xFF },{ 0xFF, 0x00, 0xFF },{ 0xFF, 0x00, 0x80 },
00095     { 0xFF, 0x00, 0x00 },{ 0xFF, 0x80, 0x00 },{ 0xFF, 0xFF, 0x00 },{ 0x80, 0xFF, 0x00 },
00096     { 0x00, 0xFF, 0x00 },{ 0x00, 0xFF, 0x80 },{ 0x00, 0xFF, 0xFF },{ 0x00, 0x80, 0xFF },
00097     { 0x00, 0x00, 0xFF },{ 0x80, 0x00, 0xFF },{ 0xFF, 0x00, 0xFF },{ 0xFF, 0x00, 0x80 }
00098 
00099 };
00100 
00101 extern void send_led();
00102 
00103 int DownLight(unsigned char *dat,int value)
00104 {
00105     for(int i=0;i<LED_DATA_NUM;i++)
00106     {
00107         if( dat[i] > value )
00108         {
00109             dat[i] = dat[i] - value;
00110         }
00111     }
00112     return(0);
00113 }
00114 
00115 int near(unsigned char *from_dat,const unsigned char *to_dat,int wide)
00116 {
00117     int ret_code_l = 1;
00118     
00119     for( int i=0;i<LED_DATA_NUM;i++)
00120     {
00121         if( from_dat[i] < to_dat[i] )
00122         {
00123             if( (from_dat[i]+wide) < to_dat[i] )
00124             {
00125                 from_dat[i] = from_dat[i] + wide;
00126                 ret_code_l = 0;
00127             }
00128             else
00129             {
00130                 from_dat[i] = to_dat[i];
00131 
00132             }
00133         }
00134         else if( from_dat[i] > to_dat[i] )
00135         {
00136             if( (from_dat[i]-wide) > to_dat[i])
00137             {
00138                 from_dat[i] -= wide;
00139                 ret_code_l = 0;
00140             }
00141             else
00142             {
00143                 from_dat[i] = to_dat[i];
00144             }
00145         }
00146     }
00147     
00148     return(ret_code_l);
00149 }
00150 
00151 int flow_star(int from,int to,const unsigned char *dat,const unsigned char *base_dat,int speed)
00152 {
00153     int i,j;
00154     
00155     if( from < to )
00156     {
00157         for(i=from;i<to;i++)
00158         {
00159             memcpy( led_data[star_line[i]] , base_dat , 3 );
00160         }
00161         
00162         send_led();
00163         wait_ms(speed);
00164         
00165         for(i=from;i<to;i++)
00166         {
00167             for(j=from;j<to;j++)
00168             {
00169                 if(j==i)
00170                 {
00171                     memcpy( led_data[star_line[j]] , dat , 3 );
00172                 }
00173                 else
00174                 {
00175                     near( led_data[star_line[j]] , base_dat , 5 );
00176                 }
00177             }
00178             send_led();
00179             wait_ms(speed);
00180             
00181         }
00182         
00183         i = 0;
00184         j = 1;
00185         while(i!=(abs(from-to)))
00186         {
00187             i = 0;
00188             for(j=from;j<to;j++)
00189             {
00190                 i += near( led_data[star_line[j]] , base_dat , 10 );
00191             }
00192             send_led();
00193             wait_ms(speed);
00194         }
00195     }
00196     else
00197     {
00198         for(i=from;i>to;i--)
00199         {
00200             memcpy( led_data[star_line[i]] , base_dat , 3 );
00201         }
00202         
00203         send_led();
00204         wait_ms(speed);
00205         
00206         for(i=from;i>to;i--)
00207         {
00208             for(j=from;j>to;j--)
00209             {
00210                 if(j==i)
00211                 {
00212                     memcpy( led_data[star_line[j]] , dat , 3 );
00213                 }
00214                 else
00215                 {
00216                     near( led_data[star_line[j]] , base_dat , 5 );
00217                 }
00218             }
00219             send_led();
00220             wait_ms(speed);
00221         }
00222         
00223         i = 0;
00224         j = 1;
00225         while(i!=(abs(from-to)))
00226         {
00227             i = 0;
00228             for(j=from;j>to;j--)
00229             {
00230                 i += near( led_data[star_line[j]] , base_dat , 5 );
00231             }
00232             send_led();
00233             wait_ms(speed);
00234         }
00235     }
00236     
00237     return(0);
00238 }
00239 
00240 int flow_star(int from,int to,const unsigned char *dat,int speed)
00241 {
00242     int i,j;
00243     
00244     if( from < to )
00245     {
00246         for(i=from;i<to;i++)
00247         {
00248             memcpy( led_data[star_line[i]] , base_led_data[star_line[i]] , 3 );
00249         }
00250         
00251         send_led();
00252         wait_ms(speed);
00253         
00254         for(i=from;i<to;i++)
00255         {
00256             for(j=from;j<to;j++)
00257             {
00258                 if(j==i)
00259                 {
00260                     memcpy( led_data[star_line[j]] , dat , 3 );
00261                 }
00262                 else
00263                 {
00264                     near( led_data[star_line[j]] , base_led_data[star_line[j]] , 5 );
00265                 }
00266             }
00267             send_led();
00268             wait_ms(speed);
00269             
00270         }
00271         
00272         i = 0;
00273         j = 1;
00274         while(i!=(abs(from-to)))
00275         {
00276             i = 0;
00277             for(j=from;j<to;j++)
00278             {
00279                 i += near( led_data[star_line[j]] , base_led_data[star_line[j]] , 10 );
00280             }
00281             send_led();
00282             wait_ms(speed);
00283         }
00284     }
00285     else
00286     {
00287         for(i=from;i>to;i--)
00288         {
00289             memcpy( led_data[star_line[i]] , base_led_data[star_line[i]] , 3 );
00290         }
00291         
00292         send_led();
00293         wait_ms(speed);
00294         
00295         for(i=from;i>to;i--)
00296         {
00297             for(j=from;j>to;j--)
00298             {
00299                 if(j==i)
00300                 {
00301                     memcpy( led_data[star_line[j]] , dat , 3 );
00302                 }
00303                 else
00304                 {
00305                     near( led_data[star_line[j]] , base_led_data[star_line[j]] , 5 );
00306                 }
00307             }
00308             send_led();
00309             wait_ms(speed);
00310         }
00311         
00312         i = 0;
00313         j = 1;
00314 #if 0
00315         while(i!=(abs(from-to)))
00316         {
00317             i = 0;
00318             for(j=from;j>to;j--)
00319             {
00320                 i += near( led_data[star_line[j]] , base_led_data[star_line[j]] , 5 );
00321             }
00322             send_led();
00323             wait_ms(speed);
00324         }
00325 #endif
00326     }
00327     
00328     return(0);
00329 }
00330 
00331 void slow_light_all(const unsigned char *base_dat,int speed)
00332 {
00333     int i,j;
00334     while(i!=LED_NUM)
00335     {
00336         i = 0;
00337         for(j=0;j<LED_NUM;j++)
00338         {
00339             i += near( led_data[j] , base_dat , 5 );
00340         }
00341         send_led();
00342         wait_ms(speed);
00343     }
00344 }
00345 
00346 void back_base(void)
00347 {
00348     for(int i=0;i<LED_NUM;i++)
00349     {
00350         near( led_data[i] , base_led_data[i] , 5 );
00351         send_led();
00352     }
00353 }
00354 
00355 int glad(int i,unsigned char *dat)
00356 {
00357     i = i % 0x2FE;
00358     
00359     if( i < 0xFF )
00360     {
00361         dat[0] = 0xFF * cos(PI/(510.0f/(double)i));
00362         dat[1] = 0xFF * sin(PI/(510.0f/(double)i));
00363         dat[2] = 0;
00364     }
00365     else if((i >= 0xFF)&&( i < 0x1FE ))
00366     {
00367         i -= 0xFF;
00368         dat[0] = 0;
00369         dat[1] = 0xFF * cos(PI/(510.0f/(double)i));
00370         dat[2] = 0xFF * sin(PI/(510.0f/(double)i));
00371     }
00372     else if((i >= 0x1FE )&&( i < 0x2FD ))
00373     {
00374         i -= 0x1FE;
00375         dat[0] = 0xFF * sin(PI/(510.0f/(double)i));
00376         dat[1] = 0;
00377         dat[2] = 0xFF * cos(PI/(510.0f/(double)i));
00378     }
00379     return(0);
00380 
00381 }
00382 
00383 void idle_cycle(int like) {
00384     static int old_like_count_i = 0;
00385     static int cycle = 0;
00386     static int gorl_flag = 0;
00387     static int gol_count = 0;
00388     int point = STAR_LINE_MAX - like; //STAR_LINE_MAX - (int)((float)like / ((float)max_count / (float)STAR_LINE_MAX));
00389     
00390     cycle++;
00391     
00392     if( old_like_count_i != like ) {
00393 
00394         flipper.detach();
00395         
00396         if( like < max_count ) {
00397             for(int j=0;j<60;j++) {
00398                 memcpy( led_data[j] , base_led_data[j] , 3 );
00399             }
00400             printf("COUNT!! %d\r\n",point);
00401             flow_star(0,point+1,_YELLOW,20);
00402             flow_star(point,point+1,color_line[point],_WHITE,50);
00403             flow_star(point,point+1,color_line[point],_WHITE,50);
00404             flow_star(point,point+1,_WHITE,color_line[point],50);
00405             memcpy(base_led_data[star_line[point]] , color_line[point] , 3 );
00406         }
00407         else if(gorl_flag == 0) {
00408             gorl_flag = 1;
00409             flow_star(0,STAR_LINE_MAX,_RED,10);
00410             flow_star(0,STAR_LINE_MAX,_PURPLE,10);
00411             flow_star(0,STAR_LINE_MAX,_BLUE,10);
00412             flow_star(0,STAR_LINE_MAX,_AQUA,10);
00413             flow_star(0,STAR_LINE_MAX,_RED,10);
00414             flow_star(0,STAR_LINE_MAX,_GREEN,10);
00415             flow_star(0,STAR_LINE_MAX,_YELLOW,_OFF,30);
00416         }
00417         else {
00418             flow_star(0,STAR_LINE_MAX,_YELLOW,30);
00419         }
00420         
00421         flipper.attach(&flip, 2.0);
00422     }
00423     old_like_count_i = like;
00424     
00425     if(gorl_flag == 1) {
00426         for(int j=0;j<60;j++) {
00427             glad(gol_count+(j*80),base_led_data[j]);
00428             memcpy( led_data[j] , base_led_data[j] , 3 );
00429         }
00430     }
00431     else {
00432         for(int i=0;i<STAR_LINE_MAX;i++) {
00433             if( i >= point ) {
00434                 memcpy(base_led_data[star_line[i]] , color_line[i] , 3 );
00435             }
00436         }
00437     }
00438     
00439     back_base();
00440     
00441     gol_count += 30 ;
00442     if( gol_count >= 0x2FD ) {
00443         gol_count = 0;
00444     }
00445 
00446 }
00447 
00448 void like_change(int like)
00449 {
00450 
00451 }
00452 
00453 void send_led()
00454 {
00455     for(int i=0;i<LED_NUM;i++)
00456     {
00457         tapeSet(i,led_data[i][0] << 16 | led_data[i][1] << 8 | led_data[i][2]);
00458     }
00459     tapeSend();
00460 }
00461 
00462 
00463 void flip() {
00464    // like_count += 1;
00465     led_data[star_line[demo_count]][0] /= 2;
00466     led_data[star_line[demo_count]][1] /= 2;
00467     led_data[star_line[demo_count]][2] /= 2;
00468     
00469     demo_count += 1;
00470     
00471     if( demo_count >= STAR_LINE_MAX )
00472     {
00473         demo_count = 0;
00474     }
00475 }
00476 
00477 int main() {
00478     int old_like_count = 1;
00479     char tag[64];
00480     char tmp[5];
00481     char tmp2[5];
00482     
00483     SW1.mode(PullUp);
00484     SW2.mode(PullUp);
00485     
00486     if( SW1 == 0 ) { // DEMO MODE
00487         tapeInit(0, LED_NUM+2);
00488 
00489         while(1) {
00490             flow_star(0,STAR_LINE_MAX,_RED   ,_OFF,30);
00491             wait(0.5);
00492             flow_star(0,STAR_LINE_MAX,_PURPLE,_OFF,30);
00493             wait(0.5);
00494             flow_star(0,STAR_LINE_MAX,_BLUE  ,_OFF,30);
00495             wait(0.5);
00496             flow_star(0,STAR_LINE_MAX,_AQUA  ,_OFF,30);
00497             wait(0.5);
00498             flow_star(0,STAR_LINE_MAX,_GREEN ,_OFF,30);
00499             wait(0.5);
00500             flow_star(0,STAR_LINE_MAX,_YELLOW,_OFF,30);
00501             wait(0.5);
00502         }
00503     }
00504 #if 0
00505     cfg.read("/local/env.ini");
00506     
00507     if (cfg.getValue("TAG", &tag[0], sizeof(tag))) {
00508         printf("'%s'='%s'\n\r", "TAG", tag);
00509     }
00510     else
00511     {
00512         sprintf(tag,"ma9");
00513     }
00514     if (cfg.getValue("COUNT", &tmp[0], sizeof(tmp))) {
00515         printf("'%s'='%s'\n\r", "COUNT", tmp);
00516         like_count = atoi(tmp);
00517     }
00518     else
00519     {
00520         like_count = 0;
00521     }
00522     if (cfg.getValue("MAX", &tmp2[0], sizeof(tmp2))) {
00523         printf("'%s'='%s'\n\r", "MAX", tmp2);
00524         max_count = atoi(tmp2);
00525     }
00526     else
00527     {
00528         max_count = 0;
00529     }
00530 #endif
00531     like_count = 0;
00532     max_count = 60;
00533     
00534     printf("ini file read\r\n");
00535     printf("HashTag:%s\r\n",tag);
00536     printf("Now Count:%d\r\n",like_count);
00537     printf("Max Count:%d\r\n",max_count);
00538     
00539     stat_led1 = 1;
00540         
00541     for(int i=0;i<LED_NUM;i++) {
00542         memcpy( led_data[i] , _OFF , 3 );
00543         memcpy( base_led_data[i] , _DARK_GLAY , 3 );
00544     }
00545     
00546     tapeInit(0, LED_NUM+2);
00547 
00548     send_led();
00549         
00550     slow_light_all(_DARK_GLAY , 50);
00551     
00552 
00553     MQTTEthernet *ipstack = new MQTTEthernet();
00554     MClient *client = new MClient(ipstack);
00555     Milkcocoa *milkcocoa = new Milkcocoa(client, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID);
00556 
00557     pc.printf("Milkcocoa mbed ver demo\n\r\n\r\n\r");
00558     
00559     
00560     wait(2);
00561     
00562     milkcocoa->connect();
00563     while(milkcocoa->getConnectStatus()!=1) {
00564         pc.printf("\n\rEther connected error\n\r");
00565         pc.printf("\n\rEther re connect\n\r");
00566         ipstack->reconnect();
00567         wait(2);
00568         milkcocoa->connect();
00569     }
00570     
00571     stat_led2 = 1;
00572     
00573     int ret = milkcocoa->on(MILKCOCOA_DATASTORE, "push", onpush);
00574         pc.printf("%d\n\r",ret);
00575     while(ret != 1){
00576         wait(1);
00577         pc.printf("%d\n\r",ret);
00578         ret = milkcocoa->on(MILKCOCOA_DATASTORE, "push", onpush);
00579     }
00580     
00581     stat_led3 = 1;
00582     
00583     while(1)
00584     {
00585         milkcocoa->loop();
00586         
00587         if(like_count != old_like_count)
00588         {
00589             printf("%04d\r\n",like_count);
00590             sprintf(&tmp[0],"%d",like_count);
00591             //cfg.setValue("COUNT", tmp);
00592             if(cfg.write("/local/env.ini"))
00593             {
00594                 printf("SET COUNT\r\n");
00595             }
00596             else
00597             {
00598                 printf("SET ERR\r\n");
00599             }
00600             old_like_count = like_count;
00601         }
00602         
00603         idle_cycle(like_count);
00604         
00605         stat_led4 = !stat_led4;
00606 
00607     }
00608 }
00609 
00610 void onpush(MQTT::MessageData& md)
00611 {
00612     MQTT::Message &message = md.message;
00613     DataElement de = DataElement((char*)message.payload);
00614     printf("onpush\n\r");
00615     like_count++;
00616 }
00617 
00618