forked from espressif/arduino-esp32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRMTWrite_RGB_LED.ino
99 lines (90 loc) · 3.06 KB
/
RMTWrite_RGB_LED.ino
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
98
99
// Copyright 2024 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @brief This example demonstrates usage of RGB LED driven by RMT
*
* The output is a visual WS2812 RGB LED color moving in a 8 x 4 LED matrix
* Parameters can be changed by the user. In a single LED circuit, it will just blink.
*/
// The effect seen in (Espressif devkits) ESP32C6, ESP32H2, ESP32C3, ESP32S2 and ESP32S3 is like a Blink of RGB LED
#ifdef PIN_LED_RGB
#define BUILTIN_RGBLED_PIN PIN_LED_RGB
#else
#define BUILTIN_RGBLED_PIN 21 // ESP32 has no builtin RGB LED (PIN_LED_RGB)
#endif
#define NR_OF_LEDS 8 * 4
#define NR_OF_ALL_BITS 24 * NR_OF_LEDS
//
// Note: This example uses a board with 32 WS2812b LEDs chained one
// after another, each RGB LED has its 24 bit value
// for color configuration (8b for each color)
//
// Bits encoded as pulses as follows:
//
// "0":
// +-------+ +--
// | | |
// | | |
// | | |
// ---| |--------------|
// + + +
// | 0.4us | 0.85 0us |
//
// "1":
// +-------------+ +--
// | | |
// | | |
// | | |
// | | |
// ---+ +-------+
// | 0.8us | 0.4us |
rmt_data_t led_data[NR_OF_ALL_BITS];
void setup() {
Serial.begin(115200);
if (!rmtInit(BUILTIN_RGBLED_PIN, RMT_TX_MODE, RMT_MEM_NUM_BLOCKS_1, 10000000)) {
Serial.println("init sender failed\n");
}
Serial.println("real tick set to: 100ns");
}
int color[] = {0x55, 0x11, 0x77}; // Green Red Blue values
int led_index = 0;
void loop() {
// Init data with only one led ON
int led, col, bit;
int i = 0;
for (led = 0; led < NR_OF_LEDS; led++) {
for (col = 0; col < 3; col++) {
for (bit = 0; bit < 8; bit++) {
if ((color[col] & (1 << (7 - bit))) && (led == led_index)) {
led_data[i].level0 = 1;
led_data[i].duration0 = 8;
led_data[i].level1 = 0;
led_data[i].duration1 = 4;
} else {
led_data[i].level0 = 1;
led_data[i].duration0 = 4;
led_data[i].level1 = 0;
led_data[i].duration1 = 8;
}
i++;
}
}
}
// make the led travel in the panel
if ((++led_index) >= NR_OF_LEDS) {
led_index = 0;
}
// Send the data and wait until it is done
rmtWrite(BUILTIN_RGBLED_PIN, led_data, NR_OF_ALL_BITS, RMT_WAIT_FOR_EVER);
delay(100);
}