ugfx/gui.c

151 lines
3.1 KiB
C
Raw Normal View History

2012-06-07 12:37:29 +00:00
#include "gui.h"
2012-06-26 10:47:25 +00:00
static struct guiNode_t *firstGUI = NULL;
2012-06-07 12:37:29 +00:00
2012-06-26 10:47:25 +00:00
static uint16_t addNode(struct guiNode_t *newNode) {
struct guiNode_t *new;
2012-06-09 17:29:56 +00:00
2012-06-26 10:47:25 +00:00
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;
2012-06-09 17:29:56 +00:00
2012-06-26 10:47:25 +00:00
new->next = chHeapAlloc(NULL, sizeof(struct guiNode_t));
if(new->next == NULL)
return 0;
new = new->next;
*new = *newNode;
new->next = NULL;
2012-06-09 17:29:56 +00:00
}
2012-06-26 10:47:25 +00:00
return 1;
}
2012-06-09 13:41:28 +00:00
2012-06-26 10:47:25 +00:00
static void deleteNode(char *name) {
struct guiNode_t *pointer, *pointer1;
2012-06-09 13:41:28 +00:00
2012-06-26 10:47:25 +00:00
if(firstGUI != NULL) {
if(strcmp(firstGUI->name, name) == 0) {
pointer = firstGUI->next;
2012-06-26 14:22:08 +00:00
chHeapFree(firstGUI);
2012-06-26 10:47:25 +00:00
firstGUI = pointer;
} else {
2012-06-26 10:47:25 +00:00
pointer = firstGUI;
2012-06-09 13:41:28 +00:00
2012-06-26 10:47:25 +00:00
while(pointer->next != NULL) {
pointer1 = pointer->next;
2012-06-09 13:41:28 +00:00
2012-06-26 10:47:25 +00:00
if(strcmp(firstGUI->name, name) == 0) {
pointer->next = pointer1->next;
2012-06-26 14:22:08 +00:00
chHeapFree(pointer1);
2012-06-26 10:47:25 +00:00
break;
}
2012-06-09 17:29:35 +00:00
2012-06-26 10:47:25 +00:00
pointer = pointer1;
2012-06-26 10:47:25 +00:00
}
2012-06-09 17:29:35 +00:00
}
}
}
2012-06-07 12:37:29 +00:00
void guiPrintNodes(BaseSequentialStream *chp) {
2012-06-26 10:47:25 +00:00
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, "name: %s\r\n", pointer->name);
chprintf(chp, "active: %d\r\n", *(pointer->active));
chprintf(chp, "state: %d\r\n", *(pointer->state));
2012-06-26 10:47:25 +00:00
chprintf(chp, "*next: 0x%x\r\n", pointer->next);
chprintf(chp, "\r\n\n");
pointer = pointer->next;
}
2012-06-07 12:37:29 +00:00
}
2012-06-26 10:47:25 +00:00
static void guiThread(const uint16_t interval) {
uint16_t x, y;
struct guiNode_t *node;
2012-06-07 17:01:37 +00:00
2012-06-26 10:47:25 +00:00
chRegSetThreadName("GUI");
2012-06-07 17:01:37 +00:00
2012-06-26 10:47:25 +00:00
while(TRUE) {
2012-06-26 11:16:36 +00:00
for(node = firstGUI; node; node = node->next) {
2012-06-26 19:17:50 +00:00
// check if GUI element is active
2012-06-26 11:16:36 +00:00
if(*(node->active) == active) {
x = tpReadX();
y = tpReadY();
2012-06-26 19:17:50 +00:00
// we got a button
2012-06-26 18:47:35 +00:00
if(node->type == button) {
if(x >= node->x0 && x <= node->x1 && y >= node->y0 && y <= node->y1)
*(node->state) = 1;
else
*(node->state) = 0;
} else {
2012-06-26 11:16:36 +00:00
*(node->state) = 0;
2012-06-26 18:47:35 +00:00
}
2012-06-26 19:17:50 +00:00
// we got a slider
if(node->type == slider) {
}
// we got a keymatrix
if(node->type == keymatrix) {
}
2012-06-26 11:16:36 +00:00
}
}
2012-06-26 10:47:25 +00:00
chThdSleepMilliseconds(interval);
}
2012-06-09 13:41:28 +00:00
}
2012-06-26 11:50:12 +00:00
Thread *guiInit(uint16_t interval, tprio_t priority) {
2012-06-09 17:29:35 +00:00
Thread *tp = NULL;
2012-06-26 11:50:49 +00:00
tp = chThdCreateFromHeap(NULL, THD_WA_SIZE(512), priority, guiThread, interval);
2012-06-09 17:29:35 +00:00
return tp;
}
2012-06-26 10:47:25 +00:00
uint8_t guiDrawButton(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, char *str, uint16_t fontColor, uint16_t buttonColor, uint8_t *active, uint8_t *state) {
struct guiNode_t *newNode;
newNode = chHeapAlloc(NULL, sizeof(struct guiNode_t));
if(newNode == NULL)
return 0;
newNode->x0 = x0;
newNode->y0 = y0;
newNode->x1 = x1;
newNode->y1 = y1;
2012-06-26 18:47:35 +00:00
newNode->type = button;
2012-06-26 10:47:25 +00:00
newNode->name = str;
newNode->active = active;
newNode->state = state;
2012-06-26 11:46:40 +00:00
if(addNode(newNode) != 1)
return 0;
2012-06-26 10:47:25 +00:00
lcdDrawRectString(x0, y0, x1, y1, str, fontColor, buttonColor);
2012-06-26 11:58:41 +00:00
chHeapFree(newNode);
2012-06-26 10:47:25 +00:00
return 1;
}