Skip to content

Commit

Permalink
refactor: improve readability of code
Browse files Browse the repository at this point in the history
Signed-off-by: Aditya Agarwal <[email protected]>
  • Loading branch information
Aditya-A-garwal committed Apr 8, 2024
1 parent 4eafe0d commit 117c731
Showing 1 changed file with 35 additions and 33 deletions.
68 changes: 35 additions & 33 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@

#include "constants.h"

const int CROSSHAIR_LEFT = CROSSHAIR_W / 2;
const int CROSSHAIR_RIGHT = DISPLAY_WIDTH - 1 - (CROSSHAIR_W / 2);
const int CROSSHAIR_TOP = CROSSHAIR_H / 2;
const int CROSSHAIR_BOTTOM = DISPLAY_HEIGHT - 1 - (CROSSHAIR_H / 2);

const int crosshairCoors[][2] = {
{0 + (CROSSHAIR_W >> 1), 0 + (CROSSHAIR_H >> 1)},
{DISPLAY_WIDTH - 1 - (CROSSHAIR_W >> 1), 0 + (CROSSHAIR_W >> 1)},
{0 + (CROSSHAIR_W >> 1), DISPLAY_HEIGHT - 1 - (CROSSHAIR_H >> 1)},
{DISPLAY_WIDTH - 1 - (CROSSHAIR_W >> 1), DISPLAY_HEIGHT - 1 - (CROSSHAIR_H >> 1)}
{CROSSHAIR_LEFT, CROSSHAIR_TOP},
{CROSSHAIR_RIGHT, CROSSHAIR_TOP},
{CROSSHAIR_LEFT, CROSSHAIR_BOTTOM},
{CROSSHAIR_RIGHT, CROSSHAIR_BOTTOM},
};

const int NUM_CROSSHAIRS = sizeof(crosshairCoors) / sizeof(crosshairCoors[0]);

MCUFRIEND_kbv tft;

TouchScreen ts(XP, YP, XM, YM, 300);
TSPoint p;

void
draw_crosshair(uint16_t x, uint16_t y, uint16_t clr) {
void draw_crosshair(uint16_t x, uint16_t y, uint16_t clr) {

const int x1 = x - (CROSSHAIR_W >> 1);
const int x2 = x + (CROSSHAIR_W >> 1);
Expand All @@ -35,39 +38,42 @@ draw_crosshair(uint16_t x, uint16_t y, uint16_t clr) {
tft.drawLine(x1, y2, x2, y1, clr);
}

void
toDisplayMode() {
void to_display_mode() {

pinMode(XM, OUTPUT);
pinMode(XP, OUTPUT);
pinMode(YM, OUTPUT);
pinMode(YP, OUTPUT);
}

bool
valid_touch() {
bool valid_touch() {

p = ts.getPoint();
if (p.z > PRESSURE_RIGHT || p.z < PRESSURE_LEFT) {
return false;
}
TSPoint p = ts.getPoint();
to_display_mode();

return true;
return (PRESSURE_LEFT <= p.z) && (p.z <= PRESSURE_RIGHT);
}

void
get_touch(uint16_t *xptr, uint16_t *yptr) {
void get_touch(uint16_t *xptr, uint16_t *yptr) {

TSPoint p;

while (!valid_touch());
while (1) {
p = ts.getPoint();
if ((PRESSURE_LEFT <= p.z) && (p.z <= PRESSURE_RIGHT)) {
break;
}
}

*xptr = p.x;
*yptr = p.y;

to_display_mode();
}

void
setup() {
void setup() {

uint16_t touchCoors[NUM_CROSSHAIRS][2];
uint16_t touch_coors[NUM_CROSSHAIRS][2];

uint16_t xbeginf = 0;
uint16_t xendf = 0;
Expand Down Expand Up @@ -95,25 +101,22 @@ setup() {
uint16_t tx;
uint16_t ty;

for (int _ = 0; _ < CALIBRATION_TOUCH_CNT; ++_) {
for (uint16_t _ = 0; _ < CALIBRATION_TOUCH_CNT; ++_) {

toDisplayMode();
draw_crosshair(cx, cy, RED);

get_touch(&tx, &ty);
ax += tx;
ay += ty;

toDisplayMode();
draw_crosshair(cx, cy, GREEN);

delay(CALIBRATION_TOUCH_PERIOD_MS);
}

touchCoors[i][0] = ax / CALIBRATION_TOUCH_CNT;
touchCoors[i][1] = ay / CALIBRATION_TOUCH_CNT;
touch_coors[i][0] = ax / CALIBRATION_TOUCH_CNT;
touch_coors[i][1] = ay / CALIBRATION_TOUCH_CNT;

toDisplayMode();
draw_crosshair(cx, cy, WHITE);

while (valid_touch());
Expand All @@ -125,8 +128,8 @@ setup() {
uint16_t cx = crosshairCoors[i][0];
uint16_t cy = crosshairCoors[i][1];

uint16_t tx = touchCoors[i][0];
uint16_t ty = touchCoors[i][1];
uint16_t tx = touch_coors[i][0];
uint16_t ty = touch_coors[i][1];

if (cx == (CROSSHAIR_W >> 1)) {
xbeginf += tx;
Expand Down Expand Up @@ -162,7 +165,7 @@ setup() {
Serial.print("const int YBEGIN = "); Serial.println(ybegin);
Serial.print("const int YEND = "); Serial.println(yend);

toDisplayMode();
to_display_mode();

tft.setTextSize(2);
tft.setTextColor(WHITE);
Expand All @@ -181,6 +184,5 @@ setup() {
tft.println(yend);
}

void
loop() {
void loop() {
}

0 comments on commit 117c731

Please sign in to comment.