|
| 1 | +#include "wifi_board.h" |
| 2 | +#include "audio_codecs/no_audio_codec.h" |
| 3 | +#include "system_reset.h" |
| 4 | +#include "application.h" |
| 5 | +#include "button.h" |
| 6 | +#include "config.h" |
| 7 | +#include "iot/thing_manager.h" |
| 8 | +#include "led/gpio_led.h" |
| 9 | +#include <wifi_station.h> |
| 10 | +#include <esp_log.h> |
| 11 | +#include <driver/i2c_master.h> |
| 12 | +#include <driver/gpio.h> |
| 13 | + |
| 14 | +#define TAG "DoitS3AiBox" |
| 15 | + |
| 16 | +class DoitS3AiBox : public WifiBoard { |
| 17 | +private: |
| 18 | + Button boot_button_; |
| 19 | + Button touch_button_; |
| 20 | + Button volume_up_button_; |
| 21 | + Button volume_down_button_; |
| 22 | + uint8_t click_times; |
| 23 | + uint32_t check_time; |
| 24 | + |
| 25 | + void InitializeButtons() { |
| 26 | + click_times = 0; |
| 27 | + check_time = 0; |
| 28 | + boot_button_.OnClick([this]() { |
| 29 | + if(click_times==0) { |
| 30 | + check_time = esp_timer_get_time()/1000; |
| 31 | + } |
| 32 | + if(esp_timer_get_time()/1000-check_time<1000) { |
| 33 | + click_times++; |
| 34 | + check_time = esp_timer_get_time()/1000; |
| 35 | + } else { |
| 36 | + click_times = 0; |
| 37 | + check_time = 0; |
| 38 | + } |
| 39 | + auto& app = Application::GetInstance(); |
| 40 | + if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) { |
| 41 | + ResetWifiConfiguration(); |
| 42 | + } |
| 43 | + app.ToggleChatState(); |
| 44 | + }); |
| 45 | + boot_button_.OnDoubleClick([this]() { |
| 46 | + click_times++; |
| 47 | + ESP_LOGI(TAG, "DoubleClick times %d", click_times); |
| 48 | + if(click_times==3) { |
| 49 | + click_times = 0; |
| 50 | + ResetWifiConfiguration(); |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + boot_button_.OnLongPress([this]() { |
| 55 | + if(click_times>=3) { |
| 56 | + ResetWifiConfiguration(); |
| 57 | + } else { |
| 58 | + click_times = 0; |
| 59 | + check_time = 0; |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + touch_button_.OnPressDown([this]() { |
| 64 | + click_times = 0; |
| 65 | + Application::GetInstance().StartListening(); |
| 66 | + }); |
| 67 | + touch_button_.OnPressUp([this]() { |
| 68 | + click_times = 0; |
| 69 | + Application::GetInstance().StopListening(); |
| 70 | + }); |
| 71 | + |
| 72 | + volume_up_button_.OnClick([this]() { |
| 73 | + click_times = 0; |
| 74 | + auto codec = GetAudioCodec(); |
| 75 | + auto volume = codec->output_volume() + 10; |
| 76 | + if (volume > 100) { |
| 77 | + volume = 100; |
| 78 | + } |
| 79 | + codec->SetOutputVolume(volume); |
| 80 | + }); |
| 81 | + |
| 82 | + volume_up_button_.OnLongPress([this]() { |
| 83 | + click_times = 0; |
| 84 | + GetAudioCodec()->SetOutputVolume(100); |
| 85 | + }); |
| 86 | + |
| 87 | + volume_down_button_.OnClick([this]() { |
| 88 | + click_times = 0; |
| 89 | + auto codec = GetAudioCodec(); |
| 90 | + auto volume = codec->output_volume() - 10; |
| 91 | + if (volume < 0) { |
| 92 | + volume = 0; |
| 93 | + } |
| 94 | + codec->SetOutputVolume(volume); |
| 95 | + }); |
| 96 | + |
| 97 | + volume_down_button_.OnLongPress([this]() { |
| 98 | + click_times = 0; |
| 99 | + GetAudioCodec()->SetOutputVolume(0); |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + // 物联网初始化,添加对 AI 可见设备 |
| 104 | + void InitializeIot() { |
| 105 | + auto& thing_manager = iot::ThingManager::GetInstance(); |
| 106 | + thing_manager.AddThing(iot::CreateThing("Speaker")); |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + void InitializeGpio(gpio_num_t gpio_num_) { |
| 111 | + gpio_config_t config = { |
| 112 | + .pin_bit_mask = (1ULL << gpio_num_), |
| 113 | + .mode = GPIO_MODE_OUTPUT, |
| 114 | + .pull_up_en = GPIO_PULLUP_ENABLE, |
| 115 | + .pull_down_en = GPIO_PULLDOWN_DISABLE, |
| 116 | + .intr_type = GPIO_INTR_DISABLE, |
| 117 | + }; |
| 118 | + ESP_ERROR_CHECK(gpio_config(&config)); |
| 119 | + gpio_set_level(gpio_num_, 1); |
| 120 | + } |
| 121 | + |
| 122 | +public: |
| 123 | + DoitS3AiBox() : |
| 124 | + boot_button_(BOOT_BUTTON_GPIO), |
| 125 | + touch_button_(TOUCH_BUTTON_GPIO), |
| 126 | + volume_up_button_(VOLUME_UP_BUTTON_GPIO), |
| 127 | + volume_down_button_(VOLUME_DOWN_BUTTON_GPIO){ |
| 128 | + // 上拉io48 置高电平 |
| 129 | + InitializeGpio(GPIO_NUM_48); |
| 130 | + InitializeButtons(); |
| 131 | + InitializeIot(); |
| 132 | + } |
| 133 | + |
| 134 | + virtual Led* GetLed() override { |
| 135 | + static GpioLed led(BUILTIN_LED_GPIO, 1); |
| 136 | + return &led; |
| 137 | + } |
| 138 | + |
| 139 | + virtual AudioCodec* GetAudioCodec() override { |
| 140 | + static NoAudioCodecSimplexPdm audio_codec(AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE, |
| 141 | + AUDIO_I2S_SPK_GPIO_BCLK, AUDIO_I2S_SPK_GPIO_LRCK, AUDIO_I2S_SPK_GPIO_DOUT, AUDIO_I2S_MIC_GPIO_WS, AUDIO_I2S_MIC_GPIO_DIN); |
| 142 | + return &audio_codec; |
| 143 | + } |
| 144 | +}; |
| 145 | + |
| 146 | +DECLARE_BOARD(DoitS3AiBox); |
0 commit comments