Files
bolar_2_lcd/main/main.cc
2026-11-27 16:24:26 +08:00

173 lines
4.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "esp_log.h"
#include "tca9555.h"
#include "screen.h"
#include "servo.h"
#include "sdcard.h"
#include "esp_heap_caps.h"
#include <cstdio>
#include <cstring>
static const char *MAIN_TAG = "MAIN";
// SD与LCD共用spi
esp_err_t spi_init(spi_host_device_t host)
{
spi_bus_config_t bus_cfg = {
.mosi_io_num = LCD_MOSI_GPIO, // 共用MOSI
.miso_io_num = SD_MISO_PIN, // SD卡需要MISO
.sclk_io_num = LCD_SCK_GPIO, // 共用SCK
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.data4_io_num = -1,
.data5_io_num = -1,
.data6_io_num = -1,
.data7_io_num = -1,
.max_transfer_sz = 4096,
.flags = 0,
};
return spi_bus_initialize(host, &bus_cfg, SPI_DMA_CH_AUTO);
}
void lcd_task(void *arg)
{
LcdScreen lcd;
esp_err_t ret;
auto &tca = TCA9555::getInstance();
// 屏幕上电+硬件复位
ret = lcd.power_up(tca);
if (ret != ESP_OK)
{
ESP_LOGE(MAIN_TAG, "LCD power up err");
while (1)
vTaskDelay(pdMS_TO_TICKS(1000));
}
// SPI初始化
ret = lcd.spi_init();
if (ret != ESP_OK)
{
ESP_LOGE(MAIN_TAG, "SPI init err");
while (1)
vTaskDelay(pdMS_TO_TICKS(1000));
}
vTaskDelay(pdMS_TO_TICKS(30));
// 屏幕 ST7735寄存器初始化
lcd.ic_init_sequence();
vTaskDelay(pdMS_TO_TICKS(50));
lcd.fill_color(COLOR_BLACK);
vTaskDelay(pdMS_TO_TICKS(20));
tca.p0_clr_bit(TCA_P0_LEDA); // 低电平开启背光
// 循环测试纯色
uint32_t loop = 0;
while (1)
{
// ESP_LOGI(MAIN_TAG, "Loop %lu RED", loop);
// lcd.fill_color(COLOR_RED);
// vTaskDelay(pdMS_TO_TICKS(3000));
// ESP_LOGI(MAIN_TAG, "Loop %lu GREEN", loop);
// lcd.fill_color(COLOR_GREEN);
// vTaskDelay(pdMS_TO_TICKS(1000));
// ESP_LOGI(MAIN_TAG, "Loop %lu BLUE", loop);
// lcd.fill_color(COLOR_BLUE);
// vTaskDelay(pdMS_TO_TICKS(1000));
// ESP_LOGI(MAIN_TAG, "Loop %lu WHITE", loop);
// lcd.fill_color(COLOR_WHITE);
// vTaskDelay(pdMS_TO_TICKS(1000));
// ESP_LOGI(MAIN_TAG, "Loop %lu YELLOW", loop);
// lcd.fill_color(COLOR_YELLOW);
// vTaskDelay(pdMS_TO_TICKS(1000));
ESP_LOGI(MAIN_TAG, "Loop %lu BLACK", loop);
lcd.fill_color(COLOR_BLACK);
lcd.draw_single_eye(64, 65, 42, 0, 14);
lcd.Flush();
vTaskDelay(pdMS_TO_TICKS(25));
loop++;
}
}
extern "C" void app_main(void)
{
esp_err_t ret;
// 第一步 先初始化TCA9555扩展IO
auto &tca = TCA9555::getInstance();
vTaskDelay(pdMS_TO_TICKS(50));
ret = tca.init();
if (ret != ESP_OK)
{
ESP_LOGE(MAIN_TAG, "TCA9555 init failed");
while (1)
vTaskDelay(pdMS_TO_TICKS(1000));
}
ret = tca.p1_set_bit(TCA_P1_MCU_VCC);
if (ret != ESP_OK)
{
ESP_LOGE(MAIN_TAG, "MCU VCC power enable fail");
}
// 等待整组外设电源稳定!最少50ms
// vTaskDelay(pdMS_TO_TICKS(50));
tca.p0_set_bit(TCA_P0_OV_RESET);
vTaskDelay(pdMS_TO_TICKS(100)); // 摄像头上电初始化
/******0.摄像头验证***** */
/******1.SD验证***** */
spi_init(SPI2_HOST);
// 开启SD卡供电 5V‑EN(P13)
tca.p1_set_bit(TCA_P1_5V_EN);
// 等待SD卡上电完成,最少150ms
vTaskDelay(pdMS_TO_TICKS(150));
SdSpiCard sd;
ret = sd.init();
if (ret == ESP_OK)
{
ESP_LOGI("SD", "SD SPI mount success, path:%s", SD_MOUNT_POINT);
// 测试写入
const char *test_str = "SPI SD Card Test Data 123456\r\n";
ret = sd.write_file("log.txt", (uint8_t *)test_str, strlen(test_str));
if (ret == ESP_OK)
ESP_LOGI("SD", "Write log.txt OK");
// 测试读取
uint8_t r_buf[256] = {0};
size_t r_len = 0;
ret = sd.read_file("log.txt", r_buf, sizeof(r_buf), r_len);
if (ret == ESP_OK)
{
ESP_LOGI("SD", "Read len:%u, content:\n%s", r_len, r_buf);
}
}else
{
ESP_LOGE("SD", "SD CARD init failed");
// return;
}
/******2.LCD验证***** */
BaseType_t res = xTaskCreate(lcd_task, "lcd_drive", 8192, &tca, 5, NULL);
if (res == pdPASS)
ESP_LOGI(MAIN_TAG, "LCD task create ok");
else
ESP_LOGE(MAIN_TAG, "Task create failed");
/********3.舵机********* */
xTaskCreate(servo_task, "servo_drive", 4096, NULL, 5, NULL);
while (1)
{
vTaskDelay(pdMS_TO_TICKS(1000));
}
}