forked from xiaozhi/xiaozhi-esp32
add reset to factory
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
set(SOURCES "AudioDevice.cc"
|
||||
"SystemInfo.cc"
|
||||
"SystemReset.cc"
|
||||
"WebSocketClient.cc"
|
||||
"OpusEncoder.cc"
|
||||
"BuiltinLed.cc"
|
||||
|
||||
67
main/SystemReset.cc
Normal file
67
main/SystemReset.cc
Normal file
@@ -0,0 +1,67 @@
|
||||
#include "SystemReset.h"
|
||||
#include "esp_log.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_partition.h"
|
||||
#include "esp_system.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
|
||||
|
||||
#define TAG "SystemReset"
|
||||
|
||||
|
||||
SystemReset::SystemReset() {
|
||||
// Configure GPIO1, GPIO2 as INPUT, reset NVS flash if the button is pressed
|
||||
gpio_config_t io_conf;
|
||||
io_conf.intr_type = GPIO_INTR_DISABLE;
|
||||
io_conf.mode = GPIO_MODE_INPUT;
|
||||
io_conf.pin_bit_mask = (1ULL << GPIO_NUM_1) | (1ULL << GPIO_NUM_2);
|
||||
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
|
||||
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
|
||||
gpio_config(&io_conf);
|
||||
}
|
||||
|
||||
|
||||
void SystemReset::CheckButtons() {
|
||||
if (gpio_get_level(GPIO_NUM_2) == 0) {
|
||||
ESP_LOGI(TAG, "Button is pressed, reset to factory");
|
||||
ResetNvsFlash();
|
||||
ResetToFactory();
|
||||
}
|
||||
|
||||
if (gpio_get_level(GPIO_NUM_1) == 0) {
|
||||
ESP_LOGI(TAG, "Button is pressed, reset NVS flash");
|
||||
ResetNvsFlash();
|
||||
}
|
||||
}
|
||||
|
||||
void SystemReset::ResetNvsFlash() {
|
||||
ESP_LOGI(TAG, "Resetting NVS flash");
|
||||
esp_err_t ret = nvs_flash_erase();
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to erase NVS flash");
|
||||
}
|
||||
}
|
||||
|
||||
void SystemReset::ResetToFactory() {
|
||||
ESP_LOGI(TAG, "Resetting to factory");
|
||||
// Erase otadata partition
|
||||
const esp_partition_t* partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_OTA, NULL);
|
||||
if (partition == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to find otadata partition");
|
||||
return;
|
||||
}
|
||||
esp_partition_erase_range(partition, 0, partition->size);
|
||||
ESP_LOGI(TAG, "Erased otadata partition");
|
||||
|
||||
// Reboot in 3 seconds
|
||||
RestartInSeconds(3);
|
||||
}
|
||||
|
||||
void SystemReset::RestartInSeconds(int seconds) {
|
||||
for (int i = seconds; i > 0; i--) {
|
||||
ESP_LOGI(TAG, "Resetting in %d seconds", i);
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
esp_restart();
|
||||
}
|
||||
17
main/SystemReset.h
Normal file
17
main/SystemReset.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef _SYSTEM_RESET_H
|
||||
#define _SYSTEM_RESET_H
|
||||
|
||||
class SystemReset {
|
||||
public:
|
||||
SystemReset();
|
||||
|
||||
void CheckButtons();
|
||||
|
||||
private:
|
||||
void ResetNvsFlash();
|
||||
void ResetToFactory();
|
||||
void RestartInSeconds(int seconds);
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
19
main/main.cc
19
main/main.cc
@@ -9,29 +9,20 @@
|
||||
#include "WifiConfigurationAp.h"
|
||||
#include "Application.h"
|
||||
#include "SystemInfo.h"
|
||||
#include "SystemReset.h"
|
||||
|
||||
#define TAG "main"
|
||||
#define STATS_TICKS pdMS_TO_TICKS(1000)
|
||||
|
||||
extern "C" void app_main(void)
|
||||
{
|
||||
// Check if the reset button is pressed
|
||||
SystemReset system_reset;
|
||||
system_reset.CheckButtons();
|
||||
|
||||
// Initialize the default event loop
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
|
||||
// Configure GPIO1 as INPUT, reset NVS flash if the button is pressed
|
||||
gpio_config_t io_conf;
|
||||
io_conf.intr_type = GPIO_INTR_DISABLE;
|
||||
io_conf.mode = GPIO_MODE_INPUT;
|
||||
io_conf.pin_bit_mask = 1ULL << 1;
|
||||
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
|
||||
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
|
||||
gpio_config(&io_conf);
|
||||
|
||||
if (gpio_get_level(GPIO_NUM_1) == 0) {
|
||||
ESP_LOGI(TAG, "Button is pressed, reset NVS flash");
|
||||
nvs_flash_erase();
|
||||
}
|
||||
|
||||
// Initialize NVS flash for WiFi configuration
|
||||
esp_err_t ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
|
||||
Reference in New Issue
Block a user