1 //*****************************************************************************
\r
8 // +----+ Copyright (c) 2009 Code Red Technologies Ltd.
\r
10 // lcd.c contains various routines to plot to the LCD display on the RDB1768
\r
11 // development board.
\r
13 // Software License Agreement
\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
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
28 #include "lcd_commands.h"
\r
30 #include "lcd_driver.h"
\r
33 #include <stdlib.h> // to provice abs() function
\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
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
51 // Plot the color data to the LCD buffer
\r
52 for(i = ((xmax - xmin + 1) * (ymax - ymin + 1)); i > 0; i--)
\r
54 LCDdriver_WriteData(color >> 8); // top 8 bits of RGB565 color
\r
55 LCDdriver_WriteData(color); // bottom 8 bits of RGB565 color
\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
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
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
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
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
98 int ddF_y = -2 * radius;
\r
102 LCD_FilledRect(x0, x0 ,y0 - radius,y0 + radius, color);
\r
103 LCD_FilledRect(x0 - radius, x0 + radius ,y0,y0, color);
\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
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
131 int f = 1 - radius;
\r
133 int ddF_y = -2 * radius;
\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
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
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
168 #define SWAP(a, b) a ^= b; b ^= a; a ^= b;
\r
170 void LCD_Line (int xmin,int xmax,int ymin,int ymax,int color)
\r
172 int Dx = xmax - xmin;
\r
173 int Dy = ymax - ymin;
\r
174 int steep = (abs(Dy) >= abs(Dx));
\r
178 // recompute Dx, Dy after swap
\r
193 int TwoDyTwoDx = TwoDy - 2*Dx; // 2*Dy - 2*Dx
\r
194 int E = TwoDy - Dx; //2*Dy - Dx
\r
198 for (x = xmin; x != xmax; x += xstep) {
\r
207 LCD_PlotPoint(xDraw, yDraw, color);
\r
210 E += TwoDyTwoDx; //E += 2*Dy - 2*Dx;
\r
213 E += TwoDy; //E += 2*Dy;
\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
222 LCD_FilledRect (0,LCD_MAX_X,0 , LCD_MAX_Y, COLOR_BLACK);
\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
233 int xmax = x + width - 1; // start at zero
\r
234 int ymax = y + height - 1; // start at zero
\r
236 unsigned char ucRowData;
\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
247 for(iRow=0;iRow<height;iRow++)
\r
249 ucRowData = *pBitMap++;
\r
251 for(iCol=0;iCol<width;iCol++)
\r
254 // Look at each input bitmap bit
\r
255 // and write as a black-pixel or
\r
258 if(ucRowData & 0x80) // 'color pixel'
\r
260 LCDdriver_WriteData(color >> 8);
\r
261 LCDdriver_WriteData(color);
\r
263 else // black pixel
\r
265 LCDdriver_WriteData(0x00);
\r
266 LCDdriver_WriteData(0x00);
\r
269 ucRowData = ucRowData<<1;
\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
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
283 LCD_WriteBitMap8x15(x, y, 15, 8, pData, color);
\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
289 unsigned char index;
\r
290 unsigned int offset;
\r
291 unsigned char *pData;
\r
295 for(i=0;i<iStrLen;i++)
\r
300 index = font_index_table[c];
\r
301 offset = index * 15;
\r
302 pData = (unsigned char *)&font_data_table[offset];
\r
304 LCD_WriteBitMap8x15(x, y, 15, 8, pData, color);
\r