-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlcd_graphic.c
More file actions
271 lines (228 loc) · 6.93 KB
/
Copy pathlcd_graphic.c
File metadata and controls
271 lines (228 loc) · 6.93 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// ============================================================================
// Copyright (c) 2013 by Terasic Technologies Inc.
// ============================================================================
//
// Permission:
//
// Terasic grants permission to use and modify this code for use
// in synthesis for all Terasic Development Boards and Altera Development
// Kits made by Terasic. Other use of this code, including the selling
// ,duplication, or modification of any portion is strictly prohibited.
//
// Disclaimer:
//
// This VHDL/Verilog or C/C++ source code is intended as a design reference
// which illustrates how these types of functions can be implemented.
// It is the user's responsibility to verify their design for
// consistency and functionality through the use of formal
// verification methods. Terasic provides no warranty regarding the use
// or functionality of this code.
//
// ============================================================================
//
// Terasic Technologies Inc
// 9F., No.176, Sec.2, Gongdao 5th Rd, East Dist, Hsinchu City, 30070. Taiwan
//
//
// web: http://www.terasic.com/
// email: support@terasic.com
//
// ============================================================================
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "hwlib.h"
#include "lcd_graphic.h"
#include "LCD_Lib.h"
#include "font.h"
//////////////////////////////////////////////
// lowest level API
void DRAW_Pixel(LCD_CANVAS *pCanvas, int X, int Y, int Color){
int nLine;
uint8_t *pFrame, Mask;
nLine = Y >> 3; //Y/8;
Mask = 0x01 << (Y % 8);
pFrame = pCanvas->pFrame + pCanvas->Width*nLine + X;
if (Color == 0x00)
*pFrame &= ~Mask;
else
*pFrame |= Mask;
}
////////////////////////////////////////////////
// high-level API for developer
// !!!! noe. this fucntion is LCD hardware depentdent
void DRAW_Refresh(LCD_CANVAS *pCanvas){
LCD_FrameCopy(pCanvas->pFrame);
}
void DRAW_Line(LCD_CANVAS *pCanvas, int X1, int Y1, int X2, int Y2, int Color){
int X_Start, X_End;
int Y_Start, Y_End;
int x,y, acc=0, inc, x_delta, y_delta;
if (X1 == X2){
if (Y1 <= Y2){
Y_Start = Y1;
Y_End = Y2;
}else{
Y_Start = Y2;
Y_End = Y1;
}
for(y=Y_Start;y<Y_End;y++)
DRAW_Pixel(pCanvas, X1, y, Color);
}else if (Y1 == Y2){
if (X1 <= X2){
X_Start = X1;
X_End = X2;
}else{
X_Start = X2;
X_End = X1;
}
for(x=X_Start;x<X_End;x++){
DRAW_Pixel(pCanvas, x, Y1, Color);
}
}else if (abs(X1-X2) >= abs(Y1-Y2)){
if (X1 <= X2){
X_Start = X1;
Y_Start = Y1;
X_End = X2;
Y_End = Y2;
}else{
X_Start = X2;
Y_Start = Y2;
X_End = X1;
Y_End = Y1;
}
x_delta = X_End - X_Start;
y_delta = Y_End - Y_Start;
inc = (y_delta >= 0)?1:-1;
y_delta = abs(y_delta);
acc = x_delta/2;
y = Y_Start;
for(x=X_Start;x<X_End;x++){
DRAW_Pixel(pCanvas, x, y, Color);
acc += y_delta;
if (acc >= x_delta){
y += inc;
acc -= x_delta;
}
}
}else{
if (Y1 <= Y2){
X_Start = X1;
Y_Start = Y1;
X_End = X2;
Y_End = Y2;
}else{
X_Start = X2;
Y_Start = Y2;
X_End = X1;
Y_End = Y1;
}
y_delta = Y_End - Y_Start;
x_delta = X_End - X_Start;
inc = (x_delta >= 0)?1:-1;
x_delta = abs(x_delta);
acc = y_delta/2;
x = X_Start;
for(y=Y_Start;y<Y_End;y++){
DRAW_Pixel(pCanvas, x, y, Color);
acc += x_delta;
if (acc >= y_delta){
x += inc;
acc -= y_delta;
}
}
}
}
void DRAW_Rect(LCD_CANVAS *pCanvas, int X1, int Y1, int X2, int Y2, int Color){
DRAW_Line(pCanvas, X1, Y1, X2, Y1, Color);
DRAW_Line(pCanvas, X2, Y1, X2, Y2, Color);
DRAW_Line(pCanvas, X2, Y2, X1, Y2, Color);
DRAW_Line(pCanvas, X1, Y2, X1, Y1, Color);
}
void DRAW_Circle(LCD_CANVAS *pCanvas, int x0, int y0, int Radius, int Color){
int x = Radius, y = 0;
int radiusError = 1-x;
while(x >= y)
{
DRAW_Pixel(pCanvas,x + x0, y + y0, Color);
DRAW_Pixel(pCanvas,y + x0, x + y0, Color);
DRAW_Pixel(pCanvas,-x + x0, y + y0, Color);
DRAW_Pixel(pCanvas,-y + x0, x + y0, Color);
DRAW_Pixel(pCanvas,-x + x0, -y + y0, Color);
DRAW_Pixel(pCanvas,-y + x0, -x + y0, Color);
DRAW_Pixel(pCanvas,x + x0, -y + y0, Color);
DRAW_Pixel(pCanvas,y + x0, -x + y0, Color);
y++;
if(radiusError<0)
radiusError+=2*y+1;
else
{
x--;
radiusError+=2*(y-x)+1;
}
}
}
void DRAW_Clear(LCD_CANVAS *pCanvas, int nValue){
#if 1
int y,x;
for(y=0;y<pCanvas->Height;y++){
for(x=0;x<pCanvas->Width;x++){
DRAW_Pixel(pCanvas, x, y, nValue);
}
}
#else
int y,x;
uint8_t *pFrame, Mask;
pFrame = pCanvas->pFrame;
memset(pFrame, 0x00, pCanvas->FrameSize);
for(y=0;y<pCanvas->Height;y++){
Mask = 0x80;
for(x=0;x<pCanvas->Width;x++){
if (nValue != 0x00){
*pFrame |= Mask;
}
Mask >>= 1;
if (Mask == 0x00){
Mask = 0x80;
pFrame++;
}
}
}
#endif
}
#ifdef SUPPORT_LCD_FONT
////////////////////////////////////////////////
/// FONT API ///////////////////////////////////
////////////////////////////////////////////////
void DRAW_PrintChar(LCD_CANVAS *pCanvas, int X0, int Y0, char Text, int Color, FONT_TABLE *font_table){
unsigned char *pFont;
uint8_t Mask;
int x, y, p;
for(y=0;y<2;y++){
Mask = 0x01;
for(p=0;p<8;p++){
pFont = font_table->pBitmap[(unsigned char)Text][y];
// pFont = font_table[y];
// pFont = (unsigned char *)&(font_table[Text][y][0]);
// pFont = &(font_courier_new_16x16[(unsigned char)Text][y][0]);
for(x=0;x<16;x++){
if (Mask & *pFont)
DRAW_Pixel(pCanvas, X0+x, Y0+y*8+p, Color);
pFont++;
}
Mask <<= 1;
}
}
}
void DRAW_PrintString(LCD_CANVAS *pCanvas, int X0, int Y0, char* pText, int Color, FONT_TABLE *font_table){
int nLen, i;
nLen = strlen(pText);
for(i=0;i<nLen;i++){
DRAW_PrintChar(pCanvas, X0+i*font_table->FontWidth, Y0, *(pText+i), Color, font_table);
}
}
#endif //SUPPORT_LCD_FONT