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