|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Antmicro <www.antmicro.com> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/logging/log.h> |
| 8 | +#include <zephyr/kernel.h> |
| 9 | +#include <zephyr/device.h> |
| 10 | +#include <zephyr/input/input.h> |
| 11 | +#include <zephyr/drivers/display.h> |
| 12 | + |
| 13 | +LOG_MODULE_REGISTER(sample, LOG_LEVEL_INF); |
| 14 | + |
| 15 | +#if !DT_NODE_EXISTS(DT_CHOSEN(zephyr_touch)) |
| 16 | +#error "Unsupported board: zephyr,touch is not assigned" |
| 17 | +#endif |
| 18 | + |
| 19 | +#if !DT_NODE_EXISTS(DT_CHOSEN(zephyr_display)) |
| 20 | +#error "Unsupported board: zephyr,display is not assigned" |
| 21 | +#endif |
| 22 | + |
| 23 | +#define WIDTH (DT_PROP(DT_CHOSEN(zephyr_display), width)) |
| 24 | +#define HEIGHT (DT_PROP(DT_CHOSEN(zephyr_display), height)) |
| 25 | +#define CROSS_DIM (WIDTH / CONFIG_SCREEN_WIDTH_TO_CROSS_DIM) |
| 26 | + |
| 27 | +#define PIXEL_FORMAT (DT_PROP_OR(DT_CHOSEN(zephyr_display), pixel_format, PIXEL_FORMAT_ARGB_8888)) |
| 28 | +#define BPP ((DISPLAY_BITS_PER_PIXEL(PIXEL_FORMAT)) / 8) |
| 29 | + |
| 30 | +#define BUFFER_SIZE (CROSS_DIM * CROSS_DIM * BPP) |
| 31 | +#define REFRESH_RATE 100 |
| 32 | + |
| 33 | +static const struct device *const display_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_display)); |
| 34 | +static const struct device *const touch_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_touch)); |
| 35 | +static struct display_buffer_descriptor buf_desc = { |
| 36 | + .buf_size = BUFFER_SIZE, .pitch = CROSS_DIM, .width = CROSS_DIM, .height = CROSS_DIM}; |
| 37 | + |
| 38 | +static uint8_t buffer_cross[BUFFER_SIZE]; |
| 39 | +static const uint8_t buffer_cross_empty[BUFFER_SIZE]; |
| 40 | +static struct k_sem sync; |
| 41 | + |
| 42 | +static struct { |
| 43 | + size_t x; |
| 44 | + size_t y; |
| 45 | + bool pressed; |
| 46 | +} touch_point, touch_point_drawn; |
| 47 | + |
| 48 | +static void touch_event_callback(struct input_event *evt, void *user_data) |
| 49 | +{ |
| 50 | + if (evt->code == INPUT_ABS_X) { |
| 51 | + touch_point.x = evt->value; |
| 52 | + } |
| 53 | + if (evt->code == INPUT_ABS_Y) { |
| 54 | + touch_point.y = evt->value; |
| 55 | + } |
| 56 | + if (evt->code == INPUT_BTN_TOUCH) { |
| 57 | + touch_point.pressed = evt->value; |
| 58 | + } |
| 59 | + if (evt->sync) { |
| 60 | + k_sem_give(&sync); |
| 61 | + } |
| 62 | +} |
| 63 | +INPUT_CALLBACK_DEFINE(touch_dev, touch_event_callback, NULL); |
| 64 | + |
| 65 | +static void clear_screen(void) |
| 66 | +{ |
| 67 | + int x; |
| 68 | + int y; |
| 69 | + |
| 70 | + for (x = 0; x < WIDTH; x += CROSS_DIM) { |
| 71 | + for (y = 0; y < HEIGHT; y += CROSS_DIM) { |
| 72 | + display_write(display_dev, x, y, &buf_desc, buffer_cross_empty); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +static void fill_cross_buffer(void) |
| 78 | +{ |
| 79 | + int i; |
| 80 | + int x; |
| 81 | + int y; |
| 82 | + int index; |
| 83 | + |
| 84 | + for (i = 0; i < BPP; i++) { |
| 85 | + for (x = 0; x < CROSS_DIM; x++) { |
| 86 | + index = BPP * (CROSS_DIM / 2 * CROSS_DIM + x); |
| 87 | + buffer_cross[index + i] = -1; |
| 88 | + } |
| 89 | + for (y = 0; y < CROSS_DIM; y++) { |
| 90 | + index = BPP * (y * CROSS_DIM + CROSS_DIM / 2); |
| 91 | + buffer_cross[index + i] = -1; |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +static int get_draw_position(int value, int upper_bound) |
| 97 | +{ |
| 98 | + if (value < CROSS_DIM / 2) { |
| 99 | + return 0; |
| 100 | + } |
| 101 | + |
| 102 | + if (value + CROSS_DIM / 2 > upper_bound) { |
| 103 | + return upper_bound - CROSS_DIM; |
| 104 | + } |
| 105 | + |
| 106 | + return value - CROSS_DIM / 2; |
| 107 | +} |
| 108 | + |
| 109 | +int main(void) |
| 110 | +{ |
| 111 | + |
| 112 | + LOG_INF("Touch sample for touchscreen: %s, dc: %s", touch_dev->name, display_dev->name); |
| 113 | + |
| 114 | + if (!device_is_ready(touch_dev)) { |
| 115 | + LOG_ERR("Device %s not found. Aborting sample.", touch_dev->name); |
| 116 | + return 0; |
| 117 | + } |
| 118 | + |
| 119 | + if (!device_is_ready(display_dev)) { |
| 120 | + LOG_ERR("Device %s not found. Aborting sample.", display_dev->name); |
| 121 | + return 0; |
| 122 | + } |
| 123 | + |
| 124 | + if (BPP == 0 || BPP > 4) { |
| 125 | + LOG_ERR("Unsupported BPP=%d", BPP); |
| 126 | + return 0; |
| 127 | + } |
| 128 | + fill_cross_buffer(); |
| 129 | + display_blanking_off(display_dev); |
| 130 | + |
| 131 | + clear_screen(); |
| 132 | + touch_point_drawn.x = CROSS_DIM / 2; |
| 133 | + touch_point_drawn.y = CROSS_DIM / 2; |
| 134 | + touch_point.x = -1; |
| 135 | + touch_point.y = -1; |
| 136 | + |
| 137 | + k_sem_init(&sync, 0, 1); |
| 138 | + |
| 139 | + while (1) { |
| 140 | + k_msleep(REFRESH_RATE); |
| 141 | + k_sem_take(&sync, K_FOREVER); |
| 142 | + LOG_INF("TOUCH %s X, Y: (%d, %d)", touch_point.pressed ? "PRESS" : "RELEASE", |
| 143 | + touch_point.x, touch_point.y); |
| 144 | + |
| 145 | + display_write(display_dev, get_draw_position(touch_point_drawn.x, WIDTH), |
| 146 | + get_draw_position(touch_point_drawn.y, HEIGHT), &buf_desc, |
| 147 | + buffer_cross_empty); |
| 148 | + |
| 149 | + display_write(display_dev, get_draw_position(touch_point.x, WIDTH), |
| 150 | + get_draw_position(touch_point.y, HEIGHT), &buf_desc, buffer_cross); |
| 151 | + |
| 152 | + touch_point_drawn.x = touch_point.x; |
| 153 | + touch_point_drawn.y = touch_point.y; |
| 154 | + } |
| 155 | + return 0; |
| 156 | +} |
0 commit comments