blynk & neopixelring & w7500

Dependencies:   BlynkNeopixelW7500 Blynk_Example_WIZwiki-W7500 WIZnetInterface_ WS2812 mbed

Fork of Blynk_Example_WIZwiki-W7500 by IOP

Revision:
2:270491d6e155
Parent:
1:030843f74e27
Child:
3:523ddb62698b
--- a/main.cpp	Tue Sep 05 23:22:02 2017 +0000
+++ b/main.cpp	Mon Nov 27 11:50:09 2017 +0000
@@ -24,32 +24,106 @@
 
 /* Comment this out to disable prints and save space */
 #define BLYNK_PRINT Serial
+#define WS2812_BUF 8
 
+#include "WS2812.h"
 #include "mbed.h"
 #include <SPI.h>
 #include "EthernetInterface.h"
 #include <BlynkSimpleEthernet2.h>
 
+WS2812 ws(D2, WS2812_BUF, 0, 1, 1, 0);
 
-EthernetInterface eth0;
+void getBuf(int buf[], int NUM, uint8_t r, uint8_t g, uint8_t b);
+uint32_t Wheel(uint8_t WheelPos);
 
 // You should get Auth Token in the Blynk App.
 // Go to the Project Settings (nut icon).
-char auth[] = "f6e02fba337e45f19f9c51567323ea8d";
+char auth[] = "5f3177d2555a42188845f99ebc0d76ad";
+
+int colorbuf[WS2812_BUF];
+int offbuf[WS2812_BUF];
+
+BLYNK_WRITE(V0)
+{
+    int d = param.asInt();
+    printf("on/off button value : %d\r\n", d);
+    if(d==0) ws.write(colorbuf);
+    else ws.write(offbuf);
+}
+
+BLYNK_WRITE(V1)
+{
+    int d = param.asInt();
+    printf("brightness value : %d\r\n", d);
+    ws.useII(WS2812::GLOBAL);
+    ws.setII(d);
+    ws.write(colorbuf);
+}
 
+BLYNK_WRITE(V2)
+{
+    int r = param[0].asInt();
+    int g = param[1].asInt();
+    int b = param[2].asInt();
+    printf("rgb value : %d %d %d\r\n", r, g, b);
+    getBuf(colorbuf, WS2812_BUF, (uint8_t)r, (uint8_t)g, (uint8_t)b);
+    ws.write(colorbuf);
+}
 
+BLYNK_WRITE(V3)
+{
+    int d = param.asInt();
+    printf("rainbow button value : %d\r\n", d);
+    while(d){
+         for(int j=0; j<256*5; j++) { 
+            for(int i=0; i< WS2812_BUF; i++) {
+                colorbuf[i]=Wheel(((i * 256 / WS2812_BUF) + j) & 255);
+            }
+            ws.write(colorbuf);
+            wait_ms(20);
+        }
+    }
+}
 
 
 int main(void) {
+    uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0xff, 0xff, 0x48};   
     printf("Hello\r\n");
-    uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0xff, 0xff, 0x25};   
-    eth0.init(mac_addr);
-    eth0.connect();
+    getBuf(offbuf, WS2812_BUF, 0x00, 0x00, 0x00);
+    getBuf(colorbuf, WS2812_BUF, 0xff, 0xff, 0xff);
     
-    Blynk.begin(auth);
-    
+    Blynk.begin(auth, "blynk-cloud.com", 8442, mac_addr);
     printf("Blynk init!\r\n");
     while(1){
         Blynk.run();
     }    
+}
+
+void getBuf(int buf[], int NUM, uint8_t r, uint8_t g, uint8_t b){
+    for(int i=0; i<NUM; i++) {
+        buf[i]=0;
+        buf[i] |= (r<<16 & 0xff0000);
+        buf[i] |= (g<<8 & 0x00ff00);
+        buf[i] |= (b & 0x0000ff);
+    }
+}
+
+//255가지의 색을 나타내는 함수
+uint32_t Wheel(uint8_t WheelPos) {
+  uint32_t data;
+  if(WheelPos < 85) {
+   data=(((WheelPos * 3)<<16) & 0xff0000) | (((255 - WheelPos * 3)<<8) & 0x00ff00 ) | (0 & 0x0000ff);
+  }
+  
+  else if(WheelPos < 170) {
+   WheelPos -= 85;
+   data= (((255-WheelPos * 3)<<16) & 0xff0000)| ((0<<8) & 0x00ff00 ) | ((WheelPos * 3) & 0x0000ff);
+  }
+  
+  else {
+   WheelPos -= 170;
+    data= ((0<<16) & 0xff0000)| (((WheelPos * 3)<<8) & 0x00ff00 ) | ((255-WheelPos * 3) & 0x0000ff);
+  }
+  return data;
 }
\ No newline at end of file