Skip to content

Commit

Permalink
Merge branch 'refactor/oop'
Browse files Browse the repository at this point in the history
  • Loading branch information
martinberlin committed Jan 9, 2021
2 parents b2f368b + 5ef7ae7 commit ee0adb3
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 85 deletions.
206 changes: 130 additions & 76 deletions components/FT6X36-IDF/FT6X36.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,30 +114,80 @@ void IRAM_ATTR FT6X36::isr(void* arg)
void FT6X36::processTouch()
{
/* Task move to Block state to wait for interrupt event */
if (xSemaphoreTake(TouchSemaphore, portMAX_DELAY)) {
readData();
uint8_t n = 0;
TRawEvent event = (TRawEvent)_touchEvent[n];
TPoint point{_touchX[n], _touchY[n]};
if (xSemaphoreTake(TouchSemaphore, portMAX_DELAY) == false) return;
readData();
uint8_t n = 0;
TRawEvent event = (TRawEvent)_touchEvent[n];
TPoint point{_touchX[n], _touchY[n]};

switch (event) {

case TRawEvent::PressDown:
_points[0] = point;
_dragMode = false;
// Note: Is in microseconds. Ref https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/esp_timer.html
_touchStartTime = esp_timer_get_time()/1000;
fireEvent(point, TEvent::TouchStart);
break;

case TRawEvent::Contact:
// Dragging makes no sense IMHO. Since the X & Y are not getting updated while dragging
// Dragging && _points[0].aboutEqual(point) - Not used IDEA 2: && (lastEvent == 2)
if (!_dragMode &&
(abs(lastX-_touchX[n]) <= maxDeviation || abs(lastY-_touchY[n])<=maxDeviation) &&
esp_timer_get_time()/1000 - _touchStartTime > 300) {
_dragMode = true;
fireEvent(point, TEvent::DragStart);
if (CONFIG_FT6X36_DEBUG_EVENTS) printf("EV: DragStart\n");
} else if (_dragMode) {

fireEvent(point, TEvent::DragMove);
if (CONFIG_FT6X36_DEBUG_EVENTS) printf("EV: DragMove\n");
}
fireEvent(point, TEvent::TouchMove);

// For me the _touchStartTime shouold be set in both PressDown & Contact events, but after Drag detection
_touchStartTime = esp_timer_get_time()/1000;
break;

case TRawEvent::LiftUp:

_points[9] = point;
_touchEndTime = esp_timer_get_time()/1000;

//printf("TIMEDIFF: %lu End: %lu\n", _touchEndTime - _touchStartTime, _touchEndTime);


printf("pt:%d\n",_touchEvent[n]);

// Only TAP detected for now
if (event == TRawEvent::Contact)
{
_points[0] = point;
_pointIdx = 1;
fireEvent(point, TEvent::Tap);
}
if (event == TRawEvent::LiftUp)
{
fireEvent(point, TEvent::TouchEnd);

}
if (_dragMode)
{
fireEvent(point, TEvent::DragEnd);
if (CONFIG_FT6X36_DEBUG_EVENTS) printf("EV: DragEnd\n");
_dragMode = false;
}

if ( _touchEndTime - _touchStartTime <= 900) {
// Do not get why this: _points[0].aboutEqual(point) (Original library)
fireEvent(point, TEvent::Tap);
_points[0] = {0, 0};
_touchStartTime = 0;
if (CONFIG_FT6X36_DEBUG_EVENTS) printf("EV: Tap\n");
_dragMode = false;
}

break;

case TRawEvent::NoEvent:
// Do nothing
if (CONFIG_FT6X36_DEBUG_EVENTS) printf("EV: NoEvent\n");
break;
}
// Store lastEvent
lastEvent = (int) event;
lastX = _touchX[0];
lastY = _touchY[0];
}


uint8_t FT6X36::read8(uint8_t regName) {
uint8_t buf;
readRegister8(regName, &buf);
Expand All @@ -146,80 +196,84 @@ uint8_t FT6X36::read8(uint8_t regName) {

bool FT6X36::readData(void)
{
uint8_t data_xy[4]; // 2 bytes X | 2 bytes Y
esp_err_t ret;
uint8_t data_size = 16; // Discarding last 2: 0x0E & 0x0F as not relevant
uint8_t data[data_size];
uint8_t touch_pnt_cnt; // Number of detected touch points

readRegister8(FT6X36_REG_NUM_TOUCHES, &touch_pnt_cnt);
if (touch_pnt_cnt==0) return 0;

// Read X value
i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create();

i2c_master_start(i2c_cmd);
i2c_master_write_byte(i2c_cmd, (FT6X36_ADDR << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(i2c_cmd, FT6X36_REG_P1_XH, I2C_MASTER_ACK);

i2c_master_start(i2c_cmd);
i2c_master_write_byte(i2c_cmd, (FT6X36_ADDR << 1) | I2C_MASTER_READ, true);
//printf("NUM_TOUCHES:%d\n",touch_pnt_cnt);

i2c_master_read_byte(i2c_cmd, &data_xy[0], I2C_MASTER_ACK); // reads FT6X36_P1_XH_REG
i2c_master_read_byte(i2c_cmd, &data_xy[1], I2C_MASTER_NACK); // reads FT6X36_P1_XL_REG
i2c_master_stop(i2c_cmd);
esp_err_t ret = i2c_master_cmd_begin(I2C_NUM_0, i2c_cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(i2c_cmd);
// Read data
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (FT6X36_ADDR<<1), ACK_CHECK_EN);
i2c_master_write_byte(cmd, 0, ACK_CHECK_EN);
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Error getting X coordinates: %s", esp_err_to_name(ret));
// no touch detected
return false;
return ret;
}

// Read Y value
i2c_cmd = i2c_cmd_link_create();

i2c_master_start(i2c_cmd);
i2c_master_write_byte(i2c_cmd, (FT6X36_ADDR << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(i2c_cmd, FT6X36_REG_P1_YH, I2C_MASTER_ACK);

i2c_master_start(i2c_cmd);
i2c_master_write_byte(i2c_cmd, (FT6X36_ADDR << 1) | I2C_MASTER_READ, true);
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (FT6X36_ADDR<<1)|1, ACK_CHECK_EN);
i2c_master_read(cmd, data, data_size, I2C_MASTER_LAST_NACK);
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);

i2c_master_read_byte(i2c_cmd, &data_xy[2], I2C_MASTER_ACK); // reads FT6X36_P1_YH_REG
i2c_master_read_byte(i2c_cmd, &data_xy[3], I2C_MASTER_NACK); // reads FT6X36_P1_YL_REG
i2c_master_stop(i2c_cmd);
ret = i2c_master_cmd_begin(I2C_NUM_0, i2c_cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(i2c_cmd);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Error getting Y coordinates: %s", esp_err_to_name(ret));
return false;
}
if (CONFIG_FT6X36_DEBUG) {
//printf("REGISTERS:\n");
for (int16_t i = 0; i < data_size; i++)
{
printf("%x:%x ", i, data[i]);
}
printf("\n");
}

uint16_t x = ((data_xy[0] & FT6X36_MSB_MASK) << 8) | (data_xy[1] & FT6X36_LSB_MASK);
uint16_t y = ((data_xy[2] & FT6X36_MSB_MASK) << 8) | (data_xy[3] & FT6X36_LSB_MASK);
// This is not right. Is not getting the 0x03 [7:6] 1st event flag
_touchEvent[0] = data_xy[0] >> 6;
const uint8_t addrShift = 6;

// READ X, Y and Touch events (X 2)
for (uint8_t i = 0; i < 2; i++)
{
_touchX[i] = data[FT6X36_REG_P1_XH + i * addrShift] & 0x0F;
_touchX[i] <<= 8;
_touchX[i] |= data[FT6X36_REG_P1_XL + i * addrShift];
_touchY[i] = data[FT6X36_REG_P1_YH + i * addrShift] & 0x0F;
_touchY[i] <<= 8;
_touchY[i] |= data[FT6X36_REG_P1_YL + i * addrShift];
_touchEvent[i] = data[FT6X36_REG_P1_XH + i * addrShift] >> 6;
}

// Make _touchX[0] and _touchY[0] rotation aware
switch (_rotation)
// Make _touchX[idx] and _touchY[idx] rotation aware
switch (_rotation)
{
case 1:
swap(x, y);
y = _touch_width - y -1;
swap(_touchX[0], _touchY[0]);
swap(_touchX[1], _touchY[1]);
_touchY[0] = _touch_width - _touchY[0] -1;
_touchY[1] = _touch_width - _touchY[1] -1;
break;
case 2:
x = _touch_width - x - 1;
y = _touch_height - y - 1;
_touchX[0] = _touch_width - _touchX[0] - 1;
_touchX[1] = _touch_width - _touchX[1] - 1;
_touchY[0] = _touch_height - _touchY[0] - 1;
_touchY[1] = _touch_height - _touchY[1] - 1;
break;
case 3:
swap(x, y);
x = _touch_height - x - 1;
swap(_touchX[0], _touchY[0]);
swap(_touchX[1], _touchY[1]);
_touchX[0] = _touch_height - _touchX[0] - 1;
_touchX[1] = _touch_height - _touchX[1] - 1;
break;
}
_touchX[0] = x;
_touchY[0] = y;

if (CONFIG_FT6X36_DEBUG)
printf("X:%d Y:%d T:%d\n", _touchX[0], _touchY[0], _touchEvent[0]);


printf("X0:%d Y0:%d EVENT:%d\n", _touchX[0], _touchY[0], _touchEvent[0]);
if (CONFIG_FT6X36_DEBUG) {
//printf("X0:%d Y0:%d EVENT:%d\n", _touchX[0], _touchY[0], _touchEvent[0]);
//printf("X1:%d Y1:%d EVENT:%d\n", _touchX[1], _touchY[1], _touchEvent[1]);
}
return true;
}

Expand Down
4 changes: 4 additions & 0 deletions components/FT6X36-IDF/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ menu "Touch FT6X36 Configuration"
int "FT6X36 debug: On 1 will output FocalTech touch events via serial"
range 0 1
default 0
config FT6X36_DEBUG_EVENTS
int "FT6X36 debug events: On 1 will output touch events only"
range 0 1
default 0
endmenu
11 changes: 8 additions & 3 deletions components/FT6X36-IDF/include/FT6X36.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,12 @@ struct TPoint
{
uint16_t x;
uint16_t y;

/**
* This is being used in the original library but I'm not using it in this implementation
*/
bool aboutEqual(const TPoint point)
{
const uint8_t maxDeviation = 5;
return abs(x - point.x) <= maxDeviation && abs(y - point.y) <= maxDeviation;
return abs(x - point.x) <= 5 && abs(y - point.y) <= 5;
}
};

Expand Down Expand Up @@ -170,7 +171,11 @@ class FT6X36
uint8_t _pointIdx = 0;
unsigned long _touchStartTime = 0;
unsigned long _touchEndTime = 0;
uint8_t lastEvent = 3; // No event
uint16_t lastX = 0;
uint16_t lastY = 0;
bool _dragMode = false;
const uint8_t maxDeviation = 5;
};

#endif
5 changes: 3 additions & 2 deletions main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ idf_component_register(
# TOUCH examples. 1- Touch indepently from Epd class uses Gdew027w3 2- Touch implemented in Gdew027w3T class
#SRCS "demos/demo-touch.cpp"
#SRCS "demos/demo-touch-epd-implemented.cpp"
#SRCS "demos/demo-touch-keyboard.cpp"

SRCS "demos/demo-touch-keyboard.cpp"

# Generic demos for any displays
# SRCS "demos/demo-sleep-clock.cpp"
# SRCS "demo-sleep-clock-v2.cpp"
SRCS "demos/demo-fonts.cpp"
# SRCS "demos/demo-fonts.cpp"

# Demo only for plasticlogic.com epapers:
# SRCS "demos/demo-epaper-plasticlogic.cpp"
Expand Down
11 changes: 7 additions & 4 deletions main/demos/demo-touch-keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
// Optional font for the writing box:
#include <Fonts/ubuntu/Ubuntu_M8pt8b.h>
bool use_custom_font = true; // false to use default Adafruit GFX font (Small 8px)
//#define DEBUG_TOUCH_COUNT 1
#define DEBUG_TOUCH_KEY 1
#define DEBUG_TOUCH_COUNT 1
//#define DEBUG_TOUCH_KEY 1

// INTGPIO is touch interrupt, goes low when it detects a touch, which coordinates are read by I2C
FT6X36 ts(CONFIG_TOUCH_INT);
Expand Down Expand Up @@ -160,8 +160,12 @@ void touchEvent(TPoint p, TEvent e)
{
#if defined(DEBUG_TOUCH_COUNT)
++t_counter;
printf("X: %d Y: %d count:%d\n", p.x, p.y, t_counter);
//printf("X: %d Y: %d count:%d Ev:%d\n", p.x, p.y, t_counter, int(e));
#endif

// Trigger keys only on TAP
if (e != TEvent::Tap) return;


// First the Y line IFs
if (p.y>line1_ystart && p.y<line2_ystart) { // LINE 1
Expand Down Expand Up @@ -244,7 +248,6 @@ void app_main(void)
printf("display.colors_supported:%d display.rotation: %d\n", display.colors_supported,display.getRotation());
drawUX();

display.update();
display.registerTouchHandler(touchEvent);

for (;;) {
Expand Down

0 comments on commit ee0adb3

Please sign in to comment.