rzeldent / esp32-smartdisplay

PlatformIO library LVGL Drivers for Sunton Smart display boards (CYD Cheap Yellow Display). This library supports these boards without any effort. ESP32-2432S024N/R/C, ESP32-2432S028R/C, ESP32-3248S035R/C, ESP32_8048S070N/C
https://github.com/rzeldent/platformio-espressif32-sunton
GNU General Public License v3.0
427 stars 75 forks source link

Enhancement for PWM'd RGB LED #200

Open noeldodd opened 2 months ago

noeldodd commented 2 months ago

I'd like to be able to dim my projects RGB LED during the night.

Any interest in something like this? Or does it exist and I just missed it?

// Move to json files:
#define PWM_R_CHANNEL 4
#define PWM_G_CHANNEL 5
#define PWM_B_CHANNEL 6
#define PWM_FREQ 5000
#define PWM_RESOLUTION 8

void setup() {
  ledcSetup(PWM_R_CHANNEL, PWM_FREQ, PWM_RESOLUTION);
  ledcSetup(PWM_G_CHANNEL, PWM_FREQ, PWM_RESOLUTION);
  ledcSetup(PWM_B_CHANNEL, PWM_FREQ, PWM_RESOLUTION);
  ledcAttachPin(RGB_LED_R,PWM_R_CHANNEL);
  ledcAttachPin(RGB_LED_G,PWM_G_CHANNEL);
  ledcAttachPin(RGB_LED_B,PWM_B_CHANNEL);
  //...
}

void smartdisplay_led_set_pwmrgb(byte r, byte g, byte b)
{
  // log_d("R:%d, G:%d, B:%d", r, g, b);
  ledcWrite(RGB_LED_R,r);
  ledcWrite(RGB_LED_G, g);
  ledcWrite(RGB_LED_B, b);
}

I looked at the source and rather than these #defines it looks like they are -D's in each boards .json definition.

Also, I have absolutely ZERO idea if those PWM channels are in use, optimal (ie minimal) freq/resolution values, or if there is a workable automatic way to let LEDC decide which to use, etc.

But I've tested this as-is, and works.

ALSO: The log statement in your smartdisplay_led_set_rgb has a typo, you output 'g' twice :)

(Edited to post correct code)

rzeldent commented 2 months ago

Hi Noel,

Thanks for pointing out the typo. Although small, is fixed in current develop.

The channels can indeed be defined in the definition files, the point is that, as you already pointed out, these PWM channels might already be in use. So that's why this was not implemented using RGB.

If you'd like to have this functionality, please make a pull request and make a function to initialize this with the PWM channels and a function to set the value. This is indeed a nice to have!

Kind regards,

Rene