aboutsummaryrefslogtreecommitdiffstats
path: root/gui.c
blob: 1d162d54052cc40c8bbc2e8805a697530bce93b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "ch.h"
#include "hal.h"
#include "gui.h"
#include "glcd.h"
#include "touchpad.h"

uint16_t x, y;
unsigned char buffer[32];

static void TouchPadThread(uint16_t updateInterval) {
	chRegSetThreadName("GUI");

	while(TRUE) {
		x = tpReadX();
		y = tpReadY();

		chThdSleepMilliseconds(updateInterval);
	}
}

static void buttonThread(struct button_t *a) {
	uint16_t x0, y0, x1, y1;

	x0 = a->x0;
	y0 = a->y0;
	x1 = a->x1;
	y1 = a->y1;

	while(TRUE) {
		if(x >= x0 && x <= x1 && y >= y0 && y <= y1)
			*(a->state) = 1;
		else
			*(a->state) = 0;

		chThdSleepMilliseconds(a->interval);
	}
}

static void barThread(struct bar_t *a) {
	uint16_t percent = 0, value = 0, value_old = 0;

	while(TRUE) {
		percent = *(a->percent);
		if(percent > 100)
			percent = 100;	

		if(a->orientation == horizontal) {
			value = ((((a->x1)-(a->x0)) * percent) / 100);
			if(value_old > value || value == 0)
				lcdFillArea(a->x0+1, a->y0+1, a->x1, a->y1, a->bkColor);
			else
				lcdDrawRect(a->x0+1, a->y0+1, a->x0+value, a->y1, filled, a->valueColor);	
		} else if(a->orientation == vertical) {
			value = ((((a->y1)-(a->y0)) * percent) / 100);
			if(value_old > value || value == 0)
				lcdFillArea(a->x0+1, a->y0+1, a->x1, a->y1, a->bkColor);
			else
				lcdDrawRect(a->x0+1, a->y0+1, a->x1, a->y0+value, filled, a->valueColor);
		}

		value_old = value;
		chThdSleepMilliseconds(a->interval);
	}
}

void guiInit(uint16_t updateInterval) {
	Thread *tp = NULL;
	tp = chThdCreateFromHeap(NULL, THD_WA_SIZE(64), HIGHPRIO-1, TouchPadThread, updateInterval);
}

Thread *guiDrawButton(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, unsigned char *str, uint16_t fontColor, uint16_t buttonColor, uint16_t interval, uint8_t *state) {
	struct button_t *button;
	Thread *tp = NULL;

	button = chHeapAlloc(NULL, sizeof(struct button_t));
	button->x0 = x0;
	button->y0 = y0;
	button->x1 = x1;
	button->y1 = y1;
	button->state = state;
	button->interval = interval;

	lcdDrawRectString(x0, y0, x1, y1, str, fontColor, buttonColor);
	tp = chThdCreateFromHeap(NULL, THD_WA_SIZE(64), NORMALPRIO, buttonThread, button);

	return tp;
}

Thread *guiDrawBarGraph(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t orientation, uint16_t frameColor, uint16_t bkColor, uint16_t valueColor, uint16_t interval, uint16_t *percent) {
	struct bar_t *bar;
	Thread *tp = NULL;

	bar = chHeapAlloc(NULL, sizeof(struct bar_t));
	bar->x0 = x0;
	bar->y0 = y0;
	bar->x1 = x1;
	bar->y1 = y1;
	bar->orientation = orientation;
	bar->frameColor = frameColor;
	bar->bkColor = bkColor;
	bar->valueColor = valueColor;
	bar->percent = percent;
	bar->interval = interval;

	lcdDrawRect(x0, y0, x1, y1, frame, frameColor);
	tp = chThdCreateFromHeap(NULL, THD_WA_SIZE(64), NORMALPRIO, barThread, bar);

	return tp;
}