完成初代的雏形设计
This commit is contained in:
3
components/console_user_cmds/CMakeLists.txt
Normal file
3
components/console_user_cmds/CMakeLists.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
idf_component_register(SRCS "console_user_cmds.c"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES console_simple_init console i2c_master_messager io_device_control wifi-connect)
|
||||
293
components/console_user_cmds/console_user_cmds.c
Normal file
293
components/console_user_cmds/console_user_cmds.c
Normal file
@@ -0,0 +1,293 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "esp_check.h"
|
||||
#include "console_simple_init.h"
|
||||
#include "console_user_cmds.h"
|
||||
#include "i2c_master_messager.h"
|
||||
#include "io_device_control.h"
|
||||
#include "wifi-connect.h"
|
||||
|
||||
static const char *wifi_status_to_str(wifi_connect_status_t status)
|
||||
{
|
||||
switch (status) {
|
||||
case WIFI_CONNECT_STATUS_IDLE:
|
||||
return "idle";
|
||||
case WIFI_CONNECT_STATUS_PROVISIONING:
|
||||
return "provisioning";
|
||||
case WIFI_CONNECT_STATUS_CONNECTING:
|
||||
return "connecting";
|
||||
case WIFI_CONNECT_STATUS_CONNECTED:
|
||||
return "connected";
|
||||
case WIFI_CONNECT_STATUS_FAILED:
|
||||
return "failed";
|
||||
case WIFI_CONNECT_STATUS_TIMEOUT:
|
||||
return "timeout";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
// hello: 最小可用命令,用于验证 console 链路是否正常。
|
||||
static int cmd_hello(int argc, char **argv)
|
||||
{
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
printf("hello from BotanicalBuddy\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// sensor: 读取一次传感器缓存数据并打印,便于快速排查现场状态。
|
||||
static int cmd_sensor(int argc, char **argv)
|
||||
{
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
i2c_master_messager_data_t data = {0};
|
||||
esp_err_t ret = i2c_master_messager_get_data(&data);
|
||||
if (ret != ESP_OK) {
|
||||
printf("sensor read failed: %s\n", esp_err_to_name(ret));
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (data.bh1750.valid) {
|
||||
printf("BH1750: lux=%.1f, ts=%lld ms\n",
|
||||
data.bh1750.lux,
|
||||
(long long)data.bh1750.last_update_ms);
|
||||
} else {
|
||||
printf("BH1750: invalid, err=%s\n", esp_err_to_name(data.bh1750.last_error));
|
||||
}
|
||||
|
||||
if (data.aht30.valid) {
|
||||
printf("AHT30: temp=%.1f C, hum=%.1f %%, ts=%lld ms\n",
|
||||
data.aht30.temperature_c,
|
||||
data.aht30.humidity_rh,
|
||||
(long long)data.aht30.last_update_ms);
|
||||
} else {
|
||||
printf("AHT30: invalid, err=%s\n", esp_err_to_name(data.aht30.last_error));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool parse_on_off_arg(const char *arg, bool *on)
|
||||
{
|
||||
if (strcmp(arg, "on") == 0 || strcmp(arg, "1") == 0) {
|
||||
*on = true;
|
||||
return true;
|
||||
}
|
||||
if (strcmp(arg, "off") == 0 || strcmp(arg, "0") == 0) {
|
||||
*on = false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// fan: 控制风扇开关,参数支持 on/off 或 1/0。
|
||||
static int cmd_fan(int argc, char **argv)
|
||||
{
|
||||
if (argc < 2) {
|
||||
printf("usage: fan <on|off>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool on = false;
|
||||
if (!parse_on_off_arg(argv[1], &on)) {
|
||||
printf("invalid arg: %s\n", argv[1]);
|
||||
printf("usage: fan <on|off>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
esp_err_t ret = io_device_control_set_fan(on);
|
||||
if (ret != ESP_OK) {
|
||||
printf("set fan failed: %s\n", esp_err_to_name(ret));
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("fan: %s\n", on ? "on" : "off");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// light: 控制补光灯开关,参数支持 on/off 或 1/0。
|
||||
static int cmd_light(int argc, char **argv)
|
||||
{
|
||||
if (argc < 2) {
|
||||
printf("usage: light <on|off>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool on = false;
|
||||
if (!parse_on_off_arg(argv[1], &on)) {
|
||||
printf("invalid arg: %s\n", argv[1]);
|
||||
printf("usage: light <on|off>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
esp_err_t ret = io_device_control_set_light(on);
|
||||
if (ret != ESP_OK) {
|
||||
printf("set light failed: %s\n", esp_err_to_name(ret));
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("light: %s\n", on ? "on" : "off");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// hot: 控制加热开关,参数支持 on/off 或 1/0。
|
||||
static int cmd_hot(int argc, char **argv)
|
||||
{
|
||||
if (argc < 2) {
|
||||
printf("usage: hot <on|off>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool on = false;
|
||||
if (!parse_on_off_arg(argv[1], &on)) {
|
||||
printf("invalid arg: %s\n", argv[1]);
|
||||
printf("usage: hot <on|off>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
esp_err_t ret = io_device_control_set_hot(on);
|
||||
if (ret != ESP_OK) {
|
||||
printf("set hot failed: %s\n", esp_err_to_name(ret));
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("hot: %s\n", on ? "on" : "off");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// cool: 控制制冷开关,参数支持 on/off 或 1/0。
|
||||
static int cmd_cool(int argc, char **argv)
|
||||
{
|
||||
if (argc < 2) {
|
||||
printf("usage: cool <on|off>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool on = false;
|
||||
if (!parse_on_off_arg(argv[1], &on)) {
|
||||
printf("invalid arg: %s\n", argv[1]);
|
||||
printf("usage: cool <on|off>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
esp_err_t ret = io_device_control_set_cool(on);
|
||||
if (ret != ESP_OK) {
|
||||
printf("set cool failed: %s\n", esp_err_to_name(ret));
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("cool: %s\n", on ? "on" : "off");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// wifi: 查询或控制配网状态,支持 status/start/stop/clear 子命令。
|
||||
static int cmd_wifi(int argc, char **argv)
|
||||
{
|
||||
if (argc < 2 || strcmp(argv[1], "status") == 0) {
|
||||
wifi_connect_config_t cfg = {0};
|
||||
esp_err_t cfg_ret = wifi_connect_get_config(&cfg);
|
||||
printf("wifi status: %s\n", wifi_status_to_str(wifi_connect_get_status()));
|
||||
if (cfg_ret == ESP_OK && cfg.has_config) {
|
||||
printf("saved ssid: %s\n", cfg.ssid);
|
||||
} else {
|
||||
printf("saved config: none\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (strcmp(argv[1], "start") == 0) {
|
||||
esp_err_t ret = wifi_connect_start();
|
||||
if (ret != ESP_OK) {
|
||||
printf("wifi start failed: %s\n", esp_err_to_name(ret));
|
||||
return 1;
|
||||
}
|
||||
printf("wifi start requested\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (strcmp(argv[1], "stop") == 0) {
|
||||
esp_err_t ret = wifi_connect_stop();
|
||||
if (ret != ESP_OK) {
|
||||
printf("wifi stop failed: %s\n", esp_err_to_name(ret));
|
||||
return 1;
|
||||
}
|
||||
printf("wifi stop requested\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (strcmp(argv[1], "clear") == 0) {
|
||||
esp_err_t ret = wifi_connect_clear_config();
|
||||
if (ret != ESP_OK) {
|
||||
printf("wifi clear failed: %s\n", esp_err_to_name(ret));
|
||||
return 1;
|
||||
}
|
||||
printf("wifi config cleared\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
printf("usage: wifi <status|start|stop|clear>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
esp_err_t console_user_cmds_register(void)
|
||||
{
|
||||
const esp_console_cmd_t hello_cmd = {
|
||||
.command = "hello",
|
||||
.help = "打印欢迎信息。用法: hello",
|
||||
.func = cmd_hello,
|
||||
};
|
||||
ESP_RETURN_ON_ERROR(esp_console_cmd_register(&hello_cmd), "console_user_cmds", "register hello failed");
|
||||
|
||||
const esp_console_cmd_t sensor_cmd = {
|
||||
.command = "sensor",
|
||||
.help = "打印当前传感器缓存数据。用法: sensor",
|
||||
.func = cmd_sensor,
|
||||
};
|
||||
ESP_RETURN_ON_ERROR(esp_console_cmd_register(&sensor_cmd), "console_user_cmds", "register sensor failed");
|
||||
|
||||
const esp_console_cmd_t fan_cmd = {
|
||||
.command = "fan",
|
||||
.help = "控制风扇。用法: fan <on|off>",
|
||||
.hint = "<on|off>",
|
||||
.func = cmd_fan,
|
||||
};
|
||||
ESP_RETURN_ON_ERROR(esp_console_cmd_register(&fan_cmd), "console_user_cmds", "register fan failed");
|
||||
|
||||
const esp_console_cmd_t light_cmd = {
|
||||
.command = "light",
|
||||
.help = "控制补光灯。用法: light <on|off>",
|
||||
.hint = "<on|off>",
|
||||
.func = cmd_light,
|
||||
};
|
||||
ESP_RETURN_ON_ERROR(esp_console_cmd_register(&light_cmd), "console_user_cmds", "register light failed");
|
||||
|
||||
const esp_console_cmd_t hot_cmd = {
|
||||
.command = "hot",
|
||||
.help = "控制加热。用法: hot <on|off>",
|
||||
.hint = "<on|off>",
|
||||
.func = cmd_hot,
|
||||
};
|
||||
ESP_RETURN_ON_ERROR(esp_console_cmd_register(&hot_cmd), "console_user_cmds", "register hot failed");
|
||||
|
||||
const esp_console_cmd_t cool_cmd = {
|
||||
.command = "cool",
|
||||
.help = "控制制冷。用法: cool <on|off>",
|
||||
.hint = "<on|off>",
|
||||
.func = cmd_cool,
|
||||
};
|
||||
ESP_RETURN_ON_ERROR(esp_console_cmd_register(&cool_cmd), "console_user_cmds", "register cool failed");
|
||||
|
||||
const esp_console_cmd_t wifi_cmd = {
|
||||
.command = "wifi",
|
||||
.help = "Wi-Fi 状态与控制。用法: wifi <status|start|stop|clear>",
|
||||
.hint = "<status|start|stop|clear>",
|
||||
.func = cmd_wifi,
|
||||
};
|
||||
ESP_RETURN_ON_ERROR(esp_console_cmd_register(&wifi_cmd), "console_user_cmds", "register wifi failed");
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
5
components/console_user_cmds/include/console_user_cmds.h
Normal file
5
components/console_user_cmds/include/console_user_cmds.h
Normal file
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "esp_err.h"
|
||||
|
||||
esp_err_t console_user_cmds_register(void);
|
||||
Reference in New Issue
Block a user