esp32-c6-touch-amoled-1.43.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #include "wifi_board.h"
  2. #include "application.h"
  3. #include "button.h"
  4. #include "config.h"
  5. #include "codecs/box_audio_codec.h"
  6. #include <esp_log.h>
  7. #include <esp_lcd_panel_vendor.h>
  8. #include <driver/i2c_master.h>
  9. #include <driver/spi_common.h>
  10. #include <wifi_station.h>
  11. #include "esp_lcd_sh8601.h"
  12. #include "display/lcd_display.h"
  13. #include "esp_io_expander_tca9554.h"
  14. #include "mcp_server.h"
  15. #include "lvgl.h"
  16. #define TAG "waveshare_c6_amoled_1_43"
  17. LV_FONT_DECLARE(font_puhui_30_4);
  18. LV_FONT_DECLARE(font_awesome_30_4);
  19. static const sh8601_lcd_init_cmd_t lcd_init_cmds[] =
  20. {
  21. {0x11, (uint8_t []){0x00}, 0, 80},
  22. {0xC4, (uint8_t []){0x80}, 1, 0},
  23. {0x53, (uint8_t []){0x20}, 1, 1},
  24. {0x63, (uint8_t []){0xFF}, 1, 1},
  25. {0x51, (uint8_t []){0x00}, 1, 1},
  26. {0x29, (uint8_t []){0x00}, 0, 10},
  27. {0x51, (uint8_t []){0xFF}, 1, 0},
  28. };
  29. class CustomLcdDisplay : public SpiLcdDisplay {
  30. public:
  31. static void MyDrawEventCb(lv_event_t *e) {
  32. lv_area_t *area = (lv_area_t *)lv_event_get_param(e);
  33. uint16_t x1 = area->x1;
  34. uint16_t x2 = area->x2;
  35. uint16_t y1 = area->y1;
  36. uint16_t y2 = area->y2;
  37. // round the start of coordinate down to the nearest 2M number
  38. area->x1 = (x1 >> 1) << 1;
  39. area->y1 = (y1 >> 1) << 1;
  40. // round the end of coordinate up to the nearest 2N+1 number
  41. area->x2 = ((x2 >> 1) << 1) + 1;
  42. area->y2 = ((y2 >> 1) << 1) + 1;
  43. }
  44. CustomLcdDisplay(esp_lcd_panel_io_handle_t io_handle,
  45. esp_lcd_panel_handle_t panel_handle,
  46. int width,
  47. int height,
  48. int offset_x,
  49. int offset_y,
  50. bool mirror_x,
  51. bool mirror_y,
  52. bool swap_xy)
  53. : SpiLcdDisplay(io_handle, panel_handle,
  54. width, height, offset_x, offset_y, mirror_x, mirror_y, swap_xy,
  55. {
  56. .text_font = &font_puhui_30_4,
  57. .icon_font = &font_awesome_30_4,
  58. .emoji_font = font_emoji_64_init(),
  59. }) {
  60. DisplayLockGuard lock(this);
  61. lv_display_add_event_cb(display_, MyDrawEventCb, LV_EVENT_INVALIDATE_AREA, NULL);
  62. }
  63. };
  64. class CustomBoard : public WifiBoard {
  65. private:
  66. i2c_master_bus_handle_t i2c_bus_;
  67. Button boot_button_;
  68. Button pwr_button_;
  69. esp_lcd_panel_handle_t panel_handle = NULL;
  70. esp_lcd_panel_io_handle_t io_handle = NULL;
  71. esp_io_expander_handle_t io_expander = NULL;
  72. CustomLcdDisplay* display_;
  73. i2c_master_dev_handle_t disp_touch_dev_handle = NULL;
  74. lv_indev_t *touch_indev = NULL; //touch
  75. uint8_t pwr_flag = 0;
  76. void InitializeI2c() {
  77. // Initialize I2C peripheral
  78. i2c_master_bus_config_t i2c_bus_cfg = {
  79. .i2c_port = (i2c_port_t)0,
  80. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  81. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  82. .clk_source = I2C_CLK_SRC_DEFAULT,
  83. .glitch_ignore_cnt = 7,
  84. .intr_priority = 0,
  85. .trans_queue_depth = 0,
  86. .flags = {
  87. .enable_internal_pullup = 1,
  88. },
  89. };
  90. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_));
  91. }
  92. void InitializeTca9554(void) {
  93. esp_err_t ret = esp_io_expander_new_i2c_tca9554(i2c_bus_, I2C_ADDRESS, &io_expander);
  94. if(ret != ESP_OK)
  95. ESP_LOGE(TAG, "TCA9554 create returned error");
  96. ret = esp_io_expander_set_dir(io_expander, IO_EXPANDER_PIN_NUM_7 | IO_EXPANDER_PIN_NUM_6, IO_EXPANDER_OUTPUT);
  97. ESP_ERROR_CHECK(ret);
  98. ret = esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_7 | IO_EXPANDER_PIN_NUM_6, 1);
  99. ESP_ERROR_CHECK(ret);
  100. }
  101. void InitializeSpi() {
  102. spi_bus_config_t buscfg = {
  103. .data0_io_num = LCD_D0,
  104. .data1_io_num = LCD_D1,
  105. .sclk_io_num = LCD_PCLK,
  106. .data2_io_num = LCD_D2,
  107. .data3_io_num = LCD_D3,
  108. .max_transfer_sz = EXAMPLE_LCD_H_RES * EXAMPLE_LCD_V_RES * sizeof(uint16_t),
  109. };
  110. ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO));
  111. }
  112. void InitializeLcdDisplay() {
  113. const esp_lcd_panel_io_spi_config_t io_config = {
  114. .cs_gpio_num = LCD_CS,
  115. .dc_gpio_num = -1,
  116. .spi_mode = 0,
  117. .pclk_hz = 40 * 1000 * 1000,
  118. .trans_queue_depth = 4,
  119. .on_color_trans_done = NULL,
  120. .user_ctx = NULL,
  121. .lcd_cmd_bits = 32,
  122. .lcd_param_bits = 8,
  123. .flags = {
  124. .quad_mode = true,
  125. },
  126. };
  127. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI2_HOST, &io_config, &io_handle));
  128. sh8601_vendor_config_t vendor_config = {
  129. .init_cmds = lcd_init_cmds, // Uncomment these line if use custom initialization commands
  130. .init_cmds_size = sizeof(lcd_init_cmds) / sizeof(lcd_init_cmds[0]), // sizeof(axs15231b_lcd_init_cmd_t),
  131. .flags =
  132. {
  133. .use_qspi_interface = 1,
  134. },
  135. };
  136. const esp_lcd_panel_dev_config_t panel_config = {
  137. .reset_gpio_num = LCD_RST,
  138. .rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB, // Implemented by LCD command `36h`
  139. .bits_per_pixel = 16, // Implemented by LCD command `3Ah` (16/18)
  140. .vendor_config = &vendor_config,
  141. };
  142. ESP_ERROR_CHECK(esp_lcd_new_panel_sh8601(io_handle, &panel_config, &panel_handle));
  143. esp_lcd_panel_set_gap(panel_handle,0x06,0x00);
  144. ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
  145. ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));
  146. display_ = new CustomLcdDisplay(io_handle, panel_handle,
  147. EXAMPLE_LCD_H_RES, EXAMPLE_LCD_V_RES, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY);
  148. }
  149. void InitializeButtons() { //接入锂电池时,可长按PWR开机/关机
  150. boot_button_.OnClick([this]() {
  151. auto& app = Application::GetInstance();
  152. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  153. ResetWifiConfiguration();
  154. }
  155. });
  156. boot_button_.OnPressDown([this]() {
  157. Application::GetInstance().StartListening();
  158. });
  159. boot_button_.OnPressUp([this]() {
  160. Application::GetInstance().StopListening();
  161. });
  162. pwr_button_.OnLongPress([this]() {
  163. if(pwr_flag == 1)
  164. {
  165. pwr_flag = 0;
  166. esp_err_t ret;
  167. ret = esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_6, 0);
  168. ESP_ERROR_CHECK(ret);
  169. }
  170. });
  171. pwr_button_.OnPressUp([this]() {
  172. if(pwr_flag == 0)
  173. {
  174. pwr_flag = 1;
  175. }
  176. });
  177. }
  178. void InitializeTouch() {
  179. i2c_device_config_t dev_cfg =
  180. {
  181. .dev_addr_length = I2C_ADDR_BIT_LEN_7,
  182. .device_address = I2C_Touch_ADDRESS,
  183. .scl_speed_hz = 300000,
  184. };
  185. ESP_ERROR_CHECK(i2c_master_bus_add_device(i2c_bus_, &dev_cfg, &disp_touch_dev_handle));
  186. touch_indev = lv_indev_create();
  187. lv_indev_set_type(touch_indev, LV_INDEV_TYPE_POINTER);
  188. lv_indev_set_read_cb(touch_indev, TouchInputReadCallback);
  189. lv_indev_set_user_data(touch_indev, disp_touch_dev_handle);
  190. }
  191. static void TouchInputReadCallback(lv_indev_t * indev, lv_indev_data_t *indevData)
  192. {
  193. i2c_master_dev_handle_t i2c_dev = (i2c_master_dev_handle_t)lv_indev_get_user_data(indev);
  194. uint8_t cmd = 0x02;
  195. uint8_t buf[5] = {0};
  196. uint16_t tp_x,tp_y;
  197. i2c_master_transmit_receive(i2c_dev,&cmd,1,buf,5,1000);
  198. if(buf[0])
  199. {
  200. tp_x = (((uint16_t)buf[1] & 0x0f)<<8) | (uint16_t)buf[2];
  201. tp_y = (((uint16_t)buf[3] & 0x0f)<<8) | (uint16_t)buf[4];
  202. if(tp_x > EXAMPLE_LCD_H_RES)
  203. {tp_x = EXAMPLE_LCD_H_RES;}
  204. if(tp_y > EXAMPLE_LCD_V_RES)
  205. {tp_y = EXAMPLE_LCD_V_RES;}
  206. indevData->point.x = tp_x;
  207. indevData->point.y = tp_y;
  208. //ESP_LOGI("tp","(%ld,%ld)",indevData->point.x,indevData->point.y);
  209. indevData->state = LV_INDEV_STATE_PRESSED;
  210. }
  211. else
  212. {
  213. indevData->state = LV_INDEV_STATE_RELEASED;
  214. }
  215. }
  216. void InitializeTools()
  217. {
  218. auto& mcp_server = McpServer::GetInstance();
  219. mcp_server.AddTool("self.disp.setbacklight", "设置屏幕亮度", PropertyList({
  220. Property("level", kPropertyTypeInteger, 0, 255)
  221. }), [this](const PropertyList& properties) -> ReturnValue {
  222. int level = properties["level"].value<int>();
  223. ESP_LOGI("setbacklight","%d",level);
  224. SetDispbacklight(level);
  225. return true;
  226. });
  227. }
  228. void SetDispbacklight(uint8_t backlight) {
  229. uint32_t lcd_cmd = 0x51;
  230. lcd_cmd &= 0xff;
  231. lcd_cmd <<= 8;
  232. lcd_cmd |= 0x02 << 24;
  233. uint8_t param = backlight;
  234. esp_lcd_panel_io_tx_param(io_handle, lcd_cmd, &param,1);
  235. }
  236. public:
  237. CustomBoard() :
  238. boot_button_(BOOT_BUTTON_GPIO),pwr_button_(PWR_BUTTON_GPIO) {
  239. InitializeI2c();
  240. InitializeTca9554();
  241. InitializeSpi();
  242. InitializeLcdDisplay();
  243. InitializeButtons();
  244. InitializeTools();
  245. }
  246. virtual AudioCodec* GetAudioCodec() override {
  247. static BoxAudioCodec audio_codec(
  248. i2c_bus_,
  249. AUDIO_INPUT_SAMPLE_RATE,
  250. AUDIO_OUTPUT_SAMPLE_RATE,
  251. AUDIO_I2S_GPIO_MCLK,
  252. AUDIO_I2S_GPIO_BCLK,
  253. AUDIO_I2S_GPIO_WS,
  254. AUDIO_I2S_GPIO_DOUT,
  255. AUDIO_I2S_GPIO_DIN,
  256. AUDIO_CODEC_PA_PIN,
  257. AUDIO_CODEC_ES8311_ADDR,
  258. AUDIO_CODEC_ES7210_ADDR,
  259. AUDIO_INPUT_REFERENCE);
  260. return &audio_codec;
  261. }
  262. virtual Display* GetDisplay() override {
  263. return display_;
  264. }
  265. };
  266. DECLARE_BOARD(CustomBoard);