|
| 1 | + |
| 2 | +#include "wifi_board.h" |
| 3 | +#include "audio_codecs/vb6824_audio_codec.h" |
| 4 | +#include "display/lcd_display.h" |
| 5 | +#include "application.h" |
| 6 | +#include "button.h" |
| 7 | +#include "config.h" |
| 8 | +#include "iot/thing_manager.h" |
| 9 | + |
| 10 | +#include <wifi_station.h> |
| 11 | +#include <esp_log.h> |
| 12 | +#include <esp_lcd_panel_vendor.h> |
| 13 | +#include <driver/spi_common.h> |
| 14 | + |
| 15 | +#include <wifi_station.h> |
| 16 | +#include <wifi_configuration_ap.h> |
| 17 | +#include <ssid_manager.h> |
| 18 | + |
| 19 | +#include "font_awesome_symbols.h" |
| 20 | +#include "settings.h" |
| 21 | +#include "assets/lang_config.h" |
| 22 | + |
| 23 | +#include "power_save_timer.h" |
| 24 | + |
| 25 | +#include "esp_timer.h" |
| 26 | + |
| 27 | +#define TAG "CustomBoard" |
| 28 | + |
| 29 | +LV_FONT_DECLARE(font_puhui_16_4); |
| 30 | +LV_FONT_DECLARE(font_awesome_16_4); |
| 31 | + |
| 32 | +class CustomBoard : public WifiBoard { |
| 33 | +private: |
| 34 | + Button boot_button_; |
| 35 | + Button volume_up_button_; |
| 36 | + Button volume_down_button_; |
| 37 | + VbAduioCodec audio_codec; |
| 38 | + LcdDisplay* display; |
| 39 | + |
| 40 | + void InitializeButtons() { |
| 41 | + boot_button_.OnClick([this]() { |
| 42 | + auto &app = Application::GetInstance(); |
| 43 | + app.ToggleChatState(); |
| 44 | + }); |
| 45 | + boot_button_.OnPressRepeaDone([this](uint16_t count) { |
| 46 | + if(count >= 3){ |
| 47 | + ResetWifiConfiguration(); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + volume_up_button_.OnClick([this]() { |
| 52 | + auto codec = GetAudioCodec(); |
| 53 | + auto volume = codec->output_volume() + 10; |
| 54 | + if (volume > 100) { |
| 55 | + volume = 100; |
| 56 | + } |
| 57 | + codec->SetOutputVolume(volume); |
| 58 | + GetDisplay()->ShowNotification(Lang::Strings::VOLUME + std::to_string(volume)); |
| 59 | + }); |
| 60 | + |
| 61 | + volume_up_button_.OnLongPress([this]() { |
| 62 | + GetAudioCodec()->SetOutputVolume(100); |
| 63 | + GetDisplay()->ShowNotification(Lang::Strings::MAX_VOLUME); |
| 64 | + }); |
| 65 | + |
| 66 | + volume_down_button_.OnClick([this]() { |
| 67 | + auto codec = GetAudioCodec(); |
| 68 | + auto volume = codec->output_volume() - 10; |
| 69 | + if (volume < 0) { |
| 70 | + volume = 0; |
| 71 | + } |
| 72 | + codec->SetOutputVolume(volume); |
| 73 | + GetDisplay()->ShowNotification(Lang::Strings::VOLUME + std::to_string(volume)); |
| 74 | + }); |
| 75 | + |
| 76 | + volume_down_button_.OnLongPress([this]() { |
| 77 | + GetAudioCodec()->SetOutputVolume(0); |
| 78 | + GetDisplay()->ShowNotification(Lang::Strings::MUTED); |
| 79 | + }); |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + // 物联网初始化,添加对 AI 可见设备 |
| 84 | + void InitializeIot() { |
| 85 | + auto& thing_manager = iot::ThingManager::GetInstance(); |
| 86 | + thing_manager.AddThing(iot::CreateThing("Speaker")); |
| 87 | + thing_manager.AddThing(iot::CreateThing("Screen")); |
| 88 | + } |
| 89 | + |
| 90 | + void InitializeSpi() { |
| 91 | + spi_bus_config_t buscfg = {}; |
| 92 | + buscfg.mosi_io_num = DISPLAY_MOSI_PIN; |
| 93 | + buscfg.miso_io_num = GPIO_NUM_NC; |
| 94 | + buscfg.sclk_io_num = DISPLAY_CLK_PIN; |
| 95 | + buscfg.quadwp_io_num = GPIO_NUM_NC; |
| 96 | + buscfg.quadhd_io_num = GPIO_NUM_NC; |
| 97 | + buscfg.max_transfer_sz = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t); |
| 98 | + ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO)); |
| 99 | + } |
| 100 | + |
| 101 | + void InitializeLcdDisplay() { |
| 102 | + esp_lcd_panel_io_handle_t panel_io = nullptr; |
| 103 | + esp_lcd_panel_handle_t panel = nullptr; |
| 104 | + esp_lcd_panel_io_spi_config_t io_config = {}; |
| 105 | + |
| 106 | + io_config.cs_gpio_num = DISPLAY_CS_PIN; |
| 107 | + io_config.dc_gpio_num = DISPLAY_DC_PIN; |
| 108 | + io_config.spi_mode = 3; |
| 109 | + io_config.pclk_hz = 80 * 1000 * 1000; |
| 110 | + io_config.trans_queue_depth = 10; |
| 111 | + io_config.lcd_cmd_bits = 8; |
| 112 | + io_config.lcd_param_bits = 8; |
| 113 | + ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI2_HOST, &io_config, &panel_io)); |
| 114 | + |
| 115 | + esp_lcd_panel_dev_config_t panel_config = {}; |
| 116 | + panel_config.reset_gpio_num = DISPLAY_RST_PIN; |
| 117 | + panel_config.rgb_ele_order = DISPLAY_RGB_ORDER; |
| 118 | + panel_config.bits_per_pixel = 16; |
| 119 | + ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(panel_io, &panel_config, &panel)); |
| 120 | + |
| 121 | + esp_lcd_panel_reset(panel); |
| 122 | + vTaskDelay(100 / portTICK_PERIOD_MS); |
| 123 | + esp_lcd_panel_reset(panel); |
| 124 | + |
| 125 | + esp_lcd_panel_init(panel); |
| 126 | + esp_lcd_panel_invert_color(panel, DISPLAY_INVERT_COLOR); |
| 127 | + esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY); |
| 128 | + esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y); |
| 129 | + display = new SpiLcdDisplay(panel_io, panel, |
| 130 | + DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY, |
| 131 | + { |
| 132 | + .text_font = &font_puhui_16_4, |
| 133 | + .icon_font = &font_awesome_16_4, |
| 134 | + .emoji_font = font_emoji_64_init(), |
| 135 | + }); |
| 136 | + } |
| 137 | + |
| 138 | +public: |
| 139 | + CustomBoard() : |
| 140 | + boot_button_(BOOT_BUTTON_GPIO, false), |
| 141 | + volume_up_button_(VOLUME_UP_BUTTON_GPIO), |
| 142 | + volume_down_button_(VOLUME_DOWN_BUTTON_GPIO), |
| 143 | + audio_codec(CODEC_TX_GPIO, CODEC_RX_GPIO){ |
| 144 | + |
| 145 | + InitializeButtons(); |
| 146 | + InitializeSpi(); |
| 147 | + InitializeLcdDisplay(); |
| 148 | + GetBacklight()->RestoreBrightness(); |
| 149 | + InitializeIot(); |
| 150 | + audio_codec.OnWakeUp([this](const std::string& command) { |
| 151 | + if (command == std::string(vb6824_get_wakeup_word())){ |
| 152 | + if(Application::GetInstance().GetDeviceState() != kDeviceStateListening){ |
| 153 | + Application::GetInstance().WakeWordInvoke("你好小智"); |
| 154 | + } |
| 155 | + }else if (command == "开始配网"){ |
| 156 | + ResetWifiConfiguration(); |
| 157 | + } |
| 158 | + }); |
| 159 | + } |
| 160 | + |
| 161 | + virtual AudioCodec* GetAudioCodec() override { |
| 162 | + return &audio_codec; |
| 163 | + } |
| 164 | + |
| 165 | + virtual Display* GetDisplay() override { |
| 166 | + return display; |
| 167 | + } |
| 168 | + |
| 169 | + virtual Backlight* GetBacklight() override { |
| 170 | + if (DISPLAY_BACKLIGHT_PIN != GPIO_NUM_NC) { |
| 171 | + static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT); |
| 172 | + return &backlight; |
| 173 | + } |
| 174 | + return nullptr; |
| 175 | + } |
| 176 | +}; |
| 177 | + |
| 178 | +DECLARE_BOARD(CustomBoard); |
0 commit comments