Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2016
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.31 KB | None | 0 0
  1. SYSTEM_THREAD(ENABLED);
  2.  
  3. #include "FastLED/FastLED.h"
  4. FASTLED_USING_NAMESPACE;
  5.  
  6. #if FASTLED_VERSION < 3001000
  7. #error "Requires FastLED 3.1 or later; check github for latest code."
  8. #endif
  9.  
  10. // UDP SETTINGS
  11. #define SERVER_PORT 49692
  12. #define DISCOVERY_PORT 49692
  13. UDP client;
  14. IPAddress multicastIP(239, 15, 18, 2);
  15. bool connectLock = false;
  16.  
  17. // ORB SETTINGS
  18. unsigned int orbID = 1;
  19.  
  20. // LED settings
  21. #define DATA_PIN 6
  22. #define NUM_LEDS 24
  23. CRGB leds[NUM_LEDS];
  24.  
  25. // UDP BUFFERS
  26. #define BUFFER_SIZE 5 + 3 * NUM_LEDS
  27. #define BUFFER_SIZE_DISCOVERY 5
  28. #define TIMEOUT_MS 500
  29. uint8_t buffer[BUFFER_SIZE];
  30. uint8_t bufferDiscovery[BUFFER_SIZE_DISCOVERY];
  31. unsigned long lastWiFiCheck = 0;
  32.  
  33. // SMOOTHING SETTINGS
  34. #define SMOOTH_STEPS 50 // Steps to take for smoothing colors
  35. #define SMOOTH_DELAY 4 // Delay between smoothing steps
  36. #define SMOOTH_BLOCK 0 // Block incoming colors while smoothing
  37.  
  38. byte nextColor[3];
  39. byte prevColor[3];
  40. byte currentColor[3];
  41. byte smoothStep = SMOOTH_STEPS;
  42. unsigned long smoothMillis;
  43.  
  44. // CUSTOM COLOR CORRECTIONS
  45. #define RED_CORRECTION 255
  46. #define GREEN_CORRECTION 255
  47. #define BLUE_CORRECTION 255
  48.  
  49. void setup()
  50. {
  51. // WiFi
  52. lastWiFiCheck = millis();
  53. initWiFi();
  54.  
  55. // Leds - choose one correction method
  56.  
  57. // 1 - FastLED predefined color correction
  58. //FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS).setCorrection(TypicalSMD5050);
  59.  
  60. // 2 - Custom color correction
  61. FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS).setCorrection(CRGB(RED_CORRECTION, GREEN_CORRECTION, BLUE_CORRECTION));
  62.  
  63. // Uncomment the below lines to dim the single built-in led to 5%
  64. ::RGB.control(true);
  65. ::RGB.brightness(5);
  66. ::RGB.control(false);
  67. }
  68.  
  69. void initWiFi()
  70. {
  71. if(!connectLock)
  72. {
  73. connectLock = true;
  74.  
  75. // Wait for WiFi connection
  76. waitUntil(WiFi.ready);
  77.  
  78. // Client
  79. client.stop();
  80. client.begin(SERVER_PORT);
  81. //client.setBuffer(BUFFER_SIZE);
  82.  
  83. // Multicast group
  84. client.joinMulticast(multicastIP);
  85.  
  86. connectLock = false;
  87. }
  88. }
  89.  
  90. void loop(){
  91. // Check WiFi connection every minute
  92. if(millis() - lastWiFiCheck > 500)
  93. {
  94. lastWiFiCheck = millis();
  95. if(!WiFi.ready() || !WiFi.connecting())
  96. {
  97. initWiFi();
  98. }
  99. }
  100.  
  101. int packetSize = client.parsePacket();
  102.  
  103. if(packetSize == BUFFER_SIZE){
  104. client.read(buffer, BUFFER_SIZE);
  105. //client.flush();
  106. unsigned int i = 0;
  107.  
  108. // Look for 0xC0FFEE
  109. if(buffer[i++] == 0xC0 && buffer[i++] == 0xFF && buffer[i++] == 0xEE)
  110. {
  111. byte commandOptions = buffer[i++];
  112. byte rcvOrbID = buffer[i++];
  113.  
  114. byte red = buffer[i++];
  115. byte green = buffer[i++];
  116. byte blue = buffer[i++];
  117.  
  118. // Command options
  119. // 1 = force off
  120. // 2 = use lamp smoothing and validate by Orb ID
  121. // 4 = validate by Orb ID
  122. // 8 = discovery
  123. if(commandOptions == 1)
  124. {
  125. // Orb ID 0 = turn off all lights
  126. // Otherwise turn off selectively
  127. if(rcvOrbID == 0 || rcvOrbID == orbID)
  128. {
  129. smoothStep = SMOOTH_STEPS;
  130. forceLedsOFF();
  131. }
  132.  
  133. return;
  134. }
  135. else if(commandOptions == 2)
  136. {
  137. if(rcvOrbID != orbID)
  138. {
  139. return;
  140. }
  141.  
  142. setSmoothColor(red, green, blue);
  143. }
  144. else if(commandOptions == 4)
  145. {
  146. if(rcvOrbID != orbID)
  147. {
  148. return;
  149. }
  150.  
  151. smoothStep = SMOOTH_STEPS;
  152. setColor(red, green, blue);
  153. setSmoothColor(red, green, blue);
  154.  
  155. return;
  156. }
  157. else if(commandOptions == 8)
  158. {
  159. // Respond to remote IP address with Orb ID
  160. IPAddress remoteIP = client.remoteIP();
  161. bufferDiscovery[0] = orbID;
  162.  
  163. client.sendPacket(bufferDiscovery, BUFFER_SIZE_DISCOVERY, remoteIP, DISCOVERY_PORT);
  164.  
  165. // Clear buffer
  166. memset(bufferDiscovery, 0, sizeof(bufferDiscovery));
  167. return;
  168. }
  169. }
  170.  
  171. }else if(packetSize > 0){
  172. // Got malformed packet
  173. }
  174.  
  175. if (smoothStep < SMOOTH_STEPS && millis() >= (smoothMillis + (SMOOTH_DELAY * (smoothStep + 1))))
  176. {
  177. smoothColor();
  178. }
  179. }
  180.  
  181. // Set color
  182. void setColor(byte red, byte green, byte blue)
  183. {
  184. for (byte i = 0; i < NUM_LEDS; i++)
  185. {
  186. leds[i] = CRGB(red, green, blue);
  187. }
  188.  
  189. FastLED.show();
  190. }
  191.  
  192.  
  193. // Set a new color to smooth to
  194. void setSmoothColor(byte red, byte green, byte blue)
  195. {
  196. if (smoothStep == SMOOTH_STEPS || SMOOTH_BLOCK == 0)
  197. {
  198. if (nextColor[0] == red && nextColor[1] == green && nextColor[2] == blue)
  199. {
  200. return;
  201. }
  202.  
  203. prevColor[0] = currentColor[0];
  204. prevColor[1] = currentColor[1];
  205. prevColor[2] = currentColor[2];
  206.  
  207. nextColor[0] = red;
  208. nextColor[1] = green;
  209. nextColor[2] = blue;
  210.  
  211. smoothMillis = millis();
  212. smoothStep = 0;
  213. }
  214. }
  215.  
  216. // Display one step to the next color
  217. void smoothColor()
  218. {
  219. smoothStep++;
  220. currentColor[0] = prevColor[0] + (((nextColor[0] - prevColor[0]) * smoothStep) / SMOOTH_STEPS);
  221. currentColor[1] = prevColor[1] + (((nextColor[1] - prevColor[1]) * smoothStep) / SMOOTH_STEPS);
  222. currentColor[2] = prevColor[2] + (((nextColor[2] - prevColor[2]) * smoothStep) / SMOOTH_STEPS);
  223.  
  224. setColor(currentColor[0], currentColor[1], currentColor[2]);
  225. }
  226.  
  227. // Force all leds OFF
  228. void forceLedsOFF()
  229. {
  230. setColor(0,0,0);
  231. clearSmoothColors();
  232. }
  233.  
  234. // Clear smooth color byte arrays
  235. void clearSmoothColors()
  236. {
  237. memset(prevColor, 0, sizeof(prevColor));
  238. memset(currentColor, 0, sizeof(nextColor));
  239. memset(nextColor, 0, sizeof(nextColor));
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement