drivers: Restructure boards and drivers into device_hal

This commit is contained in:
WanqQixiang
2021-08-25 15:14:52 +08:00
parent 9af0fae34e
commit ce7d09035f
32 changed files with 258 additions and 377 deletions
+5
View File
@@ -0,0 +1,5 @@
include($ENV{ESP_MATTER_DEVICE_PATH}/esp_matter_device.cmake)
set(button_requires )
idf_component_register(SRCS ${button_type}/button_driver.c
INCLUDE_DIRS include
REQUIRES ${button_requires})
@@ -0,0 +1,25 @@
// Copyright 2021 Espressif Systems (Shanghai) CO LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License
#include <esp_log.h>
#include <button_driver.h>
static const char *TAG = "button_driver_hollow";
esp_err_t button_driver_init(button_driver_config_t *config)
{
ESP_LOGI(TAG, "Initializing button driver");
/* Initialize the button driver here*/
return ESP_OK;
}
@@ -0,0 +1,30 @@
// Copyright 2021 Espressif Systems (Shanghai) CO LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License
#pragma once
#include <esp_err.h>
#ifdef __cplusplus
extern "C"
{
#endif
typedef struct {
int gpio;
} button_driver_config_t;
esp_err_t button_driver_init(button_driver_config_t *config);
#ifdef __cplusplus
}
#endif
+4
View File
@@ -0,0 +1,4 @@
include($ENV{ESP_MATTER_DEVICE_PATH}/esp_matter_device.cmake)
idf_component_register(SRC_DIRS "${device_name}"
INCLUDE_DIRS include
PRIV_REQUIRES ${used_driver})
+48
View File
@@ -0,0 +1,48 @@
// Copyright 2021 Espressif Systems (Shanghai) CO LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License
#include <esp_log.h>
#include <light_driver.h>
#include <button_driver.h>
#define DEVICE_VERSION_1_0
#ifdef DEVICE_VERSION_1_0
#define LED_GPIO_PIN 12 /* GPIO_NUM_12 */
#define LED_CHANNEL 0 /* LEDC_CHANNEL_0 */
#endif
static const char *TAG = "device";
static esp_err_t device_light_init()
{
light_driver_config_t config = {
.gpio = LED_GPIO_PIN,
.channel = LED_CHANNEL,
};
return light_driver_init(&config);
}
static esp_err_t device_button_init()
{
return button_driver_init(NULL);
}
esp_err_t device_init()
{
ESP_LOGI(TAG, "Initializing device");
device_light_init();
device_button_init();
return ESP_OK;
}
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.5)
if(NOT ("${IDF_TARGET}" STREQUAL "esp32" ))
message(FATAL_ERROR "please set esp32 as the IDF_TARGET using 'idf.py set-target esp32'")
endif()
SET(device_hal_path $ENV{ESP_MATTER_DEVICE_PATH}/../../)
SET(device_name esp32_devkit_c)
SET(light_type gpio)
SET(button_type hollow)
SET(used_driver light_driver button_driver)
SET(extra_components_dirs_append "${device_hal_path}/light_driver"
"${device_hal_path}/button_driver")
@@ -0,0 +1,49 @@
// Copyright 2021 Espressif Systems (Shanghai) CO LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License
#include <esp_log.h>
#include <light_driver.h>
#include <button_driver.h>
#define DEVICE_VERSION_1_0
#ifdef DEVICE_VERSION_1_0
#define LED_GPIO_PIN 8 /* GPIO_NUM_8 */
#define LED_CHANNEL 0 /* RMT_CHANNEL_0 */
#endif
static const char *TAG = "device";
static esp_err_t device_light_init()
{
light_driver_config_t config = {
.gpio = LED_GPIO_PIN,
.channel = LED_CHANNEL,
};
return light_driver_init(&config);
}
static esp_err_t device_button_init()
{
return button_driver_init(NULL);
}
esp_err_t device_init()
{
ESP_LOGI(TAG, "Initializing device");
device_light_init();
device_button_init();
return ESP_OK;
}
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.5)
if (NOT ("${IDF_TARGET}" STREQUAL "esp32c3" ))
message(FATAL_ERROR "please set esp32c3 as the IDF_TARGET using 'idf.py set-target esp32c3'")
endif()
SET(device_hal_path $ENV{ESP_MATTER_DEVICE_PATH}/../../)
SET(device_name esp32c3_devkit_m)
SET(light_type ws2812)
SET(button_type hollow)
SET(used_driver light_driver button_driver)
SET(extra_components_dirs_append "${device_hal_path}/light_driver"
"${device_hal_path}/button_driver"
"$ENV{IDF_PATH}/examples/peripherals/rmt/led_strip/components")
+26
View File
@@ -0,0 +1,26 @@
// Copyright 2021 Espressif Systems (Shanghai) CO LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License
#pragma once
#include <esp_err.h>
#ifdef __cplusplus
extern "C"
{
#endif
esp_err_t device_init();
#ifdef __cplusplus
}
#endif
+47
View File
@@ -0,0 +1,47 @@
// Copyright 2021 Espressif Systems (Shanghai) CO LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License
#include <esp_log.h>
#include <light_driver.h>
#include <button_driver.h>
#define DEVICE_VERSION_1_0
#ifdef DEVICE_VERSION_1_0
#define LED_GPIO_PIN 32 /* PIN_NUM_BCKL for M5Stack TFT */
#define LED_CHANNEL 7 /* LEDC_CHANNEL_7 */
#endif
static const char *TAG = "device";
static esp_err_t device_light_init()
{
light_driver_config_t config = {
.gpio = 32,
.channel = 7,
};
return light_driver_init(&config);
}
static esp_err_t device_button_init()
{
return button_driver_init(NULL);
}
esp_err_t device_init()
{
ESP_LOGI(TAG, "Initializing board");
device_light_init();
device_button_init();
return ESP_OK;
}
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.5)
if(NOT ("${IDF_TARGET}" STREQUAL "esp32" ))
message(FATAL_ERROR "please set esp32 as the IDF_TARGET using 'idf.py set-target esp32'")
endif()
SET(device_hal_path $ENV{ESP_MATTER_DEVICE_PATH}/../../)
SET(device_name m5stack)
SET(light_type vled)
SET(button_type hollow)
SET(used_driver light_driver button_driver)
SET(extra_components_dirs_append "${device_hal_path}/light_driver"
"${device_hal_path}/button_driver"
"$ENV{ESP_MATTER_PATH}/connectedhomeip/connectedhomeip/examples/common/m5stack-tft/repo/components/tft/"
"$ENV{ESP_MATTER_PATH}/connectedhomeip/connectedhomeip/examples/common/m5stack-tft/repo/components/spidriver/")
+10
View File
@@ -0,0 +1,10 @@
include($ENV{ESP_MATTER_DEVICE_PATH}/esp_matter_device.cmake)
set(light_requires )
if ("${light_type}" STREQUAL "ws2812")
list(APPEND light_requires led_strip)
elseif ("${light_type}" STREQUAL "vled")
list(APPEND light_requires tft spidriver)
endif()
idf_component_register(SRC_DIRS ${light_type} utils
INCLUDE_DIRS include
PRIV_REQUIRES ${light_requires})
+126
View File
@@ -0,0 +1,126 @@
// Copyright 2021 Espressif Systems (Shanghai) CO LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License
#include <esp_log.h>
#include <driver/ledc.h>
#include <hal/ledc_types.h>
#include <light_driver.h>
static const char *TAG = "light_driver_gpio";
static bool current_power = false;
static uint8_t current_brightness = 0;
esp_err_t light_driver_init(light_driver_config_t *config)
{
ESP_LOGI(TAG, "Initializing light driver");
esp_err_t err = ESP_OK;
ledc_timer_config_t ledc_timer = {
.speed_mode = LEDC_LOW_SPEED_MODE, // timer mode
.duty_resolution = LEDC_TIMER_8_BIT, // resolution of PWM duty
.timer_num = LEDC_TIMER_1, // timer index
.freq_hz = 5000, // frequency of PWM signal
.clk_cfg = LEDC_AUTO_CLK, // Auto select the source clock
};
err = ledc_timer_config(&ledc_timer);
if (err != ESP_OK) {
ESP_LOGE(TAG, "led_timerc_config failed");
return err;
}
ledc_channel_config_t ledc_channel = {
.gpio_num = config->gpio,
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = config->channel,
.intr_type = LEDC_INTR_DISABLE,
.timer_sel = LEDC_TIMER_1,
.duty = 0,
.hpoint = 0,
};
err = ledc_channel_config(&ledc_channel);
if (err != ESP_OK) {
ESP_LOGE(TAG, "ledc_channel_config failed");
}
return err;
}
esp_err_t light_driver_set_power(bool power)
{
current_power = power;
return ESP_OK;
}
esp_err_t light_driver_set_brightness(uint8_t brightness)
{
esp_err_t err;
if (brightness != 0) {
current_brightness = brightness;
}
if (!current_power) {
brightness = 0;
}
err = ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, brightness);
if (err != ESP_OK) {
ESP_LOGE(TAG, "ledc_set_duty failed");
}
err = ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0);
if (err != ESP_OK) {
ESP_LOGE(TAG, "ledc_update_duty failed");
}
return err;
}
esp_err_t light_driver_set_hue(uint16_t hue)
{
return ESP_ERR_NOT_SUPPORTED;
}
esp_err_t light_driver_set_saturation(uint8_t saturation)
{
return ESP_ERR_NOT_SUPPORTED;
}
esp_err_t light_driver_set_temperature(uint32_t temperature)
{
return ESP_ERR_NOT_SUPPORTED;
}
bool light_driver_get_power()
{
return current_power;
}
uint8_t light_driver_get_brightness()
{
return current_brightness;
}
uint16_t light_driver_get_hue()
{
return 0;
}
uint8_t light_driver_get_saturation()
{
return 0;
}
uint32_t light_driver_get_temperature()
{
return 0;
}
@@ -0,0 +1,95 @@
// Copyright 2021 Espressif Systems (Shanghai) CO LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License
#include <esp_log.h>
#include <light_driver.h>
#include <color_format.h>
static const char *TAG = "light_driver_hollow";
static bool current_power = false;
static uint8_t current_brightness = 0;
static HS_color_t current_HS = {0, 0};
static uint32_t current_temperature = 6600;
esp_err_t light_driver_init(light_driver_config_t *config)
{
ESP_LOGI(TAG, "Initializing light driver");
/* Initialize the driver here */
return ESP_OK;
}
esp_err_t light_driver_set_power(bool power)
{
ESP_LOGI(TAG, "Setting power to: %d", power);
/* Set the power state here */
return ESP_OK;
}
esp_err_t light_driver_set_brightness(uint8_t brightness)
{
ESP_LOGI(TAG, "Setting brightness to: %d", brightness);
/* Set the brightness level here */
return ESP_OK;
}
esp_err_t light_driver_set_hue(uint16_t hue)
{
ESP_LOGI(TAG, "Setting hue to: %d", hue);
/* Set the color hue here */
return ESP_OK;
}
esp_err_t light_driver_set_saturation(uint8_t saturation)
{
ESP_LOGI(TAG, "Setting saturation to: %d", saturation);
/* Set the color saturation here */
return ESP_OK;
}
esp_err_t light_driver_set_temperature(uint32_t temperature)
{
ESP_LOGI(TAG, "Setting temperature to: %d", temperature);
/* Set the color temp here*/
return ESP_OK;
}
bool light_driver_get_power()
{
return current_power;
}
uint8_t light_driver_get_brightness()
{
return current_brightness;
}
uint16_t light_driver_get_hue()
{
return current_HS.hue;
}
uint8_t light_driver_get_saturation()
{
return current_HS.saturation;
}
uint32_t light_driver_get_temperature()
{
return current_temperature;
}
@@ -0,0 +1,39 @@
// Copyright 2021 Espressif Systems (Shanghai) CO LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C"
{
#endif
typedef struct {
uint16_t hue;
uint8_t saturation;
} HS_color_t;
typedef struct {
uint8_t red;
uint8_t green;
uint8_t blue;
} RGB_color_t;
void temp_to_hs(uint32_t temperature, HS_color_t *HS);
void hsv_to_rgb(HS_color_t HS,uint8_t brightness, RGB_color_t *RGB);
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,42 @@
// Copyright 2021 Espressif Systems (Shanghai) CO LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License
#pragma once
#include <esp_err.h>
#ifdef __cplusplus
extern "C"
{
#endif
typedef struct {
int gpio;
int channel;
} light_driver_config_t;
esp_err_t light_driver_init(light_driver_config_t *config);
esp_err_t light_driver_set_power(bool power);
esp_err_t light_driver_set_brightness(uint8_t brightness);
esp_err_t light_driver_set_hue(uint16_t hue);
esp_err_t light_driver_set_saturation(uint8_t saturation);
esp_err_t light_driver_set_temperature(uint32_t temperature);
bool light_driver_get_power(void);
uint8_t light_driver_get_brightness(void);
uint16_t light_driver_get_hue(void);
uint8_t light_driver_get_saturation(void);
uint32_t light_driver_get_temperature(void);
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,88 @@
// Copyright 2021 Espressif Systems (Shanghai) CO LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License
#include <color_format.h>
void hsv_to_rgb(HS_color_t HS,uint8_t brightness, RGB_color_t *RGB)
{
uint16_t sector = HS.hue / 60;
uint16_t rgb_max = brightness;
uint16_t rgb_min = rgb_max * (100 - HS.saturation) / 100;
uint16_t offset = HS.hue % 60;
uint16_t rgb_adj = (rgb_max - rgb_min) * offset / 60;
switch (sector) {
case 0:
RGB->red = rgb_max;
RGB->green = rgb_min + rgb_adj;
RGB->blue = rgb_min;
break;
case 1:
RGB->red = rgb_max - rgb_adj;
RGB->green = rgb_max;
RGB->blue = rgb_min;
break;
case 2:
RGB->red = rgb_min;
RGB->green = rgb_max;
RGB->blue = rgb_min + rgb_adj;
break;
case 3:
RGB->red = rgb_min;
RGB->green = rgb_max - rgb_adj;
RGB->blue = rgb_max;
break;
case 4:
RGB->red = rgb_min + rgb_adj;
RGB->green = rgb_min;
RGB->blue = rgb_max;
break;
default:
RGB->red = rgb_max;
RGB->green = rgb_min;
RGB->blue = rgb_max - rgb_adj;
break;
}
}
// A Table from color temperature to hue and saturation.
// hue = temp_table[(temp - 600) / 100].hue
// saturation= temp_table[(temp - 600) / 100].saturation
// 600<= temp <= 10000
const HS_color_t temp_table[] = {
{4, 100}, {8, 100}, {11, 100}, {14, 100}, {16, 100}, {18, 100}, {20, 100}, {22, 100}, {24, 100}, {25, 100}, {27, 100},
{28, 100},{30, 100}, {31, 100}, {31, 95}, {30, 89}, {30, 85}, {29, 80}, {29, 76}, {29, 73}, {29, 69}, {28, 66}, {28, 63},
{28, 60}, {28, 57}, {28, 54}, {28, 52}, {27, 49}, {27, 47}, {27, 45}, {27, 43}, {27, 41}, {27, 39}, {27, 37}, {27, 35},
{27, 33}, {27, 31}, {27, 30}, {27, 28}, {27, 26}, {27, 25}, {27, 23}, {27, 22}, {27, 21}, {27, 19}, {27, 18}, {27, 17},
{27, 15}, {28, 14}, {28, 13}, {28, 12}, {29, 10}, {29, 9}, {30, 8}, {31, 7}, {32, 6}, {34, 5}, {36, 4}, {41, 3}, {49, 2},
{0, 0}, {294, 2}, {265, 3}, {251, 4}, {242, 5}, {237, 6}, {233, 7}, {231, 8}, {229, 9}, {228, 10}, {227, 11}, {226, 11},
{226, 12}, {225, 13}, {225, 13}, {224, 14}, {224, 14}, {224, 15}, {224, 15}, {223, 16}, {223, 16}, {223, 17}, {223, 17},
{223, 17}, {222, 18}, {222, 18}, {222, 19}, {222, 19}, {222, 19}, {222, 19}, {222, 20}, {222, 20}, {222, 20}, {222, 21},
{222, 21}
};
void temp_to_hs(uint32_t temperature, HS_color_t *HS)
{
if (temperature < 600) {
HS->hue = 0;
HS->saturation = 100;
return;
}
if (temperature > 10000) {
HS->hue = 222;
HS->saturation = 21 + (temperature -10000) * 41 / 990000;
return;
}
HS->hue = temp_table[(temperature - 600) / 100].hue;
HS->saturation = temp_table[(temperature - 600) / 100].saturation;
}
+212
View File
@@ -0,0 +1,212 @@
// Copyright 2021 Espressif Systems (Shanghai) CO LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License
#include <string.h>
#include <esp_system.h>
#include <esp_log.h>
#include <driver/ledc.h>
#include <tft.h>
#include <tftspi.h>
#include <light_driver.h>
#include <color_format.h>
#define TFT_SPI_CLOCK_INIT_HZ 8000000
#define LEDC_PWM_HZ 1000
#define BRIGHTNESS_MAX 255
static const char *TAG = "light_driver_vled";
static bool current_power = false;
static uint8_t current_brightness = 0;
static HS_color_t current_HS = {0, 0};
static uint32_t current_temperature = 6600;
static RGB_color_t mRGB;
static uint16_t DisplayHeight;
static uint16_t DisplayWidth;
static int led_driver_channel = -1;
static void SetupBrightnessControl(light_driver_config_t *config)
{
ledc_timer_config_t ledc_timer;
memset(&ledc_timer, 0, sizeof(ledc_timer));
led_driver_channel = config->channel;
ledc_timer.duty_resolution = LEDC_TIMER_8_BIT; // resolution of PWM duty
ledc_timer.freq_hz = LEDC_PWM_HZ; // frequency of PWM signal
ledc_timer.speed_mode = LEDC_HIGH_SPEED_MODE; // timer mode
ledc_timer.timer_num = LEDC_TIMER_0; // timer index
ledc_timer_config(&ledc_timer);
ledc_timer_set(LEDC_HIGH_SPEED_MODE, LEDC_TIMER_0, LEDC_PWM_HZ, LEDC_TIMER_8_BIT, LEDC_REF_TICK);
ledc_channel_config_t ledc_channel;
memset(&ledc_channel, 0, sizeof(ledc_channel));
ledc_channel.channel = led_driver_channel;
ledc_channel.duty = BRIGHTNESS_MAX;
ledc_channel.gpio_num = config->gpio;
ledc_channel.speed_mode = LEDC_HIGH_SPEED_MODE;
ledc_channel.timer_sel = LEDC_TIMER_0;
ledc_channel_config(&ledc_channel);
}
static void SetDisplayBrightness(uint8_t brightness)
{
if (ledc_set_duty(LEDC_HIGH_SPEED_MODE, led_driver_channel, brightness) ||
ledc_update_duty(LEDC_HIGH_SPEED_MODE, led_driver_channel)) {
ESP_LOGE(TAG, "Failed to set display brightness...");
}
}
static esp_err_t InitDisplay()
{
esp_err_t err;
spi_lobo_device_handle_t spi;
spi_lobo_bus_config_t buscfg;
memset((void *) &buscfg, 0, sizeof(buscfg));
buscfg.miso_io_num = PIN_NUM_MISO; // set SPI MISO pin
buscfg.mosi_io_num = PIN_NUM_MOSI; // set SPI MOSI pin
buscfg.sclk_io_num = PIN_NUM_CLK; // set SPI CLK pin
buscfg.quadwp_io_num = -1;
buscfg.quadhd_io_num = -1;
spi_lobo_device_interface_config_t devcfg;
memset((void *) &devcfg, 0, sizeof(devcfg));
devcfg.clock_speed_hz = TFT_SPI_CLOCK_INIT_HZ;
devcfg.mode = 0; // SPI mode 0
devcfg.spics_io_num = -1; // we will use external CS pin
devcfg.spics_ext_io_num = PIN_NUM_CS; // external CS pi
devcfg.flags = LB_SPI_DEVICE_HALFDUPLEX; // ALWAYS SET to HALF DUPLEX MODE!! for display spi
tft_max_rdclock = TFT_SPI_CLOCK_INIT_HZ;
// Initialize all pins used by display driver.
TFT_PinsInit();
// Initialize SPI bus and add a device for the display.
err = spi_lobo_bus_add_device(TFT_HSPI_HOST, &buscfg, &devcfg, &spi);
if (err != ESP_OK) {
return err;
}
// Configure the display to use the new SPI device.
tft_disp_spi = spi;
err = spi_lobo_device_select(spi, 1);
if (err != ESP_OK) {
return err;
}
err = spi_lobo_device_deselect(spi);
if (err != ESP_OK) {
return err;
}
// Initialize the display driver.
TFT_display_init();
// Detect maximum read speed and set it.
tft_max_rdclock = find_rd_speed();
spi_lobo_set_speed(spi, DEFAULT_SPI_CLOCK);
TFT_setGammaCurve(0);
TFT_setRotation(LANDSCAPE);
TFT_resetclipwin();
DisplayWidth = (uint16_t)(1 + tft_dispWin.x2 - tft_dispWin.x1);
DisplayHeight = (uint16_t)(1 + tft_dispWin.y2 - tft_dispWin.y1);
ESP_LOGI(TAG, "Display initialized (height %u, width %u)", DisplayHeight, DisplayWidth);
TFT_invertDisplay(INVERT_OFF);
return ESP_OK;
}
esp_err_t light_driver_init(light_driver_config_t *config)
{
ESP_LOGI(TAG, "Initializing led driver");
esp_err_t err = ESP_OK;
err = InitDisplay();
if (err != ESP_OK) {
return err;
}
SetupBrightnessControl(config);
SetDisplayBrightness(BRIGHTNESS_MAX);
return err;
}
esp_err_t light_driver_set_power(bool power)
{
current_power = power;
return ESP_OK;
}
esp_err_t light_driver_set_RGB()
{
TFT_fillWindow(TFT_BLACK);
TFT_fillCircle(DisplayWidth / 2, DisplayHeight / 2, DisplayWidth / 4, (color_t) {mRGB.red, mRGB.green, mRGB.blue});
TFT_drawCircle(DisplayWidth / 2, DisplayHeight / 2, DisplayWidth / 4, (color_t) {255, 255, 255});
return ESP_OK;
}
esp_err_t light_driver_set_brightness(uint8_t brightness)
{
if (brightness != 0) {
current_brightness = brightness;
}
if (!current_power) {
brightness = 0;
}
hsv_to_rgb(current_HS, brightness, &mRGB);
return light_driver_set_RGB();
}
esp_err_t light_driver_set_hue(uint16_t hue)
{
uint8_t brightness = current_power ? current_brightness : 0;
current_HS.hue = hue;
hsv_to_rgb(current_HS, brightness, &mRGB);
return light_driver_set_RGB();
}
esp_err_t light_driver_set_saturation(uint8_t saturation)
{
uint8_t brightness = current_power ? current_brightness : 0;
current_HS.saturation = saturation;
hsv_to_rgb(current_HS, brightness, &mRGB);
return light_driver_set_RGB();
}
esp_err_t light_driver_set_temperature(uint32_t temperature)
{
uint8_t brightness = current_power ? current_brightness : 0;
current_temperature = temperature;
temp_to_hs(current_temperature,&current_HS);
hsv_to_rgb(current_HS, brightness, &mRGB);
return light_driver_set_RGB();
}
bool light_driver_get_power()
{
return current_power;
}
uint8_t light_driver_get_brightness()
{
return current_brightness;
}
uint16_t light_driver_get_hue()
{
return current_HS.hue;
}
uint8_t light_driver_get_saturation()
{
return current_HS.saturation;
}
uint32_t light_driver_get_temperature()
{
return current_temperature;
}
@@ -0,0 +1,140 @@
// Copyright 2021 Espressif Systems (Shanghai) CO LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License
#include <esp_log.h>
#include <driver/rmt.h>
#include <led_strip.h>
#include <color_format.h>
#include <light_driver.h>
static const char *TAG = "light_driver_ws2812";
static bool current_power = false;
static uint8_t current_brightness = 0;
static uint32_t current_temp = 6600;
static HS_color_t current_HS = {0, 0};
static RGB_color_t mRGB;
static led_strip_t *strip = NULL;
esp_err_t light_driver_init(light_driver_config_t *config)
{
ESP_LOGI(TAG, "Initializing light driver");
esp_err_t err = ESP_OK;
rmt_config_t rmt_cfg = RMT_DEFAULT_CONFIG_TX(config->gpio, config->channel);
rmt_cfg.clk_div = 2;
err = rmt_config(&rmt_cfg);
if (err != ESP_OK) {
ESP_LOGE(TAG, "rmt_cfg failed");
}
err = rmt_driver_install(rmt_cfg.channel, 0, 0);
if (err != ESP_OK) {
ESP_LOGE(TAG, "rmt_driver_install failed");
}
led_strip_config_t strip_config = LED_STRIP_DEFAULT_CONFIG(1, (led_strip_dev_t)rmt_cfg.channel);
strip = led_strip_new_rmt_ws2812(&strip_config);
if (!strip) {
ESP_LOGE(TAG, "W2812 driver install failed");
err = ESP_FAIL;
}
return err;
}
esp_err_t light_driver_set_power(bool power)
{
current_power = power;
return ESP_OK;
}
esp_err_t light_driver_set_RGB()
{
esp_err_t err = ESP_OK;
if (!strip) {
ESP_LOGE(TAG, "can't find w2812 led_strip handle");
err = ESP_FAIL;
} else {
err = strip->set_pixel(strip, 0, mRGB.red, mRGB.green, mRGB.blue);
if (err != ESP_OK) {
ESP_LOGE(TAG, "strip_set_pixel failed");
return err;
}
ESP_LOGI(TAG, "led set r:%d, g:%d, b:%d", mRGB.red, mRGB.green, mRGB.blue);
err = strip->refresh(strip, 100);
if (err != ESP_OK) {
ESP_LOGE(TAG, "strip_refresh failed");
}
}
return err;
}
esp_err_t light_driver_set_brightness(uint8_t brightness)
{
if (brightness != 0) {
current_brightness = brightness;
}
if (!current_power) {
brightness = 0;
}
hsv_to_rgb(current_HS, brightness, &mRGB);
return light_driver_set_RGB();
}
esp_err_t light_driver_set_hue(uint16_t hue)
{
uint8_t brightness = current_power ? current_brightness : 0;
current_HS.hue = hue;
hsv_to_rgb(current_HS, brightness, &mRGB);
return light_driver_set_RGB();
}
esp_err_t light_driver_set_saturation(uint8_t saturation)
{
uint8_t brightness = current_power ? current_brightness : 0;
current_HS.saturation = saturation;
hsv_to_rgb(current_HS, brightness, &mRGB);
return light_driver_set_RGB();
}
esp_err_t light_driver_set_temperature(uint32_t temperature)
{
uint8_t brightness = current_power ? current_brightness : 0;
current_temp = temperature;
temp_to_hs(current_temp, &current_HS);
hsv_to_rgb(current_HS, brightness, &mRGB);
return light_driver_set_RGB();
}
bool light_driver_get_power()
{
return current_power;
}
uint8_t light_driver_get_brightness()
{
return current_brightness;
}
uint16_t light_driver_get_hue()
{
return current_HS.hue;
}
uint8_t light_driver_get_saturation()
{
return current_HS.saturation;
}
uint32_t light_driver_get_temperature()
{
return current_temp;
}