9#define TP_CS(a) ((a) ? (LPC_GPIO0->FIOSET = (1 << 6)) : (LPC_GPIO0->FIOCLR = (1 << 6)))
10#define TP_INT_IN (LPC_GPIO2->FIOPIN & (1 << 13))
13#define SPI_SPEED_4MHz 18
14#define SPI_SPEED_2MHz 36
15#define SPI_SPEED_1MHz 72
16#define SPI_SPEED_500kHz 144
17#define SPI_SPEED_400kHz 180
23#define SSP_SR_RECV_FIFO_NOT_EMPTY 2
27#define WAIT_IF_BUSY while (LPC_SSP1->SR & (1 << SSP_SR_BUSY))
34#define ABS(x) ((x) < 0 ? -(x) : (x))
40 long double a_n, b_n, c_n, d_n, e_n, f_n, divider;
55 for (
u32 i = 0; i < count; i++)
73 LPC_SSP1->CPSR = speed;
90 const u8 byte_returned = LPC_SSP1->DR;
101 LPC_PINCON->PINSEL0 &= ~((3UL << 14) | (3UL << 16) | (3UL << 18));
102 LPC_PINCON->PINSEL0 |= (2UL << 14) | (2UL << 16) | (2UL << 18);
105 LPC_SC->PCLKSEL0 &= ~(3 << 20);
106 LPC_SC->PCLKSEL0 |= (1 << 20);
110 LPC_SSP1->CR0 = 0x0007;
111 LPC_SSP1->CR1 = 0x0002;
142 return (buf >> 4) & 0xFFF;
178 if (!out_x || !out_y)
194 u16 buffer[2][9] = {{0}, {0}};
200 buffer[0][count] = tp_x;
201 buffer[1][count] = tp_y;
208 int m0, m1, m2, temp[3];
209 temp[0] = (buffer[0][0] + buffer[0][1] + buffer[0][2]) / 3;
210 temp[1] = (buffer[0][3] + buffer[0][4] + buffer[0][5]) / 3;
211 temp[2] = (buffer[0][6] + buffer[0][7] + buffer[0][8]) / 3;
212 m0 =
ABS(temp[0] - temp[1]);
213 m1 =
ABS(temp[1] - temp[2]);
214 m2 =
ABS(temp[2] - temp[0]);
221 out_tp_coords->
x = (temp[0] + temp[2]) / 2;
223 out_tp_coords->
x = (temp[0] + temp[1]) / 2;
226 out_tp_coords->
x = (temp[0] + temp[2]) / 2;
228 out_tp_coords->
x = (temp[1] + temp[2]) / 2;
230 temp[0] = (buffer[1][0] + buffer[1][1] + buffer[1][2]) / 3;
231 temp[1] = (buffer[1][3] + buffer[1][4] + buffer[1][5]) / 3;
232 temp[2] = (buffer[1][6] + buffer[1][7] + buffer[1][8]) / 3;
234 m0 =
ABS(temp[0] - temp[1]);
235 m1 =
ABS(temp[1] - temp[2]);
236 m2 =
ABS(temp[2] - temp[0]);
243 out_tp_coords->
y = (temp[0] + temp[2]) / 2;
245 out_tp_coords->
y = (temp[0] + temp[1]) / 2;
248 out_tp_coords->
y = (temp[0] + temp[2]) / 2;
250 out_tp_coords->
y = (temp[1] + temp[2]) / 2;
293 if (!lcd_3points || !tp_3points || !out_matrix)
296 long double divider = ((tp_3points[0].
x - tp_3points[2].
x) * (tp_3points[1].y - tp_3points[2].y)) -
297 ((tp_3points[1].x - tp_3points[2].x) * (tp_3points[0].
y - tp_3points[2].
y));
302 out_matrix->
a_n = ((lcd_3points[0].
x - lcd_3points[2].
x) * (tp_3points[1].y - tp_3points[2].y)) -
303 ((lcd_3points[1].x - lcd_3points[2].x) * (tp_3points[0].
y - tp_3points[2].
y));
304 out_matrix->
b_n = ((tp_3points[0].
x - tp_3points[2].
x) * (lcd_3points[1].x - lcd_3points[2].x)) -
305 ((lcd_3points[0].x - lcd_3points[2].x) * (tp_3points[1].
x - tp_3points[2].
x));
306 out_matrix->
c_n = (tp_3points[2].
x * lcd_3points[1].
x - tp_3points[1].
x * lcd_3points[2].
x) * tp_3points[0].y +
307 (tp_3points[0].x * lcd_3points[2].x - tp_3points[2].x * lcd_3points[0].x) * tp_3points[1].
y +
308 (tp_3points[1].
x * lcd_3points[0].
x - tp_3points[0].
x * lcd_3points[1].
x) * tp_3points[2].y;
309 out_matrix->
d_n = ((lcd_3points[0].
y - lcd_3points[2].
y) * (tp_3points[1].y - tp_3points[2].y)) -
310 ((lcd_3points[1].y - lcd_3points[2].y) * (tp_3points[0].
y - tp_3points[2].
y));
311 out_matrix->
e_n = ((tp_3points[0].
x - tp_3points[2].
x) * (lcd_3points[1].y - lcd_3points[2].y)) -
312 ((lcd_3points[0].y - lcd_3points[2].y) * (tp_3points[1].
x - tp_3points[2].
x));
313 out_matrix->
f_n = (tp_3points[2].
x * lcd_3points[1].
y - tp_3points[1].
x * lcd_3points[2].
y) * tp_3points[0].y +
314 (tp_3points[0].x * lcd_3points[2].y - tp_3points[2].x * lcd_3points[0].y) * tp_3points[1].
y +
315 (tp_3points[1].
x * lcd_3points[0].
y - tp_3points[0].
x * lcd_3points[1].
y) * tp_3points[2].y;
326 const LCD_Coordinate lcd_crosses[3] = {{45, 45}, {45, 270}, {190, 190}};
338 .text =
"Touch crosshairs to calibrate",
348 for (
u8 i = 0; i < 3; i++)
358 tp_crosses[i].
x = calib_coords->
x;
359 tp_crosses[i].
y = calib_coords->
y;
375 LPC_GPIO0->FIODIR |= (1 << 6);
376 LPC_GPIO2->FIODIR |= (0 << 13);
382 if (skip_calibration)
426 if (matrix.
divider == 0 || !tp_point)
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;
445 .object.button = &button,
488 return between_w && between_h;
bool LCD_IsInitialized(void)
Checks if the LCD has been initialized.
LCD_Error LCD_CalcBBoxForObject(const LCD_Obj *const obj, LCD_BBox *out_bbox)
Returns the bounding box of a temporary object.
LCD_Error LCD_SetBackgroundColor(LCD_Color color, bool redraw_objects)
Sets the background color of the screen.
LCD_Error LCD_RMRemove(LCD_ObjID id, bool redraw_underneath)
Removes an object from the render list by its ID. It also deallocates the memory used by that object ...
LCD_Error LCD_RMClear(void)
Removes all visible and non-visible objects from the screen.
LCD_Error LCD_RMRender(void)
Manually triggers an update of the screen. Useful when you want to add multiple objects at once,...
@ LCD_ERR_OK
No error occurred.
#define LCD_OBJECT(id,...)
#define LCD_TEXT2(x, y,...)
#define LCD_RECT2(x, y,...)
void POWER_TurnOnPeripheral(u8 bit)
Turns on a peripheral.
LCD_Coordinate bottom_right
Used to store a drawable component of any type.
Represents a generic object, made up of 1 or more basic components, that are rendered on the screen.
_PRIVATE u16 ads7843_read_y(void)
Reads the Y coordinate from the touch panel.
_PRIVATE TP_CalibrationMatrix current_calib_matrix
Current calibration matrix.
_PRIVATE u16 ads7843_read_x(void)
Reads the X coordinate from the touch panel.
bool TP_IsCalibrated(void)
_PRIVATE void draw_calibration_cross(u16 x, u16 y)
_PRIVATE bool poll_touch(TP_Coordinate *out_tp_coords)
_PRIVATE u8 spi_send_command(u8 cmd)
Writes a command to the ADS7843 trough SSP1 and returns the received byte.
_PRIVATE bool initialized
_PRIVATE void spi_init(void)
Initializes the SPI interface for ADS7843.
_PRIVATE bool calc_calibration_matrix(TP_CalibrationMatrix *out_matrix, const LCD_Coordinate *const lcd_3points, const TP_Coordinate *const tp_3points)
_PRIVATE LCD_ObjID calib_cross_obj_id
_PRIVATE void get_xy(u16 *out_x, u16 *out_y)
Returns the X, Y coordinates of the touched point.
#define SSP_SR_RECV_FIFO_NOT_EMPTY
_PRIVATE void delay_us(u32 count)
Delays for the specified number of microseconds.
bool TP_IsInitialized(void)
_PRIVATE u16 ads7843_read_adc(void)
Communicate (via SPI) with ADS7843 and read a 12-bit ADC (Analog to Digital Converter) value.
TP_ButtonArea TP_AssignButtonArea(LCD_Button button, LCD_Coordinate pos)
Assigns a button area to the given button.
_PRIVATE void spi_set_speed(u8 speed)
Sets SPI speed to the specified value.
_PRIVATE bool calibratated
const TP_Coordinate * TP_WaitForTouch(void)
Blocks until a touch event is detected.
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...
void TP_WaitForButtonPress(TP_ButtonArea button)
Blocks until the given button area is pressed.
void TP_Calibrate(void)
Blocks until calibration is completed successfully.
const LCD_Coordinate * TP_GetLCDCoordinateFor(const TP_Coordinate *const tp_point)
Converts the touch panel coordinates to LCD coordinates.
_PRIVATE void delete_calibration_cross(void)
void TP_Init(bool skip_calibration)
Initializes the touch panel.
#define IS_BETWEEN_EQ(value, low, hi)