STM32 – Bibliothekensammlung

gt911 .c/.h (meistens in Touchscreens verbaut)

------------------------------------------------------------------
gt911.c:
------------------------------------------------------------------
#include "gt911.h"
#include <string.h>

#define GT911_STATUS_REG       0x814E
#define GT911_POINT1_REG       0x8150

static HAL_StatusTypeDef GT911_ReadReg(GT911_HandleTypeDef *ts, uint16_t reg, uint8_t *buf, uint16_t len) {
    uint8_t reg_buf[2] = { reg & 0xFF, reg >> 8 };
    return HAL_I2C_Master_Transmit(ts->hi2c, GT911_I2C_ADDR << 1, reg_buf, 2, HAL_MAX_DELAY) == HAL_OK &&
           HAL_I2C_Master_Receive(ts->hi2c, GT911_I2C_ADDR << 1, buf, len, HAL_MAX_DELAY) == HAL_OK
           ? HAL_OK : HAL_ERROR;
}

static HAL_StatusTypeDef GT911_WriteReg(GT911_HandleTypeDef *ts, uint16_t reg, uint8_t *data, uint16_t len) {
    uint8_t buf[2 + len];
    buf[0] = reg & 0xFF;
    buf[1] = reg >> 8;
    memcpy(&buf[2], data, len);
    return HAL_I2C_Master_Transmit(ts->hi2c, GT911_I2C_ADDR << 1, buf, 2 + len, HAL_MAX_DELAY);
}

HAL_StatusTypeDef GT911_Init(GT911_HandleTypeDef *ts) {
    // Reset-Prozedur gemäß GT911-Datenblatt
    HAL_GPIO_WritePin(ts->reset_port, ts->reset_pin, GPIO_PIN_RESET);
    HAL_GPIO_WritePin(ts->int_port, ts->int_pin, GPIO_PIN_LOW);
    HAL_Delay(20);
    HAL_GPIO_WritePin(ts->reset_port, ts->reset_pin, GPIO_PIN_SET);
    HAL_Delay(50);
    return HAL_OK;
}

HAL_StatusTypeDef GT911_ReadTouch(GT911_HandleTypeDef *ts) {
    uint8_t status = 0;
    if (GT911_ReadReg(ts, GT911_STATUS_REG, &status, 1) != HAL_OK || (status & 0x80) == 0)
        return HAL_OK; // Kein neues Touch-Event

    ts->touch_count = status & 0x0F;
    if (ts->touch_count == 0 || ts->touch_count > GT911_MAX_TOUCHES) {
        ts->touch_count = 0;
        uint8_t clear = 0;
        GT911_WriteReg(ts, GT911_STATUS_REG, &clear, 1);
        return HAL_OK;
    }

    uint8_t buf[8 * GT911_MAX_TOUCHES] = {0};
    if (GT911_ReadReg(ts, GT911_POINT1_REG, buf, 8 * ts->touch_count) != HAL_OK)
        return HAL_ERROR;

    for (uint8_t i = 0; i < ts->touch_count; i++) {
        ts->points[i].id = buf[i * 8 + 0];
        ts->points[i].x  = buf[i * 8 + 1] | (buf[i * 8 + 2] << 8);
        ts->points[i].y  = buf[i * 8 + 3] | (buf[i * 8 + 4] << 8);
    }

    uint8_t clear = 0;
    GT911_WriteReg(ts, GT911_STATUS_REG, &clear, 1); // Touch-Status zurücksetzen
    return HAL_OK;
}
------------------------------------------------------------------
gt911.h:
------------------------------------------------------------------
#ifndef GT911_H
#define GT911_H

#include "stm32h7xx_hal.h"

#define GT911_I2C_ADDR     0x5D // 0x5D << 1 = 0xBA (7bit = 0x5D)
#define GT911_MAX_TOUCHES  5

typedef struct {
    uint16_t x;
    uint16_t y;
    uint8_t  id;
} GT911_TouchPoint;

typedef struct {
    I2C_HandleTypeDef *hi2c;
    uint16_t width;
    uint16_t height;
    GPIO_TypeDef *reset_port;
    uint16_t reset_pin;
    GPIO_TypeDef *int_port;
    uint16_t int_pin;
    uint8_t touch_count;
    GT911_TouchPoint points[GT911_MAX_TOUCHES];
} GT911_HandleTypeDef;

HAL_StatusTypeDef GT911_Init(GT911_HandleTypeDef *ts);
HAL_StatusTypeDef GT911_ReadTouch(GT911_HandleTypeDef *ts);

#endif // GT911_H

------------------------------------------------------------------
Anwendungsbeispiel:
------------------------------------------------------------------
#include "gt911.h"
#include <stdio.h>

extern I2C_HandleTypeDef hi2c4;

GT911_HandleTypeDef touch = {
    .hi2c = &hi2c4,
    .width = 1024,
    .height = 600,
    .reset_port = GPIOH, .reset_pin = GPIO_PIN_7,
    .int_port = GPIOE,   .int_pin = GPIO_PIN_3
};

void TouchTask(void *argument) {
    GT911_Init(&touch);

    while (1) {
        if (GT911_ReadTouch(&touch) == HAL_OK && touch.touch_count > 0) {
            for (uint8_t i = 0; i < touch.touch_count; i++) {
                printf("Touch[%d]: ID=%d X=%d Y=%d\r\n",
                       i,
                       touch.points[i].id,
                       touch.points[i].x,
                       touch.points[i].y);
            }
        }
        osDelay(20);
    }
}

Tags:

Comments

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Search


Categories


Recent Posts


Tags