Skip to content

Commit

Permalink
refactor: subclass all widgets from the widget class
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 Feb 17, 2024
1 parent eac554e commit b7de262
Show file tree
Hide file tree
Showing 13 changed files with 285 additions and 196 deletions.
36 changes: 16 additions & 20 deletions include/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,31 @@ const int YEND = 950;

// application constants

const uint16_t LOGO_X = 80;
const uint16_t LOGO_Y = 4;
const uint16_t LOGO_C = WHITE;
constexpr unsigned CANVAS_X = 4;
constexpr unsigned CANVAS_Y = 30;

constexpr unsigned CANVAS_W = 312;
constexpr unsigned CANVAS_H = 312;

const uint16_t CANVAS_W = 312;
const uint16_t CANVAS_H = 312;
constexpr unsigned CANVAS_BUFFER_W = 32;
constexpr unsigned CANVAS_BUFFER_H = 32;

const uint16_t CANVAS_X = 4;
const uint16_t CANVAS_Y = 30;
constexpr unsigned CANVAS_BUFFER_MAX_SEGMENTS = 18;
constexpr unsigned CLIENT_BUFFER_MAX_SEGMENTS = 105;


const uint16_t COLOR_SELECTOR_X = 40;
const uint16_t COLOR_SELECTOR_Y = CANVAS_Y + CANVAS_H + 30;
constexpr unsigned COLOR_SELECTOR_X = 40;
constexpr unsigned COLOR_SELECTOR_Y = CANVAS_Y + CANVAS_H + 30;

const uint16_t COLOR_SELECTOR_HPAD = 35;
const uint16_t COLOR_SELECTOR_VPAD = 35;
// const uint16_t COLOR_SELECTOR_HPAD = 35;
// const uint16_t COLOR_SELECTOR_VPAD = 35;

const uint16_t PAINT_RADIUS = 12;
// const uint16_t PAINT_RADIUS = 12;

constexpr unsigned THICKNESS_SELECTOR_X = 160;
constexpr unsigned THICKNESS_SELECTOR_Y = COLOR_SELECTOR_Y;

const uint16_t THICKNESS_SELECTOR_X = 160;
const uint16_t THICKNESS_SELECTOR_Y = COLOR_SELECTOR_Y;
const uint16_t THICKNESS_SELECTOR_PAD = 35;
// const uint16_t THICKNESS_SELECTOR_PAD = 35;


const uint16_t SAVE_X = 144;
Expand Down Expand Up @@ -106,10 +107,5 @@ const uint16_t SLOT_MENU_Y = COLOR_SELECTOR_Y - 8;
const uint16_t SLOT_MENU_W = SAVE_W * 3;
const uint16_t SLOT_MENU_H = SAVE_H * 4;

constexpr unsigned CANVAS_BUFFER_W = 32;
constexpr unsigned CANVAS_BUFFER_H = 32;

constexpr unsigned CANVAS_BUFFER_MAX_SEGMENTS = 18;
constexpr unsigned CLIENT_BUFFER_MAX_SEGMENTS = 105;

#endif
25 changes: 14 additions & 11 deletions include/widgets/button.h
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
#ifndef __WIDGETS_BUTTON_H__
#define __WIDGETS_BUTTON_H__

#include "Arduino.h"
#include "Adafruit_GFX.h"
#include "MCUFRIEND_kbv.h"
#include "widget.h"

class Button {
class Button : Widget {

MCUFRIEND_kbv *tft;

uint16_t x;
uint16_t y;
unsigned x;
unsigned y;

uint16_t w;
uint16_t h;
unsigned w;
unsigned h;

uint16_t color;

char *msg;

public:

Button(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color, char *msg, MCUFRIEND_kbv *tft);
Button(unsigned x, unsigned y, unsigned w, unsigned h, uint16_t color, char *msg, MCUFRIEND_kbv *tft);

void draw() const;
bool update(uint16_t touchX, uint16_t touchY);
void draw() const override;
void clear() const override;

bool update(unsigned touchX, unsigned touchY) override;

unsigned height() const override;
unsigned width() const override;

private:

Expand Down
55 changes: 27 additions & 28 deletions include/widgets/buttonGrid.h
Original file line number Diff line number Diff line change
@@ -1,52 +1,51 @@
#ifndef __WIDGETS_BUTTON_GRID_H__
#define __WIDGETS_BUTTON_GRID_H__

#include "Arduino.h"
#include "Adafruit_GFX.h"
#include "MCUFRIEND_kbv.h"

#include "constants.h"
#include "widget.h"

class ButtonGrid {

const char *msg = "SLOT"" ";
class ButtonGrid : Widget {

const uint16_t SLOT_CLOSE_X = 2;
const uint16_t SLOT_CLOSE_Y = 2;
const uint16_t SLOT_CLOSE_W = 13;
static constexpr char *msg = "SLOT"" ";

const uint16_t SLOT_MENU_C = GRAY;
static constexpr unsigned CLOSE_X = 2;
static constexpr unsigned CLOSE_Y = 2;
static constexpr unsigned CLOSE_W = 13;

const uint16_t SLOT_OPTION_X = 23;
const uint16_t SLOT_OPTION_Y = 8;
static constexpr unsigned HPAD = 23;
static constexpr unsigned VPAD = 8;

const uint16_t SLOT_OPTION_W = SAVE_W;
const uint16_t SLOT_OPTION_H = SAVE_H;
const uint16_t SLOT_OPTION_C = WHITE;
static constexpr uint16_t BUTTON_C = WHITE;

MCUFRIEND_kbv *tft;

uint16_t x;
uint16_t y;
unsigned x;
unsigned y;

uint16_t w;
uint16_t h;
unsigned w;
unsigned h;

uint16_t cc;
uint16_t rc;
unsigned cc;
unsigned rc;

uint16_t slot;
unsigned button_w;
unsigned button_h;

unsigned slot;

public:

ButtonGrid(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t c, uint16_t r, MCUFRIEND_kbv *tft);
ButtonGrid(unsigned x, unsigned y, unsigned w, unsigned h, unsigned cc, unsigned rc, unsigned button_w, unsigned button_h, MCUFRIEND_kbv *tft);

void draw() const override;
void clear() const override;

void draw() const;
bool update(uint16_t touchX, uint16_t touchY);
bool update(unsigned touch_x, unsigned touch_y) override;

void clear();
unsigned height() const override;
unsigned width() const override;

uint16_t getSlot() const;
unsigned getSlot() const;

private:

Expand Down
42 changes: 26 additions & 16 deletions include/widgets/canvas.h
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
#ifndef __WIDGETS_CANVAS_H__
#define __WIDGETS_CANVAS_H__

#include "Arduino.h"
#include "Adafruit_GFX.h"
#include "MCUFRIEND_kbv.h"
#include "widget.h"

class Canvas {
class Canvas : Widget {

MCUFRIEND_kbv *tft;

uint16_t x;
uint16_t y;
unsigned x;
unsigned y;

uint16_t w;
uint16_t h;
unsigned w;
unsigned h;

unsigned thickness;
uint16_t color;

public:

Canvas(uint16_t x, uint16_t y, uint16_t w, uint16_t h, MCUFRIEND_kbv *tft);
Canvas(unsigned x, unsigned y, unsigned w, unsigned h, MCUFRIEND_kbv *tft);

void draw() const override;
void clear() const override;

bool update(unsigned touch_x, unsigned touch_y) override;

unsigned height() const override;
unsigned width() const override;

unsigned heightInternal() const;
unsigned widthInternal() const;

void draw() const;
bool update(uint16_t touchX, uint16_t touchY, uint16_t curThickness, uint16_t curColor);
void setThickness(unsigned curThickness);
void setColor(uint16_t curColor);

void clear();
void clearDrawing();

uint16_t readPixel(uint16_t r, uint16_t c) const;
void writePixel(uint16_t r, uint16_t c, uint16_t color);
uint16_t readPixel(unsigned r, unsigned c) const;
void writePixel(unsigned r, unsigned c, uint16_t color);

uint16_t height() const;
uint16_t width() const;

private:

Expand Down
35 changes: 20 additions & 15 deletions include/widgets/colorSelector.h
Original file line number Diff line number Diff line change
@@ -1,38 +1,43 @@
#ifndef __WIDGETS_COLOR_SELECTOR_H__
#define __WIDGETS_COLOR_SELECTOR_H__

#include "Arduino.h"
#include "Adafruit_GFX.h"
#include "MCUFRIEND_kbv.h"
#include "widget.h"

#include "constants.h"

class ColorSelector {
class ColorSelector : Widget {

constexpr static uint16_t colors[3][3] {
constexpr static uint16_t COLORS[3][3] {
{RED, GREEN, BLUE},
{CYAN, MAGENTA, YELLOW},
{WHITE, GRAY, BLACK}
};

MCUFRIEND_kbv *tft;
constexpr static unsigned HPAD = 35;
constexpr static unsigned VPAD = 35;
constexpr static unsigned PAINT_RADIUS = 12;

uint16_t x;
uint16_t y;
constexpr static unsigned HEIGHT = (PAINT_RADIUS/2) + (3*HPAD);
constexpr static unsigned WIDTH = (PAINT_RADIUS/2) + (3*VPAD);

uint16_t hpad;
uint16_t vpad;
MCUFRIEND_kbv *tft;

uint16_t paintRadius;
unsigned x;
unsigned y;

uint16_t curColor = WHITE;
uint16_t curColor;

public:

ColorSelector(uint16_t x, uint16_t y, uint16_t hpad, uint16_t vpad, uint16_t paintRadius, MCUFRIEND_kbv *tft);
ColorSelector(unsigned x, unsigned y, MCUFRIEND_kbv *tft);

void draw() const override;
void clear() const override;

bool update(unsigned touch_x, unsigned touch_y) override;

void draw() const;
bool update(uint16_t touchX, uint16_t touchY);
unsigned height() const;
unsigned width() const;

uint16_t getColor() const;

Expand Down
32 changes: 19 additions & 13 deletions include/widgets/thicknessSelector.h
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
#ifndef __WIDGETS_THICKNESS_SELECTOR_H__
#define __WIDGETS_THICKNESS_SELECTOR_H__

#include "Arduino.h"
#include "Adafruit_GFX.h"
#include "MCUFRIEND_kbv.h"
#include "widget.h"

class ThicknessSelector {
class ThicknessSelector : Widget {

const uint16_t radii[4] = {3, 5, 7, 9};
static constexpr unsigned RADII[4] = {3, 5, 7, 9};
static constexpr unsigned PAD = 35;

MCUFRIEND_kbv *tft;
static constexpr unsigned HEIGHT = 0;
static constexpr unsigned WIDTH = 0;

uint16_t x;
uint16_t y;
MCUFRIEND_kbv *tft;

uint16_t pad;
unsigned x;
unsigned y;

uint16_t color;
uint16_t curSelected;
unsigned curSelected;

public:

ThicknessSelector(uint16_t x, uint16_t y, uint16_t pad, MCUFRIEND_kbv *tft);
ThicknessSelector(unsigned x, unsigned y, MCUFRIEND_kbv *tft);

void setColor(uint16_t clr);

void draw() const;
bool update(uint16_t touchX, uint16_t touchY);
void draw() const override;
void clear() const override;

bool update(unsigned touch_x, unsigned touch_y) override;

unsigned height() const override;
unsigned width() const override;

uint16_t getThickness() const;

private:
Expand Down
8 changes: 4 additions & 4 deletions src/canvasClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ void CanvasClient::loadCanvas(uint8_t id, Compressor<CANVAS_BUFFER_MAX_SEGMENTS>
return;
}

uint16_t imageHeight = canvas->height() - 2;
uint16_t imageWidth = canvas->width() - 2;
uint16_t imageHeight = canvas->heightInternal();
uint16_t imageWidth = canvas->widthInternal();

client.write("\x02", 1);
client.write(&id, 1);
Expand All @@ -86,7 +86,7 @@ void CanvasClient::loadCanvas(uint8_t id, Compressor<CANVAS_BUFFER_MAX_SEGMENTS>
}

for (uint16_t j = 0; j < imageWidth; ++j) {
canvas->writePixel(row + 1, j + 1, rowbuf.color[j]);
canvas->writePixel(row, j, rowbuf.color[j]);
}

for (uint16_t j = 0; j < imageWidth; ++j) {
Expand Down Expand Up @@ -125,7 +125,7 @@ void CanvasClient::saveCanvas(uint8_t id, Compressor<CANVAS_BUFFER_MAX_SEGMENTS>
}
else {
for (unsigned j = compressed[i].uncompress(rowbuf.code); j < imageWidth; ++j) {
rowbuf.code[j] = color2code(canvas->readPixel(i + 1, j + 1));
rowbuf.code[j] = color2code(canvas->readPixel(i, j));
}
compressable = compressor.compress(rowbuf.code, imageWidth);
}
Expand Down
Loading

0 comments on commit b7de262

Please sign in to comment.