From 259d446888d551d162902aeabb9d891dcefb4ba6 Mon Sep 17 00:00:00 2001 From: t-moe Date: Mon, 27 Apr 2015 01:39:02 +0200 Subject: [PATCH] Added touch support to emulator. Implemented basic touch function. --- common/app/app.c | 46 ++++++++++++- common/lowlevel/ll_touch.h | 6 ++ common/touch/touch.c | 129 +++++++++++++++++++++++++++++++++++++ common/touch/touch.h | 27 ++++++++ discovery/Makefile | 2 +- discovery/src/ll_touch.c | 6 ++ emulator/Makefile | 2 +- emulator/qt/emulatorqt.pro | 6 +- emulator/qt/ll_touch.cpp | 8 +++ emulator/qt/mainwindow.cpp | 34 ++++++++++ emulator/qt/mainwindow.h | 4 ++ 11 files changed, 265 insertions(+), 5 deletions(-) create mode 100644 common/lowlevel/ll_touch.h create mode 100644 common/touch/touch.c create mode 100644 discovery/src/ll_touch.c create mode 100644 emulator/qt/ll_touch.cpp diff --git a/common/app/app.c b/common/app/app.c index 243938f..d487e2b 100644 --- a/common/app/app.c +++ b/common/app/app.c @@ -1,15 +1,45 @@ #include "app.h" #include "tft.h" #include "system.h" +#include "touch.h" #include "pixy.h" #include #include bool pixy_connected = false; +TOUCH_AREA_STRUCT a1; + + +void touchCB(void* touchArea, TOUCH_ACTION triggeredAction) { + + switch(triggeredAction) { + case PEN_DOWN: + printf("action PEN_DOWN\n"); + break; + + case PEN_UP: + printf("action PEN_UP\n"); + break; + case PEN_MOVE: + printf("action PEN_MOVE\n"); + break; + case PEN_ENTER: + printf("action PEN_ENTER\n"); + break; + case PEN_LEAVE: + printf("action PEN_LEAVE\n"); + break; + default: + printf("action %s\n",triggeredAction); + break; + } +} + void app_init() { system_init(); tft_init(); + touch_init(); pixy_connected = (pixy_init()==0); //try to connect to pixy @@ -20,10 +50,24 @@ void app_init() { tft_draw_rectangle(40,210,60,235,BLUE); tft_fill_rectangle(100,215,200,225,GREEN); tft_draw_line(10,215,310,225,RGB(0xFF,0,0xFF)); + + + a1.hookedActions = PEN_DOWN | PEN_UP | PEN_MOVE | PEN_ENTER | PEN_LEAVE; + a1.x1 = 30; + a1.y1 = 30; + a1.x2 = 100; + a1.y2 = 60; + a1.callback = touchCB; + touch_register_area(&a1); + + tft_draw_rectangle(30,30,100,60,BLUE); + + } + int pixy_led_test(); int pixy_frame_test(); @@ -122,7 +166,7 @@ int pixy_frame_test() { -inline void interpolateBayer(uint16_t width, uint16_t x, uint16_t y, uint8_t *pixel, uint8_t* r, uint8_t* g, uint8_t* b) +void interpolateBayer(uint16_t width, uint16_t x, uint16_t y, uint8_t *pixel, uint8_t* r, uint8_t* g, uint8_t* b) { if (y&1) { diff --git a/common/lowlevel/ll_touch.h b/common/lowlevel/ll_touch.h new file mode 100644 index 0000000..c573a8e --- /dev/null +++ b/common/lowlevel/ll_touch.h @@ -0,0 +1,6 @@ +#include +#include + +bool ll_touch_init(); + + diff --git a/common/touch/touch.c b/common/touch/touch.c new file mode 100644 index 0000000..dbecbc6 --- /dev/null +++ b/common/touch/touch.c @@ -0,0 +1,129 @@ +#include "touch.h" +#include "ll_touch.h" +#include + +#define NUM_AREAS 50 //Number of Structs Reserved in Memory for TouchAreas (e.g Buttons) +TOUCH_AREA_STRUCT* areas[NUM_AREAS] = {NULL}; + +volatile int touchY=0; //Last Y Coordinate in pixels +volatile int touchX=0; //Last X Coordinate in pixels +volatile TOUCH_STATE oldState=TOUCH_UP; + +bool touch_init() { + return ll_touch_init(); +} + + +bool touch_add_raw_event(uint16_t x, uint16_t y, TOUCH_STATE state) { + bool penDown = (state==TOUCH_DOWN); + bool oldPenDown = (oldState==TOUCH_DOWN); + oldState=state; + uint16_t touchX = x; + uint16_t touchY = y; + if(penDown) + { + // tftDrawPixel(touchX,touchY,WHITE); + if(!oldPenDown) //First Touch + { + for(int z=0; z < NUM_AREAS; z++) // For every touch area + { + if(areas[z]!=NULL && touchX >= areas[z]->x1 && touchX <= areas[z]->x2 && touchY >= areas[z]->y1 && touchY <= areas[z]->y2 ) + { + areas[z]->flags=1; //PenInside=1 + if(areas[z]->hookedActions & PEN_DOWN) + areas[z]->callback(areas[z],PEN_DOWN); + } + } + } + else //Second, Third + { + for(int z=0; z < NUM_AREAS; z++) // For every touch area + { + if(areas[z]!=NULL ) + { + if(touchX >= areas[z]->x1 && touchX <= areas[z]->x2 && touchY >= areas[z]->y1 && touchY <= areas[z]->y2) + { + if(areas[z]->flags==0) //PenInside ==0 + { + areas[z]->flags=1; //PenInside=1 + if(areas[z]->hookedActions & PEN_ENTER) + areas[z]->callback(areas[z],PEN_ENTER); + } + } + else if(areas[z]->flags) //PenInside==1 + { + areas[z]->flags=0; //PenInside=0 + if(areas[z]->hookedActions & PEN_LEAVE) + areas[z]->callback(areas[z],PEN_LEAVE); + } + } + } + } + for(int z=0; z < NUM_AREAS; z++) // For every touch area + { + if(areas[z]!=NULL && areas[z]->hookedActions&PEN_MOVE) + { + if(touchX >= areas[z]->x1 && touchX <= areas[z]->x2 && touchY >= areas[z]->y1 && touchY <= areas[z]->y2) + { + areas[z]->callback(areas[z],PEN_MOVE); + } + } + } + } + else + { + if(oldPenDown) //Was the pen ever down (or was it a too short touch) + { + for(int z=0; z < NUM_AREAS; z++) // For every touch area + { + if(areas[z]!=NULL && touchX >= areas[z]->x1 && touchX <= areas[z]->x2 && touchY >= areas[z]->y1 && touchY <= areas[z]->y2 ) + { + areas[z]->flags=0; //PenInside = 0; + if(areas[z]->hookedActions & PEN_UP) + areas[z]->callback(areas[z],PEN_UP); + } + } + } + touchX=0xFFFF; + touchY=0xFFFF; + } + + return true; +} + +bool touch_have_empty(unsigned char num) +{ + for(unsigned char i=0; iChange NUM_AREAS). +{ + + for(unsigned char i=0; iflags=0; + areas[i]=area; + return true; + } + } + return false; +} + +void touch_unregister_area(TOUCH_AREA_STRUCT* area)//Unregisters an Area +{ + for(unsigned char i=0; i +#include + + +typedef enum {TOUCH_UP,TOUCH_DOWN} TOUCH_STATE ; +typedef enum {NONE=0x00,PEN_DOWN=0x01, PEN_UP=0x02, PEN_ENTER=0x04, PEN_LEAVE=0x08,PEN_MOVE=0x10} TOUCH_ACTION; + +typedef void (*TOUCH_CALLBACK)(void* touchArea, TOUCH_ACTION triggeredAction); + +typedef struct { + TOUCH_ACTION hookedActions; //Actions to listen to + unsigned int x1; //Top Left X Coordiate of Area + unsigned int y1; //Top Left Y Coordiate of Area + unsigned int x2; //Bottom Right X Coordiate of Area + unsigned int y2; //Bottom Right Y Coordiate of Area + TOUCH_CALLBACK callback; //Callback + uint8_t flags; //Internal Used, don't change +} TOUCH_AREA_STRUCT; + + +bool touch_init(); +bool touch_add_raw_event(uint16_t x, uint16_t y,TOUCH_STATE state); +bool touch_have_empty(unsigned char num); +bool touch_register_area(TOUCH_AREA_STRUCT* area); +void touch_unregister_area(TOUCH_AREA_STRUCT* area); + + diff --git a/discovery/Makefile b/discovery/Makefile index 78493b0..c7c4a45 100644 --- a/discovery/Makefile +++ b/discovery/Makefile @@ -41,7 +41,7 @@ INCLUDES:=$(addprefix -I,$(INCLUDES)) CPPFLAGS=-DUSE_USB_OTG_FS -DUSE_HOST_MODE $(INCLUDES) -CFLAGS=$(ARCH_FLAGS) -O0 -g +CFLAGS=$(ARCH_FLAGS) -O0 -g -std=c99 LIBS=pixy usbhost coreperiph stdc++ diff --git a/discovery/src/ll_touch.c b/discovery/src/ll_touch.c new file mode 100644 index 0000000..998001e --- /dev/null +++ b/discovery/src/ll_touch.c @@ -0,0 +1,6 @@ +#include "ll_touch.h" + +bool ll_touch_init() { + return false; +} + diff --git a/emulator/Makefile b/emulator/Makefile index 5917112..7fee00b 100644 --- a/emulator/Makefile +++ b/emulator/Makefile @@ -25,7 +25,7 @@ QT_LIB=$(QT_DIR)/libemulatorqt.a CPPFLAGS= -march=x86-64 -mtune=generic -fPIC $(INCLUDES) -CFLAGS= -O0 -g +CFLAGS= -O0 -g -std=c99 LIBS= pixy usb-1.0 boost_system boost_timer boost_chrono diff --git a/emulator/qt/emulatorqt.pro b/emulator/qt/emulatorqt.pro index e604c2e..2059c2f 100644 --- a/emulator/qt/emulatorqt.pro +++ b/emulator/qt/emulatorqt.pro @@ -14,12 +14,14 @@ SOURCES += \ mainwindow.cpp \ main.cpp \ ll_tft.cpp \ - ll_system.cpp + ll_system.cpp \ + ll_touch.cpp HEADERS += \ mainwindow.h \ -INCLUDEPATH+= ../../common/lowlevel/ +INCLUDEPATH+= ../../common/lowlevel/ \ + ../../common/touch/ FORMS += \ diff --git a/emulator/qt/ll_touch.cpp b/emulator/qt/ll_touch.cpp new file mode 100644 index 0000000..46e0c6c --- /dev/null +++ b/emulator/qt/ll_touch.cpp @@ -0,0 +1,8 @@ + +extern "C" { +#include "ll_touch.h" +} + +bool ll_touch_init() { + return true; +} diff --git a/emulator/qt/mainwindow.cpp b/emulator/qt/mainwindow.cpp index 2dc85ce..86ae8a2 100644 --- a/emulator/qt/mainwindow.cpp +++ b/emulator/qt/mainwindow.cpp @@ -3,6 +3,11 @@ #include #include #include +#include + +extern "C" { + #include "touch.h" +} #define DISPLAY_WIDTH 320 #define DISPLAY_HEIGHT 240 @@ -110,9 +115,38 @@ void MainWindow::paintEvent(QPaintEvent *) //render_mutex.unlock(); } +void MainWindow::mousePressEvent(QMouseEvent *evt) +{ + //qDebug() << "down" << evt->pos(); + checkAndSendEvent(evt->pos(),true); +} + +void MainWindow::mouseReleaseEvent(QMouseEvent *evt) +{ + //qDebug() << "up" << evt->pos(); + checkAndSendEvent(evt->pos(),false); +} + +void MainWindow::mouseMoveEvent(QMouseEvent *evt) +{ + //qDebug() << "move" << evt->pos(); + checkAndSendEvent(evt->pos(),true); + +} + MainWindow::~MainWindow() { delete ui; } +void MainWindow::checkAndSendEvent(QPoint pos, bool down) +{ + QPoint p = pos - QPoint(DISPLAY_X,DISPLAY_Y); + if(p.x()<0 || p.y()<0 || p.x() >= DISPLAY_WIDTH || p.y() >= DISPLAY_HEIGHT) return; + + //qDebug() << down << p; + + touch_add_raw_event(p.x(),p.y(),down?TOUCH_DOWN:TOUCH_UP); +} + diff --git a/emulator/qt/mainwindow.h b/emulator/qt/mainwindow.h index ce851e5..9807bbd 100644 --- a/emulator/qt/mainwindow.h +++ b/emulator/qt/mainwindow.h @@ -25,11 +25,15 @@ public: protected: void paintEvent(QPaintEvent * evt); + void mousePressEvent(QMouseEvent* evt); + void mouseReleaseEvent(QMouseEvent* evt); + void mouseMoveEvent(QMouseEvent* evt); ~MainWindow(); private: //QMutex render_mutex; QImage image; + void checkAndSendEvent(QPoint pos, bool down); Ui::MainWindow *ui;