MrRockchip

super_2560

May 25th, 2022 (edited)
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.70 KB | None | 0 0
  1. //// [000_000000][001_000000][002_000000][003_000000][004_000000][005_000000][006_000000][007_000000][001_FF0000]{
  2. //// [@@@_FFFFFF] [@@@_1B00EA] [@@@_1B00EA]![@@@_1B00EA]"[@@@_1B00EA]#[@@@_1B00EA]!
  3.  
  4. #include <WS2812FX.h>
  5.  
  6. #define PIN   48
  7. #define N_LEDS 8
  8.  
  9. WS2812FX strip = WS2812FX(N_LEDS, PIN, NEO_GRB + NEO_KHZ800);
  10.  
  11. String srl_string = "";
  12. String led_string = "";
  13. String rgb_string = "";
  14.  
  15. uint32_t i   = 0;
  16. uint32_t led = 0;
  17. uint32_t rgb = 0;
  18.  
  19. uint8_t r = 0;
  20. uint8_t g = 0;
  21. uint8_t b = 0;
  22. uint8_t m = 0;
  23.  
  24. char incoming_chr = "";
  25.  
  26. uint8_t command = 0;
  27.  
  28. // Initial setup
  29. void setup()
  30. {
  31.   Serial.begin(115200); // Initialize the debug serial port
  32.  /*
  33.   * It is recommended to use the lower baud rate. The higher the baud rate,
  34.   * the higher the bit error rate will be, and the control action might fail.
  35.   */
  36.   Serial3.begin(4800); // Initialize the ESP8266 <=> MEGA2560 serial port
  37.   //// strip.begin();
  38.   strip.init();
  39.   strip.setBrightness(127); // 255 ?
  40.   strip.setSpeed(255);      // 1000 ?
  41.   strip.setMode(FX_MODE_STATIC);
  42.   strip.start();
  43.   //// pinMode(LED_BUILTIN, OUTPUT); // Initialize the digital pin LED_BUILTIN as an output
  44. }
  45.  
  46. // Read a command from ESP8266 over serial
  47. void read_string()
  48. {
  49.   while (Serial3.available() > 0) {
  50.     incoming_chr = Serial3.read();
  51.     srl_string += incoming_chr;
  52.     Serial.print(incoming_chr); // DEBUG
  53.     switch (incoming_chr) {
  54.       case '[':
  55.         command = 1;
  56.         led_string = "";
  57.         rgb_string = "";
  58.         break;
  59.       case ']':
  60.         command = 0;
  61.         if ((led_string.length() == 3) && (rgb_string.length() == 6)) {
  62.           rgb = (uint32_t) strtol(&rgb_string[0], NULL, 16);
  63.           r = rgb >> 16;
  64.           g = rgb >> 8 & 0xFF;
  65.           b = rgb & 0xFF;
  66.           if ((led_string[0] == "@") && (led_string[1] == "@") && (led_string[2] == "@")) {
  67.             for (i = 0; i < N_LEDS; i++) {
  68.               strip.setPixelColor(i, r, g, b);
  69.             }
  70.           }
  71.           else {
  72.             led = (uint32_t) strtol( &led_string[0], NULL, 16);
  73.             if (led < N_LEDS) {
  74.               strip.setPixelColor(led, r, g, b);
  75.             }
  76.           }
  77.         }
  78.         led_string = "";
  79.         rgb_string = "";
  80.         srl_string = "";
  81.         break;
  82.       case '_':
  83.         if (command == 1) {
  84.           command = 2;
  85.           rgb_string = "";
  86.         }
  87.         break;
  88.           /**** ASCII: 32 - 47 ****/
  89.           /***  !"#$%&'()*+,-./ ***/
  90.       case ' ' ... '/' :
  91.         m = ((int)incoming_chr) - 32;
  92.         strip.setColor(rgb);
  93.         strip.setMode(m);
  94.         strip.show();
  95.         break;
  96.           /**** ASCII: 71 - 90 ****/
  97.           /* GHIJKLMNOPQRSTUVWXYZ */
  98.       case 'G' ... 'Z' :
  99.         m = ((int)incoming_chr) - 55;
  100.         strip.setColor(rgb);
  101.         strip.setMode(m);
  102.         strip.show();
  103.         break;
  104.           /*** ASCII: 103 - 122 ***/
  105.           /* ghijklmnopqrstuvwxyz */
  106.       case 'g' ... 'z' :
  107.         m = ((int)incoming_chr) - 67;
  108.         strip.setColor(rgb);
  109.         strip.setMode(m);
  110.         strip.show();
  111.         break;
  112.           /****** ASCII: 123 ******/
  113.           /********** { ***********/
  114.       case '{' :
  115.         m = 56;
  116.         /* INDIVIDUAL */
  117.         strip.show();
  118.         break;
  119.       default:
  120.         if (command == 1) {
  121.           led_string += incoming_chr;
  122.         }
  123.         if (command == 2) {
  124.           rgb_string += incoming_chr;
  125.         }
  126.     }
  127.   }
  128. }
  129.  
  130. // The loop function runs over and over again forever
  131. void loop()
  132. {
  133.   if (Serial3.available() > 0) {
  134.     read_string();
  135.   }
  136.   else {
  137.     switch (m) {
  138.       case 56:
  139.         /* INDIVIDUAL */
  140.         break;
  141.       default:
  142.         strip.service();
  143.     }
  144.   }
  145. }
Add Comment
Please, Sign In to add comment