From 096701a6ad8f2ba5512aff4ee430ca0d626fff7a Mon Sep 17 00:00:00 2001 From: Tectu Date: Wed, 18 Jul 2012 03:42:05 +0200 Subject: [PATCH] restructorizing --- console.c => glcd/console.c | 0 console.h => glcd/console.h | 0 fastMath.c => glcd/fastMath.c | 0 fastMath.h => glcd/fastMath.h | 0 fonts.c => glcd/fonts.c | 0 fonts.h => glcd/fonts.h | 0 glcd.c => glcd/glcd.c | 0 glcd.h => glcd/glcd.h | 0 glcd/glcd.mk | 6 + worker.h => glcd/worker.h | 0 graph.c => graph/graph.c | 0 graph.h => graph/graph.h | 0 graph/graph.mk | 3 + gui/gui.c | 311 ++++++++++++++++++++++++++++++ gui/gui.h | 95 +++++++++ gui/gui.mk | 3 + lcd.mk | 17 +- touchpad.c => touchpad/touchpad.c | 0 touchpad.h => touchpad/touchpad.h | 0 touchpad/touchpad.mk | 3 + 20 files changed, 431 insertions(+), 7 deletions(-) rename console.c => glcd/console.c (100%) rename console.h => glcd/console.h (100%) rename fastMath.c => glcd/fastMath.c (100%) rename fastMath.h => glcd/fastMath.h (100%) rename fonts.c => glcd/fonts.c (100%) rename fonts.h => glcd/fonts.h (100%) rename glcd.c => glcd/glcd.c (100%) rename glcd.h => glcd/glcd.h (100%) create mode 100644 glcd/glcd.mk rename worker.h => glcd/worker.h (100%) rename graph.c => graph/graph.c (100%) rename graph.h => graph/graph.h (100%) create mode 100644 graph/graph.mk create mode 100644 gui/gui.c create mode 100644 gui/gui.h create mode 100644 gui/gui.mk rename touchpad.c => touchpad/touchpad.c (100%) rename touchpad.h => touchpad/touchpad.h (100%) create mode 100644 touchpad/touchpad.mk diff --git a/console.c b/glcd/console.c similarity index 100% rename from console.c rename to glcd/console.c diff --git a/console.h b/glcd/console.h similarity index 100% rename from console.h rename to glcd/console.h diff --git a/fastMath.c b/glcd/fastMath.c similarity index 100% rename from fastMath.c rename to glcd/fastMath.c diff --git a/fastMath.h b/glcd/fastMath.h similarity index 100% rename from fastMath.h rename to glcd/fastMath.h diff --git a/fonts.c b/glcd/fonts.c similarity index 100% rename from fonts.c rename to glcd/fonts.c diff --git a/fonts.h b/glcd/fonts.h similarity index 100% rename from fonts.h rename to glcd/fonts.h diff --git a/glcd.c b/glcd/glcd.c similarity index 100% rename from glcd.c rename to glcd/glcd.c diff --git a/glcd.h b/glcd/glcd.h similarity index 100% rename from glcd.h rename to glcd/glcd.h diff --git a/glcd/glcd.mk b/glcd/glcd.mk new file mode 100644 index 00000000..47d2cf09 --- /dev/null +++ b/glcd/glcd.mk @@ -0,0 +1,6 @@ +LCD_GLCD_SRC = $(LCDLIB)/glcd/glcd.c \ + $(LCDLIB)/glcd/fonts.c \ + $(LCDLIB)/glcd/fastMath.c \ + $(LCDLIB)/glcd/console.c + +LCD_GLCD_INC = $(LCDLIB)/glcd diff --git a/worker.h b/glcd/worker.h similarity index 100% rename from worker.h rename to glcd/worker.h diff --git a/graph.c b/graph/graph.c similarity index 100% rename from graph.c rename to graph/graph.c diff --git a/graph.h b/graph/graph.h similarity index 100% rename from graph.h rename to graph/graph.h diff --git a/graph/graph.mk b/graph/graph.mk new file mode 100644 index 00000000..706a9412 --- /dev/null +++ b/graph/graph.mk @@ -0,0 +1,3 @@ +LCD_GRAPH_SRC = $(LCDLIB)/graph/graph.c + +LCD_GRAPH_INC = $(LCDLIB)/graph diff --git a/gui/gui.c b/gui/gui.c new file mode 100644 index 00000000..3190a885 --- /dev/null +++ b/gui/gui.c @@ -0,0 +1,311 @@ +#include "gui.h" + +static struct guiNode_t *firstGUI = NULL; +uint16_t x, y; // global touchpad coordinates + +static uint8_t addElement(struct guiNode_t *newNode) { + struct guiNode_t *new; + + if(firstGUI == NULL) { + firstGUI = chHeapAlloc(NULL, sizeof(struct guiNode_t)); + if(firstGUI == NULL) + return 0; + + *firstGUI = *newNode; + firstGUI->next = NULL; + } else { + new = firstGUI; + while(new->next != NULL) + new = new->next; + + new->next = chHeapAlloc(NULL, sizeof(struct guiNode_t)); + if(new->next == NULL) + return 0; + + new = new->next; + *new = *newNode; + new->next = NULL; + } + + return 1; +} + +static uint8_t deleteElement(char *label) { + struct guiNode_t *pointer, *pointer1; + + if(firstGUI != NULL) { + if(strcmp(firstGUI->label, label) == 0) { + pointer = firstGUI->next; + chHeapFree(firstGUI); + firstGUI = pointer; + } else { + pointer = firstGUI; + + while(pointer->next != NULL) { + pointer1 = pointer->next; + + if(strcmp(firstGUI->label, label) == 0) { + pointer->next = pointer1->next; + chHeapFree(pointer1); + break; + } + + pointer = pointer1; + + } + } + + return 1; // successful + } + + return 0; // not successful +} + +void guiPrintElements(BaseSequentialStream *chp) { + struct guiNode_t *pointer = firstGUI; + + chprintf(chp, "\r\n\nguiNodes:\r\n\n"); + + while(pointer != NULL) { + chprintf(chp, "x0: %d\r\n", pointer->x0); + chprintf(chp, "y0: %d\r\n", pointer->y0); + chprintf(chp, "x1: %d\r\n", pointer->x1); + chprintf(chp, "y1: %d\r\n", pointer->y1); + chprintf(chp, "label: %s\r\n", pointer->label); + chprintf(chp, "active: %d\r\n", *(pointer->active)); + chprintf(chp, "state: %d\r\n", *(pointer->state)); + chprintf(chp, "*next: 0x%x\r\n", pointer->next); + chprintf(chp, "\r\n\n"); + pointer = pointer->next; + } +} + +static inline void buttonUpdate(struct guiNode_t *node) { + if(x >= node->x0 && x <= node->x1 && y >= node->y0 && y <= node->y1) { + *(node->state) = 1; + } else { + *(node->state) = 0; + } +} + +static inline void sliderUpdate(struct guiNode_t *node) { + uint16_t length; + + if(node->orientation == horizontal) + length = node->x1 - node->x0; + else if(node->orientation == vertical) + length = node->y1 - node->y0; + + if(node->mode == modePassive) { + node->percentage = *(node->state); + } else if(node->mode == modeActive) { + if(x >= node->x0 && x <= node->x1 && y >= node->y0 && y <= node->y1) { + if(node->orientation == horizontal) { + node->percentage = (((x - node->x0) * 100) / length); + } else if(node->orientation == vertical) { + node->percentage = (((y - node->y0) * 100) / length); + } + } + + *(node->state) = node->percentage; + } + + // a bit of safety here + if(node->percentage > 100) + node->percentage = 100; + if(node->percentage < 0) + node->percentage = 0; + + if(node->orientation == horizontal) { + node->value = ((((node->x1)-(node->x0)) * node->percentage) / 100); + if(node->oldValue > node->value || node->value == 0) + lcdFillArea(node->x0+1, node->y0+1, node->x1, node->y1, node->bkColor); + else + lcdDrawRect(node->x0+1, node->y0+1, node->x0+node->value, node->y1, filled, node->valueColor); + } else if(node->orientation == vertical) { + node->value = ((((node->y1)-(node->y0)) * node->percentage) / 100); + if(node->oldValue > node->value || node->value == 0) + lcdFillArea(node->x0+1, node->y0+1, node->x1, node->y1, node->bkColor); + else + lcdDrawRect(node->x0+1, node->y0+1, node->x1, node->y0+node->value, filled, node->valueColor); + } + + node->oldValue = node->value; +} + +static inline void wheelUpdate(struct guiNode_t *node) { + (void)node; +} + +static inline void keymatrixUpdate(struct guiNode_t *node) { + (void)node; +} + +static void guiThread(const uint16_t interval) { + struct guiNode_t *node; + + chRegSetThreadName("GUI"); + + while(TRUE) { + for(node = firstGUI; node; node = node->next) { + // check if GUI element is set active + if(*(node->active) == active) { + x = tpReadX(); + y = tpReadY(); + + switch(node->type) { + case button: + buttonUpdate(node); + break; + case slider: + sliderUpdate(node); + break; + case wheel: + wheelUpdate(node); + break; + case keymatrix: + keymatrixUpdate(node); + break; + } + } + } + + chThdSleepMilliseconds(interval); + } +} + +Thread *guiInit(uint16_t interval, tprio_t priority) { + Thread *tp = NULL; + + tp = chThdCreateFromHeap(NULL, THD_WA_SIZE(512), priority, guiThread, interval); + + return tp; +} + +uint8_t guiDeleteElement(char *label) { + return deleteElement(label); +} + +uint8_t guiDrawButton(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, char *str, font_t font, uint16_t fontColor, uint16_t buttonColor, uint16_t shadow, char *label, uint8_t *active, uint8_t *state) { + struct guiNode_t *newNode; + uint16_t i; + + newNode = chHeapAlloc(NULL, sizeof(struct guiNode_t)); + if(newNode == NULL) + return 0; + + newNode->type = button; + newNode->label = label; + newNode->x0 = x0; + newNode->y0 = y0; + newNode->x1 = x1; + newNode->y1 = y1; + newNode->shadow = shadow; + newNode->active = active; + newNode->state = state; + + if(addElement(newNode) != 1) + return 0; + + lcdDrawRectString(x0, y0, x1, y1, str, font, fontColor, buttonColor); + + if(shadow != 0) { + for(i = 0; i < shadow; i++) { + lcdDrawLine(x0+shadow, y1+i, x1+shadow-1, y1+i, Black); + lcdDrawLine(x1+i, y0+shadow, x1+i, y1+shadow-1, Black); + } + } + + chHeapFree(newNode); + + return 1; +} + +uint8_t guiDrawSlider(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint8_t orientation, uint8_t mode, uint16_t frameColor, uint16_t bkColor, uint16_t valueColor, char *label, uint8_t *active, uint8_t *value) { + struct guiNode_t *newNode; + + newNode = chHeapAlloc(NULL, sizeof(struct guiNode_t)); + if(newNode == NULL) + return 0; + + newNode->type = slider; + newNode->label = label; + newNode->x0 = x0; + newNode->y0 = y0; + newNode->x1 = x1; + newNode->y1 = y1; + newNode->mode = mode; + newNode->bkColor = bkColor; + newNode->valueColor = valueColor; + newNode->state = value; + newNode->active = active; + newNode->orientation = orientation; + newNode->percentage = 0; + + if(addElement(newNode) != 1) + return 0; + + (void)bkColor; + (void)valueColor; + + // lcdDraw functions + lcdDrawRect(x0, y0, x1, y1, frame, frameColor); + + chHeapFree(newNode); + + return 1; +} + +uint8_t guiDrawWheel(uint16_t x0, uint16_t y0, uint16_t radius1, uint16_t radius2, uint16_t bkColor, uint16_t valueColor, char *label, uint8_t *active, uint8_t *value) { + struct guiNode_t *newNode; + + newNode = chHeapAlloc(NULL, sizeof(struct guiNode_t)); + if(newNode == NULL) + return 0; + + newNode->type = wheel; + newNode->label = label; + newNode->x0 = x0; + newNode->y0 = y0; + newNode->r1 = radius1; + newNode->r2 = radius2; + newNode->active = active; + newNode->state = value; + + if(addElement(newNode) != 1) + return 0; + + (void)bkColor; + (void)valueColor; + // lcdDraw functions + + chHeapFree(newNode); + + return 1; +} + +uint8_t guiDrawKeymatrix(uint16_t x0, uint16_t y0, uint16_t size, uint16_t space, uint16_t shadow, uint16_t buttonColor, uint16_t fontColor, font_t font, char *label, uint8_t *active, uint8_t *value) { + struct guiNode_t *newNode; + + newNode = chHeapAlloc(NULL, sizeof(struct guiNode_t)); + if(newNode == NULL) + return 0; + + newNode->type = keymatrix; + newNode->label = label; + newNode->x0 = x0; + newNode->y0 = y0; + newNode->shadow = shadow; + newNode->active = active; + newNode->state = value; + + if(addElement(newNode) != 1) + return 0; + + // lcdDraw functions + + chHeapFree(newNode); + + return 1; +} + diff --git a/gui/gui.h b/gui/gui.h new file mode 100644 index 00000000..1e3b0a6c --- /dev/null +++ b/gui/gui.h @@ -0,0 +1,95 @@ +#ifndef GUI_H +#define GUI_H + +#include "ch.h" +#include "hal.h" +#include "glcd.h" +#include "chprintf.h" +#include "touchpad.h" +#include + +struct guiNode_t { + uint8_t type; + uint16_t x0; + uint16_t y0; + uint16_t x1; + uint16_t y1; + uint16_t r1; + uint16_t r2; + uint16_t shadow; + uint16_t bkColor; + uint16_t valueColor; + uint16_t value; + uint16_t oldValue; + uint16_t percentage; + uint8_t orientation; + uint8_t mode; + uint8_t *active; + uint8_t *state; + char *label; + struct guiNode_t *next; +}; + +#ifdef __cplusplus +extern "C" { +#endif + +enum {button, slider, wheel, keymatrix}; +enum {horizontal, vertical}; +enum {inactive, active}; +enum {modePassive, modeActive}; + +/* + * Description: creates the GUI thread + * + * param: - interval: thread sleep in milliseconds after each GUI element update + * - priority: priority of the thread + * + * return: pointer to created thread + */ +Thread *guiInit(uint16_t interval, tprio_t priority); + +/* + * Description: prints all GUI elements structs (linked list) + * + * param: - chp: pointer to output stream + * + * return: none + */ +void guiPrintElements(BaseSequentialStream *chp); + +/* + * Description: deletes a GUI element from the linked list + * + * param: - label: label of the element (parameter of each guiDrawXXX function) + * + * return: 1 if successful, 0 otherwise + */ +uint8_t guiDeleteElement(char *label); + +/* + * Description: draws a button on the screen and keeps it's state up to date + * + * param: - x0, y0, x1, y1: start and end coordinates of the button's rectangle + * - str: string that gets drawn into the rectangle - button's lable + * - fontColor: color of the lable + * - buttonColor: color of the rectangle + * - shadow: draws a black shadow with N pixels size if != 0 + * - active: pass pointer to variable which holds the state 'active' or 'inactive' + * - state: pass pointer to variable whcih will keep the state of the button (pressed / unpressed)' + * + * return: 1 if button successfully created + */ +uint8_t guiDrawButton(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, char *str, font_t font, uint16_t fontColor, uint16_t buttonColor, uint16_t shadow, char *label, uint8_t *active, uint8_t *state); + +uint8_t guiDrawSlider(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint8_t orientation, uint8_t mode, uint16_t frameColor, uint16_t bkColor, uint16_t valueColor, char *label, uint8_t *active, uint8_t *value); + +uint8_t guiDrawWheel(uint16_t x0, uint16_t y0, uint16_t radius1, uint16_t radius2, uint16_t bkColor, uint16_t valueColor, char *label, uint8_t *active, uint8_t *value); + +uint8_t guiDrawKeymatrix(uint16_t x0, uint16_t y0, uint16_t buttonSize, uint16_t space, uint16_t shadow, uint16_t buttonColor, uint16_t fontColor, font_t font, char *label, uint8_t *active, uint8_t *value); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/gui/gui.mk b/gui/gui.mk new file mode 100644 index 00000000..e25cbd73 --- /dev/null +++ b/gui/gui.mk @@ -0,0 +1,3 @@ +LCD_GUI_SRC = $(LCDLIB)/gui/gui.c + +LCD_GUI_INC = $(LCDLIB)/gui diff --git a/lcd.mk b/lcd.mk index 882bb64d..3354330d 100644 --- a/lcd.mk +++ b/lcd.mk @@ -1,17 +1,20 @@ LCDLIB = $(CHIBIOS)/ext/lcd include $(LCDLIB)/drivers/drivers.mk +include $(LCDLIB)/glcd/glcd.mk +include $(LCDLIB)/touchpad/touchpad.mk +include $(LCDLIB)/graph/graph.mk include $(LCDLIB)/gui/gui.mk -LCDSRC = $(LCDLIB)/glcd.c \ - $(LCDLIB)/fonts.c \ - $(LCDLIB)/touchpad.c \ - $(LCDLIB)/graph.c \ - $(LCDLIB)/console.c \ - $(LCDLIB)/fastMath.c \ - $(LCD_DRIVERS_SRC) \ +LCDSRC = $(LCD_DRIVERS_SRC) \ + $(LCD_GLCD_SRC) \ + $(LCD_TOUCHPAD_SRC) \ + $(LCD_GRAPH_SRC) \ $(LCD_GUI_SRC) LCDINC = $(LCDLIB) \ $(LCD_DRIVERS_INC) \ + $(LCD_GLCD_INC) \ + $(LCD_TOUCHPAD_INC) \ + $(LCD_GRAPH_INC) \ $(LCD_GUI_INC) diff --git a/touchpad.c b/touchpad/touchpad.c similarity index 100% rename from touchpad.c rename to touchpad/touchpad.c diff --git a/touchpad.h b/touchpad/touchpad.h similarity index 100% rename from touchpad.h rename to touchpad/touchpad.h diff --git a/touchpad/touchpad.mk b/touchpad/touchpad.mk new file mode 100644 index 00000000..de27ae0d --- /dev/null +++ b/touchpad/touchpad.mk @@ -0,0 +1,3 @@ +LCD_TOUCHPAD_SRC = $(LCDLIB)/touchpad/touchpad.c + +LCD_TOUCHPAD_INC = $(LCDLIB)/touchpad