Removed the u8g2 thing for now and the table component is "working"
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "ili9488CursesUI",
|
||||
"version": "0.1.1",
|
||||
"description": "Simple UI for creating interfaces with the ili9488 and arduinos",
|
||||
"keywords": "ili9488, pro micro, curses",
|
||||
"repository":
|
||||
{
|
||||
"type": "git",
|
||||
"url": "https://github.com/ESilva15/ili9488CursesUI"
|
||||
},
|
||||
"authors":
|
||||
[
|
||||
{
|
||||
"name": "Eduardo Silva",
|
||||
"maintainer": true
|
||||
}
|
||||
],
|
||||
"dependencies": {
|
||||
"Adafruit_GFX_Library": "~1.11.3",
|
||||
"Adafruit_BusIO": "1.16.1"
|
||||
},
|
||||
"frameworks": "*",
|
||||
"platforms": "*"
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
#include "UIComponent.h"
|
||||
// #include "u8g2.h"
|
||||
#include "Arduino.h"
|
||||
#include "HardwareSerial.h"
|
||||
#include "UIDrawing.h"
|
||||
// #include <U8g2lib.h>
|
||||
#include <cstdint>
|
||||
|
||||
#define DEBUG 1
|
||||
|
||||
UIElement::UIElement(Arduino_GFX *d, UIDimensions dims, UIDecorations *decor,
|
||||
char *title)
|
||||
: display(d), dims(dims), decor(decor), title(title) {}
|
||||
|
||||
UIDimensions::UIDimensions(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
|
||||
: x(x), y(y), width(w), height(h) {}
|
||||
|
||||
int16_t UIElement::getContentAreaX0() {
|
||||
if (this->decor->hasBorder) {
|
||||
return this->dims.x + DEFAULT_BORDER_THICKNESS + DEFAULT_MARGIN;
|
||||
}
|
||||
|
||||
return this->dims.x;
|
||||
}
|
||||
|
||||
int16_t UIElement::getContentAreaY0() {
|
||||
if (this->decor->hasBorder) {
|
||||
Serial2.print("Value to return: ");
|
||||
Serial2.print(this->dims.y);
|
||||
Serial2.print(" + ");
|
||||
Serial2.print(this->getTitleAreaHeight());
|
||||
Serial2.print(" + ");
|
||||
Serial2.print(DEFAULT_TITLE_CONTENT_SPACING);
|
||||
Serial2.print(" = ");
|
||||
Serial2.println(this->dims.y + this->getTitleAreaHeight() +
|
||||
DEFAULT_TITLE_CONTENT_SPACING);
|
||||
return this->dims.y + this->getTitleAreaHeight() +
|
||||
DEFAULT_TITLE_CONTENT_SPACING;
|
||||
}
|
||||
|
||||
return this->dims.y;
|
||||
}
|
||||
|
||||
uint16_t UIElement::getContentAreaHeight() {
|
||||
return this->dims.height - (DEFAULT_MARGIN * 2) - DEFAULT_BORDER_THICKNESS -
|
||||
this->getTitleAreaHeight();
|
||||
}
|
||||
|
||||
uint16_t UIElement::getContentAreaWidth() {
|
||||
return this->dims.width - ((DEFAULT_BORDER_THICKNESS + DEFAULT_MARGIN) * 2);
|
||||
}
|
||||
|
||||
int16_t UIElement::getTitleAreaX0() {
|
||||
return this->dims.x + DEFAULT_BORDER_THICKNESS + DEFAULT_MARGIN;
|
||||
}
|
||||
|
||||
int16_t UIElement::getTitleAreaY0() { return this->dims.y; }
|
||||
|
||||
uint16_t UIElement::getTitleAreaHeight() {
|
||||
int16_t x = 0, y = 0;
|
||||
uint16_t w = 0, h = 0;
|
||||
|
||||
this->display->setTextSize(this->decor->titleSize);
|
||||
this->display->getTextBounds((const char *)this->title, 0, 0, &x, &y, &w, &h);
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
uint16_t UIElement::getTitleAreaWidth() {
|
||||
int16_t x = 0, y = 0;
|
||||
uint16_t w = 0, h = 0;
|
||||
|
||||
this->display->setTextSize(this->decor->titleSize);
|
||||
this->display->getTextBounds((const char *)this->title, 0, 0, &x, &y, &w, &h);
|
||||
|
||||
return w;
|
||||
}
|
||||
|
||||
void UIElement::replaceString(char *oldVal, char *newVal) {
|
||||
size_t newValLen = strlen(newVal);
|
||||
size_t oldValLen = strlen(oldVal);
|
||||
|
||||
int16_t x0 = this->getContentAreaX0();
|
||||
int16_t y0 = this->getContentAreaY0();
|
||||
|
||||
this->display->setTextSize(this->decor->textSize);
|
||||
|
||||
// If the new value is shorter, delete the extra in advance
|
||||
if (oldValLen > newValLen) {
|
||||
// If the old value is longer than the new value, we have to delete the
|
||||
// extra of the old value, which is whatever starts at the new value len
|
||||
for (size_t start = newValLen; start < oldValLen; start++) {
|
||||
this->display->setCursor(x0 + start * (CHR_WIDTH(this->decor->textSize)),
|
||||
y0);
|
||||
this->display->setTextColor(this->decor->bgColor);
|
||||
this->display->print(oldVal[start]);
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t k = 0; k < newValLen; k++) {
|
||||
if (k <= oldValLen) {
|
||||
if (oldVal[k] != newVal[k]) {
|
||||
this->display->setCursor(x0 + k * (CHR_WIDTH(this->decor->textSize)),
|
||||
y0 + 0);
|
||||
this->display->setTextColor(this->decor->bgColor);
|
||||
this->display->print(oldVal[k]);
|
||||
}
|
||||
}
|
||||
|
||||
this->display->setCursor(x0 + k * (CHR_WIDTH(this->decor->textSize)),
|
||||
y0 + 0);
|
||||
this->display->setTextColor(this->decor->fgColor);
|
||||
this->display->print(newVal[k]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
#ifndef __UI_COMPONENT
|
||||
#define __UI_COMPONENT
|
||||
|
||||
#include "Arduino_GFX.h"
|
||||
#include "UIDecorations.h"
|
||||
#include <stdint.h>
|
||||
|
||||
enum ComponentType {
|
||||
STRING,
|
||||
TABLE,
|
||||
};
|
||||
|
||||
struct UIDimensions {
|
||||
uint16_t x = 0, y = 0;
|
||||
uint16_t width = 0, height = 0;
|
||||
|
||||
UIDimensions(uint16_t x, uint16_t y, uint16_t w, uint16_t h);
|
||||
};
|
||||
|
||||
struct UIElement {
|
||||
Arduino_GFX *display;
|
||||
UIDimensions dims;
|
||||
UIDecorations *decor;
|
||||
char *title;
|
||||
uint64_t lastUpdate = 0;
|
||||
uint16_t refreshRate = 0;
|
||||
ComponentType type;
|
||||
|
||||
UIElement(Arduino_GFX *d, UIDimensions dims, UIDecorations *decor,
|
||||
char *Title);
|
||||
|
||||
// Getters
|
||||
int16_t getContentAreaX0();
|
||||
int16_t getContentAreaY0();
|
||||
uint16_t getContentAreaHeight();
|
||||
uint16_t getContentAreaWidth();
|
||||
int16_t getTitleAreaX0();
|
||||
int16_t getTitleAreaY0();
|
||||
uint16_t getTitleAreaHeight();
|
||||
uint16_t getTitleAreaWidth();
|
||||
|
||||
// Positioning
|
||||
void horizontalCenter();
|
||||
void placeBelow(UIElement *reference);
|
||||
void placeRight(UIElement *reference);
|
||||
|
||||
// Drawing
|
||||
void drawBox();
|
||||
void noBox();
|
||||
void replaceString(char *oldVal, char *newVal);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,6 @@
|
||||
#include "UIDecorations.h"
|
||||
|
||||
UIDecorations::UIDecorations() {}
|
||||
UIDecorations::UIDecorations(uint16_t bg, uint16_t fg, uint16_t title,
|
||||
uint16_t border, uint8_t titleSize,
|
||||
uint8_t textSize) {}
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef __UI_DECORATIONS
|
||||
#define __UI_DECORATIONS
|
||||
|
||||
#include "UIDrawing.h"
|
||||
#include <stdint.h>
|
||||
|
||||
struct UIDecorations {
|
||||
bool hasBorder = true;
|
||||
uint16_t bgColor = MAIN_BG_COLOR, fgColor = MAIN_FG_COLOR;
|
||||
uint16_t titleColor = MAIN_FG_COLOR, borderColor = RED;
|
||||
uint8_t titleSize = 2, textSize = 4;
|
||||
|
||||
UIDecorations();
|
||||
UIDecorations(uint16_t bg, uint16_t fg, uint16_t title, uint16_t border,
|
||||
uint8_t titleSize, uint8_t textSize);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,94 @@
|
||||
#include "UIDrawing.h"
|
||||
#include "Arduino_GFX.h"
|
||||
#include "UIComponent.h"
|
||||
#include <cstdint>
|
||||
|
||||
/* TODO:
|
||||
* On the placement functions, change 5 to a variable or something
|
||||
*/
|
||||
|
||||
#define DEBUG
|
||||
|
||||
uint16_t calculateHeight(int16_t titleSize, int16_t textSize, int16_t nLines) {
|
||||
return CHR_HEIGHT(titleSize) + CHR_HEIGHT(textSize) +
|
||||
DEFAULT_TITLE_CONTENT_SPACING +
|
||||
(2 * (DEFAULT_BORDER_THICKNESS + DEFAULT_MARGIN));
|
||||
}
|
||||
|
||||
uint16_t calculateWidth(int16_t textSize, int16_t nChars) {
|
||||
return CHR_WIDTH(textSize) * nChars +
|
||||
(2 * (DEFAULT_MARGIN + DEFAULT_BORDER_THICKNESS));
|
||||
}
|
||||
|
||||
void UIElement::horizontalCenter() {
|
||||
uint16_t scrW = this->display->width();
|
||||
this->dims.x = (scrW - this->dims.width) / 2;
|
||||
}
|
||||
|
||||
// We can add a mode of alignment here, center, left, right, whatever
|
||||
void UIElement::placeBelow(UIElement *ref) {
|
||||
// For now the default alignment will be left until I need some other
|
||||
this->dims.x = ref->dims.x;
|
||||
this->dims.y = ref->dims.y + ref->dims.height + 5;
|
||||
}
|
||||
|
||||
void UIElement::placeRight(UIElement *ref) {
|
||||
this->dims.x = ref->dims.x + ref->dims.width + 5;
|
||||
}
|
||||
|
||||
void UIElement::drawBox() {
|
||||
// This is the border rectangle
|
||||
if (this->decor->hasBorder) {
|
||||
for (int k = 0; k < DEFAULT_BORDER_THICKNESS; k++) {
|
||||
this->display->drawRect(
|
||||
this->dims.x + k, this->dims.y + k, this->dims.width - (k * 2),
|
||||
this->dims.height - (k * 2), this->decor->borderColor);
|
||||
}
|
||||
}
|
||||
|
||||
// For debugging purpouses
|
||||
// title area
|
||||
int16_t x = 0, y = 0;
|
||||
uint16_t w = 0, h = 0;
|
||||
|
||||
// U8G2 u8g2; // No display pin setup needed since we're only using font data
|
||||
// u8g2.setFont(u8g2_font_9x18_tf);
|
||||
// int ascent = u8g2.getAscent();
|
||||
|
||||
h = this->getTitleAreaHeight();
|
||||
uint16_t titleAreaWidth = this->getTitleAreaWidth();
|
||||
int16_t titleTopLeftX = this->getTitleAreaX0();
|
||||
#ifdef DEBUG
|
||||
// Title area bounds
|
||||
this->display->drawRect(titleTopLeftX, this->getTitleAreaY0(), titleAreaWidth,
|
||||
h, GREEN);
|
||||
#endif
|
||||
|
||||
// content area
|
||||
x = this->getContentAreaX0();
|
||||
y = this->getContentAreaY0();
|
||||
w = this->getContentAreaWidth();
|
||||
h = this->getContentAreaHeight();
|
||||
|
||||
#ifdef DEBUG
|
||||
// Content area bounds
|
||||
this->display->drawRect(x, y, w, h, BLUE);
|
||||
#endif
|
||||
|
||||
if (this->decor->hasBorder) {
|
||||
// Remove the border line behind the title
|
||||
this->display->fillRect(titleTopLeftX - DEFAULT_MARGIN, this->dims.y,
|
||||
titleAreaWidth + DEFAULT_MARGIN,
|
||||
DEFAULT_BORDER_THICKNESS, MAIN_BG_COLOR);
|
||||
|
||||
// Render the title
|
||||
this->display->setCursor(titleTopLeftX, this->dims.y);
|
||||
this->display->print(this->title);
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
// Element bounds
|
||||
this->display->drawRect(this->dims.x, this->dims.y, this->dims.width,
|
||||
this->dims.height, PURPLE);
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef __CURSES_UI_VISUALS
|
||||
#define __CURSES_UI_VISUALS
|
||||
|
||||
#include <Arduino_GFX.h>
|
||||
|
||||
#define RIGHT 0
|
||||
#define INVERTED 1
|
||||
#define LEFT 2
|
||||
#define NORMAL 3
|
||||
|
||||
#define DEFAULT_BORDER_THICKNESS 3
|
||||
#define DEFAULT_TITLE_CONTENT_SPACING 3
|
||||
#define DEFAULT_MARGIN 3
|
||||
|
||||
#define MAIN_BG_COLOR RGB565(20, 10, 10)
|
||||
#define MAIN_FG_COLOR RGB565(255, 255, 255)
|
||||
|
||||
#define CHR_WIDTH(size) (6 * size)
|
||||
#define CHR_HEIGHT(size) (8 * size)
|
||||
#define CHR_SPACE(size) (size)
|
||||
|
||||
uint16_t calculateHeight(int16_t titleSize, int16_t textSize, int16_t nLines);
|
||||
uint16_t calculateWidth(int16_t textSize, int16_t nChars);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,42 @@
|
||||
#include "UIString.h"
|
||||
#include "UIComponent.h"
|
||||
#include <Arduino_GFX.h>
|
||||
|
||||
UIString::UIString() : UIElement(nullptr, {0, 0, 0, 0}, nullptr, (char *)"") {
|
||||
this->type = STRING;
|
||||
}
|
||||
|
||||
UIString::UIString(Arduino_GFX *d, UIDimensions dims, UIDecorations *decor,
|
||||
char *title)
|
||||
: UIElement(d, dims, decor, title) {
|
||||
this->type = STRING;
|
||||
}
|
||||
|
||||
void UIString::Update(const char *v) {
|
||||
uint64_t time = millis();
|
||||
if ((time - this->lastUpdate) <= this->refreshRate) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If the value is larger than our buffer - should add a way of knowing we
|
||||
// got an error here
|
||||
if (strlen(v) >= this->bufferSize) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If the value hasn't changed we do not need to re-render
|
||||
if (strcmp(this->value, v) == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
char newVal[this->bufferSize];
|
||||
sprintf(newVal, "%s", v);
|
||||
|
||||
char oldVal[this->bufferSize];
|
||||
sprintf(oldVal, "%s", this->value);
|
||||
|
||||
// Figure out the x0 and y0 for the text
|
||||
replaceString(oldVal, newVal);
|
||||
|
||||
strncpy(this->value, v, this->bufferSize);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef __UI_STRING
|
||||
#define __UI_STRING
|
||||
|
||||
#include "UIComponent.h"
|
||||
|
||||
// Representation of single line strings
|
||||
struct UIString : UIElement {
|
||||
static const size_t bufferSize = 256;
|
||||
char value[bufferSize];
|
||||
|
||||
UIString();
|
||||
UIString(Arduino_GFX *d, UIDimensions dims, UIDecorations *decor,
|
||||
char *title);
|
||||
void Update(const char *value);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,74 @@
|
||||
#include "UITable.h"
|
||||
#include "HardwareSerial.h"
|
||||
#include "UIDecorations.h"
|
||||
#include "UIDrawing.h"
|
||||
|
||||
#define DEBUG
|
||||
|
||||
UITable::UITable(Arduino_GFX *gfx, UIDimensions dims, UIDecorations *decor,
|
||||
char *title)
|
||||
: UIElement(gfx, dims, decor, title) {
|
||||
this->type = TABLE;
|
||||
|
||||
// Default decoration for every cell
|
||||
UIDecorations *cellDecor = new UIDecorations();
|
||||
|
||||
for (int k = 0; k < ROWS * COLUMNS; k++) {
|
||||
this->tableData[k].decor = cellDecor;
|
||||
this->tableData[k].dims.x = 0;
|
||||
this->tableData[k].dims.y = 0;
|
||||
this->tableData[k].dims.width = 0;
|
||||
this->tableData[k].dims.height = 0;
|
||||
this->tableData[k].display = this->display;
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t UITable::getContentAreaHeight() {
|
||||
return this->dims.height - (DEFAULT_MARGIN * 2) - DEFAULT_BORDER_THICKNESS -
|
||||
this->getTitleAreaHeight();
|
||||
}
|
||||
|
||||
/* Very shitty hardcoded function to get this going, only works for th
|
||||
* relative
|
||||
* POS NAME DELTA
|
||||
* 3 | 24 | 5
|
||||
*/
|
||||
void UITable::setup() {
|
||||
// Define the cell dimensions
|
||||
int tableCellW[] = {3 * (CHR_WIDTH(this->decor->textSize) + 2),
|
||||
24 * (CHR_WIDTH(this->decor->textSize) + 2),
|
||||
5 * (CHR_WIDTH(this->decor->textSize) + 2)};
|
||||
int cellH = CHR_HEIGHT(this->decor->textSize) + CELL_MARGIN;
|
||||
|
||||
// Initialize the cells
|
||||
uint16_t yOffset = this->getContentAreaY0();
|
||||
for (int r = 0; r < ROWS; r++) {
|
||||
int16_t xOffset = this->getContentAreaX0();
|
||||
for (int c = 0; c < COLUMNS; c++) {
|
||||
this->tableData[r * COLUMNS + c].decor->textSize = this->decor->textSize;
|
||||
this->tableData[r * COLUMNS + c].decor->hasBorder = false;
|
||||
this->tableData[r * COLUMNS + c].dims.x = xOffset + CELL_MARGIN;
|
||||
this->tableData[r * COLUMNS + c].dims.y = (cellH * r) + yOffset;
|
||||
this->tableData[r * COLUMNS + c].dims.height = cellH;
|
||||
this->tableData[r * COLUMNS + c].dims.width = tableCellW[c];
|
||||
|
||||
xOffset += tableCellW[c];
|
||||
|
||||
#ifdef DEBUG
|
||||
this->tableData[r * COLUMNS + c].drawBox();
|
||||
this->tableData[r * COLUMNS + c].Update("123");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize the table dimensions
|
||||
this->dims.height = ROWS * cellH + this->getTitleAreaHeight();
|
||||
this->dims.width =
|
||||
(3 + 24 + 5 + CELL_MARGIN) * CHR_WIDTH(this->decor->textSize);
|
||||
}
|
||||
|
||||
void UITable::initDimensions() {
|
||||
// Calculate the height
|
||||
|
||||
// Calculate the width
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#ifndef __UI_TABLE
|
||||
#define __UI_TABLE
|
||||
|
||||
#include "Arduino_GFX.h"
|
||||
#include "UIComponent.h"
|
||||
#include "UIString.h"
|
||||
|
||||
#define COLUMNS 3
|
||||
#define ROWS 5
|
||||
#define CELL_MARGIN 2
|
||||
|
||||
/* Very shitty table element, only works for the relative as of now
|
||||
*/
|
||||
struct UITable : UIElement {
|
||||
// will hold rows * columns UIStrings to fill the table
|
||||
int rows = ROWS;
|
||||
int columns = COLUMNS;
|
||||
UIString tableData[ROWS * COLUMNS];
|
||||
|
||||
UITable(Arduino_GFX *gfx, UIDimensions dims, UIDecorations *decor, char *t);
|
||||
|
||||
// void Update(StandingLine standings[5]);
|
||||
void initDimensions();
|
||||
void setup();
|
||||
|
||||
// Getters
|
||||
uint16_t getContentAreaHeight();
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user