ledc: Support gamma curve fade feature on esp32c6 and esp32h2

This commit is contained in:
Song Ruo Jing
2023-02-23 11:24:48 +08:00
parent 8fc0343e5a
commit 79f34fe524
32 changed files with 1093 additions and 58 deletions

View File

@@ -1,6 +1,8 @@
LED Control (LEDC)
==================
{IDF_TARGET_LEDC_CHAN_NUM:default="6", esp32="16", esp32s2="8", esp32s3="8"}
{IDF_TARGET_LEDC_CHAN_NUM: default="6", esp32="16", esp32s2="8", esp32s3="8"}
{IDF_TARGET_LEDC_MAX_FADE_RANGE_NUM: default="1", esp32c6="16", esp32h2="16"}
:link_to_translation:`zh_CN:[中文]`
@@ -269,6 +271,12 @@ The LEDC hardware provides the means to gradually transition from one duty cycle
* :cpp:func:`ledc_set_fade_with_step`
* :cpp:func:`ledc_set_fade`
.. only:: SOC_LEDC_GAMMA_CURVE_FADE_SUPPORTED
On {IDF_TARGET_NAME}, the hardware additionally allows to perform up to {IDF_TARGET_LEDC_MAX_FADE_RANGE_NUM} consecutive linear fades without CPU intervention. This feature can be useful if you want to do a fade with gamma correction.
The luminance perceived by human eyes does not have a linear relationship with the PWM duty cycle. In order to make human feel the LED is dimming or lightening linearly, the change in duty cycle should be non-linear, which is the so-called gamma correction. The LED controller can simulate a gamma curve fading by piecewise linear approximation. :cpp:func:`ledc_fill_multi_fade_param_list` is a function that can help to construct the parameters for the piecewise linear fades. First, you need to allocate a memory block for saving the fade parameters, then by providing start/end PWM duty cycle values, gamma correction function, and the total number of desired linear segments to the helper function, it will fill the calculation results into the allocated space. You can also construct the array of :cpp:type:`ledc_fade_param_config_t` manually. Once the fade parameter structs are prepared, a consecutive fading can be configured by passing the pointer to the prepared :cpp:type:`ledc_fade_param_config_t` list and the total number of fade ranges to :cpp:func:`ledc_set_multi_fade`.
.. only:: esp32
Start fading with :cpp:func:`ledc_fade_start`. A fade can be operated in blocking or non-blocking mode, please check :cpp:enum:`ledc_fade_mode_t` for the difference between the two available fade modes. Note that with either fade mode, the next fade or fixed-duty update will not take effect until the last fade finishes. Due to hardware limitations, there is no way to stop a fade before it reaches its target duty.
@@ -361,9 +369,13 @@ The duty resolution is normally set using :cpp:type:`ledc_timer_bit_t`. This enu
Application Example
-------------------
The LEDC basic example: :example:`peripherals/ledc/ledc_basic`.
The LEDC change duty cycle and fading control example: :example:`peripherals/ledc/ledc_fade`.
The LEDC basic example: :example:`peripherals/ledc/ledc_basic`.
.. only:: SOC_LEDC_GAMMA_CURVE_FADE_SUPPORTED
The LEDC color control with Gamma correction on RGB LED example: :example:`peripherals/ledc/ledc_gamma_curve_fade`.
API Reference
-------------

View File

@@ -1,6 +1,8 @@
LED PWM 控制器
==============
{IDF_TARGET_LEDC_CHAN_NUM:default="6", esp32="16", esp32s2="8", esp32s3="8"}
{IDF_TARGET_LEDC_CHAN_NUM: default="6", esp32="16", esp32s2="8", esp32s3="8"}
{IDF_TARGET_LEDC_MAX_FADE_RANGE_NUM: default="1", esp32c6="16", esp32h2="16"}
:link_to_translation:`en:[English]`
@@ -269,6 +271,12 @@ LED PWM 控制器硬件可逐渐改变占空比的数值。要使用此功能,
* :cpp:func:`ledc_set_fade_with_step`
* :cpp:func:`ledc_set_fade`
.. only:: SOC_LEDC_GAMMA_CURVE_FADE_SUPPORTED
{IDF_TARGET_NAME} 的硬件额外支持多达 {IDF_TARGET_LEDC_MAX_FADE_RANGE_NUM} 次,无需 CPU 介入的连续渐变。此功能可以更加有效便捷得实现一个带伽马校正的渐变。
众所周知,人眼所感知的亮度与 PWM 占空比并非成线性关系。为了能使人感观上认为一盏灯明暗的变化是线性的,我们对其 PWM 信号的占空比控制必须为非线性的俗称伽马校正。LED PWM 控制器可以通过多段线型拟合来模仿伽马曲线渐变。 你需要自己在应用程序中分配一段用以保存渐变参数的内存块,并提供开始和结束的占空比,伽马校正公式,以及期望的线性渐变段数信息,:cpp:func:`ledc_fill_multi_fade_param_list` 就能快速生成所有分段线性渐变的参数。或者你也可以自己直接构造一个 :cpp:type:`ledc_fade_param_config_t` 的数组。在获得所有渐变参数后,通过将 :cpp:type:`ledc_fade_param_config_t` 数组的指针和渐变区间数传入 :cpp:func:`ledc_set_multi_fade`,一次连续渐变的配置就完成了。
.. only:: esp32
最后需要调用 :cpp:func:`ledc_fade_start` 开启渐变。渐变可以在阻塞或非阻塞模式下运行,具体区别请查看 :cpp:enum:`ledc_fade_mode_t`。需要特别注意的是,不管在哪种模式下,下一次渐变或单次占空比配置的指令生效都必须等到前一次渐变结束。由于 {IDF_TARGET_NAME} 的硬件限制,在渐变达到原先预期的占空比前想要中止本次渐变是不被支持的。
@@ -361,9 +369,13 @@ LED PWM 控制器 API 会在设定的频率和占空比分辨率超过 LED PWM
应用实例
-------------------
使用 LEDC 基本实例请参照 :example:`peripherals/ledc/ledc_basic`
使用 LEDC 改变占空比和渐变控制的实例请参照 :example:`peripherals/ledc/ledc_fade`
使用 LEDC 基本实例请参照 :example:`peripherals/ledc/ledc_basic`
.. only:: SOC_LEDC_GAMMA_CURVE_FADE_SUPPORTED
使用 LEDC 对 RGB LED 实现带伽马校正的颜色控制实例请参照 :example:`peripherals/ledc/ledc_gamma_curve_fade`
API 参考
-------------