1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
| #include "bsp/bsp_lcd.h" #include "driver/gpio.h" #include "esp_log.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h"
#ifdef LCD_DRIVER_ST7735
static const char *TAG = "BSP_LCD_ST7735";
#define ST7735_NOP 0x00 #define ST7735_SWRESET 0x01 #define ST7735_RDDID 0x04 #define ST7735_RDDST 0x09 #define ST7735_SLPIN 0x10 #define ST7735_SLPOUT 0x11 #define ST7735_PTLON 0x12 #define ST7735_NORON 0x13 #define ST7735_INVOFF 0x20 #define ST7735_INVON 0x21 #define ST7735_DISPOFF 0x28 #define ST7735_DISPON 0x29 #define ST7735_CASET 0x2A #define ST7735_RASET 0x2B #define ST7735_RAMWR 0x2C #define ST7735_RAMRD 0x2E #define ST7735_PTLAR 0x30 #define ST7735_VSCRDEF 0x33 #define ST7735_COLMOD 0x3A #define ST7735_MADCTL 0x36 #define ST7735_VSCRSADD 0x37 #define ST7735_FRMCTR1 0xB1 #define ST7735_FRMCTR2 0xB2 #define ST7735_FRMCTR3 0xB3 #define ST7735_INVCTR 0xB4 #define ST7735_DISSET5 0xB6 #define ST7735_PWCTR1 0xC0 #define ST7735_PWCTR2 0xC1 #define ST7735_PWCTR3 0xC2 #define ST7735_PWCTR4 0xC3 #define ST7735_PWCTR5 0xC4 #define ST7735_VMCTR1 0xC5 #define ST7735_VMCTR2 0xC7 #define ST7735_GMCTRP1 0xE0 #define ST7735_GMCTRN1 0xE1
static void lcd_send_cmd(lcd_config_t *config, uint8_t cmd) { hal_gpio_set_level(config->dc_pin, GPIO_LEVEL_LOW); hal_spi_send(&config->spi_config, &cmd, 1); }
static void lcd_send_data(lcd_config_t *config, uint8_t data) { hal_gpio_set_level(config->dc_pin, GPIO_LEVEL_HIGH); hal_spi_send(&config->spi_config, &data, 1); }
static void lcd_send_data16(lcd_config_t *config, uint16_t data) { uint8_t data_buf[2]; data_buf[0] = (data >> 8) & 0xFF; data_buf[1] = data & 0xFF; hal_gpio_set_level(config->dc_pin, GPIO_LEVEL_HIGH); hal_spi_send(&config->spi_config, data_buf, 2); }
bool bsp_lcd_init(lcd_config_t *config) { ESP_LOGI(TAG, "Initializing LCD");
hal_gpio_init(config->rst_pin, GPIO_MODE_OUTPUT); hal_gpio_init(config->dc_pin, GPIO_MODE_OUTPUT); hal_gpio_init(config->spi_config.cs_pin, GPIO_MODE_OUTPUT);
hal_gpio_set_level(config->rst_pin, GPIO_LEVEL_HIGH); vTaskDelay(pdMS_TO_TICKS(100)); hal_gpio_set_level(config->rst_pin, GPIO_LEVEL_LOW); vTaskDelay(pdMS_TO_TICKS(100)); hal_gpio_set_level(config->rst_pin, GPIO_LEVEL_HIGH); vTaskDelay(pdMS_TO_TICKS(100));
if (!hal_spi_init(&config->spi_config)) { ESP_LOGE(TAG, "SPI initialization failed"); return false; }
lcd_send_cmd(config, ST7735_SWRESET); vTaskDelay(pdMS_TO_TICKS(150));
lcd_send_cmd(config, ST7735_SLPOUT); vTaskDelay(pdMS_TO_TICKS(150));
lcd_send_cmd(config, ST7735_COLMOD); lcd_send_data(config, 0x05);
lcd_send_cmd(config, ST7735_FRMCTR1); lcd_send_data(config, 0x01); lcd_send_data(config, 0x2C); lcd_send_data(config, 0x2D);
lcd_send_cmd(config, ST7735_FRMCTR2); lcd_send_data(config, 0x01); lcd_send_data(config, 0x2C); lcd_send_data(config, 0x2D);
lcd_send_cmd(config, ST7735_FRMCTR3); lcd_send_data(config, 0x01); lcd_send_data(config, 0x2C); lcd_send_data(config, 0x2D); lcd_send_data(config, 0x01); lcd_send_data(config, 0x2C); lcd_send_data(config, 0x2D);
lcd_send_cmd(config, ST7735_INVCTR); lcd_send_data(config, 0x07);
lcd_send_cmd(config, ST7735_MADCTL); lcd_send_data(config, 0xC8);
lcd_send_cmd(config, ST7735_VSCRDEF); lcd_send_data(config, 0x00); lcd_send_data(config, 0x40); lcd_send_data(config, 0x00);
lcd_send_cmd(config, ST7735_PWCTR1); lcd_send_data(config, 0xA2); lcd_send_data(config, 0x02);
lcd_send_cmd(config, ST7735_PWCTR2); lcd_send_data(config, 0x84);
lcd_send_cmd(config, ST7735_PWCTR3); lcd_send_data(config, 0x0A); lcd_send_data(config, 0x00);
lcd_send_cmd(config, ST7735_PWCTR4); lcd_send_data(config, 0x8A);
lcd_send_cmd(config, ST7735_PWCTR5); lcd_send_data(config, 0x0A); lcd_send_data(config, 0x00);
lcd_send_cmd(config, ST7735_VMCTR1); lcd_send_data(config, 0x08);
lcd_send_cmd(config, ST7735_VMCTR2); lcd_send_data(config, 0x00);
lcd_send_cmd(config, ST7735_GMCTRP1); lcd_send_data(config, 0x0F); lcd_send_data(config, 0x1A); lcd_send_data(config, 0x0F); lcd_send_data(config, 0x18); lcd_send_data(config, 0x2F); lcd_send_data(config, 0x2C); lcd_send_data(config, 0x24); lcd_send_data(config, 0x2E); lcd_send_data(config, 0x25); lcd_send_data(config, 0x2A); lcd_send_data(config, 0x2B); lcd_send_data(config, 0x2C); lcd_send_data(config, 0x2B); lcd_send_data(config, 0x2F); lcd_send_data(config, 0x00);
lcd_send_cmd(config, ST7735_GMCTRN1); lcd_send_data(config, 0x0F); lcd_send_data(config, 0x1B); lcd_send_data(config, 0x0F); lcd_send_data(config, 0x17); lcd_send_data(config, 0x1F); lcd_send_data(config, 0x22); lcd_send_data(config, 0x1F); lcd_send_data(config, 0x20); lcd_send_data(config, 0x29); lcd_send_data(config, 0x2A); lcd_send_data(config, 0x29); lcd_send_data(config, 0x2B); lcd_send_data(config, 0x2E); lcd_send_data(config, 0x30); lcd_send_data(config, 0x00);
lcd_send_cmd(config, ST7735_NORON); vTaskDelay(pdMS_TO_TICKS(10));
lcd_send_cmd(config, ST7735_DISPON); vTaskDelay(pdMS_TO_TICKS(100));
bsp_lcd_fill_color(config, LCD_COLOR_BLACK);
ESP_LOGI(TAG, "LCD initialization complete"); return true; }
void bsp_lcd_set_orientation(lcd_config_t *config, int orientation) { uint8_t madctl = 0xC8; if (orientation == 0) { madctl = 0x08; } lcd_send_cmd(config, ST7735_MADCTL); lcd_send_data(config, madctl); }
void bsp_lcd_fill_color(lcd_config_t *config, lcd_color_t color) { lcd_send_cmd(config, ST7735_CASET); lcd_send_data16(config, 0); lcd_send_data16(config, config->width - 1);
lcd_send_cmd(config, ST7735_RASET); lcd_send_data16(config, 0); lcd_send_data16(config, config->height - 1);
lcd_send_cmd(config, ST7735_RAMWR); for (uint32_t i = 0; i < config->width * config->height; i++) { lcd_send_data16(config, color); } }
void bsp_lcd_draw_pixel(lcd_config_t *config, uint16_t x, uint16_t y, lcd_color_t color) { if (x >= config->width || y >= config->height) return;
lcd_send_cmd(config, ST7735_CASET); lcd_send_data16(config, x); lcd_send_data16(config, x);
lcd_send_cmd(config, ST7735_RASET); lcd_send_data16(config, y); lcd_send_data16(config, y);
lcd_send_cmd(config, ST7735_RAMWR); lcd_send_data16(config, color); }
void bsp_lcd_draw_hline(lcd_config_t *config, uint16_t x, uint16_t y, uint16_t length, lcd_color_t color) { if (y >= config->height) return; if (x + length > config->width) length = config->width - x; if (length <= 0) return;
lcd_send_cmd(config, ST7735_CASET); lcd_send_data16(config, x); lcd_send_data16(config, x + length - 1);
lcd_send_cmd(config, ST7735_RASET); lcd_send_data16(config, y); lcd_send_data16(config, y);
lcd_send_cmd(config, ST7735_RAMWR); for (uint16_t i = 0; i < length; i++) { lcd_send_data16(config, color); } }
void bsp_lcd_draw_vline(lcd_config_t *config, uint16_t x, uint16_t y, uint16_t length, lcd_color_t color) { if (x >= config->width) return; if (y + length > config->height) length = config->height - y; if (length <= 0) return;
lcd_send_cmd(config, ST7735_CASET); lcd_send_data16(config, x); lcd_send_data16(config, x);
lcd_send_cmd(config, ST7735_RASET); lcd_send_data16(config, y); lcd_send_data16(config, y + length - 1);
lcd_send_cmd(config, ST7735_RAMWR); for (uint16_t i = 0; i < length; i++) { lcd_send_data16(config, color); } }
void bsp_lcd_draw_rect(lcd_config_t *config, uint16_t x, uint16_t y, uint16_t width, uint16_t height, lcd_color_t color) { for (uint16_t i = 0; i < height; i++) { bsp_lcd_draw_hline(config, x, y + i, width, color); } }
void bsp_lcd_draw_circle(lcd_config_t *config, uint16_t x0, uint16_t y0, uint16_t radius, lcd_color_t color) { int16_t f = 1 - radius; int16_t ddF_x = 1; int16_t ddF_y = -2 * radius; int16_t x = 0; int16_t y = radius;
bsp_lcd_draw_pixel(config, x0, y0 + radius, color); bsp_lcd_draw_pixel(config, x0, y0 - radius, color); bsp_lcd_draw_pixel(config, x0 + radius, y0, color); bsp_lcd_draw_pixel(config, x0 - radius, y0, color);
while (x < y) { if (f >= 0) { y--; ddF_y += 2; f += ddF_y; } x++; ddF_x += 2; f += ddF_x;
bsp_lcd_draw_pixel(config, x0 + x, y0 + y, color); bsp_lcd_draw_pixel(config, x0 - x, y0 + y, color); bsp_lcd_draw_pixel(config, x0 + x, y0 - y, color); bsp_lcd_draw_pixel(config, x0 - x, y0 - y, color); bsp_lcd_draw_pixel(config, x0 + y, y0 + x, color); bsp_lcd_draw_pixel(config, x0 - y, y0 + x, color); bsp_lcd_draw_pixel(config, x0 + y, y0 - x, color); bsp_lcd_draw_pixel(config, x0 - y, y0 - x, color); } }
void bsp_lcd_draw_string(lcd_config_t *config, uint16_t x, uint16_t y, const char *str, lcd_color_t color, lcd_color_t bg_color) { uint16_t char_width = 8; uint16_t char_height = 16;
while (*str) { for (int i = 0; i < char_height; i++) { for (int j = 0; j < char_width; j++) { if ((*str >= 'A' && *str <= 'Z') || (*str >= '0' && *str <= '9')) { if ( (j % 2 == 0) && (i % 2 == 0) ){ bsp_lcd_draw_pixel(config, x + j, y + i, color); } else { bsp_lcd_draw_pixel(config, x + j, y + i, bg_color); } } else { bsp_lcd_draw_pixel(config, x + j, y + i, bg_color); } } } x += char_width; str++; } }
void bsp_lcd_set_backlight(lcd_config_t *config, uint8_t brightness) { }
void bsp_lcd_power_off(lcd_config_t *config) { lcd_send_cmd(config, ST7735_DISPOFF); lcd_send_cmd(config, ST7735_SLPIN); bsp_lcd_set_backlight(config, 0); }
void bsp_lcd_power_on(lcd_config_t *config) { lcd_send_cmd(config, ST7735_SLPOUT); lcd_send_cmd(config, ST7735_DISPON); bsp_lcd_set_backlight(config, 100); }
#endif
|