first commit
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
#include "iot/thing.h"
|
||||
#include "board.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
|
||||
#define TAG "Battery"
|
||||
|
||||
namespace iot {
|
||||
|
||||
// 这里仅定义 Battery 的属性和方法,不包含具体的实现
|
||||
class Battery : public Thing {
|
||||
private:
|
||||
int level_ = 0;
|
||||
bool charging_ = false;
|
||||
bool discharging_ = false;
|
||||
|
||||
public:
|
||||
Battery() : Thing("Battery", "The battery of the device") {
|
||||
// 定义设备的属性
|
||||
properties_.AddNumberProperty("level", "Current battery level", [this]() -> int {
|
||||
auto& board = Board::GetInstance();
|
||||
if (board.GetBatteryLevel(level_, charging_, discharging_)) {
|
||||
return level_;
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
properties_.AddBooleanProperty("charging", "Whether the battery is charging", [this]() -> int {
|
||||
return charging_;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace iot
|
||||
|
||||
DECLARE_THING(Battery);
|
||||
@@ -0,0 +1,58 @@
|
||||
#include "iot/thing.h"
|
||||
#include "board.h"
|
||||
#include "audio_codec.h"
|
||||
|
||||
#include <driver/gpio.h>
|
||||
#include <esp_log.h>
|
||||
|
||||
#define TAG "Lamp"
|
||||
|
||||
namespace iot {
|
||||
|
||||
// 这里仅定义 Lamp 的属性和方法,不包含具体的实现
|
||||
class Lamp : public Thing {
|
||||
private:
|
||||
#ifdef CONFIG_IDF_TARGET_ESP32
|
||||
gpio_num_t gpio_num_ = GPIO_NUM_12;
|
||||
#else
|
||||
gpio_num_t gpio_num_ = GPIO_NUM_18;
|
||||
#endif
|
||||
bool power_ = false;
|
||||
|
||||
void InitializeGpio() {
|
||||
gpio_config_t config = {
|
||||
.pin_bit_mask = (1ULL << gpio_num_),
|
||||
.mode = GPIO_MODE_OUTPUT,
|
||||
.pull_up_en = GPIO_PULLUP_DISABLE,
|
||||
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
||||
.intr_type = GPIO_INTR_DISABLE,
|
||||
};
|
||||
ESP_ERROR_CHECK(gpio_config(&config));
|
||||
gpio_set_level(gpio_num_, 0);
|
||||
}
|
||||
|
||||
public:
|
||||
Lamp() : Thing("Lamp", "A test lamp"), power_(false) {
|
||||
InitializeGpio();
|
||||
|
||||
// 定义设备的属性
|
||||
properties_.AddBooleanProperty("power", "Whether the lamp is on", [this]() -> bool {
|
||||
return power_;
|
||||
});
|
||||
|
||||
// 定义设备可以被远程执行的指令
|
||||
methods_.AddMethod("turn_on", "Turn on the lamp", ParameterList(), [this](const ParameterList& parameters) {
|
||||
power_ = true;
|
||||
gpio_set_level(gpio_num_, 1);
|
||||
});
|
||||
|
||||
methods_.AddMethod("turn_off", "Turn off the lamp", ParameterList(), [this](const ParameterList& parameters) {
|
||||
power_ = false;
|
||||
gpio_set_level(gpio_num_, 0);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace iot
|
||||
|
||||
DECLARE_THING(Lamp);
|
||||
@@ -0,0 +1,54 @@
|
||||
#include "iot/thing.h"
|
||||
#include "board.h"
|
||||
#include "display/lcd_display.h"
|
||||
#include "settings.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <string>
|
||||
|
||||
#define TAG "Screen"
|
||||
|
||||
namespace iot {
|
||||
|
||||
// 这里仅定义 Screen 的属性和方法,不包含具体的实现
|
||||
class Screen : public Thing {
|
||||
public:
|
||||
Screen() : Thing("Screen", "A screen that can set theme and brightness") {
|
||||
// 定义设备的属性
|
||||
properties_.AddStringProperty("theme", "Current theme", [this]() -> std::string {
|
||||
auto theme = Board::GetInstance().GetDisplay()->GetTheme();
|
||||
return theme;
|
||||
});
|
||||
|
||||
properties_.AddNumberProperty("brightness", "Current brightness percentage", [this]() -> int {
|
||||
// 这里可以添加获取当前亮度的逻辑
|
||||
auto backlight = Board::GetInstance().GetBacklight();
|
||||
return backlight ? backlight->brightness() : 100;
|
||||
});
|
||||
|
||||
// 定义设备可以被远程执行的指令
|
||||
methods_.AddMethod("set_theme", "Set the screen theme", ParameterList({
|
||||
Parameter("theme_name", "Valid string values are 'light' and 'dark'", kValueTypeString, true)
|
||||
}), [this](const ParameterList& parameters) {
|
||||
std::string theme_name = static_cast<std::string>(parameters["theme_name"].string());
|
||||
auto display = Board::GetInstance().GetDisplay();
|
||||
if (display) {
|
||||
display->SetTheme(theme_name);
|
||||
}
|
||||
});
|
||||
|
||||
methods_.AddMethod("set_brightness", "Set the brightness", ParameterList({
|
||||
Parameter("brightness", "An integer between 0 and 100", kValueTypeNumber, true)
|
||||
}), [this](const ParameterList& parameters) {
|
||||
uint8_t brightness = static_cast<uint8_t>(parameters["brightness"].number());
|
||||
auto backlight = Board::GetInstance().GetBacklight();
|
||||
if (backlight) {
|
||||
backlight->SetBrightness(brightness, true);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace iot
|
||||
|
||||
DECLARE_THING(Screen);
|
||||
@@ -0,0 +1,33 @@
|
||||
#include "iot/thing.h"
|
||||
#include "board.h"
|
||||
#include "audio_codec.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
|
||||
#define TAG "Speaker"
|
||||
|
||||
namespace iot {
|
||||
|
||||
// 这里仅定义 Speaker 的属性和方法,不包含具体的实现
|
||||
class Speaker : public Thing {
|
||||
public:
|
||||
Speaker() : Thing("AudioSpeaker", "The audio speaker of the device") {
|
||||
// 定义设备的属性
|
||||
properties_.AddNumberProperty("volume", "Current audio volume value", [this]() -> int {
|
||||
auto codec = Board::GetInstance().GetAudioCodec();
|
||||
return codec->output_volume();
|
||||
});
|
||||
|
||||
// 定义设备可以被远程执行的指令
|
||||
methods_.AddMethod("set_volume", "Set the audio volume", ParameterList({
|
||||
Parameter("volume", "An integer between 0 and 100", kValueTypeNumber, true)
|
||||
}), [this](const ParameterList& parameters) {
|
||||
auto codec = Board::GetInstance().GetAudioCodec();
|
||||
codec->SetOutputVolume(static_cast<uint8_t>(parameters["volume"].number()));
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace iot
|
||||
|
||||
DECLARE_THING(Speaker);
|
||||
Reference in New Issue
Block a user