From 328161602609e2b1f82fbf9897ee4002f77df6ce Mon Sep 17 00:00:00 2001 From: t-moe Date: Sat, 2 May 2015 11:58:44 +0200 Subject: [PATCH] Added some more touch functions. Improved pixy test. Drag the Image around! --- common/app/screen_pixytest.c | 61 ++++++++++++++++++++++++++++++++++-- common/touch/touch.c | 16 +++++++--- common/touch/touch.h | 15 ++++++--- 3 files changed, 81 insertions(+), 11 deletions(-) diff --git a/common/app/screen_pixytest.c b/common/app/screen_pixytest.c index 5f07c69..9c2709b 100644 --- a/common/app/screen_pixytest.c +++ b/common/app/screen_pixytest.c @@ -1,19 +1,57 @@ #include "screen_pixytest.h" #include "button.h" #include "tft.h" +#include "touch.h" #include "pixy.h" #include #include "system.h" -static bool pixy_connected = false; +static volatile bool pixy_connected = false; static BUTTON_STRUCT b_back; +static TOUCH_AREA_STRUCT a_area; static void b_back_cb(void* button) { gui_screen_back(); } +POINT_STRUCT pixy_pos; +POINT_STRUCT old_pos; +static void touchCB(void* touchArea, TOUCH_ACTION triggeredAction) { + POINT_STRUCT p = touch_get_last_point(); + switch(triggeredAction) { + case PEN_ENTER: + case PEN_DOWN: + old_pos = p; + break; + case PEN_MOVE: + { + int16_t deltaX = p.x - old_pos.x; + int16_t deltaY = p.y - old_pos.y; + old_pos=p; + printf("%d %d\n",deltaX,deltaY); + if(pixy_connected) { + int16_t new_x = pixy_pos.x+deltaX*2; + int16_t new_y = pixy_pos.y-deltaY*2; + if(new_x<0) new_x=0; + if(new_x>1000) new_x=1000; + if(new_y<0) new_y=0; + if(new_y>1000) new_y=1000; + pixy_pos.x = new_x; + pixy_pos.y= new_y; + } + } + break; + case PEN_UP: + case PEN_LEAVE: + printf("Leave/up\n"); + default: break; + } + + +} + static void enter(void* screen) { tft_clear(WHITE); @@ -30,14 +68,27 @@ static void enter(void* screen) { b_back.callback=b_back_cb; //Call b_back_cb as Callback gui_button_add(&b_back); //Register Button (and run the callback from now on) + //Area test + a_area.hookedActions = PEN_DOWN | PEN_MOVE | PEN_ENTER | PEN_UP | PEN_LEAVE; + a_area.x1 = 0; + a_area.y1 = 0; + a_area.x2 = 317; + a_area.y2 = 197; + a_area.callback = touchCB; + touch_register_area(&a_area); + + //Pixy stuff pixy_connected = (pixy_init()==0); //try to connect to pixy - + if(pixy_connected) { + pixy_pos.x=pixy_pos.y=500; + } } static void leave(void* screen) { gui_button_remove(&b_back); + touch_unregister_area(&a_area); } int pixy_led_test(); @@ -51,6 +102,7 @@ static void update(void* screen) { pixy_close(); //Ensure that all pixy resources are freed (failsafe) if(pixy_init()==0) { //try to connect to pixy pixy_connected=true; + pixy_pos.x=pixy_pos.y=500; printf("pixy reinitialized\n"); } } @@ -66,6 +118,11 @@ static void update(void* screen) { if(pixy_frame_test()!=0) { pixy_connected=false; } + + + pixy_rcs_set_position(0,pixy_pos.x); + pixy_rcs_set_position(1,pixy_pos.y); + //system_delay(500); } } diff --git a/common/touch/touch.c b/common/touch/touch.c index dbecbc6..ad8c589 100644 --- a/common/touch/touch.c +++ b/common/touch/touch.c @@ -5,8 +5,7 @@ #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 POINT_STRUCT pos; volatile TOUCH_STATE oldState=TOUCH_UP; bool touch_init() { @@ -14,12 +13,12 @@ bool touch_init() { } -bool touch_add_raw_event(uint16_t x, uint16_t y, TOUCH_STATE state) { +bool touch_add_raw_event(uint16_t touchX, uint16_t touchY, TOUCH_STATE state) { bool penDown = (state==TOUCH_DOWN); bool oldPenDown = (oldState==TOUCH_DOWN); oldState=state; - uint16_t touchX = x; - uint16_t touchY = y; + pos.x=touchX; + pos.y=touchY; if(penDown) { // tftDrawPixel(touchX,touchY,WHITE); @@ -127,3 +126,10 @@ void touch_unregister_area(TOUCH_AREA_STRUCT* area)//Unregisters an Area } } } + + +POINT_STRUCT touch_get_last_point() { + return pos; +} + + diff --git a/common/touch/touch.h b/common/touch/touch.h index 329538b..603a83a 100644 --- a/common/touch/touch.h +++ b/common/touch/touch.h @@ -13,20 +13,27 @@ 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 + uint16_t x1; //Top Left X Coordiate of Area + uint16_t y1; //Top Left Y Coordiate of Area + uint16_t x2; //Bottom Right X Coordiate of Area + uint16_t y2; //Bottom Right Y Coordiate of Area TOUCH_CALLBACK callback; //Callback uint8_t flags; //Internal Used, don't change } TOUCH_AREA_STRUCT; +typedef struct { + uint16_t x; + uint16_t y; +} POINT_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); +POINT_STRUCT touch_get_last_point(); #endif /* TOUCH_H */