diff --git a/src/UIBar.cpp b/src/UIBar.cpp index 25737e6..b2dd6d9 100644 --- a/src/UIBar.cpp +++ b/src/UIBar.cpp @@ -1,6 +1,8 @@ #include "UIBar.h" +#include "UIComponent.h" #include "UIDrawing.h" +UIBar::UIBar() {}; UIBar::UIBar(Arduino_GFX *d, UIDimensions dims, UIDecorations decor, char *title) : UIElement(d, dims, decor, title) {} diff --git a/src/UIBar.h b/src/UIBar.h index 9321ba7..9bec301 100644 --- a/src/UIBar.h +++ b/src/UIBar.h @@ -9,6 +9,7 @@ struct UIBar : UIElement { uint32_t value = 0; // current tach value uint8_t range = 0; // define the tach range, ie: 8 for 8000rpm + UIBar(); UIBar(Arduino_GFX *d, UIDimensions dims, UIDecorations decor, char *title); void Update(char *val); diff --git a/src/UIComponent.cpp b/src/UIComponent.cpp index 7f7a6c6..1828718 100644 --- a/src/UIComponent.cpp +++ b/src/UIComponent.cpp @@ -1,4 +1,6 @@ #include "UIComponent.h" +#include "values.h" +#include "windowPool.h" // #include "u8g2.h" #include "Arduino.h" #include "HardwareSerial.h" @@ -130,7 +132,7 @@ void UIElement::replaceString(char *oldVal, char *newVal) { } } -bool UIElement::AddChild() { +bool UIElement::AddChild(ComponentType t) { if (childrenCount >= MAX_CHILDREN) { Serial2.println("Failed to add child: childrenCount exceeds MAX_CHILDREN"); Serial2.print( childrenCount); @@ -139,7 +141,7 @@ bool UIElement::AddChild() { return false; } - int16_t newElem = WindowPool::Allocate(); + int16_t newElem = WindowPool::Allocate(t); if (newElem < 0) { Serial2.println("Failed to allocate new component"); return false; @@ -160,56 +162,3 @@ std::uint8_t UIElement::ChildrenCount() { return this->childrenCount; } -namespace WindowPool { - UIElement pool[MAX_WINDOWS]; - bool inUse[MAX_WINDOWS] = {false}; - - static inline int16_t getNextIndex() { - for (int k = 0; k < MAX_WINDOWS; k++) { - if (!inUse[k]) { - return k; - } - } - - return -1; - } - - int16_t Allocate() { - int16_t nextIndex = getNextIndex(); - if (nextIndex < 0) { - Serial2.println("Failed to allocate comp: poolIndex exceeds MAX_WINDOWS"); - Serial2.print( nextIndex); - Serial2.print( " >= "); - Serial2.println( MAX_WINDOWS); - return -1; - } - - // Mark it as in use - inUse[nextIndex] = true; - - return nextIndex; - } - - void Deallocate(int16_t id) { - pool[id].SetTitle((char*)""); - pool[id].SetUIDimensions(UIDimensions(0, 0, 0, 0)); - pool[id].SetUIDecorations(UIDecorations()); - - inUse[id] = false; - } - - UIElement* GetHandle(int16_t id) { - return &pool[id]; - } - - void PrintInUse() { - for (int k = 0; k < MAX_WINDOWS; k++) { - if (inUse[k]) { - Serial2.print(k); - Serial2.print(" : "); - Serial2.println(inUse[k]); - } - } - } -} - diff --git a/src/UIComponent.h b/src/UIComponent.h index cccab39..163808e 100644 --- a/src/UIComponent.h +++ b/src/UIComponent.h @@ -1,21 +1,13 @@ -#ifndef __UI_COMPONENT -#define __UI_COMPONENT +#ifndef __UI_COMPONENT__ +#define __UI_COMPONENT__ #include "Arduino_GFX.h" +#include "values.h" #include "UIDecorations.h" #include "UIDimensions.h" #include #include -#define MAX_WINDOWS 512 -#define MAX_CHILDREN 32 -#define MAX_TITLE_LEN 32 - -enum ComponentType { - STRING, - TABLE, -}; - class UIElement { private: uint8_t childrenCount = 0; @@ -38,7 +30,7 @@ public: int16_t children[MAX_CHILDREN]; // Children Handling - bool AddChild(); + bool AddChild(ComponentType t); UIElement* GetChild(int16_t childID); uint8_t ChildrenCount(); @@ -71,18 +63,4 @@ public: void replaceString(char *oldVal, char *newVal); }; -namespace WindowPool { - extern UIElement pool[MAX_WINDOWS]; - extern bool inUse[MAX_WINDOWS]; - - int16_t Allocate(); - // I will finish this later on, still have to figure out how to properly - // destroy the UIComponent - void Deallocate(); - UIElement* GetHandle(int16_t index); - - // Debugging - void PrintInUse(); -}; - #endif diff --git a/src/UIScreen.cpp b/src/UIScreen.cpp index 49cf5da..cdab4e5 100644 --- a/src/UIScreen.cpp +++ b/src/UIScreen.cpp @@ -1,4 +1,5 @@ #include "UIScreen.h" +#include "windowPool.h" #include "UIComponent.h" #include "UIDecorations.h" #include "UIDimensions.h" @@ -10,7 +11,7 @@ Curses::Screen::Screen(Arduino_GFX *d, UIDimensions dims, void Curses::Screen::Setup(const char* title) { // This should always return the first available component - this->mainWindowID = WindowPool::Allocate(); + this->mainWindowID = WindowPool::Allocate(BASE); if (this->mainWindowID != 0) { // Something wrong happened Serial2.print("Unexpected ID for main window: "); diff --git a/src/UIString.h b/src/UIString.h index d294cdd..344602a 100644 --- a/src/UIString.h +++ b/src/UIString.h @@ -1,11 +1,11 @@ -#ifndef __UI_STRING -#define __UI_STRING +#ifndef __UI_STRING__ +#define __UI_STRING__ #include "UIComponent.h" // Representation of single line strings struct UIString : UIElement { - static const size_t bufferSize = 64; + static const std::size_t bufferSize = 64; char value[bufferSize]; UIString(); diff --git a/src/UITable.cpp b/src/UITable.cpp index ea760ed..07747b3 100644 --- a/src/UITable.cpp +++ b/src/UITable.cpp @@ -6,6 +6,7 @@ #define DEBUG +UITable::UITable() {}; UITable::UITable(Arduino_GFX *gfx, UIDimensions dims, UIDecorations decor, int nRows, int nCols, int *colWidths, char *title) : UIElement(gfx, dims, decor, title) { diff --git a/src/UITable.h b/src/UITable.h index 8008918..0a7e05b 100644 --- a/src/UITable.h +++ b/src/UITable.h @@ -18,6 +18,7 @@ struct UITable : UIElement { int *colWidths; UIString **tableData; + UITable(); UITable(Arduino_GFX *gfx, UIDimensions dims, UIDecorations decor, int nR, int nC, int *cWidth, char *t); diff --git a/src/values.h b/src/values.h new file mode 100644 index 0000000..7f4e1dd --- /dev/null +++ b/src/values.h @@ -0,0 +1,15 @@ +#ifndef __VALUES__ +#define __VALUES__ + +#define MAX_WINDOWS 512 +#define MAX_CHILDREN 32 +#define MAX_TITLE_LEN 32 + +enum ComponentType { + BASE, + BAR, + STRING, + TABLE, +}; + +#endif diff --git a/src/windowPool.cpp b/src/windowPool.cpp new file mode 100644 index 0000000..4345f8a --- /dev/null +++ b/src/windowPool.cpp @@ -0,0 +1,98 @@ +#include "windowPool.h" +#include "UIComponent.h" +#include "UIBar.h" +#include "UIString.h" +#include "UITable.h" +#include "values.h" + +namespace WindowPool { + WindowEntry pool[MAX_WINDOWS]; + bool inUse[MAX_WINDOWS] = {false}; + + static inline int16_t getNextIndex() { + for (int k = 0; k < MAX_WINDOWS; k++) { + if (!inUse[k]) { + return k; + } + } + + return -1; + } + + int16_t Allocate(ComponentType t) { + int16_t nextIndex = getNextIndex(); + if (nextIndex < 0) { + Serial2.println("Failed to allocate comp: poolIndex exceeds MAX_WINDOWS"); + Serial2.print( nextIndex); + Serial2.print( " >= "); + Serial2.println( MAX_WINDOWS); + return -1; + } + + pool[nextIndex].type = t; + + switch(pool[nextIndex].type) { + case BAR: + pool[nextIndex].bar = new UIBar(); + break; + case STRING: + pool[nextIndex].str = new UIString(); + break; + case TABLE: + pool[nextIndex].table = new UITable(); + break; + case BASE: + pool[nextIndex].base = new UIElement(); + break; + } + + // Mark it as in use + inUse[nextIndex] = true; + + return nextIndex; + } + + void Deallocate(int16_t id) { + switch(pool[id].type) { + case BAR: + break; + case STRING: + break; + case TABLE: + break; + case BASE: + break; + } + + inUse[id] = false; + } + + UIElement* GetHandle(int16_t id) { + switch(pool[id].type) { + case BAR: + return pool[id].bar; + break; + case STRING: + return pool[id].str; + break; + case TABLE: + return pool[id].table; + break; + case BASE: + return pool[id].base; + break; + } + + return nullptr; + } + + void PrintInUse() { + for (int k = 0; k < MAX_WINDOWS; k++) { + if (inUse[k]) { + Serial2.print(k); + Serial2.print(" : "); + Serial2.println(inUse[k]); + } + } + } +} diff --git a/src/windowPool.h b/src/windowPool.h new file mode 100644 index 0000000..df88642 --- /dev/null +++ b/src/windowPool.h @@ -0,0 +1,36 @@ +#ifndef __WINDOW_POOL__ +#define __WINDOW_POOL__ + +#include +#include "values.h" + +struct UIElement; +struct UIString; +struct UIBar; +struct UITable; + +namespace WindowPool { + struct WindowEntry { + ComponentType type; + union { + UIElement* base; + UIString* str; + UIBar* bar; + UITable* table; + }; + }; + + extern WindowEntry pool[MAX_WINDOWS]; + extern bool inUse[MAX_WINDOWS]; + + int16_t Allocate(ComponentType t); + // I will finish this later on, still have to figure out how to properly + // destroy the UIComponent + void Deallocate(); + UIElement* GetHandle(int16_t index); + + // Debugging + void PrintInUse(); +}; + +#endif