discoverpixy
Modules | Data Structures | Typedefs | Enumerations | Functions
Touch
Collaboration diagram for Touch:

Modules

 Calibrate (Screen)
 

Data Structures

struct  TOUCH_AREA_STRUCT
 
struct  POINT_STRUCT
 

Typedefs

typedef void(* TOUCH_CALLBACK) (void *touchArea, TOUCH_ACTION triggeredAction)
 

Enumerations

enum  TOUCH_STATE { TOUCH_UP, TOUCH_DOWN }
 
enum  TOUCH_ACTION {
  NONE = 0x00, PEN_DOWN = 0x01, PEN_UP = 0x02, PEN_ENTER = 0x04,
  PEN_LEAVE = 0x08, PEN_MOVE = 0x10
}
 

Functions

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 ()
 
void touch_set_calibration_values (int xs, int dx, int ys, int dy)
 
void touch_set_value_convert_mode (bool use_calibration)
 

Detailed Description

The Touch module provides access to the touch controller, and executes a callback if a certain region is touched

Typedef Documentation

typedef void(* TOUCH_CALLBACK) (void *touchArea, TOUCH_ACTION triggeredAction)

Prototype for Event Listeners (called for every occurring, hooked action)

Note
You should NOT execute long running things in this callback nor should you update the gui. But you can call gui_screen_navigate() for instance.
Parameters
touchAreaThe pointer to the TOUCH_AREA_STRUCT in which the event occurred
triggeredActionThe Action which occurred

Definition at line 67 of file touch.h.

Enumeration Type Documentation

Enum to describe the hooked actions for which you want to receive events for. You can OR-combine them.

See also
touch_register_area
Enumerator
NONE 

Do not receive any events.

PEN_DOWN 

Receive an event when the pen goes down inside the region.

PEN_UP 

Receive an event when the pen goes up inside the region.

PEN_ENTER 

Receive an event when the pen enters the region (pen was down before)

PEN_LEAVE 

Receive an event when the pen leaves the region (pen was inside region before)

PEN_MOVE 

Receive an event when the pen moves inside the region (pen is down)

Definition at line 52 of file touch.h.

52  {
53  NONE = 0x00,
54  PEN_DOWN = 0x01,
55  PEN_UP = 0x02,
56  PEN_ENTER = 0x04,
57  PEN_LEAVE = 0x08,
58  PEN_MOVE = 0x10
59 } TOUCH_ACTION;
TOUCH_ACTION
Definition: touch.h:52
Receive an event when the pen moves inside the region (pen is down)
Definition: touch.h:58
Receive an event when the pen goes down inside the region.
Definition: touch.h:54
Receive an event when the pen goes up inside the region.
Definition: touch.h:55
Receive an event when the pen enters the region (pen was down before)
Definition: touch.h:56
Do not receive any events.
Definition: touch.h:53
Receive an event when the pen leaves the region (pen was inside region before)
Definition: touch.h:57

Enum to describe the current Touch State.

See also
touch_add_raw_event
Enumerator
TOUCH_UP 

The display is currently not touched.

TOUCH_DOWN 

The display is currently touched at some point.

Definition at line 43 of file touch.h.

43  {
44  TOUCH_UP,
45  TOUCH_DOWN
46 } TOUCH_STATE ;
The display is currently not touched.
Definition: touch.h:44
The display is currently touched at some point.
Definition: touch.h:45
TOUCH_STATE
Definition: touch.h:43

Function Documentation

bool touch_add_raw_event ( uint16_t  x,
uint16_t  y,
TOUCH_STATE  state 
)

Processes a native touch event. Call this function when the pen goes down (TOUCH_DOWN), when it moves (TOUCH_DOWN) and also when it goes up again (TOUCH_UP)! It's safe to call this function from an (SPI)-Interrupt.

Parameters
xThe x-Coordinate of the touch event
yThe y-Coordinate of the touch event
stateWhether the pen is up or down
Returns
True on success

Definition at line 72 of file touch.c.

73 {
74  //Update current and old position/state
75  bool penDown = (state == TOUCH_DOWN);
76  bool oldPenDown = (oldState == TOUCH_DOWN);
77  oldState = state;
78 
79  if (calibration) { //If in Calibration mode
80  if (penDown) {
81  pos.x = touchX;
82  pos.y = touchY;
83  } else {
84  if (oldPenDown) { //Run only if we got at least one pen down
85  calibration = 0; //Calibration finish (Touch X and Y are the values from the last measure, where the pen was down)
86  }
87  }
88 
89  return true;
90  }
91 
92  //If we reach this point we're not in calibration mode and we need to process the event and call the registred handlers..
93 
94  if (use_calibration) { //the underlying touch hardware uses calibration
95  //Calculate the real touch position out of the passed ones, and the calibration values
96  pos.x = touchX = (((long)(DWIDTH - 2 * CCENTER) * 2 * (long)((long)touchX - cal_xs) / cal_dx + 1) >> 1) + CCENTER;
97  pos.y = touchY = (((long)(DHEIGHT - 2 * CCENTER) * 2 * (long)((long)touchY - cal_ys) / cal_dy + 1) >> 1) + CCENTER;
98  } else { //no conversion needed for the underlying hardware
99  pos.x = touchX;
100  pos.y = touchY;
101  }
102 
103  if (penDown) { //pen is down now
104  //tft_draw_pixel(touchX,touchY,WHITE);
105  if (!oldPenDown) { //pen wasn't down before (positive edge) => First Touch
106  for (int z = 0; z < NUM_AREAS; z++) { // For every touch area
107  //Check if pos is inside area
108  if (areas[z] != NULL && touchX >= areas[z]->x1 && touchX <= areas[z]->x2 && touchY >= areas[z]->y1 && touchY <= areas[z]->y2) {
109  areas[z]->flags = 1; //Save PenInside=1
110 
111  if (areas[z]->hookedActions & PEN_DOWN) { //The user wants to receive pen down events
112  areas[z]->callback(areas[z], PEN_DOWN); //Send event to user callback
113  }
114  }
115  }
116  } else { //Pen was down before => Second, Third event in row
117  for (int z = 0; z < NUM_AREAS; z++) { // For every touch area
118  if (areas[z] != NULL) {
119  //Check if pos is inside area
120  if (touchX >= areas[z]->x1 && touchX <= areas[z]->x2 && touchY >= areas[z]->y1 && touchY <= areas[z]->y2) {
121  if (areas[z]->flags == 0) { //Pen was not inside before (PenInside==0)
122  areas[z]->flags = 1; //Pen is inside now (PenInside=1)
123 
124  if (areas[z]->hookedActions & PEN_ENTER) { //The user wants to receive pen enter events
125  areas[z]->callback(areas[z], PEN_ENTER);
126  }
127  }
128  } else if (areas[z]->flags) { //Pos not inside area, but it was before (PenInside==1)
129  areas[z]->flags = 0; //Pen is no longer inside (PenInside=0)
130 
131  if (areas[z]->hookedActions & PEN_LEAVE) { //The user wants to receive pen leave events
132  areas[z]->callback(areas[z], PEN_LEAVE);
133  }
134  }
135  }
136  }
137  }
138 
139  for (int z = 0; z < NUM_AREAS; z++) { // For every touch area
140  if (areas[z] != NULL && (areas[z]->hookedActions & PEN_MOVE)) { //User want's to receive pen move events
141  //Check if pos is inside area
142  if (touchX >= areas[z]->x1 && touchX <= areas[z]->x2 && touchY >= areas[z]->y1 && touchY <= areas[z]->y2) {
143  areas[z]->callback(areas[z], PEN_MOVE);
144  }
145  }
146  }
147  } else { //pen is not down now
148  if (oldPenDown) { //but it was down before (negative edge)
149  for (int z = 0; z < NUM_AREAS; z++) { // For every touch area
150  //Check if pos is inside area
151  if (areas[z] != NULL && touchX >= areas[z]->x1 && touchX <= areas[z]->x2 && touchY >= areas[z]->y1 && touchY <= areas[z]->y2) {
152  areas[z]->flags = 0; //The pen is no longer inside (PenInside = 0);
153 
154  if (areas[z]->hookedActions & PEN_UP) { //user want's to receive pen up events
155  areas[z]->callback(areas[z], PEN_UP);
156  }
157  }
158  }
159  }
160  }
161 
162  return true;
163 }
Receive an event when the pen moves inside the region (pen is down)
Definition: touch.h:58
Receive an event when the pen goes down inside the region.
Definition: touch.h:54
#define CCENTER
Receive an event when the pen goes up inside the region.
Definition: touch.h:55
uint16_t y
The Y-Coordinate of the point.
Definition: touch.h:88
int cal_ys
Definition: touch.c:47
uint8_t flags
For internal use, don't change, don't initialize.
Definition: touch.h:79
uint16_t x
The X-Coordinate of the point.
Definition: touch.h:87
#define DHEIGHT
The display is currently touched at some point.
Definition: touch.h:45
static enum @0 state
volatile POINT_STRUCT pos
Definition: touch.c:38
volatile TOUCH_STATE oldState
Definition: touch.c:39
TOUCH_CALLBACK callback
Callback which is executed when an event occurred in this Area.
Definition: touch.h:78
#define NUM_AREAS
Definition: touch.c:35
Receive an event when the pen enters the region (pen was down before)
Definition: touch.h:56
volatile bool calibration
Definition: touch.c:40
bool use_calibration
Definition: touch.c:42
int cal_xs
Definition: touch.c:45
#define DWIDTH
TOUCH_AREA_STRUCT * areas[NUM_AREAS]
Definition: touch.c:36
int cal_dy
Definition: touch.c:48
int cal_dx
Definition: touch.c:46
Receive an event when the pen leaves the region (pen was inside region before)
Definition: touch.h:57
POINT_STRUCT touch_get_last_point ( )

Gets the last touched point

Returns
The Coordinates of the last touched points

Definition at line 211 of file touch.c.

212 {
213  return pos;
214 }
volatile POINT_STRUCT pos
Definition: touch.c:38

Here is the caller graph for this function:

bool touch_have_empty ( unsigned char  num)

Checks whether or not we have memory to manage and track additional num TOUCH_AREA_STRUCTs

Parameters
numThe number of touch areas you would like to allocate
Returns
True if there's enough memory to allocate num TOUCH_AREAs

Definition at line 165 of file touch.c.

166 {
167  //go through pointer array and check for free spaces
168  for (unsigned char i = 0; i < NUM_AREAS; i++) {
169  if (areas[i] == NULL) {
170  num--; //a free space was found, we need one less
171  }
172 
173  if (num == 0) {
174  return true; //enough free spaces found
175  }
176  }
177 
178  return false; //not enough free spaces found
179 }
#define NUM_AREAS
Definition: touch.c:35
TOUCH_AREA_STRUCT * areas[NUM_AREAS]
Definition: touch.c:36

Here is the caller graph for this function:

bool touch_init ( )

Initializes the Touch Controller. Call this method before using any touch_* functions

Returns
true on success

Definition at line 61 of file touch.c.

62 {
63  return ll_touch_init();
64 }
bool ll_touch_init()

Here is the call graph for this function:

Here is the caller graph for this function:

bool touch_register_area ( TOUCH_AREA_STRUCT area)

Registers a new touch Area. You will receive events for this area from now on.

Parameters
areaA pointer to the configured TOUCH_AREA_STRUCT
Returns
True if everything was successful and the corresponding Touch Area will be monitored from now on

Definition at line 181 of file touch.c.

182 {
183  //go through pointer array and check for free space
184  for (unsigned char i = 0; i < NUM_AREAS; i++) {
185  if (areas[i] == NULL) { //free space found
186  area->flags = 0; //we start with empty flags (PenInside=0)
187  areas[i] = area; //save pointer into list
188  return true;
189  }
190  }
191 
192  return false; //no free space found
193 }
uint8_t flags
For internal use, don't change, don't initialize.
Definition: touch.h:79
#define NUM_AREAS
Definition: touch.c:35
TOUCH_AREA_STRUCT * areas[NUM_AREAS]
Definition: touch.c:36

Here is the caller graph for this function:

void touch_set_calibration_values ( int  xs,
int  dx,
int  ys,
int  dy 
)

Set's the new calibration values

Parameters
xsx offset (to calibration point 1)
dxx difference (between calibration point 1 and 2)
ysy offset (to calibration point 1)
dyy difference (between calibration point 1 and 2)

Definition at line 51 of file touch.c.

52 {
53  cal_xs = xs;
54  cal_ys = ys;
55  cal_dx = dx;
56  cal_dy = dy;
57 }
int cal_ys
Definition: touch.c:47
int cal_xs
Definition: touch.c:45
int cal_dy
Definition: touch.c:48
int cal_dx
Definition: touch.c:46

Here is the caller graph for this function:

void touch_set_value_convert_mode ( bool  use_calibration)

Set's the new value convert mode. Per default use_calibration is false.

Parameters
use_calibrationwhether or not the current platform needs display calibration

Definition at line 66 of file touch.c.

67 {
68  use_calibration = uc;
69 }
bool use_calibration
Definition: touch.c:42
void touch_unregister_area ( TOUCH_AREA_STRUCT area)

Unregisters a touch area. You will no longer receive events for this area

Parameters
areaA pointer to the TOUCH_AREA_STRUCT instance

Definition at line 195 of file touch.c.

196 {
197  if (area == NULL) {
198  return;
199  }
200 
201  //go through pointer array and find the area to remove
202  for (unsigned char i = 0; i < NUM_AREAS; i++) {
203  if (areas[i] == area) { //area found in pointer array at pos i
204  areas[i] = NULL; //set pointer in list to NULL again
205  break;
206  }
207  }
208 }
#define NUM_AREAS
Definition: touch.c:35
TOUCH_AREA_STRUCT * areas[NUM_AREAS]
Definition: touch.c:36

Here is the caller graph for this function: