Added touch support to emulator. Implemented basic touch function.
This commit is contained in:
@@ -1,15 +1,45 @@
|
||||
#include "app.h"
|
||||
#include "tft.h"
|
||||
#include "system.h"
|
||||
#include "touch.h"
|
||||
#include "pixy.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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)
|
||||
{
|
||||
|
||||
6
common/lowlevel/ll_touch.h
Normal file
6
common/lowlevel/ll_touch.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
bool ll_touch_init();
|
||||
|
||||
|
||||
129
common/touch/touch.c
Normal file
129
common/touch/touch.c
Normal file
@@ -0,0 +1,129 @@
|
||||
#include "touch.h"
|
||||
#include "ll_touch.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#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; i<NUM_AREAS; i++)
|
||||
{
|
||||
if(areas[i]==NULL) num--;
|
||||
if(num==0) return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
bool touch_register_area(TOUCH_AREA_STRUCT* area) //Registers an Area (fill Struct first). Return false if no more Space in the Pointertable (-->Change NUM_AREAS).
|
||||
{
|
||||
|
||||
for(unsigned char i=0; i<NUM_AREAS; i++)
|
||||
{
|
||||
if(areas[i]==NULL)
|
||||
{
|
||||
area->flags=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<NUM_AREAS; i++)
|
||||
{
|
||||
if(areas[i]==area)
|
||||
{
|
||||
areas[i]=NULL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#include<stdbool.h>
|
||||
#include<stdint.h>
|
||||
|
||||
|
||||
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);
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user