display.c 3.53 KB
Newer Older
1
#include "display.h"
Gerd's avatar
Gerd committed
2
3
#include "Fonts/fonts.h"
#include "FreeRTOS.h"
4
#include "LCD_Driver.h"
5
6
7
8
9
10
#include "epicardium.h"
#include "gfx.h"
#include "gpio.h"
#include "task.h"
#include "tmr.h"
#include "tmr_utils.h"
Gerd's avatar
Gerd committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24

static TaskHandle_t lock = NULL;

static int check_lock()
{
	TaskHandle_t task = xTaskGetCurrentTaskHandle();
	if (task != lock) {
		return -EBUSY;
	} else {
		return 0;
	}
}

int epic_disp_print(
25
26
	int16_t posx,
	int16_t posy,
Gerd's avatar
Gerd committed
27
28
29
	const char *pString,
	uint16_t fg,
	uint16_t bg
30
31
32
33
34
35
36
37
38
39
40
41
) {
	return epic_disp_print_adv(DISP_FONT20, posx, posy, pString, fg, bg);
}

static const sFONT *font_map[] = {
	[DISP_FONT8] = &Font8,   [DISP_FONT12] = &Font12,
	[DISP_FONT16] = &Font16, [DISP_FONT20] = &Font20,
	[DISP_FONT24] = &Font24,
};

int epic_disp_print_adv(
	uint8_t font,
42
43
	int16_t posx,
	int16_t posy,
44
45
46
	const char *pString,
	uint16_t fg,
	uint16_t bg
Gerd's avatar
Gerd committed
47
48
) {
	int cl = check_lock();
49
50
51
	if (font >= (sizeof(font_map) / sizeof(sFONT *))) {
		return -EINVAL;
	}
Gerd's avatar
Gerd committed
52
53
54
	if (cl < 0) {
		return cl;
	} else {
55
56
57
58
59
60
61
62
63
		gfx_puts(
			font_map[font],
			&display_screen,
			posx,
			posy,
			pString,
			fg,
			bg
		);
Gerd's avatar
Gerd committed
64
65
66
67
68
69
70
71
72
73
		return 0;
	}
}

int epic_disp_clear(uint16_t color)
{
	int cl = check_lock();
	if (cl < 0) {
		return cl;
	} else {
74
		gfx_clear_to_color(&display_screen, color);
Gerd's avatar
Gerd committed
75
76
77
78
		return 0;
	}
}

79
int epic_disp_pixel(int16_t x, int16_t y, uint16_t color)
80
81
82
83
84
{
	int cl = check_lock();
	if (cl < 0) {
		return cl;
	} else {
85
		gfx_setpixel(&display_screen, x, y, color);
86
87
88
89
		return 0;
	}
}

Gerd's avatar
Gerd committed
90
int epic_disp_line(
91
92
93
94
	int16_t xstart,
	int16_t ystart,
	int16_t xend,
	int16_t yend,
Gerd's avatar
Gerd committed
95
	uint16_t color,
Rahix's avatar
Rahix committed
96
	enum disp_linestyle linestyle,
Gerd's avatar
Gerd committed
97
98
99
100
101
102
	uint16_t pixelsize
) {
	int cl = check_lock();
	if (cl < 0) {
		return cl;
	} else {
103
		/* TODO add linestyle support to gfx code */
104
		gfx_line(
105
106
107
108
109
110
111
			&display_screen,
			xstart,
			ystart,
			xend,
			yend,
			pixelsize,
			color
Gerd's avatar
Gerd committed
112
113
114
115
116
117
		);
		return 0;
	}
}

int epic_disp_rect(
118
119
120
121
	int16_t xstart,
	int16_t ystart,
	int16_t xend,
	int16_t yend,
Gerd's avatar
Gerd committed
122
	uint16_t color,
Rahix's avatar
Rahix committed
123
	enum disp_fillstyle fillstyle,
Gerd's avatar
Gerd committed
124
125
126
	uint16_t pixelsize
) {
	int cl = check_lock();
127
	if (cl < 0)
Gerd's avatar
Gerd committed
128
		return cl;
129
130
131
132
133
134
135
136
137
138
139

	switch (fillstyle) {
	case FILLSTYLE_EMPTY:
		gfx_rectangle(
			&display_screen,
			xstart,
			ystart,
			xend - xstart,
			yend - ystart,
			pixelsize,
			color
Gerd's avatar
Gerd committed
140
		);
141
142
143
144
145
146
147
148
149
150
151
		break;
	case FILLSTYLE_FILLED:
		gfx_rectangle_fill(
			&display_screen,
			xstart,
			ystart,
			xend - xstart,
			yend - ystart,
			color
		);
		break;
Gerd's avatar
Gerd committed
152
	}
153
	return 0;
Gerd's avatar
Gerd committed
154
155
156
}

int epic_disp_circ(
157
158
	int16_t x,
	int16_t y,
Gerd's avatar
Gerd committed
159
160
	uint16_t rad,
	uint16_t color,
Rahix's avatar
Rahix committed
161
	enum disp_fillstyle fillstyle,
Gerd's avatar
Gerd committed
162
163
164
	uint16_t pixelsize
) {
	int cl = check_lock();
165
	if (cl < 0)
Gerd's avatar
Gerd committed
166
		return cl;
167
168
169
170
171
172
173
174

	switch (fillstyle) {
	case FILLSTYLE_EMPTY:
		gfx_circle(&display_screen, x, y, rad, pixelsize, color);
		break;
	case FILLSTYLE_FILLED:
		gfx_circle_fill(&display_screen, x, y, rad, color);
		break;
Gerd's avatar
Gerd committed
175
	}
176
177

	return 0;
Gerd's avatar
Gerd committed
178
179
180
181
182
183
184
185
}

int epic_disp_update()
{
	int cl = check_lock();
	if (cl < 0) {
		return cl;
	}
186

187
	gfx_update(&display_screen);
188
189
190
191
192
193
194
195
196
197
198
199
	return 0;
}

int epic_disp_framebuffer(union disp_framebuffer *fb)
{
	int cl = check_lock();
	if (cl < 0) {
		return cl;
	}

	LCD_Set(fb->raw, sizeof(fb->raw));
	return 0;
Gerd's avatar
Gerd committed
200
201
}

202
203
int epic_disp_backlight(uint16_t brightness)
{
204
	/* TODO: lock? */
205
206
207
208
	LCD_SetBacklight(brightness);
	return 0;
}

Gerd's avatar
Gerd committed
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
int epic_disp_open()
{
	TaskHandle_t task = xTaskGetCurrentTaskHandle();
	if (lock == task) {
		return 0;
	} else if (lock == NULL) {
		lock = task;
		return 0;
	} else {
		return -EBUSY;
	}
}

int epic_disp_close()
{
	if (check_lock() < 0 && lock != NULL) {
		return -EBUSY;
	} else {
		lock = NULL;
		return 0;
	}
}

void disp_forcelock()
{
	TaskHandle_t task = xTaskGetCurrentTaskHandle();
	lock              = task;
}