Skip to content

Commit

Permalink
refactor: seperate gui library code from main code by moving to the l…
Browse files Browse the repository at this point in the history
…ib directory

Signed-off-by: Aditya Agarwal <[email protected]>
  • Loading branch information
Aditya-A-garwal committed Mar 21, 2024
1 parent df6664a commit 03a5ce9
Show file tree
Hide file tree
Showing 31 changed files with 168 additions and 172 deletions.
9 changes: 9 additions & 0 deletions include/touchscreen_constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "Arduino.h"

constexpr unsigned PRESSURE_LEFT = 10;
constexpr unsigned PRESSURE_RIGHT = 1400;

constexpr int XP = 8;
constexpr int XM = A2;
constexpr int YP = A3;
constexpr int YM = 9;
124 changes: 10 additions & 114 deletions include/touchscreen_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class Touchscreen {

// UNO R4 MINIMA
// UNO R4
constexpr static unsigned XBEGIN = 177;
constexpr static unsigned XEND = 863;
constexpr static unsigned YBEGIN = 121;
Expand Down Expand Up @@ -44,125 +44,21 @@ class Touchscreen {

Touchscreen() = delete;

Touchscreen(int xp, int yp, int xm, int ym)
: xp {xp}
, yp {yp}
, xm {xm}
, ym {ym}
, ts(xp, yp, xm, ym, 300)
{}
Touchscreen(int xp, int yp, int xm, int ym);

void set_dimensions(unsigned w, unsigned h) {
width = w;
height = h;
}
void set_dimensions(unsigned w, unsigned h);
void set_pressure(unsigned plo, unsigned phi);

void set_pressure(unsigned plo, unsigned phi) {
pressure_lo = plo;
pressure_hi = phi;
}
bool get_press(unsigned *x, unsigned *y);
bool get_release(unsigned *x, unsigned *y);
bool get_stylus_position(unsigned *x, unsigned *y);

bool get_press(unsigned *x, unsigned *y) {

if (!press_available) {
return false;
}

press_available = false;

*x = press_x;
*y = press_y;

return true;
}

bool get_release(unsigned *x, unsigned *y) {

if (!release_available) {
return false;
}

release_available = false;

*x = release_x;
*y = release_y;

return true;
}

bool get_stylus_position(unsigned *x, unsigned *y) {

if (!is_pressed) {
return false;
}

*x = cur_x;
*y = cur_y;

return true;
}

void read_screen() {

unsigned x, y;
bool flag {false};

p = ts.getPoint();
if (pressure_lo <= p.z && p.z <= pressure_hi) {

x = p.x, y = p.y;
convert(&x, &y);

flag = true;
}

if (!flag && is_pressed) { // new release

is_pressed = false;
release_available = true;

release_x = press_x;
release_y = press_y;
}
else if (flag) {
if (is_pressed) { // slide (should be ignored)

release_available = false;
press_available = false;
}
else { // new press

is_pressed = true;
press_available = true;

press_x = x;
press_y = y;
}

cur_x = x;
cur_y = y;
}

reset();
}
void read_screen();

private:

void reset() {
pinMode(xp, OUTPUT);
pinMode(yp, OUTPUT);
pinMode(xm, OUTPUT);
pinMode(ym, OUTPUT);
}

void convert(unsigned *x, unsigned *y) {

*x = constrain(*x, XBEGIN, XEND);
*y = constrain(*y, YBEGIN, YEND);

*x = map(*x, XBEGIN, XEND, 0, width - 1);
*y = map(*y, YBEGIN, YEND, height - 1, 0);
}
void reset();
void convert(unsigned *x, unsigned *y);
};

#endif
17 changes: 1 addition & 16 deletions include/constants.h → lib/gui/include/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,4 @@ constexpr uint8_t color_2_code(uint16_t color) {
}
}


// touch constants

constexpr unsigned PRESSURE_LEFT = 10;
constexpr unsigned PRESSURE_RIGHT = 1400;

constexpr int XP = 8;
constexpr int XM = A2;
constexpr int YP = A3;
constexpr int YM = 9;

// application constants

constexpr unsigned MAX_WIFI_RETRY = 2;

#endif
#endif
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class DrawableCanvas : public BasicWidget {

constexpr static uint16_t DRAWABLE_W = WIDTH - 2;
constexpr static uint16_t DRAWABLE_H = HEIGHT - 2;
constexpr static unsigned MAX_INLINE_COMPRESSED_SEGMENTS = 7;
constexpr static unsigned MAX_INLINE_COMPRESSED_SEGMENTS = 9;

protected:

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions lib/gui/library.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"dependencies": {
"adafruit/Adafruit GFX Library": "^1.11.9",
"MCUFRIEND_kbv": "https://github.com/slviajero/MCUFRIEND_kbv.git"
},
"build": {
"libarchive": false
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ platform = https://github.com/platformio/platform-renesas-ra.git
board = uno_r4_wifi
framework = arduino
lib_deps =
adafruit/Adafruit GFX Library@^1.11.9
adafruit/Adafruit TouchScreen@^1.1.5
https://github.com/slviajero/MCUFRIEND_kbv.git
adafruit/Adafruit GFX Library@^1.11.9
; https://github.com/slviajero/MCUFRIEND_kbv.git
; build_type=debug
55 changes: 16 additions & 39 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
#include "Arduino.h"

#include "WiFiS3.h"

#include "Adafruit_GFX.h"
#include "MCUFRIEND_kbv.h"
#include "touchscreen_constants.h"
#include "touchscreen_driver.h"

#include "constants.h"
#include "bitmaps.h"

#include "touchscreen_driver.h"

#include "widgets/app.h"
#include "widgets/view.h"
#include "widgets/drawablecanvas.h"
Expand Down Expand Up @@ -754,21 +750,8 @@ void init_information_view() {

void setup() {

// HeapStats_t stats;

Serial.begin(9600);

// Serial.println("-----");
// vPortGetHeapStats(&stats);
// Serial.print("Available Heap Space: "); Serial.println(stats.xAvailableHeapSpaceInBytes);
// Serial.print("Minimum Ever Free Bytes: "); Serial.println(stats.xMinimumEverFreeBytesRemaining);
// Serial.print("Number of Free blocks: "); Serial.println(stats.xNumberOfFreeBlocks);
// Serial.print("Number of successful allocs: "); Serial.println(stats.xNumberOfSuccessfulAllocations);
// Serial.print("Number of successful frees: "); Serial.println(stats.xNumberOfSuccessfulFrees);
// Serial.print("Size of largest free block: "); Serial.println(stats.xSizeOfLargestFreeBlockInBytes);
// Serial.print("Size of smallest free block: "); Serial.println(stats.xSizeOfSmallestFreeBlockInBytes);
// Serial.println("-----");

tft.begin(0x9486);

ts.set_dimensions(tft.width(), tft.height());
Expand All @@ -786,17 +769,6 @@ void setup() {

app->make_active_view(startup_view);
app->draw();

// Serial.println("-----");
// vPortGetHeapStats(&stats);
// Serial.print("Available Heap Space: "); Serial.println(stats.xAvailableHeapSpaceInBytes);
// Serial.print("Minimum Ever Free Bytes: "); Serial.println(stats.xMinimumEverFreeBytesRemaining);
// Serial.print("Number of Free blocks: "); Serial.println(stats.xNumberOfFreeBlocks);
// Serial.print("Number of successful allocs: "); Serial.println(stats.xNumberOfSuccessfulAllocations);
// Serial.print("Number of successful frees: "); Serial.println(stats.xNumberOfSuccessfulFrees);
// Serial.print("Size of largest free block: "); Serial.println(stats.xSizeOfLargestFreeBlockInBytes);
// Serial.print("Size of smallest free block: "); Serial.println(stats.xSizeOfSmallestFreeBlockInBytes);
// Serial.println("-----");
}

void loop() {
Expand Down Expand Up @@ -832,7 +804,6 @@ void loop() {

[[noreturn]]
void err(const char msg[]) {

Serial.println(msg);
for (;;);
}
Expand Down Expand Up @@ -1088,11 +1059,11 @@ void connect_cb(unsigned *args) {
bool verify_server() {

char addr[15 + 1 + 5 + 1];
unsigned addr_len;
unsigned addr_len {0};

char *ip;
char *port;
char *delim;
char *ip {nullptr};
char *port {nullptr};
char *delim {nullptr};

unsigned server_port;

Expand All @@ -1110,6 +1081,7 @@ bool verify_server() {
return false;
}


ip = addr;
port = delim + 1;
*delim = NULL;
Expand All @@ -1125,21 +1097,23 @@ bool verify_server() {
server_port = temp;
}
{
char *saveptr;
unsigned num_dots;
unsigned len;
signed temp;
char *saveptr {nullptr};
unsigned num_dots {0};
unsigned len {0};
signed temp {0};

for (char *ptr = ip; *ptr != NULL; ++ptr) {
if (*ptr == '.') {
++num_dots;
}
}


if (num_dots != 3) {
return false;
}


for (char *ptr = strtok_r(ip, ".", &saveptr); ptr != nullptr; ptr = strtok_r(NULL, ".", &saveptr)) {

if (ptr != ip) {
Expand All @@ -1163,6 +1137,7 @@ bool verify_server() {
}
}
}

}

canvas->set_server_addr(ip, server_port);
Expand All @@ -1171,6 +1146,8 @@ bool verify_server() {

void try_connect(unsigned *args) {

constexpr static unsigned MAX_WIFI_RETRY = 2;

char ssid[17];
char pass[17];

Expand Down
Loading

0 comments on commit 03a5ce9

Please sign in to comment.