forked from xiaozhi/xiaozhi-esp32
iot增加屏幕背光亮度调节 (#150)
This commit is contained in:
@@ -134,6 +134,7 @@ private:
|
||||
void InitializeIot() {
|
||||
auto& thing_manager = iot::ThingManager::GetInstance();
|
||||
thing_manager.AddThing(iot::CreateThing("Speaker"));
|
||||
thing_manager.AddThing(iot::CreateThing("Backlight"));
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
@@ -126,6 +126,7 @@ private:
|
||||
void InitializeIot() {
|
||||
auto& thing_manager = iot::ThingManager::GetInstance();
|
||||
thing_manager.AddThing(iot::CreateThing("Speaker"));
|
||||
thing_manager.AddThing(iot::CreateThing("Backlight"));
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
@@ -104,6 +104,7 @@ private:
|
||||
void InitializeIot() {
|
||||
auto& thing_manager = iot::ThingManager::GetInstance();
|
||||
thing_manager.AddThing(iot::CreateThing("Speaker"));
|
||||
thing_manager.AddThing(iot::CreateThing("Backlight"));
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
@@ -130,6 +130,7 @@ private:
|
||||
void InitializeIot() {
|
||||
auto& thing_manager = iot::ThingManager::GetInstance();
|
||||
thing_manager.AddThing(iot::CreateThing("Speaker"));
|
||||
thing_manager.AddThing(iot::CreateThing("Backlight"));
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
@@ -150,6 +150,7 @@ private:
|
||||
void InitializeIot() {
|
||||
auto& thing_manager = iot::ThingManager::GetInstance();
|
||||
thing_manager.AddThing(iot::CreateThing("Speaker"));
|
||||
thing_manager.AddThing(iot::CreateThing("Backlight"));
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
@@ -143,6 +143,7 @@ private:
|
||||
void InitializeIot() {
|
||||
auto& thing_manager = iot::ThingManager::GetInstance();
|
||||
thing_manager.AddThing(iot::CreateThing("Speaker"));
|
||||
thing_manager.AddThing(iot::CreateThing("Backlight"));
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
@@ -131,7 +131,8 @@ private:
|
||||
// 物联网初始化,添加对 AI 可见设备
|
||||
void InitializeIot() {
|
||||
auto& thing_manager = iot::ThingManager::GetInstance();
|
||||
thing_manager.AddThing(iot::CreateThing("Speaker"));
|
||||
thing_manager.AddThing(iot::CreateThing("Speaker"));
|
||||
thing_manager.AddThing(iot::CreateThing("Backlight"));
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
@@ -198,6 +198,7 @@ private:
|
||||
void InitializeIot() {
|
||||
auto& thing_manager = iot::ThingManager::GetInstance();
|
||||
thing_manager.AddThing(iot::CreateThing("Speaker"));
|
||||
thing_manager.AddThing(iot::CreateThing("Backlight"));
|
||||
}
|
||||
void InitializeMute() {
|
||||
gpio_reset_pin(AUDIO_MUTE_PIN);
|
||||
|
||||
@@ -23,6 +23,7 @@ public:
|
||||
virtual void SetEmotion(const std::string &emotion);
|
||||
virtual void SetChatMessage(const std::string &role, const std::string &content);
|
||||
virtual void SetIcon(const char* icon);
|
||||
virtual void SetBacklight(uint8_t brightness) = 0;
|
||||
|
||||
int width() const { return width_; }
|
||||
int height() const { return height_; }
|
||||
|
||||
@@ -132,7 +132,7 @@ void LcdDisplay::InitializeBacklight(gpio_num_t backlight_pin) {
|
||||
.speed_mode = LEDC_LOW_SPEED_MODE,
|
||||
.duty_resolution = LEDC_TIMER_10_BIT,
|
||||
.timer_num = LEDC_TIMER_0,
|
||||
.freq_hz = 5000,
|
||||
.freq_hz = 20000,//背光pwm频率需要高一点,防止电感啸叫
|
||||
.clk_cfg = LEDC_AUTO_CLK,
|
||||
.deconfigure = false
|
||||
};
|
||||
|
||||
@@ -31,7 +31,6 @@ protected:
|
||||
DisplayFonts fonts_;
|
||||
|
||||
void InitializeBacklight(gpio_num_t backlight_pin);
|
||||
void SetBacklight(uint8_t brightness);
|
||||
|
||||
virtual void SetupUI();
|
||||
virtual bool Lock(int timeout_ms = 0) override;
|
||||
@@ -47,6 +46,7 @@ public:
|
||||
void SetChatMessage(const std::string &role, const std::string &content) override;
|
||||
void SetEmotion(const std::string &emotion) override;
|
||||
void SetIcon(const char* icon) override;
|
||||
void SetBacklight(uint8_t brightness);
|
||||
};
|
||||
|
||||
#endif // LCD_DISPLAY_H
|
||||
|
||||
@@ -7,7 +7,9 @@ class NoDisplay : public Display {
|
||||
private:
|
||||
virtual bool Lock(int timeout_ms = 0) override;
|
||||
virtual void Unlock() override;
|
||||
|
||||
virtual void SetBacklight(uint8_t brightness) override {
|
||||
// 空实现,因为这是一个无显示设备
|
||||
}
|
||||
public:
|
||||
NoDisplay();
|
||||
~NoDisplay();
|
||||
|
||||
44
main/iot/things/blaklight.cc
Normal file
44
main/iot/things/blaklight.cc
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "iot/thing.h"
|
||||
#include "board.h"
|
||||
#include "display/lcd_display.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
|
||||
#define TAG "Backlight"
|
||||
|
||||
namespace iot {
|
||||
|
||||
// 这里仅定义 Backlight 的属性和方法,不包含具体的实现
|
||||
class Backlight : public Thing {
|
||||
public:
|
||||
Backlight() : Thing("Backlight", "当前 AI 机器人屏幕的亮度") {
|
||||
// 定义设备的属性
|
||||
properties_.AddNumberProperty("light", "当前亮度值", [this]() -> int {
|
||||
// 这里可以添加获取当前亮度的逻辑
|
||||
return current_brightness_;
|
||||
});
|
||||
|
||||
// 定义设备可以被远程执行的指令
|
||||
methods_.AddMethod("SetLight", "设置亮度", ParameterList({
|
||||
Parameter("light", "0到100之间的整数", kValueTypeNumber, true)
|
||||
}), [this](const ParameterList& parameters) {
|
||||
auto display = Board::GetInstance().GetDisplay();
|
||||
uint8_t target_brightness = static_cast<uint8_t>(parameters["light"].number());
|
||||
int step = (target_brightness > current_brightness_) ? 1 : -1;
|
||||
for (int brightness = current_brightness_; brightness != target_brightness; brightness += step) {
|
||||
display->SetBacklight(static_cast<uint8_t>(brightness));
|
||||
// 可以根据需要调整渐变速度,这里假设每次调整间隔 10 毫秒
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
}
|
||||
display->SetBacklight(target_brightness);
|
||||
current_brightness_ = target_brightness; // 保存当前亮度值
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
int current_brightness_ = 100; // 保存当前亮度值
|
||||
};
|
||||
|
||||
} // namespace iot
|
||||
|
||||
DECLARE_THING(Backlight);
|
||||
Reference in New Issue
Block a user