]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_MPU_LPC1768_GCC_RedSuite/src/LCD/lcd.c
First Red Suite project for FreeRTOS MPU.
[freertos] / Demo / CORTEX_MPU_LPC1768_GCC_RedSuite / src / LCD / lcd.c
1 //*****************************************************************************\r
2 //   +--+       \r
3 //   | ++----+   \r
4 //   +-++    |  \r
5 //     |     |  \r
6 //   +-+--+  |   \r
7 //   | +--+--+  \r
8 //   +----+    Copyright (c) 2009 Code Red Technologies Ltd. \r
9 //\r
10 // lcd.c contains various routines to plot to the LCD display on the RDB1768\r
11 // development board.\r
12 //\r
13 // Software License Agreement\r
14 // \r
15 // The software is owned by Code Red Technologies and/or its suppliers, and is \r
16 // protected under applicable copyright laws.  All rights are reserved.  Any \r
17 // use in violation of the foregoing restrictions may subject the user to criminal \r
18 // sanctions under applicable laws, as well as to civil liability for the breach \r
19 // of the terms and conditions of this license.\r
20 // \r
21 // THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED\r
22 // OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF\r
23 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.\r
24 // USE OF THIS SOFTWARE FOR COMMERCIAL DEVELOPMENT AND/OR EDUCATION IS SUBJECT\r
25 // TO A CURRENT END USER LICENSE AGREEMENT (COMMERCIAL OR EDUCATIONAL) WITH\r
26 // CODE RED TECHNOLOGIES LTD. \r
27 \r
28 #include "lcd_commands.h"\r
29 #include "lcd.h"\r
30 #include "lcd_driver.h"\r
31 #include "font.h"\r
32 \r
33 #include <stdlib.h>             // to provice abs() function\r
34 \r
35 // Routine to draw a filled rectangle to the LCD.\r
36 // Two corners of rectangle are at (xmin,ymin) and (xmax,ymax).\r
37 // The Rectangle is filled with the RGB565 color specified\r
38 void LCD_FilledRect(int xmin,int xmax,int ymin,int ymax,int color)\r
39 {\r
40     int i;\r
41 \r
42     // Specify to LCD controller coordinates we are writing to...\r
43     LCDdriver_WriteCom(DD_CASET);       // Set the column address\r
44     LCDdriver_WriteData(xmin);          // min address\r
45     LCDdriver_WriteData(xmax);          // max address\r
46     LCDdriver_WriteCom(DD_RASET);       // Set the row address\r
47     LCDdriver_WriteData(ymin + 1);      // min address\r
48     LCDdriver_WriteData(ymax + 1);      // max address\r
49     LCDdriver_WriteCom(DD_RAMWR);       // RAM Write command\r
50 \r
51     // Plot the color data to the LCD buffer\r
52     for(i = ((xmax - xmin + 1) * (ymax - ymin + 1)); i > 0; i--)\r
53     {\r
54         LCDdriver_WriteData(color >> 8);        // top 8 bits of RGB565 color\r
55         LCDdriver_WriteData(color);                     // bottom 8 bits of RGB565 color\r
56     }\r
57 }\r
58 \r
59 // Routine to draw an unfilled rectangle to the LCD.\r
60 // Two corners of rectangle are at (xmin,ymin) and (xmax,ymax).\r
61 // The Rectangle is drawn in the RGB565 color specified\r
62 void LCD_Rect(int xmin,int xmax,int ymin,int ymax,int color)\r
63 {\r
64         // Draw 4 lines of rectange as 4 filled rectanges, each of 1 pixel wide\r
65         LCD_FilledRect(xmin,xmin,ymin,ymax,color);\r
66         LCD_FilledRect(xmax,xmax,ymin,ymax,color);\r
67         LCD_FilledRect(xmin,xmax,ymin,ymin,color);\r
68         LCD_FilledRect(xmin,xmax,ymax,ymax,color);\r
69 }\r
70 \r
71 \r
72 \r
73 // Plot a point on the screen in the 6:5:6 color format\r
74 void LCD_PlotPoint(int x,int y,int color)\r
75 {\r
76     LCDdriver_WriteCom(DD_CASET);       // Set the column address \r
77     LCDdriver_WriteData(x);                     // min address\r
78     LCDdriver_WriteData(x);                     // max address\r
79     LCDdriver_WriteCom(DD_RASET);       // Set the row address\r
80     LCDdriver_WriteData(y + 1);         // min address\r
81     LCDdriver_WriteData(y + 1);         // max address\r
82     LCDdriver_WriteCom(DD_RAMWR);       // RAM Write command\r
83     LCDdriver_WriteData(color >> 8);    // top 8 bits of RGB565 color\r
84     LCDdriver_WriteData(color);                 // top 8 bits of RGB565 color\r
85 }\r
86 \r
87 // Routine to draw a filled circle to the LCD.\r
88 // The centre of the circle is at (x0,y0) and the circle has the \r
89 // specifed radius. The circle is filled with the RGB565 color \r
90 // The circle is drawn using the "Midpoint circle algorithm", \r
91 // also known as "Bresenham's circle algorithm". In order to fill\r
92 // the circle, the algorithm has been modifed to draw a line between\r
93 // each two points, rather than plotting the two points individually.\r
94 void LCD_FilledCircle (int x0, int y0, int radius, int color)\r
95 {\r
96   int f = 1 - radius;\r
97   int ddF_x = 1;\r
98   int ddF_y = -2 * radius;\r
99   int x = 0;\r
100   int y = radius;\r
101   \r
102   LCD_FilledRect(x0, x0 ,y0 - radius,y0 + radius, color); \r
103   LCD_FilledRect(x0 - radius, x0 + radius ,y0,y0, color);  \r
104   \r
105   while(x < y)\r
106   {\r
107     if(f >= 0) \r
108     {\r
109       y--;\r
110       ddF_y += 2;\r
111       f += ddF_y;\r
112     }\r
113     x++;\r
114     ddF_x += 2;\r
115     f += ddF_x;    \r
116 \r
117     LCD_FilledRect(x0-x, x0+x ,y0 +y, y0 + y, color);    \r
118     LCD_FilledRect(x0-x, x0+x ,y0 - y, y0 - y, color); \r
119     LCD_FilledRect(x0-y, x0+y ,y0 + x, y0 + x, color);         \r
120     LCD_FilledRect(x0-y, x0+y ,y0 - x, y0 - x, color); \r
121   }\r
122 }\r
123 \r
124 // Routine to draw an unfilled circle to the LCD.\r
125 // The centre of the circle is at (x0,y0) and the circle has the \r
126 // specifed radius. The circle is drawn in the RGB565 color \r
127 // The circle is drawn using the "Midpoint circle algorithm", \r
128 // also known as "Bresenham's circle algorithm". \r
129 void LCD_Circle (int x0, int y0, int radius, int color)\r
130 {\r
131   int f = 1 - radius;\r
132   int ddF_x = 1;\r
133   int ddF_y = -2 * radius;\r
134   int x = 0;\r
135   int y = radius;\r
136 \r
137   LCD_PlotPoint(x0, y0 + radius, color);\r
138   LCD_PlotPoint(x0, y0 - radius, color);\r
139   LCD_PlotPoint(x0 + radius, y0, color);\r
140   LCD_PlotPoint(x0 - radius, y0, color);\r
141 \r
142   while(x < y)\r
143   {\r
144     if(f >= 0) \r
145     {\r
146       y--;\r
147       ddF_y += 2;\r
148       f += ddF_y;\r
149     }\r
150     x++;\r
151     ddF_x += 2;\r
152     f += ddF_x;    \r
153     LCD_PlotPoint(x0 + x, y0 + y, color);\r
154     LCD_PlotPoint(x0 - x, y0 + y, color);\r
155     LCD_PlotPoint(x0 + x, y0 - y, color);\r
156     LCD_PlotPoint(x0 - x, y0 - y, color);\r
157     LCD_PlotPoint(x0 + y, y0 + x, color);\r
158     LCD_PlotPoint(x0 - y, y0 + x, color);\r
159     LCD_PlotPoint(x0 + y, y0 - x, color);\r
160     LCD_PlotPoint(x0 - y, y0 - x, color);\r
161   }\r
162 }\r
163 \r
164 // Routine to draw a line in the RGB565 color to the LCD.\r
165 // The line is drawn from (xmin,ymin) to (xmax,ymax).\r
166 // The algorithm used to draw the line is "Bresenham's line\r
167 // algorithm". \r
168 #define SWAP(a, b)  a ^= b; b ^= a; a ^= b; \r
169 \r
170 void LCD_Line (int xmin,int xmax,int ymin,int ymax,int color)\r
171 {\r
172    int Dx = xmax - xmin; \r
173    int Dy = ymax - ymin;\r
174    int steep = (abs(Dy) >= abs(Dx));\r
175    if (steep) {\r
176        SWAP(xmin, ymin);\r
177        SWAP(xmax, ymax);\r
178        // recompute Dx, Dy after swap\r
179        Dx = xmax - xmin;\r
180        Dy = ymax - ymin;\r
181    }\r
182    int xstep = 1;\r
183    if (Dx < 0) {\r
184        xstep = -1;\r
185        Dx = -Dx;\r
186    }\r
187    int ystep = 1;\r
188    if (Dy < 0) {\r
189        ystep = -1;              \r
190        Dy = -Dy; \r
191    }\r
192    int TwoDy = 2*Dy; \r
193    int TwoDyTwoDx = TwoDy - 2*Dx; // 2*Dy - 2*Dx\r
194    int E = TwoDy - Dx; //2*Dy - Dx\r
195    int y = ymin;\r
196    int xDraw, yDraw;\r
197    int x;\r
198    for (x = xmin; x != xmax; x += xstep) {              \r
199        if (steep) {                     \r
200            xDraw = y;\r
201            yDraw = x;\r
202        } else {                 \r
203            xDraw = x;\r
204            yDraw = y;\r
205        }\r
206        // plot\r
207        LCD_PlotPoint(xDraw, yDraw, color);\r
208        // next\r
209        if (E > 0) {\r
210            E += TwoDyTwoDx; //E += 2*Dy - 2*Dx;\r
211            y = y + ystep;\r
212        } else {\r
213            E += TwoDy; //E += 2*Dy;\r
214        }\r
215    }\r
216 }\r
217 \r
218 // Routine to clear the LCD.\r
219 // Implemented by drawing a black rectangle across the whole screen\r
220 void LCD_ClearScreen(void)\r
221 {       \r
222         LCD_FilledRect (0,LCD_MAX_X,0 , LCD_MAX_Y, COLOR_BLACK);\r
223 }\r
224 \r
225 \r
226 \r
227 // Routine to write a single character to screen in the font pointed\r
228 // to by pBitMap.  This routine is intended to be used via the \r
229 // LCD_PrintChar() and LCD_PrintString() routines, rather than called\r
230 // directly from user code.\r
231 void LCD_WriteBitMap8x15(int x, int y, int height, int width, unsigned char *pBitMap, int color)\r
232 {\r
233         int xmax = x + width - 1;       // start at zero\r
234         int ymax = y + height - 1;      // start at zero\r
235         int iRow, iCol;\r
236         unsigned char ucRowData;\r
237         \r
238     LCDdriver_WriteCom(DD_CASET);       // Column address set\r
239     LCDdriver_WriteData(x);             // Start column\r
240     LCDdriver_WriteData(xmax);          // End column\r
241     LCDdriver_WriteCom(DD_RASET);       // Row address set\r
242     LCDdriver_WriteData(y);             // Start row\r
243     LCDdriver_WriteData(ymax);          // End row\r
244     LCDdriver_WriteCom(DD_RAMWR);       // Memory write\r
245     \r
246     \r
247     for(iRow=0;iRow<height;iRow++)\r
248     {\r
249         ucRowData = *pBitMap++;\r
250         \r
251         for(iCol=0;iCol<width;iCol++)\r
252         {\r
253 \r
254                 // Look at each input bitmap bit\r
255                 // and write as a black-pixel or\r
256                 // a color-pixel.\r
257                 \r
258                 if(ucRowData & 0x80)  // 'color pixel'\r
259                 {\r
260                 LCDdriver_WriteData(color >> 8); \r
261                 LCDdriver_WriteData(color);\r
262                 }\r
263                 else                            // black pixel\r
264                 {\r
265                         LCDdriver_WriteData(0x00);\r
266                 LCDdriver_WriteData(0x00);\r
267                 }\r
268                 \r
269                 ucRowData = ucRowData<<1;\r
270         }\r
271     }\r
272 \r
273 }\r
274 \r
275 \r
276 // Prints the character 'c' to the LCD in the appropriate color.\r
277 void LCD_PrintChar(int x, int y, unsigned char c, int color )\r
278 {\r
279     const unsigned char index = font_index_table[c];\r
280     const unsigned int offset = index * 15;\r
281     unsigned char *pData = (unsigned char *)&font_data_table[offset];   \r
282 \r
283     LCD_WriteBitMap8x15(x, y, 15, 8, pData, color);\r
284 }\r
285 \r
286 // Prints the string to the LCD in the appropriate color.\r
287 void LCD_PrintString(int x, int y, char *pcString, int iStrLen, int color)\r
288 {\r
289     unsigned char index;\r
290     unsigned int offset;\r
291     unsigned char *pData;\r
292     unsigned char c;\r
293         int i;\r
294         \r
295         for(i=0;i<iStrLen;i++)\r
296         {\r
297                 c = pcString[i];\r
298                 if(c==0)\r
299                         break;\r
300                 index = font_index_table[c];\r
301             offset = index * 15;\r
302             pData = (unsigned char *)&font_data_table[offset];\r
303 \r
304             LCD_WriteBitMap8x15(x, y, 15, 8, pData, color);     \r
305             x += 8;\r
306         }\r
307         \r
308 }