适配新版本idf。
This commit is contained in:
@ -1,10 +1,11 @@
|
||||
idf_component_register(SRCS
|
||||
cmd_nvs.h cmd_nvs.c
|
||||
cmd_system.c
|
||||
cmd_system_common.c
|
||||
CustomCommand.cpp
|
||||
idf_component_register(
|
||||
SRCS
|
||||
cmd_nvs.h cmd_nvs.c
|
||||
cmd_system.c
|
||||
cmd_system_common.c
|
||||
cmd_system_sleep.c
|
||||
cmd_wifi.h cmd_wifi.c
|
||||
Command.h Command.cpp
|
||||
INCLUDE_DIRS .
|
||||
REQUIRES console spi_flash nvs_flash driver esp_wifi LedController Communication
|
||||
)
|
||||
|
||||
target_sources(${COMPONENT_LIB} PRIVATE cmd_system_sleep.c)
|
||||
)
|
67
components/command/Command.cpp
Normal file
67
components/command/Command.cpp
Normal file
@ -0,0 +1,67 @@
|
||||
#include "Command.h"
|
||||
#include "Application.h"
|
||||
#include "LedController.h"
|
||||
#include "MqttClient.h"
|
||||
#include "esp_console.h"
|
||||
#include <argtable3/argtable3.h>
|
||||
#include <esp_log.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(static_cast<LedController::Channel>(atoi(argv[1])), atoi(argv[2]));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int mqtt_command(int argc, char **argv) {
|
||||
MqttClient::instance()->initialize(argv[1], argv[2]);
|
||||
Application::instance()->setField("mqtt_username", std::string(argv[1]));
|
||||
Application::instance()->setField("mqtt_password", std::string(argv[2]));
|
||||
return 0;
|
||||
}
|
||||
|
||||
void register_custom() {
|
||||
const esp_console_cmd_t heap_cmd = {
|
||||
.command = "amass",
|
||||
.help = "test command.",
|
||||
.hint = NULL,
|
||||
.func = &custom_command,
|
||||
.argtable = nullptr,
|
||||
};
|
||||
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,
|
||||
.argtable = nullptr,
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&led_cmd));
|
||||
|
||||
const esp_console_cmd_t mqtt_cmd = {
|
||||
.command = "mqtt",
|
||||
.help = "mqtt client.",
|
||||
.hint = NULL,
|
||||
.func = &mqtt_command,
|
||||
.argtable = nullptr,
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&mqtt_cmd));
|
||||
}
|
||||
|
||||
Command::Command()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Command::~Command()
|
||||
{
|
||||
|
||||
}
|
18
components/command/Command.h
Normal file
18
components/command/Command.h
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef __COMMAND_H__
|
||||
#define __COMMAND_H__
|
||||
|
||||
#include <list>
|
||||
#include <string>
|
||||
|
||||
class Command {
|
||||
public:
|
||||
Command();
|
||||
~Command();
|
||||
|
||||
private:
|
||||
std::list<std::string> m_commands;
|
||||
};
|
||||
|
||||
void register_custom();
|
||||
|
||||
#endif // __COMMAND_H__
|
@ -1,104 +0,0 @@
|
||||
#include "CustomCommand.h"
|
||||
#include "Application.h"
|
||||
#include "LedController.h"
|
||||
#include "MqttClient.h"
|
||||
#include "esp_console.h"
|
||||
#include <argtable3/argtable3.h>
|
||||
#include <esp_log.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(static_cast<LedController::Channel>(atoi(argv[1])), atoi(argv[2]));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int mqtt_command(int argc, char **argv) {
|
||||
MqttClient::instance()->initialize(argv[1], argv[2]);
|
||||
Application::instance()->setField("mqtt_username", std::string(argv[1]));
|
||||
Application::instance()->setField("mqtt_password", std::string(argv[2]));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct {
|
||||
struct arg_int *timeout;
|
||||
struct arg_str *ssid;
|
||||
struct arg_str *password;
|
||||
struct arg_end *end;
|
||||
} join_args;
|
||||
|
||||
static int wifi_connect(int argc, char **argv) {
|
||||
int nerrors = arg_parse(argc, argv, (void **)&join_args);
|
||||
if (nerrors != 0) {
|
||||
arg_print_errors(stderr, join_args.end, argv[0]);
|
||||
return 1;
|
||||
}
|
||||
ESP_LOGI(__func__, "Connecting to '%s'", join_args.ssid->sval[0]);
|
||||
|
||||
/* set default value*/
|
||||
if (join_args.timeout->count == 0) {
|
||||
join_args.timeout->ival[0] = Application::WifiJoinTimeoutMillisecond;
|
||||
}
|
||||
|
||||
bool connected = Application::instance()->wifiConnect(join_args.ssid->sval[0], join_args.password->sval[0],
|
||||
join_args.timeout->ival[0]);
|
||||
if (!connected) {
|
||||
ESP_LOGW(__func__, "Connection timed out");
|
||||
return 1;
|
||||
}
|
||||
Application::instance()->setField("ssid", std::string(join_args.ssid->sval[0]));
|
||||
Application::instance()->setField("psk", std::string(join_args.password->sval[0]));
|
||||
ESP_LOGI(__func__, "Connected");
|
||||
return 0;
|
||||
}
|
||||
|
||||
void register_custom() {
|
||||
const esp_console_cmd_t heap_cmd = {
|
||||
.command = "amass",
|
||||
.help = "test command.",
|
||||
.hint = NULL,
|
||||
.func = &custom_command,
|
||||
.argtable = nullptr,
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&heap_cmd));
|
||||
|
||||
join_args.timeout = arg_int0(NULL, "timeout", "<t>", "Connection timeout, ms");
|
||||
join_args.ssid = arg_str1(NULL, NULL, "<ssid>", "SSID of AP");
|
||||
join_args.password = arg_str0(NULL, NULL, "<pass>", "PSK of AP");
|
||||
join_args.end = arg_end(2);
|
||||
|
||||
const esp_console_cmd_t join_cmd = {
|
||||
.command = "join",
|
||||
.help = "Join WiFi AP as a station",
|
||||
.hint = NULL,
|
||||
.func = &wifi_connect,
|
||||
.argtable = &join_args,
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&join_cmd));
|
||||
|
||||
const esp_console_cmd_t led_cmd = {
|
||||
.command = "led",
|
||||
.help = "led pwm duty.",
|
||||
.hint = NULL,
|
||||
.func = &led_command,
|
||||
.argtable = nullptr,
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&led_cmd));
|
||||
|
||||
const esp_console_cmd_t mqtt_cmd = {
|
||||
.command = "mqtt",
|
||||
.help = "mqtt client.",
|
||||
.hint = NULL,
|
||||
.func = &mqtt_command,
|
||||
.argtable = nullptr,
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&mqtt_cmd));
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
#ifndef __CUSTOMCOMMAND_H__
|
||||
#define __CUSTOMCOMMAND_H__
|
||||
|
||||
|
||||
|
||||
|
||||
void register_custom();
|
||||
|
||||
|
||||
|
||||
#endif // __CUSTOMCOMMAND_H__
|
@ -14,5 +14,12 @@
|
||||
void register_system(void)
|
||||
{
|
||||
register_system_common();
|
||||
register_system_sleep();
|
||||
|
||||
#if SOC_LIGHT_SLEEP_SUPPORTED
|
||||
register_system_light_sleep();
|
||||
#endif
|
||||
|
||||
#if SOC_DEEP_SLEEP_SUPPORTED
|
||||
register_system_deep_sleep();
|
||||
#endif
|
||||
}
|
||||
|
@ -19,7 +19,8 @@ void register_system(void);
|
||||
void register_system_common(void);
|
||||
|
||||
// Register deep and light sleep functions
|
||||
void register_system_sleep(void);
|
||||
void register_system_deep_sleep(void);
|
||||
void register_system_light_sleep(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -81,6 +81,12 @@ static int get_version(int argc, char **argv)
|
||||
case CHIP_ESP32C2:
|
||||
model = "ESP32-C2";
|
||||
break;
|
||||
case CHIP_ESP32P4:
|
||||
model = "ESP32-P4";
|
||||
break;
|
||||
case CHIP_ESP32C5:
|
||||
model = "ESP32-C5";
|
||||
break;
|
||||
default:
|
||||
model = "Unknown";
|
||||
break;
|
||||
@ -153,7 +159,7 @@ static void register_free(void)
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
||||
}
|
||||
|
||||
/* 'heap' command prints minumum heap size */
|
||||
/* 'heap' command prints minimum heap size */
|
||||
static int heap_size(int argc, char **argv)
|
||||
{
|
||||
uint32_t heap_size = heap_caps_get_minimum_free_size(MALLOC_CAP_DEFAULT);
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
||||
*/
|
||||
@ -29,18 +29,8 @@
|
||||
|
||||
static const char *TAG = "cmd_system_sleep";
|
||||
|
||||
static void register_deep_sleep(void);
|
||||
static void register_light_sleep(void);
|
||||
|
||||
void register_system_sleep(void)
|
||||
{
|
||||
register_deep_sleep();
|
||||
register_light_sleep();
|
||||
}
|
||||
|
||||
|
||||
#if SOC_DEEP_SLEEP_SUPPORTED
|
||||
/** 'deep_sleep' command puts the chip into deep sleep mode */
|
||||
|
||||
static struct {
|
||||
struct arg_int *wakeup_time;
|
||||
#if SOC_PM_SUPPORT_EXT0_WAKEUP || SOC_PM_SUPPORT_EXT1_WAKEUP
|
||||
@ -82,7 +72,7 @@ static int deep_sleep(int argc, char **argv)
|
||||
ESP_LOGI(TAG, "Enabling wakeup on GPIO%d, wakeup on %s level",
|
||||
io_num, level ? "HIGH" : "LOW");
|
||||
|
||||
ESP_ERROR_CHECK( esp_sleep_enable_ext1_wakeup(1ULL << io_num, level) );
|
||||
ESP_ERROR_CHECK( esp_sleep_enable_ext1_wakeup_io(1ULL << io_num, level) );
|
||||
ESP_LOGE(TAG, "GPIO wakeup from deep sleep currently unsupported on ESP32-C3");
|
||||
}
|
||||
#endif // SOC_PM_SUPPORT_EXT1_WAKEUP
|
||||
@ -90,12 +80,11 @@ static int deep_sleep(int argc, char **argv)
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
rtc_gpio_isolate(GPIO_NUM_12);
|
||||
#endif //CONFIG_IDF_TARGET_ESP32
|
||||
|
||||
esp_deep_sleep_start();
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void register_deep_sleep(void)
|
||||
void register_system_deep_sleep(void)
|
||||
{
|
||||
int num_args = 1;
|
||||
deep_sleep_args.wakeup_time =
|
||||
@ -125,9 +114,10 @@ static void register_deep_sleep(void)
|
||||
};
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
||||
}
|
||||
#endif // SOC_DEEP_SLEEP_SUPPORTED
|
||||
|
||||
#if SOC_LIGHT_SLEEP_SUPPORTED
|
||||
/** 'light_sleep' command puts the chip into light sleep mode */
|
||||
|
||||
static struct {
|
||||
struct arg_int *wakeup_time;
|
||||
struct arg_int *wakeup_gpio_num;
|
||||
@ -177,6 +167,7 @@ static int light_sleep(int argc, char **argv)
|
||||
fsync(fileno(stdout));
|
||||
esp_light_sleep_start();
|
||||
esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
|
||||
|
||||
const char *cause_str;
|
||||
switch (cause) {
|
||||
case ESP_SLEEP_WAKEUP_GPIO:
|
||||
@ -196,7 +187,7 @@ static int light_sleep(int argc, char **argv)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void register_light_sleep(void)
|
||||
void register_system_light_sleep(void)
|
||||
{
|
||||
light_sleep_args.wakeup_time =
|
||||
arg_int0("t", "time", "<t>", "Wake up time, ms");
|
||||
@ -220,3 +211,4 @@ static void register_light_sleep(void)
|
||||
};
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
||||
}
|
||||
#endif // SOC_LIGHT_SLEEP_SUPPORTED
|
||||
|
136
components/command/cmd_wifi.c
Normal file
136
components/command/cmd_wifi.c
Normal file
@ -0,0 +1,136 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
||||
*/
|
||||
/* Console example — WiFi commands
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "esp_log.h"
|
||||
#include "esp_console.h"
|
||||
#include "argtable3/argtable3.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_event.h"
|
||||
#include "cmd_wifi.h"
|
||||
|
||||
#define JOIN_TIMEOUT_MS (10000)
|
||||
|
||||
static EventGroupHandle_t wifi_event_group;
|
||||
const int CONNECTED_BIT = BIT0;
|
||||
|
||||
|
||||
static void event_handler(void* arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void* event_data)
|
||||
{
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
esp_wifi_connect();
|
||||
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
|
||||
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
||||
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
|
||||
}
|
||||
}
|
||||
|
||||
static void initialise_wifi(void)
|
||||
{
|
||||
esp_log_level_set("wifi", ESP_LOG_WARN);
|
||||
static bool initialized = false;
|
||||
if (initialized) {
|
||||
return;
|
||||
}
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
wifi_event_group = xEventGroupCreate();
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
esp_netif_t *ap_netif = esp_netif_create_default_wifi_ap();
|
||||
assert(ap_netif);
|
||||
esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
|
||||
assert(sta_netif);
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
|
||||
ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &event_handler, NULL) );
|
||||
ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL) );
|
||||
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
|
||||
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_NULL) );
|
||||
ESP_ERROR_CHECK( esp_wifi_start() );
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
static bool wifi_join(const char *ssid, const char *pass, int timeout_ms)
|
||||
{
|
||||
initialise_wifi();
|
||||
wifi_config_t wifi_config = { 0 };
|
||||
strlcpy((char *) wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid));
|
||||
if (pass) {
|
||||
strlcpy((char *) wifi_config.sta.password, pass, sizeof(wifi_config.sta.password));
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
|
||||
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
|
||||
esp_wifi_connect();
|
||||
|
||||
int bits = xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
|
||||
pdFALSE, pdTRUE, timeout_ms / portTICK_PERIOD_MS);
|
||||
return (bits & CONNECTED_BIT) != 0;
|
||||
}
|
||||
|
||||
/** Arguments used by 'join' function */
|
||||
static struct {
|
||||
struct arg_int *timeout;
|
||||
struct arg_str *ssid;
|
||||
struct arg_str *password;
|
||||
struct arg_end *end;
|
||||
} join_args;
|
||||
|
||||
static int connect(int argc, char **argv)
|
||||
{
|
||||
int nerrors = arg_parse(argc, argv, (void **) &join_args);
|
||||
if (nerrors != 0) {
|
||||
arg_print_errors(stderr, join_args.end, argv[0]);
|
||||
return 1;
|
||||
}
|
||||
ESP_LOGI(__func__, "Connecting to '%s'",
|
||||
join_args.ssid->sval[0]);
|
||||
|
||||
/* set default value*/
|
||||
if (join_args.timeout->count == 0) {
|
||||
join_args.timeout->ival[0] = JOIN_TIMEOUT_MS;
|
||||
}
|
||||
|
||||
bool connected = wifi_join(join_args.ssid->sval[0],
|
||||
join_args.password->sval[0],
|
||||
join_args.timeout->ival[0]);
|
||||
if (!connected) {
|
||||
ESP_LOGW(__func__, "Connection timed out");
|
||||
return 1;
|
||||
}
|
||||
ESP_LOGI(__func__, "Connected");
|
||||
return 0;
|
||||
}
|
||||
|
||||
void register_wifi(void)
|
||||
{
|
||||
join_args.timeout = arg_int0(NULL, "timeout", "<t>", "Connection timeout, ms");
|
||||
join_args.ssid = arg_str1(NULL, NULL, "<ssid>", "SSID of AP");
|
||||
join_args.password = arg_str0(NULL, NULL, "<pass>", "PSK of AP");
|
||||
join_args.end = arg_end(2);
|
||||
|
||||
const esp_console_cmd_t join_cmd = {
|
||||
.command = "join",
|
||||
.help = "Join WiFi AP as a station",
|
||||
.hint = NULL,
|
||||
.func = &connect,
|
||||
.argtable = &join_args
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&join_cmd) );
|
||||
}
|
25
components/command/cmd_wifi.h
Normal file
25
components/command/cmd_wifi.h
Normal file
@ -0,0 +1,25 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
||||
*/
|
||||
/* Console example — declarations of command registration functions.
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Register WiFi functions
|
||||
void register_wifi(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
Reference in New Issue
Block a user