Skip to content
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

Merged
merged 9 commits into from
Jun 24, 2022

Conversation

PilnyTomas
Copy link
Contributor

@PilnyTomas PilnyTomas commented May 26, 2022

Description of Change

This solution enables users to use Blink example with RGB LED on board.
When using function digitalWrite with constant LED_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

@PilnyTomas PilnyTomas changed the title Implement simple RGB driver via digitalWriteů solving #6783 Implement simple RGB driver via digitalWrite; solving #6783 May 26, 2022
@@ -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
Copy link
Member

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?)

Copy link
Contributor Author

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 ?

Copy link
Contributor Author

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

*/

//#ifdef BOARD_HAS_NEOPIXEL
void RGBLedWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue_val);
Copy link
Member

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).

@@ -0,0 +1,51 @@
#include "esp32-hal-rgb-led.h"

#ifdef BOARD_HAS_NEOPIXEL
Copy link
Member

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){
Copy link
Member

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

Copy link
Contributor Author

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.

#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);
Copy link
Member

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 :)

Copy link
Contributor Author

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"
*/

Copy link
Member

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


extern void ARDUINO_ISR_ATTR __pinMode(uint8_t pin, uint8_t mode)
{
log_d("foo");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forgotten?


extern void ARDUINO_ISR_ATTR __pinMode(uint8_t pin, uint8_t mode)
{
log_d("foo");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forgotten?


extern void ARDUINO_ISR_ATTR __pinMode(uint8_t pin, uint8_t mode)
{
log_d("foo");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forgotten?

@@ -0,0 +1,13 @@
#include "esp32-hal-rgb-led.h"

extern void ARDUINO_ISR_ATTR __pinMode(uint8_t pin, uint8_t mode)
Copy link
Member

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.

Copy link
Contributor Author

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)

Copy link
Member

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.
}

Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Member

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

Copy link
Member

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.

@me-no-dev me-no-dev merged commit 097ff8b into espressif:master Jun 24, 2022
VojtechBartoska added a commit that referenced this pull request Jul 18, 2022
* 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>
robertlipe referenced this pull request in PlummersSoftwareLLC/NightDriverStrip May 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

Implement LED_BUILTIN constant + digitalWrite() overload for RGB LED
4 participants