display.c 3.08 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
25
26
27
28
29
30
31
32
33
34

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(
	uint16_t posx,
	uint16_t posy,
	const char *pString,
	uint16_t fg,
	uint16_t bg
) {
	int cl = check_lock();
	if (cl < 0) {
		return cl;
	} else {
35
		gfx_puts(&Font20, &display_screen, posx, posy, pString, fg, bg);
Gerd's avatar
Gerd committed
36
37
38
39
40
41
42
43
44
45
		return 0;
	}
}

int epic_disp_clear(uint16_t color)
{
	int cl = check_lock();
	if (cl < 0) {
		return cl;
	} else {
46
		gfx_clear_to_color(&display_screen, color);
Gerd's avatar
Gerd committed
47
48
49
50
		return 0;
	}
}

51
52
53
54
55
56
int epic_disp_pixel(uint16_t x, uint16_t y, uint16_t color)
{
	int cl = check_lock();
	if (cl < 0) {
		return cl;
	} else {
57
		gfx_setpixel(&display_screen, x, y, color);
58
59
60
61
		return 0;
	}
}

Gerd's avatar
Gerd committed
62
63
64
65
66
67
int epic_disp_line(
	uint16_t xstart,
	uint16_t ystart,
	uint16_t xend,
	uint16_t yend,
	uint16_t color,
Rahix's avatar
Rahix committed
68
	enum disp_linestyle linestyle,
Gerd's avatar
Gerd committed
69
70
71
72
73
74
	uint16_t pixelsize
) {
	int cl = check_lock();
	if (cl < 0) {
		return cl;
	} else {
75
		/* TODO add linestyle support to gfx code */
76
		gfx_line(
77
78
79
80
81
82
83
			&display_screen,
			xstart,
			ystart,
			xend,
			yend,
			pixelsize,
			color
Gerd's avatar
Gerd committed
84
85
86
87
88
89
90
91
92
93
94
		);
		return 0;
	}
}

int epic_disp_rect(
	uint16_t xstart,
	uint16_t ystart,
	uint16_t xend,
	uint16_t yend,
	uint16_t color,
Rahix's avatar
Rahix committed
95
	enum disp_fillstyle fillstyle,
Gerd's avatar
Gerd committed
96
97
98
	uint16_t pixelsize
) {
	int cl = check_lock();
99
	if (cl < 0)
Gerd's avatar
Gerd committed
100
		return cl;
101
102
103
104
105
106
107
108
109
110
111

	switch (fillstyle) {
	case FILLSTYLE_EMPTY:
		gfx_rectangle(
			&display_screen,
			xstart,
			ystart,
			xend - xstart,
			yend - ystart,
			pixelsize,
			color
Gerd's avatar
Gerd committed
112
		);
113
114
115
116
117
118
119
120
121
122
123
		break;
	case FILLSTYLE_FILLED:
		gfx_rectangle_fill(
			&display_screen,
			xstart,
			ystart,
			xend - xstart,
			yend - ystart,
			color
		);
		break;
Gerd's avatar
Gerd committed
124
	}
125
	return 0;
Gerd's avatar
Gerd committed
126
127
128
129
130
131
132
}

int epic_disp_circ(
	uint16_t x,
	uint16_t y,
	uint16_t rad,
	uint16_t color,
Rahix's avatar
Rahix committed
133
	enum disp_fillstyle fillstyle,
Gerd's avatar
Gerd committed
134
135
136
	uint16_t pixelsize
) {
	int cl = check_lock();
137
	if (cl < 0)
Gerd's avatar
Gerd committed
138
		return cl;
139
140
141
142
143
144
145
146

	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
147
	}
148
149

	return 0;
Gerd's avatar
Gerd committed
150
151
152
153
154
155
156
157
}

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

159
	gfx_update(&display_screen);
160
161
162
163
164
165
166
167
168
169
170
171
	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
172
173
}

174
175
int epic_disp_backlight(uint16_t brightness)
{
176
	/* TODO: lock? */
177
178
179
180
	LCD_SetBacklight(brightness);
	return 0;
}

Gerd's avatar
Gerd committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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;
}