pwd 测试成功。

This commit is contained in:
2024-05-17 01:08:15 +08:00
parent 860f83ca0d
commit dce2cf64bb
11 changed files with 158 additions and 50 deletions

View File

@ -0,0 +1,5 @@
idf_component_register(SRCS
LedController.cpp
INCLUDE_DIRS .
REQUIRES console spi_flash driver
)

View File

@ -0,0 +1,63 @@
#include "LedController.h"
#include <cstring>
#include <iostream>
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<float>(0x1FFF * duty) / 100;
if ((channel < 0) || (channel >= sizeof(m_channels) / sizeof(m_channels[0]))) return;
std::cout<<"set channle "<<channel<<" duty: "<<duty<<std::endl;
ledc_set_duty(m_channels[channel].speed_mode, m_channels[channel].channel, duty);
ledc_update_duty(m_channels[channel].speed_mode, m_channels[channel].channel);
}
LedController *LedController::instance() {
static LedController self;
return &self;
}

View File

@ -0,0 +1,16 @@
#ifndef __LEDCONTROLLER_H__
#define __LEDCONTROLLER_H__
#include <driver/ledc.h>
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__

View File

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

View File

@ -0,0 +1,36 @@
#include "CustomCommand.h"
#include "LedController.h"
#include "esp_console.h"
#include <iostream>
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));
}

View File

@ -0,0 +1,11 @@
#ifndef __CUSTOMCOMMAND_H__
#define __CUSTOMCOMMAND_H__
void register_custom();
#endif // __CUSTOMCOMMAND_H__

View File

@ -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));
}

View File

@ -1,6 +0,0 @@
#ifndef __CMD_CUSTOM_H__
#define __CMD_CUSTOM_H__
void register_custom();
#endif // __CMD_CUSTOM_H__