diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 33a9148c..f68420db 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -73,6 +73,8 @@ elseif(CONFIG_BOARD_TYPE_ESP32S3_Touch_AMOLED_1_8) set(BOARD_TYPE "esp32-s3-touch-amoled-1.8") elseif(CONFIG_BOARD_TYPE_BREAD_COMPACT_WIFI_LCD) set(BOARD_TYPE "bread-compact-wifi-lcd") +elseif(CONFIG_BOARD_TYPE_TUDOUZI) + set(BOARD_TYPE "tudouzi") elseif(CONFIG_BOARD_TYPE_LILYGO_T_CIRCLE_S3) set(BOARD_TYPE "lilygo-t-circle-s3") elseif(CONFIG_BOARD_TYPE_MOVECALL_MOJI_ESP32S3) diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild index 6512dde0..7da76517 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -72,6 +72,8 @@ choice BOARD_TYPE bool "ESP-SparkBot开发板" config BOARD_TYPE_ESP32S3_Touch_AMOLED_1_8 bool "Waveshare ESP32-S3-Touch-AMOLED-1.8" + config BOARD_TYPE_TUDOUZI + bool "土豆子" config BOARD_TYPE_LILYGO_T_CIRCLE_S3 bool "LILYGO T-Circle-S3" config BOARD_TYPE_MOVECALL_MOJI_ESP32S3 diff --git a/main/boards/tudouzi/axp2101.cc b/main/boards/tudouzi/axp2101.cc new file mode 100644 index 00000000..b905f8b3 --- /dev/null +++ b/main/boards/tudouzi/axp2101.cc @@ -0,0 +1,59 @@ +#include "axp2101.h" +#include "board.h" +#include "display.h" + +#include + +#define TAG "Axp2101" + +Axp2101::Axp2101(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : I2cDevice(i2c_bus, addr) { + // ** EFUSE defaults ** + WriteReg(0x22, 0b110); // PWRON > OFFLEVEL as POWEROFF Source enable + WriteReg(0x27, 0x10); // hold 4s to power off + + WriteReg(0x93, 0x1C); // 配置 aldo2 输出为 3.3V + + uint8_t value = ReadReg(0x90); // XPOWERS_AXP2101_LDO_ONOFF_CTRL0 + value = value | 0x02; // set bit 1 (ALDO2) + WriteReg(0x90, value); // and power channels now enabled + + WriteReg(0x64, 0x03); // CV charger voltage setting to 4.2V + + WriteReg(0x61, 0x05); // set Main battery precharge current to 125mA + WriteReg(0x62, 0x0A); // set Main battery charger current to 400mA ( 0x08-200mA, 0x09-300mA, 0x0A-400mA ) + WriteReg(0x63, 0x15); // set Main battery term charge current to 125mA + + WriteReg(0x14, 0x00); // set minimum system voltage to 4.1V (default 4.7V), for poor USB cables + WriteReg(0x15, 0x00); // set input voltage limit to 3.88v, for poor USB cables + WriteReg(0x16, 0x05); // set input current limit to 2000mA + + WriteReg(0x24, 0x01); // set Vsys for PWROFF threshold to 3.2V (default - 2.6V and kill battery) + WriteReg(0x50, 0x14); // set TS pin to EXTERNAL input (not temperature) +} + +int Axp2101::GetBatteryCurrentDirection() { + return (ReadReg(0x01) & 0b01100000) >> 5; +} + +bool Axp2101::IsCharging() { + return GetBatteryCurrentDirection() == 1; +} + +bool Axp2101::IsDischarging() { + return GetBatteryCurrentDirection() == 2; +} + +bool Axp2101::IsChargingDone() { + uint8_t value = ReadReg(0x01); + return (value & 0b00000111) == 0b00000100; +} + +int Axp2101::GetBatteryLevel() { + return ReadReg(0xA4); +} + +void Axp2101::PowerOff() { + uint8_t value = ReadReg(0x10); + value = value | 0x01; + WriteReg(0x10, value); +} diff --git a/main/boards/tudouzi/axp2101.h b/main/boards/tudouzi/axp2101.h new file mode 100644 index 00000000..db9a497e --- /dev/null +++ b/main/boards/tudouzi/axp2101.h @@ -0,0 +1,19 @@ +#ifndef __AXP2101_H__ +#define __AXP2101_H__ + +#include "i2c_device.h" + +class Axp2101 : public I2cDevice { +public: + Axp2101(i2c_master_bus_handle_t i2c_bus, uint8_t addr); + bool IsCharging(); + bool IsDischarging(); + bool IsChargingDone(); + int GetBatteryLevel(); + void PowerOff(); + +private: + int GetBatteryCurrentDirection(); +}; + +#endif diff --git a/main/boards/tudouzi/config.h b/main/boards/tudouzi/config.h new file mode 100644 index 00000000..a2729001 --- /dev/null +++ b/main/boards/tudouzi/config.h @@ -0,0 +1,41 @@ +#ifndef _BOARD_CONFIG_H_ +#define _BOARD_CONFIG_H_ + +#include + +#define AUDIO_INPUT_SAMPLE_RATE 24000 +#define AUDIO_OUTPUT_SAMPLE_RATE 24000 + +#define AUDIO_INPUT_REFERENCE true + +#define AUDIO_I2S_GPIO_MCLK GPIO_NUM_40 +#define AUDIO_I2S_GPIO_WS GPIO_NUM_47 +#define AUDIO_I2S_GPIO_BCLK GPIO_NUM_38 +#define AUDIO_I2S_GPIO_DIN GPIO_NUM_39 +#define AUDIO_I2S_GPIO_DOUT GPIO_NUM_48 + +#define AUDIO_CODEC_PA_PIN GPIO_NUM_9 +#define AUDIO_CODEC_I2C_SDA_PIN GPIO_NUM_42 +#define AUDIO_CODEC_I2C_SCL_PIN GPIO_NUM_41 +#define AUDIO_CODEC_ES8311_ADDR ES8311_CODEC_DEFAULT_ADDR +#define AUDIO_CODEC_ES7210_ADDR ES7210_CODEC_DEFAULT_ADDR + +#define BUILTIN_LED_GPIO GPIO_NUM_3 +#define BOOT_BUTTON_GPIO GPIO_NUM_0 +#define VOLUME_UP_BUTTON_GPIO GPIO_NUM_1 +#define VOLUME_DOWN_BUTTON_GPIO GPIO_NUM_2 + +#define DISPLAY_SDA_PIN GPIO_NUM_7 +#define DISPLAY_SCL_PIN GPIO_NUM_8 +#define DISPLAY_WIDTH 128 +#define DISPLAY_HEIGHT 64 +#define DISPLAY_MIRROR_X false +#define DISPLAY_MIRROR_Y false + +#define ML307_RX_PIN GPIO_NUM_5 +#define ML307_TX_PIN GPIO_NUM_6 + +#define AXP2101_I2C_ADDR 0x34 + + +#endif // _BOARD_CONFIG_H_ diff --git a/main/boards/tudouzi/kevin_box_board.cc b/main/boards/tudouzi/kevin_box_board.cc new file mode 100644 index 00000000..168a2e48 --- /dev/null +++ b/main/boards/tudouzi/kevin_box_board.cc @@ -0,0 +1,214 @@ +#include "ml307_board.h" +#include "audio_codecs/box_audio_codec.h" +#include "display/ssd1306_display.h" +#include "application.h" +#include "button.h" +#include "config.h" +#include "axp2101.h" +#include "iot/thing_manager.h" +#include "led/single_led.h" + +#include +#include +#include +#include +#include + +#define TAG "Tudouzi" + +class Tudouzi : public Ml307Board { +private: + i2c_master_bus_handle_t display_i2c_bus_; + i2c_master_bus_handle_t codec_i2c_bus_; + Axp2101* axp2101_ = nullptr; + Button boot_button_; + Button volume_up_button_; + Button volume_down_button_; + uint8_t _data_buffer[2]; + esp_timer_handle_t power_save_timer_ = nullptr; + + void InitializePowerSaveTimer() { + esp_timer_create_args_t power_save_timer_args = { + .callback = [](void *arg) { + auto board = static_cast(arg); + board->PowerSaveCheck(); + }, + .arg = this, + .dispatch_method = ESP_TIMER_TASK, + .name = "Power Save Timer", + .skip_unhandled_events = false, + }; + ESP_ERROR_CHECK(esp_timer_create(&power_save_timer_args, &power_save_timer_)); + ESP_ERROR_CHECK(esp_timer_start_periodic(power_save_timer_, 1000000)); + } + + void PowerSaveCheck() { + // 电池放电模式下,如果待机超过一定时间,则自动关机 + const int seconds_to_shutdown = 600; + static int seconds = 0; + if (Application::GetInstance().GetDeviceState() != kDeviceStateIdle) { + seconds = 0; + return; + } + if (!axp2101_->IsDischarging()) { + seconds = 0; + return; + } + + seconds++; + if (seconds >= seconds_to_shutdown) { + axp2101_->PowerOff(); + } + } + + void MountStorage() { + // Mount the storage partition + esp_vfs_spiffs_conf_t conf = { + .base_path = "/storage", + .partition_label = "storage", + .max_files = 5, + .format_if_mount_failed = true, + }; + esp_vfs_spiffs_register(&conf); + } + + void Enable4GModule() { + // Make GPIO HIGH to enable the 4G module + gpio_config_t ml307_enable_config = { + .pin_bit_mask = (1ULL << 4), + .mode = GPIO_MODE_OUTPUT, + .pull_up_en = GPIO_PULLUP_DISABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .intr_type = GPIO_INTR_DISABLE, + }; + gpio_config(&ml307_enable_config); + gpio_set_level(GPIO_NUM_4, 1); + } + + void InitializeDisplayI2c() { + i2c_master_bus_config_t bus_config = { + .i2c_port = (i2c_port_t)0, + .sda_io_num = DISPLAY_SDA_PIN, + .scl_io_num = DISPLAY_SCL_PIN, + .clk_source = I2C_CLK_SRC_DEFAULT, + .glitch_ignore_cnt = 7, + .intr_priority = 0, + .trans_queue_depth = 0, + .flags = { + .enable_internal_pullup = 1, + }, + }; + ESP_ERROR_CHECK(i2c_new_master_bus(&bus_config, &display_i2c_bus_)); + } + + void InitializeCodecI2c() { + // Initialize I2C peripheral + i2c_master_bus_config_t i2c_bus_cfg = { + .i2c_port = (i2c_port_t)1, + .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN, + .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN, + .clk_source = I2C_CLK_SRC_DEFAULT, + .glitch_ignore_cnt = 7, + .intr_priority = 0, + .trans_queue_depth = 0, + .flags = { + .enable_internal_pullup = 1, + }, + }; + ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &codec_i2c_bus_)); + } + + void InitializeButtons() { + boot_button_.OnPressDown([this]() { + Application::GetInstance().StartListening(); + }); + boot_button_.OnPressUp([this]() { + Application::GetInstance().StopListening(); + }); + + volume_up_button_.OnClick([this]() { + auto codec = GetAudioCodec(); + auto volume = codec->output_volume() + 10; + if (volume > 100) { + volume = 100; + } + codec->SetOutputVolume(volume); + GetDisplay()->ShowNotification("音量 " + std::to_string(volume)); + }); + + volume_up_button_.OnLongPress([this]() { + GetAudioCodec()->SetOutputVolume(100); + GetDisplay()->ShowNotification("最大音量"); + }); + + volume_down_button_.OnClick([this]() { + auto codec = GetAudioCodec(); + auto volume = codec->output_volume() - 10; + if (volume < 0) { + volume = 0; + } + codec->SetOutputVolume(volume); + GetDisplay()->ShowNotification("音量 " + std::to_string(volume)); + }); + + volume_down_button_.OnLongPress([this]() { + GetAudioCodec()->SetOutputVolume(0); + GetDisplay()->ShowNotification("已静音"); + }); + } + + // 物联网初始化,添加对 AI 可见设备 + void InitializeIot() { + auto& thing_manager = iot::ThingManager::GetInstance(); + thing_manager.AddThing(iot::CreateThing("Speaker")); + } + +public: + Tudouzi() : Ml307Board(ML307_TX_PIN, ML307_RX_PIN, 4096), + boot_button_(BOOT_BUTTON_GPIO), + volume_up_button_(VOLUME_UP_BUTTON_GPIO), + volume_down_button_(VOLUME_DOWN_BUTTON_GPIO) { + InitializeDisplayI2c(); + InitializeCodecI2c(); + axp2101_ = new Axp2101(codec_i2c_bus_, AXP2101_I2C_ADDR); + + MountStorage(); + Enable4GModule(); + + InitializeButtons(); + // InitializePowerSaveTimer(); + InitializeIot(); + } + + virtual Led* GetLed() override { + static SingleLed led(BUILTIN_LED_GPIO); + return &led; + } + + virtual AudioCodec* GetAudioCodec() override { + static BoxAudioCodec audio_codec(codec_i2c_bus_, AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE, + AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN, + AUDIO_CODEC_PA_PIN, AUDIO_CODEC_ES8311_ADDR, AUDIO_CODEC_ES7210_ADDR, AUDIO_INPUT_REFERENCE); + return &audio_codec; + } + + virtual Display* GetDisplay() override { + static Ssd1306Display display(display_i2c_bus_, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y); + return &display; + } + + virtual bool GetBatteryLevel(int &level, bool& charging) override { + static int last_level = 0; + static bool last_charging = false; + level = axp2101_->GetBatteryLevel(); + charging = axp2101_->IsCharging(); + if (level != last_level || charging != last_charging) { + last_level = level; + last_charging = charging; + ESP_LOGI(TAG, "Battery level: %d, charging: %d", level, charging); + } + return true; + } +}; + +DECLARE_BOARD(Tudouzi); \ No newline at end of file