Advertisement
jasper_fracture

main.c - LED Ring, minor changes to original file

Feb 11th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.11 KB | None | 0 0
  1. /* Created 19 Nov 2016 by Chris Osborn <[email protected]>
  2.  * http://insentricity.com
  3.  *
  4.  * Demo of driving WS2812 RGB LEDs using the RMT peripheral.
  5.  *
  6.  * This code is placed in the public domain (or CC0 licensed, at your option).
  7.  */
  8.  
  9. #include <freertos/FreeRTOS.h>
  10. #include <freertos/task.h>
  11. #include <soc/rmt_struct.h>
  12. #include <esp_system.h>
  13. #include <nvs_flash.h>
  14. #include <driver/gpio.h>
  15. #include <stdio.h>
  16. #include "ws2812.h"
  17.  
  18. #define WS2812_PIN  15
  19.  
  20. #define delay_ms(ms) vTaskDelay((ms) / portTICK_RATE_MS)
  21.  
  22. void rainbow(void *pvParameters)
  23. {
  24.   const uint8_t anim_step = 10;
  25.   const uint8_t anim_max = 250;
  26.   const uint8_t pixel_count = 24; // Number of your "pixels"
  27.   const uint8_t delay = 25; // duration between color changes
  28.   rgbVal color = makeRGBVal(anim_max, 0, 0);
  29.   uint8_t step = 0;
  30.   rgbVal color2 = makeRGBVal(anim_max, 0, 0);
  31.   uint8_t step2 = 0;
  32.   rgbVal *pixels;
  33.  
  34.  
  35.   pixels = malloc(sizeof(rgbVal) * pixel_count);
  36.  
  37.   while (1) {
  38.     color = color2;
  39.     step = step2;
  40.  
  41.     for (uint8_t i = 0; i < pixel_count; i++) {
  42.       pixels[i] = color;
  43.  
  44.       if (i == 1) {
  45.         color2 = color;
  46.         step2 = step;
  47.       }
  48.  
  49.       switch (step) {
  50.       case 0:
  51.         color.g += anim_step;
  52.         if (color.g >= anim_max)
  53.           step++;
  54.         break;
  55.       case 1:
  56.         color.r -= anim_step;
  57.         if (color.r == 0)
  58.           step++;
  59.         break;
  60.       case 2:
  61.         color.b += anim_step;
  62.         if (color.b >= anim_max)
  63.           step++;
  64.         break;
  65.       case 3:
  66.         color.g -= anim_step;
  67.         if (color.g == 0)
  68.           step++;
  69.         break;
  70.       case 4:
  71.         color.r += anim_step;
  72.         if (color.r >= anim_max)
  73.           step++;
  74.         break;
  75.       case 5:
  76.         color.b -= anim_step;
  77.         if (color.b == 0)
  78.           step = 0;
  79.         break;
  80.       }
  81.     }
  82.  
  83.     ws2812_setColors(pixel_count, pixels);
  84.  
  85.     delay_ms(delay);
  86.   }
  87. }
  88.  
  89. void app_main()
  90. {
  91.   nvs_flash_init();
  92.  
  93.   ws2812_init(WS2812_PIN);
  94. //  xTaskCreate(rainbow, "ws2812 rainbow demo", 256, NULL, 10, NULL);
  95.     rainbow(NULL);
  96.  
  97.   return;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement