Skip to content

Preliminary Giga Touch support on Zephyr #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions src/Arduino_GigaDisplayTouch.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@

/* Includes ------------------------------------------------------------------*/
#include "Wire.h"
#include <Arduino.h>
#ifdef __MBED__
#include "mbed.h"
#include "pinDefinitions.h"
#include <Arduino.h>
#endif

/* Exported defines ----------------------------------------------------------*/
#define GT911_I2C_ADDR_BA_BB (0x5D | 0x80) // 0xBA/0xBB - 0x5D (7bit address)
Expand Down Expand Up @@ -69,16 +71,18 @@ struct GDTpoint_s {
*/
class Arduino_GigaDisplayTouch {
public:
/**
* @brief Construct a new touch controller for Giga Display Shield.
*
* @param wire A reference to the Wire interface to be used for communication
* with the touch controller.
* @param intPin The interrupt pin number for the touch controller.
* @param rstPin The reset pin number for the touch controller.
* @param addr The device address for the touch controller.
*/
#if defined(ARDUINO_GIGA)
/**
* @brief Construct a new touch controller for Giga Display Shield.
*
* @param wire A reference to the Wire interface to be used for communication
* with the touch controller.
* @param intPin The interrupt pin number for the touch controller.
* @param rstPin The reset pin number for the touch controller.
* @param addr The device address for the touch controller.
*/
#if defined(__ZEPHYR__)
Arduino_GigaDisplayTouch();
#elif defined(ARDUINO_GIGA)
Arduino_GigaDisplayTouch(TwoWire &wire = Wire1,
uint8_t intPin = PinNameToIndex(PI_1),
uint8_t rstPin = PinNameToIndex(PI_2),
Expand All @@ -92,6 +96,7 @@ class Arduino_GigaDisplayTouch {
Arduino_GigaDisplayTouch(TwoWire &wire, uint8_t intPin, uint8_t rstPin,
uint8_t addr);
#endif

~Arduino_GigaDisplayTouch();

/**
Expand Down Expand Up @@ -121,11 +126,12 @@ class Arduino_GigaDisplayTouch {
void onDetect(void (*handler)(uint8_t, GDTpoint_t *));

private:
#if defined(__MBED__)
TwoWire &_wire;
uint8_t _intPin;
mbed::InterruptIn _irqInt;
uint8_t _rstPin;
uint8_t _addr;
mbed::InterruptIn _irqInt;
GDTpoint_t _points[GT911_MAX_CONTACTS];
void (*_gt911TouchHandler)(uint8_t, GDTpoint_t *);

Expand All @@ -134,6 +140,8 @@ class Arduino_GigaDisplayTouch {
uint8_t _gt911ReadOp(uint16_t reg, uint8_t *data, uint8_t len);
void _gt911onIrq();
uint8_t _gt911ReadInputCoord(uint8_t *pointsbuf, uint8_t &contacts);

#endif
};

#endif /* __ARDUINO_GIGADISPLAYTOUCH_H */
#endif /* __ARDUINO_GIGADISPLAYTOUCH_H */
4 changes: 3 additions & 1 deletion src/Arduino_GigaDisplayTouchMbed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

/* Includes -----------------------------------------------------------------*/
#ifdef __MBED__
#include "Arduino_GigaDisplayTouch.h"

#if __has_include("lvgl.h")
Expand Down Expand Up @@ -281,4 +282,5 @@ uint8_t Arduino_GigaDisplayTouch::_gt911ReadInputCoord(uint8_t *pointsbuf,
return 0;
}

/**** END OF FILE ****/
#endif /* __MBED__ */
/**** END OF FILE ****/
103 changes: 103 additions & 0 deletions src/Arduino_GigaDisplayTouchZephyr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright 2025 Arduino SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#ifdef __ZEPHYR__

#include "Arduino_GigaDisplayTouch.h"
#include <zephyr/device.h>
#include <zephyr/drivers/display.h>
#include <zephyr/input/input.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/util.h>

typedef struct {
size_t x;
size_t y;
bool pressed;
} touch_point_t;

static uint8_t zephyr_touch_cb_slot_num;
static struct k_sem zephyr_touch_event_sync;
static touch_point_t zephyr_touch_points[CONFIG_INPUT_GT911_MAX_TOUCH_POINTS];

typedef void (*zephyr_input_callback_t)(struct input_event *evt,
void *user_data);
extern "C" void zephyr_input_register_callback(zephyr_input_callback_t cb);
static void touch_event_callback(struct input_event *evt, void *user_data);

Arduino_GigaDisplayTouch::Arduino_GigaDisplayTouch() {}

Arduino_GigaDisplayTouch::~Arduino_GigaDisplayTouch() {}

bool Arduino_GigaDisplayTouch::begin() {
k_sem_init(&zephyr_touch_event_sync, 0, 1);
zephyr_input_register_callback(touch_event_callback);
return true;
}

void Arduino_GigaDisplayTouch::end() {}

uint8_t Arduino_GigaDisplayTouch::getTouchPoints(GDTpoint_t *points) {
// First wait to see if we get any events.
if (k_sem_take(&zephyr_touch_event_sync, K_NO_WAIT) != 0) {
return 0;
}

uint8_t count_pressed = 0;
for (uint8_t i = 0; i <= zephyr_touch_cb_slot_num; i++) {
if (zephyr_touch_points[i].pressed) {
points[count_pressed].x = zephyr_touch_points[i].x;
points[count_pressed].y = zephyr_touch_points[i].y;
count_pressed++;
}
}
return count_pressed;
}

void Arduino_GigaDisplayTouch::onDetect(void (*handler)(uint8_t,
GDTpoint_t *)) {
UNUSED(handler);
}

static void touch_event_callback(struct input_event *evt, void *user_data) {
static const struct device *const touch_dev =
DEVICE_DT_GET(DT_CHOSEN(zephyr_touch));

if (evt->dev != touch_dev) {
return;
}

switch (evt->code) {
case INPUT_ABS_MT_SLOT:
zephyr_touch_cb_slot_num = evt->value;
break;
case INPUT_ABS_X:
zephyr_touch_points[zephyr_touch_cb_slot_num].x = evt->value;
break;
case INPUT_ABS_Y:
zephyr_touch_points[zephyr_touch_cb_slot_num].y = evt->value;
break;
case INPUT_BTN_TOUCH:
zephyr_touch_points[zephyr_touch_cb_slot_num].pressed = evt->value;
break;
}

if (evt->sync) {
k_sem_give(&zephyr_touch_event_sync);
}
}
#endif