Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 KB | None | 0 0
  1. /**********************************
  2. Generic template for creating *duino firmware that works with
  3. BiblioPixel DriverSerial. Currently assumes the use of FastLED
  4. but could be reworked for other libraries.
  5. If using "SmartMatrix" style panels checkout:
  6. https://github.com/ManiacalLabs/BiblioPixelSmartMatrix
  7. **********************************/
  8. #include "FastLED.h"
  9.  
  10. /****************************
  11. All Firmware options go here!
  12. ****************************/
  13. // Uncomment below line if EEPROM available on your board
  14. // #define USE_EEPROM
  15.  
  16. // If not using EEPROM, you can specify device ID here
  17. #ifndef USE_EEPROM
  18. const uint8_t deviceID = 0;
  19. #endif
  20.  
  21. // How many leds in your strip?
  22. #define NUM_LEDS 150
  23.  
  24. #define DATA_PIN MOSI
  25. //#define CLOCK_PIN SCK
  26.  
  27. #define LED_TYPE NEOPIXEL
  28. //#define COLOR_ORDER BGR
  29.  
  30. CRGB leds[NUM_LEDS]; // Define the array of leds
  31. /***************************
  32. End Firmware options
  33. ***************************/
  34.  
  35. #ifdef USE_EEPROM
  36. #include <EEPROM.h>
  37. #endif
  38.  
  39. #define FIRMWARE_VER 3
  40.  
  41. namespace CMDTYPE
  42. {
  43. enum CMDTYPE
  44. {
  45. SETUP_DATA = 1,
  46. PIXEL_DATA = 2,
  47. BRIGHTNESS = 3,
  48. GETID = 4,
  49. SETID = 5,
  50. GETVER = 6
  51. };
  52. }
  53.  
  54. namespace RETURN_CODES
  55. {
  56. enum RETURN_CODES
  57. {
  58. SUCCESS = 255,
  59. REBOOT = 42,
  60. ERROR = 0,
  61. ERROR_SIZE = 1,
  62. ERROR_UNSUPPORTED = 2,
  63. ERROR_PIXEL_COUNT = 3,
  64. ERROR_BAD_CMD = 4,
  65. };
  66. }
  67.  
  68. typedef struct __attribute__((__packed__))
  69. {
  70. uint8_t type;
  71. uint16_t pixelCount;
  72. uint8_t spiSpeed;
  73. } config_t;
  74.  
  75. uint16_t numLEDs = NUM_LEDS;
  76. uint8_t bytesPerPixel = 3;
  77.  
  78. inline void setupFastLED()
  79. {
  80. //There are many options that could be used here.
  81. //Checkout the FastLED blink example or their documentation for more:
  82. // https://github.com/FastLED/FastLED/blob/master/examples/Blink/Blink.ino
  83. //But one of the below should work for standard one or two pin LEDs
  84. //Just change the config options above
  85.  
  86. //Data and Clock LEDs
  87. //FastLED.addLeds<LED_TYPE, CLOCK_PIN, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  88.  
  89. //Data only LEDs
  90. //FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  91.  
  92. //Neopixel
  93. FastLED.addLeds<LED_TYPE, DATA_PIN>(leds, NUM_LEDS);
  94.  
  95. FastLED.clear();
  96. FastLED.show();
  97. }
  98.  
  99. void setup()
  100. {
  101. // Full USB 1.1 speed, assuming your chip has native USB
  102. Serial.begin(12000000);
  103. Serial.setTimeout(1000);
  104.  
  105. setupFastLED();
  106. }
  107.  
  108. #define EMPTYMAX 100
  109. inline void getData()
  110. {
  111. static char cmd = 0;
  112. static uint16_t size = 0;
  113. static uint16_t count = 0;
  114. static uint8_t emptyCount = 0;
  115. static size_t c = 0;
  116. static uint16_t packSize = numLEDs * bytesPerPixel;
  117.  
  118. if (Serial.available())
  119. {
  120. cmd = Serial.read();
  121. size = 0;
  122. Serial.readBytes((char*)&size, 2);
  123.  
  124. if (cmd == CMDTYPE::PIXEL_DATA)
  125. {
  126. count = 0;
  127. emptyCount = 0;
  128.  
  129. if (size == packSize)
  130. {
  131. while (count < packSize)
  132. {
  133. c = Serial.readBytes(((char*)&leds) + count, packSize - count);
  134. if (c == 0)
  135. {
  136. emptyCount++;
  137. if(emptyCount > EMPTYMAX) break;
  138. }
  139. else
  140. {
  141. emptyCount = 0;
  142. }
  143.  
  144. count += c;
  145. }
  146. }
  147.  
  148. uint8_t resp = RETURN_CODES::SUCCESS;
  149. if (count == packSize)
  150. {
  151. FastLED.show();
  152. }
  153. else
  154. resp = RETURN_CODES::ERROR_SIZE;
  155.  
  156. Serial.write(resp);
  157. }
  158. else if(cmd == CMDTYPE::GETID)
  159. {
  160. #ifdef USE_EEPROM
  161. Serial.write(EEPROM.read(16));
  162. #else
  163. Serial.write(deviceID);
  164. #endif
  165. }
  166. else if(cmd == CMDTYPE::SETID)
  167. {
  168. if(size != 1)
  169. {
  170. Serial.write(RETURN_CODES::ERROR_SIZE);
  171. }
  172. else
  173. {
  174. uint8_t id = Serial.read();
  175. #ifdef USE_EEPROM
  176. EEPROM.write(16, id);
  177. #endif
  178. Serial.write(RETURN_CODES::SUCCESS);
  179. }
  180. }
  181. else if (cmd == CMDTYPE::SETUP_DATA)
  182. {
  183. // for(int i=0; i<size; i++) Serial.read();
  184.  
  185. uint8_t result = RETURN_CODES::SUCCESS;
  186. config_t temp;
  187.  
  188. if (size != sizeof(config_t))
  189. {
  190. result = RETURN_CODES::ERROR_SIZE;
  191. }
  192. else
  193. {
  194. size_t read = Serial.readBytes((char*)&temp, sizeof(config_t));
  195. if (read != size)
  196. {
  197. result = RETURN_CODES::ERROR_SIZE;
  198. }
  199. else
  200. {
  201. if(temp.pixelCount / bytesPerPixel != NUM_LEDS)
  202. result = RETURN_CODES::ERROR_PIXEL_COUNT;
  203. }
  204. }
  205.  
  206. Serial.write(result);
  207. }
  208. else if (cmd == CMDTYPE::BRIGHTNESS)
  209. {
  210. uint8_t result = RETURN_CODES::SUCCESS;
  211. if (size != 1)
  212. result = RETURN_CODES::ERROR_SIZE;
  213. else
  214. {
  215. uint8_t brightness = 255;
  216. size_t read = Serial.readBytes((char*)&brightness, 1);
  217. if (read != size)
  218. result = RETURN_CODES::ERROR_SIZE;
  219. else
  220. {
  221. FastLED.setBrightness(brightness);
  222. }
  223. }
  224.  
  225. Serial.write(result);
  226. }
  227. else if (cmd == CMDTYPE::GETVER)
  228. {
  229. Serial.write(RETURN_CODES::SUCCESS);
  230. Serial.write(FIRMWARE_VER);
  231. }
  232. else
  233. {
  234. Serial.write(RETURN_CODES::ERROR_BAD_CMD);
  235. }
  236.  
  237.  
  238. Serial.flush();
  239. }
  240. }
  241.  
  242. void loop()
  243. {
  244. getData();
  245. FastLED.delay(0);
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement