MrRockchip

lantern_part2560

Nov 9th, 2021 (edited)
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.51 KB | None | 0 0
  1. //// [000.E662FE][001.E662FE][002.E662FE][003.E662FE][004.E662FE][005.E662FE][006.E662FE][007.E662FE]!
  2.  
  3. #include <Adafruit_NeoPixel.h>
  4.  
  5. #define PIN   48
  6. #define N_LEDS 8
  7.  
  8. Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_LEDS, PIN, NEO_GRB + NEO_KHZ800);
  9.  
  10. String srl_string = "";
  11. String led_string = "";
  12. String rgb_string = "";
  13.  
  14. uint32_t i   = 0;
  15. uint32_t led = 0;
  16. uint32_t rgb = 0;
  17.  
  18. uint8_t r = 0;
  19. uint8_t g = 0;
  20. uint8_t b = 0;
  21.  
  22. char incoming_chr = "";
  23.  
  24. uint8_t command = 0;
  25.  
  26. // Initial setup
  27. void setup()
  28. {
  29.   Serial.begin(115200); // Initialize the debug serial port
  30.  /*
  31.   * It is recommended to use the lower baud rate. The higher the baud rate,
  32.   * the higher the bit error rate will be, and the control action might fail.
  33.   */
  34.   Serial3.begin(4800); // Initialize the ESP8266 <=> MEGA2560 serial port
  35.   strip.begin();
  36.   //// pinMode(LED_BUILTIN, OUTPUT); // Initialize the digital pin LED_BUILTIN as an output
  37. }
  38.  
  39. // Read a command from ESP8266 over serial
  40. void read_string()
  41. {
  42.   while (Serial3.available() > 0) {
  43.     incoming_chr = Serial3.read();
  44.     srl_string += incoming_chr;
  45.     //// Serial.print(incoming_chr);
  46.     switch (incoming_chr) {
  47.       case '[':
  48.         command = 1;
  49.         led_string = "";
  50.         rgb_string = "";
  51.         break;
  52.       case ']':
  53.         command = 0;
  54.         if ((led_string.length() == 3) && (rgb_string.length() == 6)) {
  55.           rgb = (uint32_t) strtol(&rgb_string[0], NULL, 16);
  56.           r = rgb >> 16;
  57.           g = rgb >> 8 & 0xFF;
  58.           b = rgb & 0xFF;
  59.           if ((led_string[0] == "A") && (led_string[1] == "L") && (led_string[2] == "L")) {
  60.             for (i=0; i<N_LEDS; i++) {
  61.               strip.setPixelColor(i, r, g, b);
  62.             }
  63.           }
  64.           else {
  65.             led = (uint32_t) strtol( &led_string[0], NULL, 16);
  66.             if (led < N_LEDS) {
  67.               strip.setPixelColor(led, r, g, b);
  68.             }
  69.           }
  70.         }
  71.         led_string = "";
  72.         rgb_string = "";
  73.         srl_string = "";
  74.         break;
  75.       case '.':
  76.         if (command == 1) {
  77.           command = 2;
  78.           rgb_string = "";
  79.         }
  80.         break;
  81.       case '!':
  82.         strip.show();
  83.         break;
  84.       default:
  85.         if (command == 1) {
  86.           led_string += incoming_chr;
  87.         }
  88.         if (command == 2) {
  89.           rgb_string += incoming_chr;
  90.         }
  91.     }
  92.   }
  93. }
  94.  
  95. // The loop function runs over and over again forever
  96. void loop()
  97. {
  98.   if (Serial3.available() > 0) {
  99.     read_string();
  100.   }
  101. }
Add Comment
Please, Sign In to add comment