rcbench
Servo and ESC test bench: panel, IOmcu and shared C
Loading...
Searching...
No Matches
touch.c File Reference

The GT911 polling task: reads contacts, maps them into the panel's coordinates, crops them to the UI's area and publishes snapshots and events. More...

#include "touch.h"
#include <stdatomic.h>
#include <string.h>
#include "board.h"
#include "esp_check.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#include "freertos/task.h"
#include "gfx_turn.h"
#include "gt911.h"
#include "touch_inject.h"
#include "touch_merge.h"

Data Structures

struct  touch_state_t
struct  queued_t

Functions

static uint32_t now_ms (void)
static void publish (const touch_point_t *pts, int count)
 Run the tracker over pts and queue its events, numbered.
static void crop_and_publish (touch_point_t *pts, int count)
 Publish only the contacts inside the UI's area: the UI draws BOARD_UI_H_RES x BOARD_UI_V_RES of the panel.
static void touch_task (void *arg)
esp_err_t touch_init (const touch_config_t *cfg)
 Reset the controller, probe it and start the polling task.
esp_err_t touch_deinit (void)
 Stop the task and release the controller.
int touch_snapshot (touch_point_t *out, int max)
bool touch_pressed (touch_point_t *out)
bool touch_wait_event (touch_event_t *out, uint32_t *seq, uint32_t timeout_ms)
uint32_t touch_published (void)
void touch_flush_events (void)
uint16_t touch_i2c_address (void)
uint32_t touch_age_ms (void)

Variables

static const char * TAG = "touch"
static touch_state_t s_touch

Detailed Description

The GT911 polling task: reads contacts, maps them into the panel's coordinates, crops them to the UI's area and publishes snapshots and events.

Screen rotation 180° turns the glass's contacts with the picture: the task reads the one flag the display's flush sets once a turned frame is on the glass (gfx_turn_shown()), once per report.

SPDX-License-Identifier: MIT

Function Documentation

◆ crop_and_publish()

void crop_and_publish ( touch_point_t * pts,
int count )
static

Publish only the contacts inside the UI's area: the UI draws BOARD_UI_H_RES x BOARD_UI_V_RES of the panel.

Parameters
ptsContacts in panel pixels, each source already mapped with its own map; cropped in place.
countHow many.

◆ touch_age_ms()

uint32_t touch_age_ms ( void )

Milliseconds since the controller last answered a read, or UINT32_MAX when touch never came up.

"Answered" means the I2C (Inter-Integrated Circuit) transaction succeeded, not that a finger was down. An untouched panel is healthy and reports an age near zero, so this separates "nobody is touching it" from "the controller stopped answering", which the event stream cannot.

An application that can act on input, and only on input, treats a large age as a fault rather than as quiet.

◆ touch_deinit()

esp_err_t touch_deinit ( void )

Stop the task and release the controller.

Returns
ESP_OK, also when touch was not up.

◆ touch_flush_events()

void touch_flush_events ( void )

Drop any queued events (useful after a screen change).

◆ touch_i2c_address()

uint16_t touch_i2c_address ( void )

I2C address the controller answered on, or 0 before init.

◆ touch_init()

esp_err_t touch_init ( const touch_config_t * cfg)

Reset the controller, probe it and start the polling task.

Parameters
cfgThe set-up; NULL takes TOUCH_CONFIG_DEFAULT().
Returns
ESP_OK, also when already up; ESP_ERR_NO_MEM for the task, lock or queue; otherwise the board's or the GT911's error.

◆ touch_pressed()

bool touch_pressed ( touch_point_t * out)

True when at least one finger is down; fills out if given.

◆ touch_published()

uint32_t touch_published ( void )

The number of the last event the driver offered to its queue, stored after the offer. Read before draining the queue: an event up to it that the drain did not deliver was dropped, which a loss at the end of the stream, with no later event to show the gap, needs.

◆ touch_snapshot()

int touch_snapshot ( touch_point_t * out,
int max )

Copy the current contact set. Returns the number of active contacts, which may exceed max (only max of them are written).

◆ touch_wait_event()

bool touch_wait_event ( touch_event_t * out,
uint32_t * seq,
uint32_t timeout_ms )

Pop the next input event. timeout_ms of 0 polls, UINT32_MAX blocks. Returns false when nothing arrived in time.

seq, when not NULL, receives the event's number. The driver numbers every event it offers to its queue, from 1 and in order, whether or not the queue kept it, so a number skipped is an event the queue dropped because nobody collected it. touch_loss.h turns the numbers into losses.