-
Notifications
You must be signed in to change notification settings - Fork 7.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement simple RGB driver via digitalWrite; solving #6783 #6808
Conversation
@@ -91,6 +91,13 @@ static InterruptHandle_t __pinInterruptHandlers[SOC_GPIO_PIN_COUNT] = {0,}; | |||
|
|||
extern void ARDUINO_ISR_ATTR __pinMode(uint8_t pin, uint8_t mode) | |||
{ | |||
#ifdef BOARD_HAS_NEOPIXEL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we consider moving the board-specific code to the variants
folder?
I'm worried that the more similar features we add for specific boards, the more complex the core code will become. It would be better to handle this in the board variant folder. It would also demonstrate authors of 3rd party board packages how to add such custom functionality.
To avoid duplication of code, you can still define RGBLedWrite
somewhere in the core, but preferably in a separate source file (esp32-hal-rgb-led.c?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little bit confused about what should go where.
pinMode
-> variants/
... new file?
RGBLedWrite
-> esp32-hal-rgb-led.c
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be solved in 583e030
cores/esp32/esp32-hal-rgb-led.h
Outdated
*/ | ||
|
||
//#ifdef BOARD_HAS_NEOPIXEL | ||
void RGBLedWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue_val); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please cleanup the commented includes and ifdefs. Arduino starts function names with lower case. I would suggest that you rename the function to neopixelWrite
. rgbLed
is very general and should be attributed to actual RGB Led with separate leads for each color (and controlled through PWM).
cores/esp32/esp32-hal-rgb-led.c
Outdated
@@ -0,0 +1,51 @@ | |||
#include "esp32-hal-rgb-led.h" | |||
|
|||
#ifdef BOARD_HAS_NEOPIXEL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems you have the guard here but it is commented in the header?
static bool initialized = false; | ||
|
||
uint8_t _pin; | ||
if(pin == LED_BUILTIN){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be evaluated only if the board has neopixel, else LED_BUILTIN would be lower than SOC_GPIO_PIN_COUNT
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The entire function was safeguarded #ifdef BOARD_HAS_NEOPIXEL
But I will remove safeguard for the entire function a wrap only this.
cores/esp32/esp32-hal-rgb-led.c
Outdated
#ifdef BOARD_HAS_NEOPIXEL | ||
|
||
void RGBLedWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue_val){ | ||
log_d("RGB: %d %d %d", red_val, green_val, blue_val); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
such logs should be with level "Verbose" instead. Blinking led will print a lot of things :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was also a forgotten debug output. I will remove it in the next commit.
#include "soc/soc_caps.h" | ||
#include "pins_arduino.h" | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should have safeguard against LED_BRIGHTNESS not being defined:
#ifndef LED_BRIGHTNESS
#define LED_BRIGHTNESS 64
#endif
variants/esp32s2/variant.cpp
Outdated
|
||
extern void ARDUINO_ISR_ATTR __pinMode(uint8_t pin, uint8_t mode) | ||
{ | ||
log_d("foo"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
forgotten?
variants/esp32c3/variant.cpp
Outdated
|
||
extern void ARDUINO_ISR_ATTR __pinMode(uint8_t pin, uint8_t mode) | ||
{ | ||
log_d("foo"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
forgotten?
variants/esp32s3/variant.cpp
Outdated
|
||
extern void ARDUINO_ISR_ATTR __pinMode(uint8_t pin, uint8_t mode) | ||
{ | ||
log_d("foo"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
forgotten?
variants/esp32c3/variant.cpp
Outdated
@@ -0,0 +1,13 @@ | |||
#include "esp32-hal-rgb-led.h" | |||
|
|||
extern void ARDUINO_ISR_ATTR __pinMode(uint8_t pin, uint8_t mode) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this necessary? pinMode in the core should just return if pin is not valid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was trying to implement this #6808 (review)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand, I just question the necessity to have such code in the variant just to get something as common as neopixel on the board working.
@igrr any comments?
Something like this in the core's __pinMode
would skip execution:
if(pin > SOC_GPIO_PIN_COUNT && pin == LED_BUILTIN){
return; // or pin -= SOC_GPIO_PIN_COUNT; to allow setting the mode.
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PilnyTomas this is still outstanding. Requiring variant.cpp to blink a led is not desired situation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@igrr Could you please give us more info about why should we have board-specific code in the variants folder instead of having in the common file?
Should I keep it like this, or revert back to the common file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking that this is behavior we want only on specific boards which have a WS2812B, and where we'd like to provide such emulation.
Makers of other boards should be able implement some different logic, or opt out of such emulation. In the latter case, the code for emulating an LED ideally shouldn't be compiled or linked.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you do not have that LED defined, code will not be compiled anyway. requiring all boards with RGB leds to include a variant cpp for two lines is a bit too much IMO. There are plenty boards with RGB leds
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although, given that you are introducing a BOARD_HAS_NEOPIXEL macro, maybe we can indeed move this functionality into the core.
* Improve RGB LED Driver Replaces the use of the `LED_BUILTIN` variable by creating a new variable called `RGB_BUILTIN`. On boards with both a regular LED and RGB LED, this change provides functionality to control either LED. The `LED_BRIGHTNESS` variable is changed to `RGB_BRIGHTNESS`, which aligns more closely with the `RGB_BUILTIN` variable name. `BOARD_HAS_NEOPIXEL` is no longer necessary; it is replaced by `RGB_BUILTIN`. * Update BlinkRGB example Update example code for changes with the RGB driver: - Replace `LED_BUILTIN` and `BOARD_HAS_NEOPIXEL` with `RGB_BUILTIN` - Replace `LED_BRIGHTNESS` with `RGB_BRIGHTNESS` * Update board variants Update board variants for changes with the RGB driver: - Remove `BOARD_HAS_NEOPIXEL` - Define `RGB_BUILTIN` pin - Replace `LED_BRIGHTNESS` with `RGB_BRIGHTNESS` to align with `RGB_BUILTIN` name Co-authored-by: Rodrigo Garcia <rodrigo.garcia@espressif.com> Co-authored-by: Vojtěch Bartoška <76958047+VojtechBartoska@users.noreply.github.com>
Description of Change
This solution enables users to use Blink example with RGB LED on board.
When using function
digitalWrite
with constantLED_BUILTIN
as pin parameter an RGB LED driver in the background will set white / off.Added new example BlinkRGB extending the original Blink example with a demonstration of separate RGB channel setup.
Tests scenarios
Tested on
S3 DevkitC
C3 DevkitC
S2 Kaluga
Related links
Closes #6783