Files
capacitance-lcd-driver/main/main.c

115 lines
3.1 KiB
C

#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "board_config.h"
#include "capacitive_touch.h"
#include "lcd_reg.h"
#include "upper_app.h"
static const char *TAG = "touch";
static void lcd_fill_white(void)
{
enum { ROWS_PER_TRANSFER = 20 };
static uint16_t white_buffer[LCD_WIDTH * ROWS_PER_TRANSFER];
for (uint32_t i = 0; i < LCD_WIDTH * ROWS_PER_TRANSFER; i++) {
white_buffer[i] = 0xFFFF;
}
lcd_set_window(0, 0, LCD_WIDTH - 1, LCD_HEIGHT - 1);
for (uint16_t row = 0; row < LCD_HEIGHT; row += ROWS_PER_TRANSFER) {
uint16_t row_count = LCD_HEIGHT - row;
if (row_count > ROWS_PER_TRANSFER) {
row_count = ROWS_PER_TRANSFER;
}
lcd_write_pixels(white_buffer, (uint32_t)LCD_WIDTH * row_count * 2);
}
}
static void lcd_draw_touch_marker(uint16_t x, uint16_t y)
{
enum { MARKER_SIZE = 9 };
static uint16_t marker[MARKER_SIZE * MARKER_SIZE];
for (uint32_t i = 0; i < MARKER_SIZE * MARKER_SIZE; i++) {
marker[i] = 0xF800;
}
uint16_t half = MARKER_SIZE / 2;
uint16_t x0 = x > half ? x - half : 0;
uint16_t y0 = y > half ? y - half : 0;
uint16_t x1 = x0 + MARKER_SIZE - 1;
uint16_t y1 = y0 + MARKER_SIZE - 1;
if (x1 >= LCD_WIDTH) {
x1 = LCD_WIDTH - 1;
x0 = x1 - MARKER_SIZE + 1;
}
if (y1 >= LCD_HEIGHT) {
y1 = LCD_HEIGHT - 1;
y0 = y1 - MARKER_SIZE + 1;
}
lcd_set_window(x0, y0, x1, y1);
lcd_write_pixels(marker, sizeof(marker));
}
static void touch_task(void *arg)
{
bool was_pressed = false;
(void)arg;
ESP_LOGI(TAG, "touch init started");
capacitive_touch_init();
ESP_LOGI(TAG, "touch init done");
ESP_LOGI(TAG, "lcd restore started");
lcd_reg_restore();
lcd_fill_white();
ESP_LOGI(TAG, "lcd restore done");
ESP_LOGI(TAG, "capacitive touch terminal output started");
while (1) {
capacitive_touch_point_t point;
if (capacitive_touch_read(&point)) {
if (!was_pressed) {
ESP_LOGI(TAG, "TOUCH DOWN");
}
ESP_LOGI(TAG, "TOUCH POINT x=%u y=%u raw_x=%u raw_y=%u id=%u count=%u",
point.x, point.y, point.raw_x, point.raw_y, point.id, point.count);
lcd_draw_touch_marker(point.x, point.y);
was_pressed = true;
} else if (was_pressed && !capacitive_touch_is_pressed()) {
ESP_LOGI(TAG, "TOUCH UP");
was_pressed = false;
}
vTaskDelay(pdMS_TO_TICKS(50));
}
}
void app_main(void)
{
ESP_LOGI(TAG, "lcd init started");
lcd_reg_init();
ESP_LOGI(TAG, "lcd init done");
ESP_LOGI(TAG, "lcd pins: CLK=GPIO%d MOSI=GPIO%d RES=GPIO%d DC=GPIO%d CS=GPIO%d BLK=GPIO%d",
LCD_PIN_CLK, LCD_PIN_MOSI, LCD_PIN_RES, LCD_PIN_DC, LCD_PIN_CS, LCD_PIN_BLK);
ESP_LOGI(TAG, "white screen test started");
lcd_fill_white();
ESP_LOGI(TAG, "white screen test done");
xTaskCreate(touch_task, "touch_task", 4096, NULL, 5, NULL);
while (1) {
vTaskDelay(pdMS_TO_TICKS(1000));
}
}