aboutsummaryrefslogtreecommitdiffstats
path: root/gui.c
blob: 114979cc3eb17a27495f7e716062e9c82229be80 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "ch.h"
#include "hal.h"
#include "gui.h"
#include "glcd.h"
#include "touchpad.h"

volatile uint16_t x, y;

static msg_t 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 msg_t keymatrixThread(struct keymatrix_t *a) {
	uint16_t i, x0, y0, size, off;

	x0 = a->x0;
	y0 = a->y0;
	off = a->off;
	size = a->size;
	memset(a->number, 0, sizeof(a->number));

	while(TRUE) {

		for(i = 0; i < a->digits; i++) {
			while(tpIRQ());
			if(x >= x0 && x <= x0+size && y >= y0 && y <= y0+size)
				a->number[i] = 7;
			else if(x >= x0+off && x <= x0+off+size && y >= y0 && y <= y0+size)
				a->number[i] = 8;
			else if(x >= x0+off+off && x <= x0+off+off+size && y >= y0 && y <= y0+size)
				a->number[i] = 9;
			else if(x >= x0 && x <= x0+size && y >= y0+off && y <= y0+off+size)
				a->number[i] = 4;
			else if(x >= x0+off && x <= x0+off+size && y >= y0+off && y <= y0+off+size)
				a->number[i] = 5;
			else if(x >= x0+off+off && x <= x0+off+off+size && y >= y0+off && y <= y0+off+size)
				a->number[i] = 6;
			else if(x >= x0 && x <= x0+size && y >= y0+off+off && y <= y0+off+off+size)
				a->number[i] = 1;
			else if(x >= x0+off && x <= x0+off+size && y >= y0+off+off && y <= y0+off+off+size)
				a->number[i] = 2;
			else if(x >= x0+off+off && x <= x0+off+off+size && y >= y0+off+off && y <= y0+off+off+size)
				a->number[i] = 3;
			else if(x >= x0 && x <= x0+size && y >= y0+off+off+off && y <= y0+off+off+off+size)
				a->number[i] = 0;
			else if(x >= x0+off && x <= x0+off+size && y >= y0+off+off+off && y <= y0+off+off+off+size)
				a->number[i] = 0;
			else if(x >= x0+off+off && x <= x0+off+off+size && y >= y0+off+off+off && y <= y0+off+off+off+size)
				a->number[i] = 0;
			while(!tpIRQ());
			chThdSleepMilliseconds(a->interval);
		}
	}
}

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

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

		chThdSleepMilliseconds(updateInterval);
	}
}


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(512), NORMALPRIO, buttonThread, button);

	return tp;
}

Thread *guiDrawKeymatrix(uint16_t x0, uint16_t y0, uint16_t size, uint16_t space, uint16_t fontColor, uint16_t buttonColor, uint16_t interval, uint16_t digits, uint32_t *number) {
	struct keymatrix_t *keymatrix;
	Thread *tp = NULL;
	uint16_t off;
	uint8_t i, j;

	off = size+space;

	keymatrix = chHeapAlloc(NULL, sizeof(struct keymatrix_t));
	keymatrix->x0 = x0;
	keymatrix->y0 = y0;
	keymatrix->off = off;
	keymatrix->size = size;
	keymatrix->digits = digits;
	keymatrix->number = number;
	keymatrix->interval = interval;

	lcdDrawRectString(x0, y0, x0+size, y0+size, "7", fontColor, buttonColor);
	lcdDrawRectString(x0+off, y0, x0+off+size, y0+size, "8", fontColor, buttonColor);
	lcdDrawRectString(x0+off+off, y0, x0+off+off+size, y0+size, "9", fontColor, buttonColor);
	lcdDrawRectString(x0, y0+off, x0+size, y0+off+size, "4", fontColor, buttonColor);
	lcdDrawRectString(x0+off, y0+off, x0+off+size, y0+off+size, "5", fontColor, buttonColor);
	lcdDrawRectString(x0+off+off, y0+off, x0+off+off+size, y0+off+size, "6", fontColor, buttonColor);
	lcdDrawRectString(x0, y0+off+off, x0+size, y0+off+off+size, "1", fontColor, buttonColor);
	lcdDrawRectString(x0+off, y0+off+off, x0+off+size, y0+off+off+size, "2", fontColor, buttonColor);
	lcdDrawRectString(x0+off+off, y0+off+off, x0+off+off+size, y0+off+off+size, "3", fontColor, buttonColor);
	lcdDrawRectString(x0, y0+off+off+off, x0+size, y0+off+off+off+size, "*", fontColor, buttonColor);
	lcdDrawRectString(x0+off, y0+off+off+off, x0+off+size, y0+off+off+off+size, "0", fontColor, buttonColor);
	lcdDrawRectString(x0+off+off, y0+off+off+off, x0+off+off+size, y0+off+off+off+size, "#", fontColor, buttonColor);

	tp = chThdCreateFromHeap(NULL, THD_WA_SIZE(64), NORMALPRIO, keymatrixThread, keymatrix);

	return tp;
}