magiclick_2p5_board.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #include "dual_network_board.h"
  2. #include "display/lcd_display.h"
  3. #include "codecs/es8311_audio_codec.h"
  4. #include "application.h"
  5. #include "button.h"
  6. #include "led/circular_strip.h"
  7. #include "config.h"
  8. #include "assets/lang_config.h"
  9. #include "font_awesome_symbols.h"
  10. #include <esp_lcd_panel_vendor.h>
  11. #include <wifi_station.h>
  12. #include <esp_log.h>
  13. #include <driver/i2c_master.h>
  14. #include <driver/spi_common.h>
  15. #include <esp_lcd_panel_io.h>
  16. #include <esp_lcd_panel_ops.h>
  17. #include <esp_lcd_gc9a01.h>
  18. #include "power_manager.h"
  19. #include "power_save_timer.h"
  20. #include "esp_wifi.h"
  21. #define TAG "magiclick_2p5"
  22. LV_FONT_DECLARE(font_puhui_16_4);
  23. LV_FONT_DECLARE(font_awesome_16_4);
  24. class GC9107Display : public SpiLcdDisplay {
  25. public:
  26. GC9107Display(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
  27. int width, int height, int offset_x, int offset_y, bool mirror_x, bool mirror_y, bool swap_xy)
  28. : SpiLcdDisplay(panel_io, panel, width, height, offset_x, offset_y, mirror_x, mirror_y, swap_xy,
  29. {
  30. .text_font = &font_puhui_16_4,
  31. .icon_font = &font_awesome_16_4,
  32. .emoji_font = font_emoji_32_init(),
  33. }) {
  34. }
  35. };
  36. static const gc9a01_lcd_init_cmd_t gc9107_lcd_init_cmds[] = {
  37. // {cmd, { data }, data_size, delay_ms}
  38. {0xfe, (uint8_t[]){0x00}, 0, 0},
  39. {0xef, (uint8_t[]){0x00}, 0, 0},
  40. {0xb0, (uint8_t[]){0xc0}, 1, 0},
  41. {0xb1, (uint8_t[]){0x80}, 1, 0},
  42. {0xb2, (uint8_t[]){0x27}, 1, 0},
  43. {0xb3, (uint8_t[]){0x13}, 1, 0},
  44. {0xb6, (uint8_t[]){0x19}, 1, 0},
  45. {0xb7, (uint8_t[]){0x05}, 1, 0},
  46. {0xac, (uint8_t[]){0xc8}, 1, 0},
  47. {0xab, (uint8_t[]){0x0f}, 1, 0},
  48. {0x3a, (uint8_t[]){0x05}, 1, 0},
  49. {0xb4, (uint8_t[]){0x04}, 1, 0},
  50. {0xa8, (uint8_t[]){0x08}, 1, 0},
  51. {0xb8, (uint8_t[]){0x08}, 1, 0},
  52. {0xea, (uint8_t[]){0x02}, 1, 0},
  53. {0xe8, (uint8_t[]){0x2A}, 1, 0},
  54. {0xe9, (uint8_t[]){0x47}, 1, 0},
  55. {0xe7, (uint8_t[]){0x5f}, 1, 0},
  56. {0xc6, (uint8_t[]){0x21}, 1, 0},
  57. {0xc7, (uint8_t[]){0x15}, 1, 0},
  58. {0xf0,
  59. (uint8_t[]){0x1D, 0x38, 0x09, 0x4D, 0x92, 0x2F, 0x35, 0x52, 0x1E, 0x0C,
  60. 0x04, 0x12, 0x14, 0x1f},
  61. 14, 0},
  62. {0xf1,
  63. (uint8_t[]){0x16, 0x40, 0x1C, 0x54, 0xA9, 0x2D, 0x2E, 0x56, 0x10, 0x0D,
  64. 0x0C, 0x1A, 0x14, 0x1E},
  65. 14, 0},
  66. {0xf4, (uint8_t[]){0x00, 0x00, 0xFF}, 3, 0},
  67. {0xba, (uint8_t[]){0xFF, 0xFF}, 2, 0},
  68. };
  69. class magiclick_2p5 : public DualNetworkBoard {
  70. private:
  71. i2c_master_bus_handle_t codec_i2c_bus_;
  72. Button main_button_;
  73. Button left_button_;
  74. Button right_button_;
  75. GC9107Display* display_;
  76. PowerSaveTimer* power_save_timer_;
  77. PowerManager* power_manager_;
  78. esp_lcd_panel_io_handle_t panel_io = nullptr;
  79. esp_lcd_panel_handle_t panel = nullptr;
  80. void InitializePowerManager() {
  81. power_manager_ = new PowerManager(GPIO_NUM_48);
  82. power_manager_->OnChargingStatusChanged([this](bool is_charging) {
  83. if (is_charging) {
  84. power_save_timer_->SetEnabled(false);
  85. } else {
  86. power_save_timer_->SetEnabled(true);
  87. }
  88. });
  89. }
  90. void InitializePowerSaveTimer() {
  91. power_save_timer_ = new PowerSaveTimer(240, 60, -1);
  92. power_save_timer_->OnEnterSleepMode([this]() {
  93. ESP_LOGI(TAG, "Enabling sleep mode");
  94. display_->SetChatMessage("system", "");
  95. display_->SetEmotion("sleepy");
  96. GetBacklight()->SetBrightness(1);
  97. });
  98. power_save_timer_->OnExitSleepMode([this]() {
  99. display_->SetChatMessage("system", "");
  100. display_->SetEmotion("neutral");
  101. GetBacklight()->RestoreBrightness();
  102. });
  103. power_save_timer_->SetEnabled(true);
  104. }
  105. void Enable4GModule() {
  106. // enable the 4G module
  107. gpio_reset_pin(ML307_POWER_PIN);
  108. gpio_set_direction(ML307_POWER_PIN, GPIO_MODE_OUTPUT);
  109. gpio_set_level(ML307_POWER_PIN, ML307_POWER_OUTPUT_INVERT ? 0 : 1);
  110. }
  111. void Disable4GModule() {
  112. // enable the 4G module
  113. gpio_reset_pin(ML307_POWER_PIN);
  114. gpio_set_direction(ML307_POWER_PIN, GPIO_MODE_OUTPUT);
  115. gpio_set_level(ML307_POWER_PIN, ML307_POWER_OUTPUT_INVERT ? 1 : 0);
  116. }
  117. void InitializeCodecI2c() {
  118. // Initialize I2C peripheral
  119. i2c_master_bus_config_t i2c_bus_cfg = {
  120. .i2c_port = I2C_NUM_0,
  121. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  122. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  123. .clk_source = I2C_CLK_SRC_DEFAULT,
  124. .glitch_ignore_cnt = 7,
  125. .intr_priority = 0,
  126. .trans_queue_depth = 0,
  127. .flags = {
  128. .enable_internal_pullup = 1,
  129. },
  130. };
  131. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &codec_i2c_bus_));
  132. }
  133. void CheckNetType() {
  134. if (GetNetworkType() == NetworkType::WIFI) {
  135. Disable4GModule();
  136. } else if (GetNetworkType() == NetworkType::ML307) {
  137. Enable4GModule();
  138. }
  139. }
  140. void InitializeButtons() {
  141. main_button_.OnClick([this]() {
  142. auto& app = Application::GetInstance();
  143. if (GetNetworkType() == NetworkType::WIFI) {
  144. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  145. // cast to WifiBoard
  146. auto& wifi_board = static_cast<WifiBoard&>(GetCurrentBoard());
  147. wifi_board.ResetWifiConfiguration();
  148. Disable4GModule();
  149. }
  150. } else if(GetNetworkType() == NetworkType::ML307) {
  151. Enable4GModule();
  152. // stop WiFi
  153. esp_wifi_stop();
  154. }
  155. });
  156. main_button_.OnDoubleClick([this]() {
  157. auto& app = Application::GetInstance();
  158. if (app.GetDeviceState() == kDeviceStateStarting || app.GetDeviceState() == kDeviceStateWifiConfiguring) {
  159. SwitchNetworkType();
  160. }
  161. });
  162. main_button_.OnPressDown([this]() {
  163. power_save_timer_->WakeUp();
  164. Application::GetInstance().StartListening();
  165. });
  166. main_button_.OnPressUp([this]() {
  167. Application::GetInstance().StopListening();
  168. });
  169. left_button_.OnClick([this]() {
  170. power_save_timer_->WakeUp();
  171. auto codec = GetAudioCodec();
  172. auto volume = codec->output_volume() - 10;
  173. if (volume < 0) {
  174. volume = 0;
  175. }
  176. codec->SetOutputVolume(volume);
  177. GetDisplay()->ShowNotification(Lang::Strings::VOLUME + std::to_string(volume));
  178. });
  179. left_button_.OnLongPress([this]() {
  180. power_save_timer_->WakeUp();
  181. GetAudioCodec()->SetOutputVolume(0);
  182. GetDisplay()->ShowNotification(Lang::Strings::MUTED);
  183. });
  184. right_button_.OnClick([this]() {
  185. power_save_timer_->WakeUp();
  186. auto codec = GetAudioCodec();
  187. auto volume = codec->output_volume() + 10;
  188. if (volume > 100) {
  189. volume = 100;
  190. }
  191. codec->SetOutputVolume(volume);
  192. GetDisplay()->ShowNotification(Lang::Strings::VOLUME + std::to_string(volume));
  193. });
  194. right_button_.OnLongPress([this]() {
  195. power_save_timer_->WakeUp();
  196. GetAudioCodec()->SetOutputVolume(100);
  197. GetDisplay()->ShowNotification(Lang::Strings::MAX_VOLUME);
  198. });
  199. }
  200. void InitializeLedPower() {
  201. // 设置GPIO模式
  202. gpio_reset_pin(BUILTIN_LED_POWER);
  203. gpio_set_direction(BUILTIN_LED_POWER, GPIO_MODE_OUTPUT);
  204. gpio_set_level(BUILTIN_LED_POWER, BUILTIN_LED_POWER_OUTPUT_INVERT ? 0 : 1);
  205. }
  206. void InitializeSpi() {
  207. spi_bus_config_t buscfg = {};
  208. buscfg.mosi_io_num = DISPLAY_SDA_PIN;
  209. buscfg.miso_io_num = GPIO_NUM_NC;
  210. buscfg.sclk_io_num = DISPLAY_SCL_PIN;
  211. buscfg.quadwp_io_num = GPIO_NUM_NC;
  212. buscfg.quadhd_io_num = GPIO_NUM_NC;
  213. buscfg.max_transfer_sz = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t);
  214. ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
  215. }
  216. void InitializeGc9107Display(){
  217. // esp_lcd_panel_io_handle_t panel_io = nullptr;
  218. // esp_lcd_panel_handle_t panel = nullptr;
  219. // 液晶屏控制IO初始化
  220. ESP_LOGD(TAG, "Install panel IO");
  221. esp_lcd_panel_io_spi_config_t io_config = {};
  222. io_config.cs_gpio_num = DISPLAY_CS_PIN;
  223. io_config.dc_gpio_num = DISPLAY_DC_PIN;
  224. io_config.spi_mode = 0;
  225. io_config.pclk_hz = 40 * 1000 * 1000;
  226. io_config.trans_queue_depth = 10;
  227. io_config.lcd_cmd_bits = 8;
  228. io_config.lcd_param_bits = 8;
  229. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &panel_io));
  230. // 初始化液晶屏驱动芯片GC9107
  231. ESP_LOGD(TAG, "Install LCD driver");
  232. gc9a01_vendor_config_t gc9107_vendor_config = {
  233. .init_cmds = gc9107_lcd_init_cmds,
  234. .init_cmds_size = sizeof(gc9107_lcd_init_cmds) / sizeof(gc9a01_lcd_init_cmd_t),
  235. };
  236. esp_lcd_panel_dev_config_t panel_config = {};
  237. panel_config.reset_gpio_num = DISPLAY_RST_PIN;
  238. panel_config.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB;
  239. panel_config.bits_per_pixel = 16;
  240. panel_config.vendor_config = &gc9107_vendor_config;
  241. esp_lcd_new_panel_gc9a01(panel_io, &panel_config, &panel);
  242. esp_lcd_panel_reset(panel);
  243. esp_lcd_panel_init(panel);
  244. esp_lcd_panel_invert_color(panel, false);
  245. esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY);
  246. esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y);
  247. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel, true));
  248. display_ = new GC9107Display(panel_io, panel,
  249. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY);
  250. }
  251. public:
  252. magiclick_2p5() : DualNetworkBoard(ML307_TX_PIN, ML307_RX_PIN, GPIO_NUM_NC, 0),
  253. main_button_(MAIN_BUTTON_GPIO),
  254. left_button_(LEFT_BUTTON_GPIO),
  255. right_button_(RIGHT_BUTTON_GPIO) {
  256. InitializeLedPower();
  257. CheckNetType();
  258. InitializePowerManager();
  259. InitializePowerSaveTimer();
  260. InitializeCodecI2c();
  261. InitializeButtons();
  262. InitializeSpi();
  263. InitializeGc9107Display();
  264. GetBacklight()->RestoreBrightness();
  265. }
  266. virtual Led* GetLed() override {
  267. static CircularStrip led(BUILTIN_LED_GPIO, BUILTIN_LED_NUM);
  268. return &led;
  269. }
  270. virtual AudioCodec* GetAudioCodec() override {
  271. static Es8311AudioCodec audio_codec(codec_i2c_bus_, I2C_NUM_0, AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
  272. AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN,
  273. AUDIO_CODEC_PA_PIN, AUDIO_CODEC_ES8311_ADDR);
  274. return &audio_codec;
  275. }
  276. virtual Display* GetDisplay() override {
  277. return display_;
  278. }
  279. virtual Backlight* GetBacklight() override {
  280. static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
  281. return &backlight;
  282. }
  283. virtual bool GetBatteryLevel(int& level, bool& charging, bool& discharging) override {
  284. static bool last_discharging = false;
  285. charging = power_manager_->IsCharging();
  286. discharging = power_manager_->IsDischarging();
  287. if (discharging != last_discharging) {
  288. power_save_timer_->SetEnabled(discharging);
  289. last_discharging = discharging;
  290. }
  291. level = power_manager_->GetBatteryLevel();
  292. return true;
  293. }
  294. virtual void SetPowerSaveMode(bool enabled) override {
  295. if (!enabled) {
  296. power_save_timer_->WakeUp();
  297. }
  298. DualNetworkBoard::SetPowerSaveMode(enabled);
  299. }
  300. };
  301. DECLARE_BOARD(magiclick_2p5);