Landtiger LPC1768 C BigLib 1
A self made, custom C library for the LandTiger board.
 
Loading...
Searching...
No Matches
touch.h File Reference
#include "glcd_types.h"
#include "types.h"

Go to the source code of this file.

Classes

struct  TP_Coordinate
 
struct  TP_ButtonArea
 

Functions

void TP_Init (bool skip_calibration)
 Initializes the touch panel.
 
bool TP_IsInitialized (void)
 
bool TP_IsCalibrated (void)
 
void TP_Calibrate (void)
 Blocks until calibration is completed successfully.
 
const TP_CoordinateTP_WaitForTouch (void)
 Blocks until a touch event is detected.
 
const LCD_CoordinateTP_GetLCDCoordinateFor (const TP_Coordinate *const tp_point)
 Converts the touch panel coordinates to LCD coordinates.
 
TP_ButtonArea TP_AssignButtonArea (LCD_Button button, LCD_Coordinate pos)
 Assigns a button area to the given button.
 
void TP_WaitForButtonPress (TP_ButtonArea button)
 Blocks until the given button area is pressed.
 
bool TP_HasButtonBeenPressed (TP_ButtonArea button, const TP_Coordinate *const touch_point)
 Checks if the given button area has been pressed during the touch event which occurred at the given touch point.
 

Function Documentation

◆ TP_AssignButtonArea()

TP_ButtonArea TP_AssignButtonArea ( LCD_Button  button,
LCD_Coordinate  pos 
)

Assigns a button area to the given button.

Parameters
buttonThe button to assign the area to.
posThe position of the button.
Returns
The button area.

Definition at line 436 of file touch.c.

437{
438 const LCD_Obj obj = {
439 .comps_size = 1,
440 .comps =
441 (LCD_Component[]){
443 .type = LCD_COMP_BUTTON,
444 .pos = pos,
445 .object.button = &button,
446 },
447 },
448 };
449
450 LCD_BBox bbox;
451 if (LCD_CalcBBoxForObject(&obj, &bbox) != LCD_ERR_OK)
452 {
453 return (TP_ButtonArea){0};
454 }
455
456 return (TP_ButtonArea){
457 .pos = pos,
458 .width = abs(bbox.bottom_right.x - bbox.top_left.x),
459 .height = abs(bbox.bottom_right.y - bbox.top_left.y),
460 };
461}
LCD_Error LCD_CalcBBoxForObject(const LCD_Obj *const obj, LCD_BBox *out_bbox)
Returns the bounding box of a temporary object.
Definition glcd.c:621
@ LCD_ERR_OK
No error occurred.
Definition glcd_errors.h:8
@ LCD_COMP_BUTTON
Definition glcd_types.h:163
LCD_Coordinate top_left
Definition glcd_types.h:62
LCD_Coordinate bottom_right
Definition glcd_types.h:62
Used to store a drawable component of any type.
Definition glcd_types.h:168
Represents a generic object, made up of 1 or more basic components, that are rendered on the screen.
Definition glcd_types.h:189
u8 comps_size
Definition glcd_types.h:192

◆ TP_Calibrate()

void TP_Calibrate ( void  )

Blocks until calibration is completed successfully.

Note
This function is automatically called if skip_calibration is false in TP_Init.

Definition at line 399 of file touch.c.

400{
401 if (!initialized)
402 return;
403
404 while (!calibrate())
405 ;
406}
_PRIVATE bool initialized
Definition touch.c:48
bool calibrate(void)
Definition touch.c:322

◆ TP_GetLCDCoordinateFor()

const LCD_Coordinate * TP_GetLCDCoordinateFor ( const TP_Coordinate *const  tp_point)

Converts the touch panel coordinates to LCD coordinates.

Parameters
tp_pointThe touch panel coordinates.
Returns
The LCD coordinates, or NULL if either the touch panel is not initialized or tp_point = NULL.

Definition at line 419 of file touch.c.

420{
421 if (!initialized || !tp_point)
422 return NULL;
423
424 static LCD_Coordinate lcd_point;
426 if (matrix.divider == 0 || !tp_point)
427 return NULL;
428
429 lcd_point.x = ((matrix.a_n * tp_point->x) + (matrix.b_n * tp_point->y) + matrix.c_n) / matrix.divider;
430 lcd_point.y = ((matrix.d_n * tp_point->x) + (matrix.e_n * tp_point->y) + matrix.f_n) / matrix.divider;
431 return &lcd_point;
432}
long double b_n
Definition touch.c:40
long double a_n
Definition touch.c:40
long double e_n
Definition touch.c:40
long double f_n
Definition touch.c:40
long double d_n
Definition touch.c:40
long double c_n
Definition touch.c:40
long double divider
Definition touch.c:40
_PRIVATE TP_CalibrationMatrix current_calib_matrix
Current calibration matrix.
Definition touch.c:46

◆ TP_HasButtonBeenPressed()

bool TP_HasButtonBeenPressed ( TP_ButtonArea  button,
const TP_Coordinate *const  touch_point 
)

Checks if the given button area has been pressed during the touch event which occurred at the given touch point.

Parameters
buttonThe button to check.
touch_pointThe touch point to check.
Returns
True if the button has been pressed, false otherwise.

Definition at line 475 of file touch.c.

476{
477 if (!touch_point)
478 return false;
479
480 // We should map the coordinates to the LCD coordinates, because the buttons pos
481 // is in LCD coordinates!
482 const LCD_Coordinate *touch_lcd_pos = TP_GetLCDCoordinateFor(touch_point);
483 if (!touch_lcd_pos)
484 return false;
485
486 const bool between_w = IS_BETWEEN_EQ(touch_lcd_pos->x, button.pos.x + 1, button.pos.x + button.width - 1);
487 const bool between_h = IS_BETWEEN_EQ(touch_lcd_pos->y, button.pos.y + 1, button.pos.y + button.height - 1);
488 return between_w && between_h;
489}
LCD_Coordinate pos
Definition touch.h:16
u16 height
Definition touch.h:17
u16 width
Definition touch.h:17
const LCD_Coordinate * TP_GetLCDCoordinateFor(const TP_Coordinate *const tp_point)
Converts the touch panel coordinates to LCD coordinates.
Definition touch.c:419
#define IS_BETWEEN_EQ(value, low, hi)
Definition types.h:23

◆ TP_Init()

void TP_Init ( bool  skip_calibration)

Initializes the touch panel.

Parameters
skip_calibrationIf true, calibration is skipped.
Note
If you skip calibration, the TP_WaitForTouch() could return incorrect values.

Definition at line 373 of file touch.c.

374{
375 LPC_GPIO0->FIODIR |= (1 << 6); // P0.6 CS is output
376 LPC_GPIO2->FIODIR |= (0 << 13); // P2.13 TP_INT is input
377 TP_CS(1);
378
379 spi_init(); // Initialize SPI interface for touch panel
380 initialized = true;
381
382 if (skip_calibration)
383 return;
384
385 TP_Calibrate();
386 // assert(calibratated && initialized); // Should never fail.
387}
_PRIVATE void spi_init(void)
Initializes the SPI interface for ADS7843.
Definition touch.c:95
#define TP_CS(a)
Definition touch.c:9
void TP_Calibrate(void)
Blocks until calibration is completed successfully.
Definition touch.c:399

◆ TP_IsCalibrated()

bool TP_IsCalibrated ( void  )

Definition at line 394 of file touch.c.

395{
396 return calibratated;
397}
_PRIVATE bool calibratated
Definition touch.c:48

◆ TP_IsInitialized()

bool TP_IsInitialized ( void  )

Definition at line 389 of file touch.c.

390{
391 return initialized;
392}

◆ TP_WaitForButtonPress()

void TP_WaitForButtonPress ( TP_ButtonArea  button)

Blocks until the given button area is pressed.

Parameters
buttonThe button to wait for.

Definition at line 463 of file touch.c.

464{
465 if (!initialized)
466 return;
467
468 TP_Coordinate *touch_point;
469 do
470 {
471 touch_point = (TP_Coordinate *)TP_WaitForTouch();
472 } while (!TP_HasButtonBeenPressed(button, touch_point));
473}
const TP_Coordinate * TP_WaitForTouch(void)
Blocks until a touch event is detected.
Definition touch.c:408
bool TP_HasButtonBeenPressed(TP_ButtonArea button, const TP_Coordinate *const touch_point)
Checks if the given button area has been pressed during the touch event which occurred at the given t...
Definition touch.c:475

◆ TP_WaitForTouch()

const TP_Coordinate * TP_WaitForTouch ( void  )

Blocks until a touch event is detected.

Returns
The X, Y coordinates of the touched point, or NULL if the touch panel is not initialized.
Note
If you call this function without first calibrating the panel, the returned value may be incorrect.

Definition at line 408 of file touch.c.

409{
410 if (!initialized)
411 return NULL;
412
413 static TP_Coordinate tp_coords;
414 while (!poll_touch(&tp_coords))
415 ;
416 return &tp_coords;
417}
_PRIVATE bool poll_touch(TP_Coordinate *out_tp_coords)
Definition touch.c:188