Advertisement
MrRockchip

super_s_part2560

Jun 9th, 2022
753
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.53 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;                   // m = FX_MODE_STATIC = 0
  23. uint8_t  slider_s = 4;
  24. uint16_t s = (64000 >> slider_s); // 4000 value for 4 speed
  25.  
  26. char incoming_chr = "";
  27.  
  28. uint8_t command = 0;
  29.  
  30. // Initial setup
  31. void setup()
  32. {
  33.   Serial.begin(115200); // Initialize the debug serial port
  34.  /*
  35.   * It is recommended to use the lower baud rate. The higher the baud rate,
  36.   * the higher the bit error rate will be, and the control action might fail.
  37.   */
  38.   Serial3.begin(4800); // Initialize the ESP8266 <=> MEGA2560 serial port
  39.   //// strip.begin();
  40.   strip.init();
  41.   strip.setBrightness(127); // 255 ?
  42.   strip.setSpeed(s); // 4000 value for 4 speed
  43.   strip.setMode(m);  // m = FX_MODE_STATIC = 0
  44.   strip.start();
  45.   //// pinMode(LED_BUILTIN, OUTPUT); // Initialize the digital pin LED_BUILTIN as an output
  46. }
  47.  
  48. // Read a command from ESP8266 over serial
  49. void read_string()
  50. {
  51.   while (Serial3.available() > 0) {
  52.     incoming_chr = Serial3.read();
  53.     srl_string += incoming_chr;
  54.     Serial.print(incoming_chr); // DEBUG
  55.     switch (incoming_chr) {
  56.       case '[':
  57.         command = 1;
  58.         led_string = "";
  59.         rgb_string = "";
  60.         break;
  61.       case ']':
  62.         command = 0;
  63.         if ((led_string.length() == 3) && (rgb_string.length() == 6)) {
  64.           rgb = (uint32_t) strtol(&rgb_string[0], NULL, 16);
  65.           r = rgb >> 16;
  66.           g = rgb >> 8 & 0xFF;
  67.           b = rgb & 0xFF;
  68.           if ((led_string[0] == "^") && (led_string[1] == "^") && (led_string[2] == "^")) {
  69.             for (i = 0; i < N_LEDS; i++) {
  70.               strip.setPixelColor(i, r, g, b);
  71.             }
  72.           }
  73.           else {
  74.             led = (uint32_t) strtol( &led_string[0], NULL, 16);
  75.             if (led < N_LEDS) {
  76.               strip.setPixelColor(led, r, g, b);
  77.             }
  78.           }
  79.         }
  80.         led_string = "";
  81.         rgb_string = "";
  82.         srl_string = "";
  83.         break;
  84.       case '_':
  85.         if (command == 1) {
  86.           command = 2;
  87.           rgb_string = "";
  88.         }
  89.         break;
  90.           /**** ASCII: 32 - 47 ****/
  91.           /***  !"#$%&'()*+,-./ ***/
  92.       case ' ' ... '/' :
  93.         m = ((uint8_t)incoming_chr) - 32;
  94.         strip.setColor(rgb);
  95.         strip.setMode(m);
  96.         strip.show();
  97.         break;
  98.           /**** ASCII: 71 - 90 ****/
  99.           /* GHIJKLMNOPQRSTUVWXYZ */
  100.       case 'G' ... 'Z' :
  101.         m = ((uint8_t)incoming_chr) - 55;
  102.         strip.setColor(rgb);
  103.         strip.setMode(m);
  104.         strip.show();
  105.         break;
  106.           /*** ASCII: 103 - 122 ***/
  107.           /* ghijklmnopqrstuvwxyz */
  108.       case 'g' ... 'z' :
  109.         m = ((uint8_t)incoming_chr) - 67;
  110.         strip.setColor(rgb);
  111.         strip.setMode(m);
  112.         strip.show();
  113.         break;
  114.           /****** ASCII: 123 ******/
  115.           /********** { ***********/
  116.       case '{' :
  117.         m = 56;
  118.         /* INDIVIDUAL */
  119.         strip.show();
  120.         break;
  121.           /**** ASCII: 58 - 64 ****/
  122.           /******* :;<=>?@ ********/
  123.       case ':' ... '@' :
  124.         slider_s = ((uint8_t)incoming_chr) -  58;
  125.         s = (64000 >> slider_s);
  126.         /*
  127.         if (m != 56) {
  128.           strip.setColor(rgb);
  129.         }
  130.         */
  131.         strip.setSpeed(s);
  132.         strip.show();
  133.         break;
  134.           /*** ASCII: 124 - 126 ***/
  135.           /********* |}~ **********/
  136.       case '|' ... '~' :
  137.         slider_s = ((uint8_t)incoming_chr) - 117;
  138.         s = (64000 >> slider_s);
  139.         /*
  140.         if (m != 56) {
  141.           strip.setColor(rgb);
  142.         }
  143.         */
  144.         strip.setSpeed(s);
  145.         strip.show();
  146.         break;
  147.       default:
  148.         if (command == 1) {
  149.           led_string += incoming_chr;
  150.         }
  151.         if (command == 2) {
  152.           rgb_string += incoming_chr;
  153.         }
  154.     }
  155.   }
  156. }
  157.  
  158. // The loop function runs over and over again forever
  159. void loop()
  160. {
  161.   if (Serial3.available() > 0) {
  162.     read_string();
  163.   }
  164.   else {
  165.     switch (m) {
  166.       case 56:
  167.         /* INDIVIDUAL */
  168.         break;
  169.       default:
  170.         strip.service();
  171.     }
  172.   }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement