|
| 1 | +#include "wifi_board.h" |
| 2 | +#include "audio_codecs/es8311_audio_codec.h" |
| 3 | +#include "application.h" |
| 4 | +#include "button.h" |
| 5 | +#include "config.h" |
| 6 | +#include "iot/thing_manager.h" |
| 7 | +#include "sdkconfig.h" |
| 8 | + |
| 9 | +#include <wifi_station.h> |
| 10 | +#include <esp_log.h> |
| 11 | +#include <driver/i2c_master.h> |
| 12 | +#include <driver/spi_common.h> |
| 13 | +#include "esp_adc/adc_oneshot.h" |
| 14 | +#include "esp_adc/adc_cali.h" |
| 15 | +#include "esp_adc/adc_cali_scheme.h" |
| 16 | + |
| 17 | +#include <driver/gpio.h> |
| 18 | +#include "esp_timer.h" |
| 19 | +#include "led/circular_strip.h" |
| 20 | + |
| 21 | +#define TAG "esp_spot_s3" |
| 22 | + |
| 23 | +bool button_released_ = false; |
| 24 | +bool shutdown_ready_ = false; |
| 25 | +esp_timer_handle_t shutdown_timer; |
| 26 | + |
| 27 | +class EspSpotS3Bot : public WifiBoard { |
| 28 | +private: |
| 29 | + i2c_master_bus_handle_t i2c_bus_; |
| 30 | + Button boot_button_; |
| 31 | + Button key_button_; |
| 32 | + adc_oneshot_unit_handle_t adc1_handle; |
| 33 | + adc_cali_handle_t adc1_cali_handle; |
| 34 | + bool do_calibration = false; |
| 35 | + bool key_long_pressed = false; |
| 36 | + int64_t last_key_press_time = 0; |
| 37 | + static const int64_t LONG_PRESS_TIMEOUT_US = 5 * 1000000ULL; |
| 38 | + |
| 39 | + void InitializeI2c() { |
| 40 | + // Initialize I2C peripheral |
| 41 | + i2c_master_bus_config_t i2c_bus_cfg = { |
| 42 | + .i2c_port = I2C_NUM_0, |
| 43 | + .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN, |
| 44 | + .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN, |
| 45 | + .clk_source = I2C_CLK_SRC_DEFAULT, |
| 46 | + .glitch_ignore_cnt = 7, |
| 47 | + .intr_priority = 0, |
| 48 | + .trans_queue_depth = 0, |
| 49 | + .flags = { |
| 50 | + .enable_internal_pullup = 1, |
| 51 | + }, |
| 52 | + }; |
| 53 | + ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_)); |
| 54 | + } |
| 55 | + |
| 56 | + void InitializeADC() { |
| 57 | + adc_oneshot_unit_init_cfg_t init_config1 = { |
| 58 | + .unit_id = ADC_UNIT_1 |
| 59 | + }; |
| 60 | + ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, &adc1_handle)); |
| 61 | + |
| 62 | + adc_oneshot_chan_cfg_t chan_config = { |
| 63 | + .atten = ADC_ATTEN, |
| 64 | + .bitwidth = ADC_WIDTH, |
| 65 | + }; |
| 66 | + ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, VBAT_ADC_CHANNEL, &chan_config)); |
| 67 | + |
| 68 | + adc_cali_handle_t handle = NULL; |
| 69 | + esp_err_t ret = ESP_FAIL; |
| 70 | + |
| 71 | +#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED |
| 72 | + adc_cali_curve_fitting_config_t cali_config = { |
| 73 | + .unit_id = ADC_UNIT_1, |
| 74 | + .atten = ADC_ATTEN, |
| 75 | + .bitwidth = ADC_WIDTH, |
| 76 | + }; |
| 77 | + ret = adc_cali_create_scheme_curve_fitting(&cali_config, &handle); |
| 78 | + if (ret == ESP_OK) { |
| 79 | + do_calibration = true; |
| 80 | + adc1_cali_handle = handle; |
| 81 | + ESP_LOGI(TAG, "ADC Curve Fitting calibration succeeded"); |
| 82 | + } |
| 83 | +#endif // ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED |
| 84 | + } |
| 85 | + |
| 86 | + void InitializeButtons() { |
| 87 | + boot_button_.OnClick([this]() { |
| 88 | + auto& app = Application::GetInstance(); |
| 89 | + ResetWifiConfiguration(); |
| 90 | + }); |
| 91 | + |
| 92 | + key_button_.OnClick([this]() { |
| 93 | + auto& app = Application::GetInstance(); |
| 94 | + app.ToggleChatState(); |
| 95 | + key_long_pressed = false; |
| 96 | + }); |
| 97 | + |
| 98 | + key_button_.OnLongPress([this]() { |
| 99 | + int64_t now = esp_timer_get_time(); |
| 100 | + auto* led = static_cast<CircularStrip*>(this->GetLed()); |
| 101 | + |
| 102 | + if (key_long_pressed) { |
| 103 | + if ((now - last_key_press_time) < LONG_PRESS_TIMEOUT_US) { |
| 104 | + ESP_LOGW(TAG, "Key button long pressed the second time within 5s, shutting down..."); |
| 105 | + led->SetSingleColor(0, {0, 0, 0}); |
| 106 | + |
| 107 | + gpio_hold_dis(MCU_VCC_CTL); |
| 108 | + gpio_set_level(MCU_VCC_CTL, 0); |
| 109 | + |
| 110 | + } else { |
| 111 | + last_key_press_time = now; |
| 112 | + BlinkGreenFor5s(); |
| 113 | + } |
| 114 | + key_long_pressed = true; |
| 115 | + } else { |
| 116 | + ESP_LOGW(TAG, "Key button first long press! Waiting second within 5s to shutdown..."); |
| 117 | + last_key_press_time = now; |
| 118 | + key_long_pressed = true; |
| 119 | + |
| 120 | + BlinkGreenFor5s(); |
| 121 | + } |
| 122 | + }); |
| 123 | + } |
| 124 | + |
| 125 | + void InitializePowerCtl() { |
| 126 | + InitializeGPIO(); |
| 127 | + |
| 128 | + gpio_set_level(MCU_VCC_CTL, 1); |
| 129 | + gpio_hold_en(MCU_VCC_CTL); |
| 130 | + |
| 131 | + gpio_set_level(PERP_VCC_CTL, 1); |
| 132 | + gpio_hold_en(PERP_VCC_CTL); |
| 133 | + } |
| 134 | + |
| 135 | + void InitializeGPIO() { |
| 136 | + gpio_config_t io_pa = { |
| 137 | + .pin_bit_mask = (1ULL << AUDIO_CODEC_PA_PIN), |
| 138 | + .mode = GPIO_MODE_OUTPUT, |
| 139 | + .pull_up_en = GPIO_PULLUP_DISABLE, |
| 140 | + .pull_down_en = GPIO_PULLDOWN_DISABLE, |
| 141 | + .intr_type = GPIO_INTR_DISABLE |
| 142 | + }; |
| 143 | + gpio_config(&io_pa); |
| 144 | + gpio_set_level(AUDIO_CODEC_PA_PIN, 0); |
| 145 | + |
| 146 | + gpio_config_t io_conf_1 = { |
| 147 | + .pin_bit_mask = (1ULL << MCU_VCC_CTL), |
| 148 | + .mode = GPIO_MODE_OUTPUT, |
| 149 | + .pull_up_en = GPIO_PULLUP_DISABLE, |
| 150 | + .pull_down_en = GPIO_PULLDOWN_DISABLE, |
| 151 | + .intr_type = GPIO_INTR_DISABLE |
| 152 | + }; |
| 153 | + gpio_config(&io_conf_1); |
| 154 | + |
| 155 | + gpio_config_t io_conf_2 = { |
| 156 | + .pin_bit_mask = (1ULL << PERP_VCC_CTL), |
| 157 | + .mode = GPIO_MODE_OUTPUT, |
| 158 | + .pull_up_en = GPIO_PULLUP_DISABLE, |
| 159 | + .pull_down_en = GPIO_PULLDOWN_DISABLE, |
| 160 | + .intr_type = GPIO_INTR_DISABLE |
| 161 | + }; |
| 162 | + gpio_config(&io_conf_2); |
| 163 | + } |
| 164 | + |
| 165 | + void InitializeIot() { |
| 166 | + auto& thing_manager = iot::ThingManager::GetInstance(); |
| 167 | + thing_manager.AddThing(iot::CreateThing("Speaker")); |
| 168 | + thing_manager.AddThing(iot::CreateThing("Battery")); |
| 169 | + } |
| 170 | + |
| 171 | + |
| 172 | + void BlinkGreenFor5s() { |
| 173 | + auto* led = static_cast<CircularStrip*>(GetLed()); |
| 174 | + if (!led) { |
| 175 | + return; |
| 176 | + } |
| 177 | + |
| 178 | + led->Blink({50, 25, 0}, 100); |
| 179 | + |
| 180 | + esp_timer_create_args_t timer_args = { |
| 181 | + .callback = [](void* arg) { |
| 182 | + auto* self = static_cast<EspSpotS3Bot*>(arg); |
| 183 | + auto* led = static_cast<CircularStrip*>(self->GetLed()); |
| 184 | + if (led) { |
| 185 | + led->SetSingleColor(0, {0, 0, 0}); |
| 186 | + } |
| 187 | + }, |
| 188 | + .arg = this, |
| 189 | + .dispatch_method = ESP_TIMER_TASK, |
| 190 | + .name = "blinkGreenFor5s_timer" |
| 191 | + }; |
| 192 | + |
| 193 | + esp_timer_handle_t blink_timer = nullptr; |
| 194 | + ESP_ERROR_CHECK(esp_timer_create(&timer_args, &blink_timer)); |
| 195 | + ESP_ERROR_CHECK(esp_timer_start_once(blink_timer, LONG_PRESS_TIMEOUT_US)); |
| 196 | + } |
| 197 | + |
| 198 | +public: |
| 199 | + EspSpotS3Bot() : boot_button_(BOOT_BUTTON_GPIO), key_button_(KEY_BUTTON_GPIO, true) { |
| 200 | + InitializePowerCtl(); |
| 201 | + InitializeADC(); |
| 202 | + InitializeI2c(); |
| 203 | + InitializeButtons(); |
| 204 | + InitializeIot(); |
| 205 | + } |
| 206 | + |
| 207 | + virtual Led* GetLed() override { |
| 208 | + static CircularStrip led(LED_PIN, 1); |
| 209 | + return &led; |
| 210 | + } |
| 211 | + |
| 212 | + virtual AudioCodec* GetAudioCodec() override { |
| 213 | + static Es8311AudioCodec audio_codec(i2c_bus_, I2C_NUM_0, |
| 214 | + AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE, AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK, |
| 215 | + AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN, AUDIO_CODEC_PA_PIN, |
| 216 | + AUDIO_CODEC_ES8311_ADDR, false); |
| 217 | + return &audio_codec; |
| 218 | + } |
| 219 | + |
| 220 | + virtual bool GetBatteryLevel(int &level, bool &charging, bool &discharging) { |
| 221 | + if (!adc1_handle) { |
| 222 | + InitializeADC(); |
| 223 | + } |
| 224 | + |
| 225 | + int raw_value = 0; |
| 226 | + int voltage = 0; |
| 227 | + |
| 228 | + ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, VBAT_ADC_CHANNEL, &raw_value)); |
| 229 | + |
| 230 | + if (do_calibration) { |
| 231 | + ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc1_cali_handle, raw_value, &voltage)); |
| 232 | + voltage = voltage * 3 / 2; // compensate for voltage divider |
| 233 | + ESP_LOGI(TAG, "Calibrated voltage: %d mV", voltage); |
| 234 | + } else { |
| 235 | + ESP_LOGI(TAG, "Raw ADC value: %d", raw_value); |
| 236 | + voltage = raw_value; |
| 237 | + } |
| 238 | + |
| 239 | + voltage = voltage < EMPTY_BATTERY_VOLTAGE ? EMPTY_BATTERY_VOLTAGE : voltage; |
| 240 | + voltage = voltage > FULL_BATTERY_VOLTAGE ? FULL_BATTERY_VOLTAGE : voltage; |
| 241 | + |
| 242 | + // 计算电量百分比 |
| 243 | + level = (voltage - EMPTY_BATTERY_VOLTAGE) * 100 / (FULL_BATTERY_VOLTAGE - EMPTY_BATTERY_VOLTAGE); |
| 244 | + |
| 245 | + charging = gpio_get_level(MCU_VCC_CTL); |
| 246 | + ESP_LOGI(TAG, "Battery Level: %d%%, Charging: %s", level, charging ? "Yes" : "No"); |
| 247 | + return true; |
| 248 | + } |
| 249 | +}; |
| 250 | + |
| 251 | +DECLARE_BOARD(EspSpotS3Bot); |
0 commit comments