From dce2cf64bbba1c28dcc2f00ae53eed8743462116 Mon Sep 17 00:00:00 2001 From: amass <168062547@qq.com> Date: Fri, 17 May 2024 01:08:15 +0800 Subject: [PATCH] =?UTF-8?q?pwd=20=E6=B5=8B=E8=AF=95=E6=88=90=E5=8A=9F?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/LedController/CMakeLists.txt | 5 ++ components/LedController/LedController.cpp | 63 ++++++++++++++++++++++ components/LedController/LedController.h | 16 ++++++ components/command/CMakeLists.txt | 4 +- components/command/CustomCommand.cpp | 36 +++++++++++++ components/command/CustomCommand.h | 11 ++++ components/command/cmd_custom.c | 18 ------- components/command/cmd_custom.h | 6 --- main/CMakeLists.txt | 2 +- main/{main.c => main.cpp} | 45 ++++++++-------- sdkconfig.defaults | 2 +- 11 files changed, 158 insertions(+), 50 deletions(-) create mode 100644 components/LedController/CMakeLists.txt create mode 100644 components/LedController/LedController.cpp create mode 100644 components/LedController/LedController.h create mode 100644 components/command/CustomCommand.cpp create mode 100644 components/command/CustomCommand.h delete mode 100644 components/command/cmd_custom.c delete mode 100644 components/command/cmd_custom.h rename main/{main.c => main.cpp} (72%) diff --git a/components/LedController/CMakeLists.txt b/components/LedController/CMakeLists.txt new file mode 100644 index 0000000..a6e4934 --- /dev/null +++ b/components/LedController/CMakeLists.txt @@ -0,0 +1,5 @@ +idf_component_register(SRCS + LedController.cpp + INCLUDE_DIRS . + REQUIRES console spi_flash driver +) diff --git a/components/LedController/LedController.cpp b/components/LedController/LedController.cpp new file mode 100644 index 0000000..315725d --- /dev/null +++ b/components/LedController/LedController.cpp @@ -0,0 +1,63 @@ +#include "LedController.h" +#include +#include + +LedController::LedController() { + memset(m_channels, 0, sizeof(m_channels)); +} + +bool LedController::initialize() { + ledc_timer_config_t ledc_timer; + memset(&ledc_timer, 0, sizeof(ledc_timer)); + ledc_timer.speed_mode = LEDC_HIGH_SPEED_MODE; + ledc_timer.duty_resolution = LEDC_TIMER_13_BIT; + ledc_timer.timer_num = LEDC_TIMER_0; + ledc_timer.freq_hz = 4000; + ledc_timer.clk_cfg = LEDC_AUTO_CLK; + auto status = ledc_timer_config(&ledc_timer); + if (status != ESP_OK) { + std::cout << "ledc_timer_config() failed." << std::endl; + } + + m_channels[0].gpio_num = 18; // gpio18 + m_channels[0].speed_mode = LEDC_HIGH_SPEED_MODE; + m_channels[0].channel = LEDC_CHANNEL_0; + m_channels[0].timer_sel = LEDC_TIMER_0; + m_channels[0].duty = 0; + m_channels[0].hpoint = 0; + m_channels[0].flags.output_invert = 0; + + m_channels[1].gpio_num = 19; // gpio19 + m_channels[1].speed_mode = LEDC_HIGH_SPEED_MODE; + m_channels[1].channel = LEDC_CHANNEL_1; + m_channels[1].timer_sel = LEDC_TIMER_0; + m_channels[1].duty = 0; + m_channels[1].hpoint = 0; + m_channels[1].flags.output_invert = 0; + + for (int i = 0; i < sizeof(m_channels) / sizeof(m_channels[0]); i++) { + status = ledc_channel_config(&m_channels[i]); + if (status != ESP_OK) { + std::cout << "ledc_timer_config() failed." << std::endl; + } + + ledc_set_duty(m_channels[i].speed_mode, m_channels[i].channel, 4096); + ledc_update_duty(m_channels[i].speed_mode, m_channels[i].channel); + } + std::cout << "led controller initialize finished." << std::endl; + return true; +} + +void LedController::setDuty(int32_t channel, int32_t duty) { + duty = static_cast(0x1FFF * duty) / 100; + + if ((channel < 0) || (channel >= sizeof(m_channels) / sizeof(m_channels[0]))) return; + std::cout<<"set channle "< + +class LedController { +public: + static LedController *instance(); + bool initialize(); + void setDuty(int32_t channel, int32_t duty); + +protected: + LedController(); + ledc_channel_config_t m_channels[2]; +}; +#endif // __LEDCONTROLLER_H__ \ No newline at end of file diff --git a/components/command/CMakeLists.txt b/components/command/CMakeLists.txt index 3ff1a85..011b331 100644 --- a/components/command/CMakeLists.txt +++ b/components/command/CMakeLists.txt @@ -1,9 +1,9 @@ idf_component_register(SRCS cmd_system.c cmd_system_common.c - cmd_custom.c + CustomCommand.cpp INCLUDE_DIRS . - REQUIRES console spi_flash driver + REQUIRES console spi_flash driver LedController ) target_sources(${COMPONENT_LIB} PRIVATE cmd_system_sleep.c) \ No newline at end of file diff --git a/components/command/CustomCommand.cpp b/components/command/CustomCommand.cpp new file mode 100644 index 0000000..e6b5aa6 --- /dev/null +++ b/components/command/CustomCommand.cpp @@ -0,0 +1,36 @@ +#include "CustomCommand.h" +#include "LedController.h" +#include "esp_console.h" +#include + +static int custom_command(int argc, char **argv) { + printf("i am amass.\n"); + return 0; +} + +static int led_command(int argc, char **argv) { + for (int i = 0; i < argc; i++) { + std::cout << i << " " << argv[i] << std::endl; + } + LedController::instance()->setDuty(atoi(argv[1]), atoi(argv[2])); + + return 0; +} + +void register_custom() { + const esp_console_cmd_t heap_cmd = { + .command = "amass", + .help = "test command.", + .hint = NULL, + .func = &custom_command, + }; + ESP_ERROR_CHECK(esp_console_cmd_register(&heap_cmd)); + + const esp_console_cmd_t led_cmd = { + .command = "led", + .help = "led pwm duty.", + .hint = NULL, + .func = &led_command, + }; + ESP_ERROR_CHECK(esp_console_cmd_register(&led_cmd)); +} diff --git a/components/command/CustomCommand.h b/components/command/CustomCommand.h new file mode 100644 index 0000000..fd56826 --- /dev/null +++ b/components/command/CustomCommand.h @@ -0,0 +1,11 @@ +#ifndef __CUSTOMCOMMAND_H__ +#define __CUSTOMCOMMAND_H__ + + + + +void register_custom(); + + + +#endif // __CUSTOMCOMMAND_H__ \ No newline at end of file diff --git a/components/command/cmd_custom.c b/components/command/cmd_custom.c deleted file mode 100644 index 8345a0d..0000000 --- a/components/command/cmd_custom.c +++ /dev/null @@ -1,18 +0,0 @@ -#include "cmd_custom.h" -#include "esp_console.h" - - -static int custom_command(int argc, char **argv) { - printf("i am amass.\n"); - return 0; -} - -void register_custom() { - const esp_console_cmd_t heap_cmd = { - .command = "amass", - .help = "test command.", - .hint = NULL, - .func = &custom_command, - }; - ESP_ERROR_CHECK(esp_console_cmd_register(&heap_cmd)); -} diff --git a/components/command/cmd_custom.h b/components/command/cmd_custom.h deleted file mode 100644 index 6bc1e20..0000000 --- a/components/command/cmd_custom.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef __CMD_CUSTOM_H__ -#define __CMD_CUSTOM_H__ - -void register_custom(); - -#endif // __CMD_CUSTOM_H__ \ No newline at end of file diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 69203c1..0c1ef5d 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,2 +1,2 @@ -idf_component_register(SRCS "main.c" +idf_component_register(SRCS "main.cpp" INCLUDE_DIRS ".") diff --git a/main/main.c b/main/main.cpp similarity index 72% rename from main/main.c rename to main/main.cpp index 33f89ab..eaff316 100644 --- a/main/main.c +++ b/main/main.cpp @@ -1,4 +1,5 @@ -#include "cmd_custom.h" +#include "CustomCommand.h" +#include "LedController.h" #include "cmd_system.h" #include "driver/uart.h" #include "esp_console.h" @@ -22,7 +23,7 @@ static void initialize_nvs(); static void initialize_filesystem(); static void initialize_console(); -void app_main() { +extern "C" void app_main() { const char *prompt = LOG_COLOR_I PROMPT_STR "> " LOG_RESET_COLOR; initialize_nvs(); initialize_filesystem(); @@ -31,6 +32,8 @@ void app_main() { register_system_common(); register_system_sleep(); register_custom(); + + LedController::instance()->initialize(); while (true) { char *line = linenoise(prompt); if (line == NULL) { /* Break on EOF or error */ @@ -68,24 +71,22 @@ static void initialize_console() { esp_vfs_dev_uart_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR); esp_vfs_dev_uart_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF); - const uart_config_t uart_config = { - .baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE, - .data_bits = UART_DATA_8_BITS, - .parity = UART_PARITY_DISABLE, - .stop_bits = UART_STOP_BITS_1, - .source_clk = UART_SCLK_REF_TICK, - }; - - ESP_ERROR_CHECK(uart_driver_install(CONFIG_ESP_CONSOLE_UART_NUM, 256, 0, 0, NULL, 0)); - ESP_ERROR_CHECK(uart_param_config(CONFIG_ESP_CONSOLE_UART_NUM, &uart_config)); + uart_config_t uart_config = {0}; + uart_config.baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE; + uart_config.data_bits = UART_DATA_8_BITS; + uart_config.parity = UART_PARITY_DISABLE; + uart_config.stop_bits = UART_STOP_BITS_1; + uart_config.source_clk = UART_SCLK_REF_TICK; + ESP_ERROR_CHECK(uart_driver_install(static_cast(CONFIG_ESP_CONSOLE_UART_NUM), 256, 0, 0, NULL, 0)); + ESP_ERROR_CHECK(uart_param_config(static_cast(CONFIG_ESP_CONSOLE_UART_NUM), &uart_config)); esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM); - esp_console_config_t console_config = { - .max_cmdline_args = 8, - .max_cmdline_length = 256, - .hint_color = atoi(LOG_COLOR_CYAN), - }; + esp_console_config_t console_config = {0}; + console_config.max_cmdline_args = 8; + console_config.max_cmdline_length = 256; + console_config.hint_color = atoi(LOG_COLOR_CYAN); + ESP_ERROR_CHECK(esp_console_init(&console_config)); linenoiseSetMultiLine(1); @@ -111,11 +112,11 @@ static void initialize_nvs() { static void initialize_filesystem(void) { static wl_handle_t wl_handle; - const esp_vfs_fat_mount_config_t mount_config = { - .max_files = 4, - .format_if_mount_failed = true, - }; - esp_err_t err = esp_vfs_fat_spiflash_mount_rw_wl(MOUNT_PATH, "storage", &mount_config, &wl_handle); + esp_vfs_fat_mount_config_t config; + memset(&config, 0, sizeof(esp_vfs_fat_mount_config_t)); + config.max_files = 4; + config.format_if_mount_failed = true; + esp_err_t err = esp_vfs_fat_spiflash_mount_rw_wl(MOUNT_PATH, "storage", &config, &wl_handle); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err)); return; diff --git a/sdkconfig.defaults b/sdkconfig.defaults index 35982bd..b2d26d6 100644 --- a/sdkconfig.defaults +++ b/sdkconfig.defaults @@ -14,7 +14,7 @@ CONFIG_PARTITION_TABLE_FILENAME="partitions.csv" CONFIG_FREERTOS_USE_TRACE_FACILITY=y CONFIG_FREERTOS_USE_STATS_FORMATTING_FUNCTIONS=y -CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y +CONFIG_ESPTOOLPY_FLASHSIZE_16MB=y # On chips with USB serial, disable secondary console which does not make sense when using console component CONFIG_ESP_CONSOLE_SECONDARY_NONE=y