Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 097ff8b

Browse filesBrowse files
authored
Implement simple RGB driver via digitalWrite; solving espressif#6783 (espressif#6808)
* Initial implementation of RGB driver via digitalWrite * Moved constants to pins_arduino.h * Changed pin definition + added example * Wrapped BlinkRGB in #ifdef BOARD_HAS_NEOPIXEL * Removed forgotten log from example * Moved RGBLedWrite to new file esp32-hal-rgb-led and created pinMode in variatn.cpp * Updated example - lowered single channel brightness to LED_BRIGHTNESS * Changed function name from RGBLedWrite to neopixelWrite + code polishing * Moved pinSetup portion related to RGB back to common file
1 parent f376316 commit 097ff8b
Copy full SHA for 097ff8b

File tree

Expand file treeCollapse file tree

10 files changed

+150
-0
lines changed
Filter options
Expand file treeCollapse file tree

10 files changed

+150
-0
lines changed

‎CMakeLists.txt

Copy file name to clipboardExpand all lines: CMakeLists.txt
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ set(CORE_SRCS
3636
cores/esp32/esp32-hal-matrix.c
3737
cores/esp32/esp32-hal-misc.c
3838
cores/esp32/esp32-hal-psram.c
39+
cores/esp32/esp32-hal-rgb-led.c
3940
cores/esp32/esp32-hal-sigmadelta.c
4041
cores/esp32/esp32-hal-spi.c
4142
cores/esp32/esp32-hal-time.c

‎cores/esp32/esp32-hal-gpio.c

Copy file name to clipboardExpand all lines: cores/esp32/esp32-hal-gpio.c
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ static InterruptHandle_t __pinInterruptHandlers[SOC_GPIO_PIN_COUNT] = {0,};
9191

9292
extern void ARDUINO_ISR_ATTR __pinMode(uint8_t pin, uint8_t mode)
9393
{
94+
#ifdef BOARD_HAS_NEOPIXEL
95+
if (pin == LED_BUILTIN){
96+
__pinMode(LED_BUILTIN-SOC_GPIO_PIN_COUNT, mode);
97+
return;
98+
}
99+
#endif
100+
94101
if (!GPIO_IS_VALID_GPIO(pin)) {
95102
log_e("Invalid pin selected");
96103
return;
@@ -127,6 +134,14 @@ extern void ARDUINO_ISR_ATTR __pinMode(uint8_t pin, uint8_t mode)
127134

128135
extern void ARDUINO_ISR_ATTR __digitalWrite(uint8_t pin, uint8_t val)
129136
{
137+
#ifdef BOARD_HAS_NEOPIXEL
138+
if(pin == LED_BUILTIN){
139+
//use RMT to set all channels on/off
140+
const uint8_t comm_val = val != 0 ? LED_BRIGHTNESS : 0;
141+
neopixelWrite(LED_BUILTIN, comm_val, comm_val, comm_val);
142+
return;
143+
}
144+
#endif
130145
gpio_set_level((gpio_num_t)pin, val);
131146
}
132147

‎cores/esp32/esp32-hal-gpio.h

Copy file name to clipboardExpand all lines: cores/esp32/esp32-hal-gpio.h
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ extern "C" {
2626

2727
#include "esp32-hal.h"
2828
#include "soc/soc_caps.h"
29+
#include "pins_arduino.h"
2930

3031
#if (CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3)
3132
#define NUM_OUPUT_PINS 46
@@ -63,6 +64,7 @@ extern "C" {
6364
#define ONLOW_WE 0x0C
6465
#define ONHIGH_WE 0x0D
6566

67+
6668
#define digitalPinIsValid(pin) GPIO_IS_VALID_GPIO(pin)
6769
#define digitalPinCanOutput(pin) GPIO_IS_VALID_OUTPUT_GPIO(pin)
6870

‎cores/esp32/esp32-hal-rgb-led.c

Copy file name to clipboard
+47Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "esp32-hal-rgb-led.h"
2+
3+
4+
void neopixelWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue_val){
5+
rmt_data_t led_data[24];
6+
static rmt_obj_t* rmt_send = NULL;
7+
static bool initialized = false;
8+
9+
uint8_t _pin = pin;
10+
#ifdef BOARD_HAS_NEOPIXEL
11+
if(pin == LED_BUILTIN){
12+
_pin = LED_BUILTIN-SOC_GPIO_PIN_COUNT;
13+
}
14+
#endif
15+
16+
if(!initialized){
17+
if((rmt_send = rmtInit(_pin, RMT_TX_MODE, RMT_MEM_64)) == NULL){
18+
log_e("RGB LED driver initialization failed!");
19+
rmt_send = NULL;
20+
return;
21+
}
22+
rmtSetTick(rmt_send, 100);
23+
initialized = true;
24+
}
25+
26+
int color[] = {green_val, red_val, blue_val}; // Color coding is in order GREEN, RED, BLUE
27+
int i = 0;
28+
for(int col=0; col<3; col++ ){
29+
for(int bit=0; bit<8; bit++){
30+
if((color[col] & (1<<(7-bit)))){
31+
// HIGH bit
32+
led_data[i].level0 = 1; // T1H
33+
led_data[i].duration0 = 8; // 0.8us
34+
led_data[i].level1 = 0; // T1L
35+
led_data[i].duration1 = 4; // 0.4us
36+
}else{
37+
// LOW bit
38+
led_data[i].level0 = 1; // T0H
39+
led_data[i].duration0 = 4; // 0.4us
40+
led_data[i].level1 = 0; // T0L
41+
led_data[i].duration1 = 8; // 0.8us
42+
}
43+
i++;
44+
}
45+
}
46+
rmtWrite(rmt_send, led_data, 24);
47+
}

‎cores/esp32/esp32-hal-rgb-led.h

Copy file name to clipboard
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef MAIN_ESP32_HAL_RGB_LED_H_
2+
#define MAIN_ESP32_HAL_RGB_LED_H_
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
#include "esp32-hal.h"
9+
10+
#ifndef LED_BRIGHTNESS
11+
#define LED_BRIGHTNESS 64
12+
#endif
13+
14+
void neopixelWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue_val);
15+
16+
#ifdef __cplusplus
17+
}
18+
#endif
19+
20+
#endif /* MAIN_ESP32_HAL_RGB_LED_H_ */

‎cores/esp32/esp32-hal.h

Copy file name to clipboardExpand all lines: cores/esp32/esp32-hal.h
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ void yield(void);
8888
#include "esp32-hal-timer.h"
8989
#include "esp32-hal-bt.h"
9090
#include "esp32-hal-psram.h"
91+
#include "esp32-hal-rgb-led.h"
9192
#include "esp32-hal-cpu.h"
9293

9394
void analogWrite(uint8_t pin, int value);
+39Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
BlinkRGB
3+
4+
Demonstrates usage of onboard RGB LED on some ESP dev boards.
5+
6+
Calling digitalWrite(LED_BUILTIN, HIGH) will use hidden RGB driver.
7+
8+
RGBLedWrite demonstrates controll of each channel:
9+
void neopixelWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue_val)
10+
11+
WARNING: After using digitalWrite to drive RGB LED it will be impossible to drive the same pin
12+
with normal HIGH/LOW level
13+
*/
14+
//#define LED_BRIGHTNESS 64 // Change white brightness (max 255)
15+
16+
// the setup function runs once when you press reset or power the board
17+
18+
void setup() {
19+
// No need to initialize the RGB LED
20+
}
21+
22+
// the loop function runs over and over again forever
23+
void loop() {
24+
#ifdef BOARD_HAS_NEOPIXEL
25+
digitalWrite(LED_BUILTIN, HIGH); // Turn the RGB LED white
26+
delay(1000);
27+
digitalWrite(LED_BUILTIN, LOW); // Turn the RGB LED off
28+
delay(1000);
29+
30+
neopixelWrite(LED_BUILTIN,LED_BRIGHTNESS,0,0); // Red
31+
delay(1000);
32+
neopixelWrite(LED_BUILTIN,0,LED_BRIGHTNESS,0); // Green
33+
delay(1000);
34+
neopixelWrite(LED_BUILTIN,0,0,LED_BRIGHTNESS); // Blue
35+
delay(1000);
36+
neopixelWrite(LED_BUILTIN,0,0,0); // Off / black
37+
delay(1000);
38+
#endif
39+
}

‎variants/esp32c3/pins_arduino.h

Copy file name to clipboardExpand all lines: variants/esp32c3/pins_arduino.h
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22
#define Pins_Arduino_h
33

44
#include <stdint.h>
5+
#include "soc/soc_caps.h"
56

67
#define EXTERNAL_NUM_INTERRUPTS 22
78
#define NUM_DIGITAL_PINS 22
89
#define NUM_ANALOG_INPUTS 6
910

11+
static const uint8_t LED_BUILTIN = SOC_GPIO_PIN_COUNT+8;
12+
#define BUILTIN_LED LED_BUILTIN // backward compatibility
13+
#define LED_BUILTIN LED_BUILTIN
14+
#define BOARD_HAS_NEOPIXEL
15+
#define LED_BRIGHTNESS 64
16+
1017
#define analogInputToDigitalPin(p) (((p)<NUM_ANALOG_INPUTS)?(analogChannelToDigitalPin(p)):-1)
1118
#define digitalPinToInterrupt(p) (((p)<NUM_DIGITAL_PINS)?(p):-1)
1219
#define digitalPinHasPWM(p) (p < EXTERNAL_NUM_INTERRUPTS)

‎variants/esp32s2/pins_arduino.h

Copy file name to clipboardExpand all lines: variants/esp32s2/pins_arduino.h
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@
22
#define Pins_Arduino_h
33

44
#include <stdint.h>
5+
#include "soc/soc_caps.h"
56

67
#define EXTERNAL_NUM_INTERRUPTS 46
78
#define NUM_DIGITAL_PINS 48
89
#define NUM_ANALOG_INPUTS 20
910

11+
static const uint8_t LED_BUILTIN = SOC_GPIO_PIN_COUNT+18; // GPIO pin for Saola-1 & DevKitM-1 = 18
12+
//static const uint8_t LED_BUILTIN = SOC_GPIO_PIN_COUNT+45; // GPIO pin for Kaluga = 45
13+
#define BUILTIN_LED LED_BUILTIN // backward compatibility
14+
#define LED_BUILTIN LED_BUILTIN
15+
#define BOARD_HAS_NEOPIXEL
16+
#define LED_BRIGHTNESS 64
17+
1018
#define analogInputToDigitalPin(p) (((p)<20)?(analogChannelToDigitalPin(p)):-1)
1119
#define digitalPinToInterrupt(p) (((p)<48)?(p):-1)
1220
#define digitalPinHasPWM(p) (p < 46)

‎variants/esp32s3/pins_arduino.h

Copy file name to clipboardExpand all lines: variants/esp32s3/pins_arduino.h
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define Pins_Arduino_h
33

44
#include <stdint.h>
5+
#include "soc/soc_caps.h"
56

67
#define USB_VID 0x303a
78
#define USB_PID 0x1001
@@ -10,6 +11,15 @@
1011
#define NUM_DIGITAL_PINS 48
1112
#define NUM_ANALOG_INPUTS 20
1213

14+
// Some boards have too low voltage on this pin (board design bug)
15+
// Use different pin with 3V and connect with 48
16+
// and change this setup for the chosen pin (for example 38)
17+
static const uint8_t LED_BUILTIN = SOC_GPIO_PIN_COUNT+48;
18+
#define BUILTIN_LED LED_BUILTIN // backward compatibility
19+
#define LED_BUILTIN LED_BUILTIN
20+
#define BOARD_HAS_NEOPIXEL
21+
#define LED_BRIGHTNESS 64
22+
1323
#define analogInputToDigitalPin(p) (((p)<20)?(analogChannelToDigitalPin(p)):-1)
1424
#define digitalPinToInterrupt(p) (((p)<48)?(p):-1)
1525
#define digitalPinHasPWM(p) (p < 46)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.