Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
stm32_adafruit_lcd.c
00001 /** 00002 ****************************************************************************** 00003 * @file stm32_adafruit_lcd.c 00004 * @author MCD Application Team 00005 * @version V1.0.0 00006 * @date 22-April-2014 00007 * @brief This file includes the driver for Liquid Crystal Display (LCD) module 00008 * mounted on the Adafruit 1.8" TFT LCD shield (reference ID 802), 00009 * that is used with the STM32 Nucleo board through SPI interface. 00010 ****************************************************************************** 00011 * @attention 00012 * 00013 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2> 00014 * 00015 * Redistribution and use in source and binary forms, with or without modification, 00016 * are permitted provided that the following conditions are met: 00017 * 1. Redistributions of source code must retain the above copyright notice, 00018 * this list of conditions and the following disclaimer. 00019 * 2. Redistributions in binary form must reproduce the above copyright notice, 00020 * this list of conditions and the following disclaimer in the documentation 00021 * and/or other materials provided with the distribution. 00022 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00023 * may be used to endorse or promote products derived from this software 00024 * without specific prior written permission. 00025 * 00026 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00027 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00028 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00029 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00030 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00031 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00032 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00033 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00034 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00035 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00036 * 00037 ****************************************************************************** 00038 */ 00039 00040 /* File Info : ----------------------------------------------------------------- 00041 User NOTES 00042 1. How To use this driver: 00043 -------------------------- 00044 - The LCD st7735 component driver MUST be included with this driver. 00045 00046 2. Driver description: 00047 --------------------- 00048 + Initialization steps: 00049 o Initialize the LCD using the BSP_LCD_Init() function. 00050 00051 + Display on LCD 00052 o Clear the whole LCD using the BSP_LCD_Clear() function or only one specified 00053 string line using the BSP_LCD_ClearStringLine() function. 00054 o Display a character on the specified line and column using the BSP_LCD_DisplayChar() 00055 function or a complete string line using the BSP_LCD_DisplayStringAtLine() function. 00056 o Display a string line on the specified position (x,y in pixel) and align mode 00057 using the BSP_LCD_DisplayStringAtLine() function. 00058 o Draw and fill a basic shapes (dot, line, rectangle, circle, ellipse, ..) 00059 on LCD using a set of functions. 00060 00061 ------------------------------------------------------------------------------*/ 00062 00063 /* Includes ------------------------------------------------------------------*/ 00064 #include "stm32_adafruit_lcd.h" 00065 00066 00067 /** @addtogroup BSP 00068 * @{ 00069 */ 00070 00071 /** @addtogroup STM32_ADAFRUIT 00072 * @{ 00073 */ 00074 00075 /** @addtogroup STM32_ADAFRUIT_LCD 00076 * @{ 00077 */ 00078 00079 /** @defgroup STM32_ADAFRUIT_LCD_Private_TypesDefinitions 00080 * @{ 00081 */ 00082 00083 /** 00084 * @} 00085 */ 00086 00087 /** @defgroup STM32_ADAFRUIT_LCD_Private_Defines 00088 * @{ 00089 */ 00090 #define POLY_X(Z) ((int32_t)((Points + (Z))->X)) 00091 #define POLY_Y(Z) ((int32_t)((Points + (Z))->Y)) 00092 #define NULL (void *)0 00093 00094 /** 00095 * @} 00096 */ 00097 00098 /** @defgroup STM32_ADAFRUIT_LCD_Private_Macros 00099 * @{ 00100 */ 00101 #define ABS(X) ((X) > 0 ? (X) : -(X)) 00102 00103 /** 00104 * @} 00105 */ 00106 00107 /** @defgroup STM32_ADAFRUIT_LCD_Private_Variables 00108 * @{ 00109 */ 00110 LCD_DrawPropTypeDef DrawProp; 00111 00112 static LCD_DrvTypeDef *lcd_drv; 00113 /** 00114 * @} 00115 */ 00116 00117 /** @defgroup STM32_ADAFRUIT_LCD_Private_FunctionPrototypes 00118 * @{ 00119 */ 00120 static void DrawChar(uint16_t Xpos, uint16_t Ypos, const uint8_t *c); 00121 static void FillTriangle(uint16_t x1, uint16_t x2, uint16_t x3, uint16_t y1, uint16_t y2, uint16_t y3); 00122 /** 00123 * @} 00124 */ 00125 00126 00127 /** @defgroup STM32_ADAFRUIT_LCD_Private_Functions 00128 * @{ 00129 */ 00130 00131 /** 00132 * @brief Initializes the LCD. 00133 * @param None 00134 * @retval LCD state 00135 */ 00136 uint8_t BSP_LCD_Init(void) 00137 { 00138 uint8_t ret = LCD_ERROR; 00139 00140 /* Default value for draw propriety */ 00141 DrawProp.BackColor = 0xFFFF; 00142 DrawProp.pFont = &Font24; 00143 DrawProp.TextColor = 0x0000; 00144 00145 lcd_drv = &st7735_drv; 00146 00147 /* LCD Init */ 00148 lcd_drv->Init(); 00149 00150 /* Clear the LCD screen */ 00151 BSP_LCD_Clear(LCD_COLOR_WHITE); 00152 00153 /* Initialize the font */ 00154 BSP_LCD_SetFont(&LCD_DEFAULT_FONT); 00155 00156 ret = LCD_OK; 00157 00158 return ret; 00159 } 00160 00161 /** 00162 * @brief Gets the LCD X size. 00163 * @param None 00164 * @retval Used LCD X size 00165 */ 00166 uint32_t BSP_LCD_GetXSize(void) 00167 { 00168 return(lcd_drv->GetLcdPixelWidth()); 00169 } 00170 00171 /** 00172 * @brief Gets the LCD Y size. 00173 * @param None 00174 * @retval Used LCD Y size 00175 */ 00176 uint32_t BSP_LCD_GetYSize(void) 00177 { 00178 return(lcd_drv->GetLcdPixelHeight()); 00179 } 00180 00181 /** 00182 * @brief Gets the LCD text color. 00183 * @param None 00184 * @retval Used text color. 00185 */ 00186 uint16_t BSP_LCD_GetTextColor(void) 00187 { 00188 return DrawProp.TextColor; 00189 } 00190 00191 /** 00192 * @brief Gets the LCD background color. 00193 * @param None 00194 * @retval Used background color 00195 */ 00196 uint16_t BSP_LCD_GetBackColor(void) 00197 { 00198 return DrawProp.BackColor; 00199 } 00200 00201 /** 00202 * @brief Sets the LCD text color. 00203 * @param Color: Text color code RGB(5-6-5) 00204 * @retval None 00205 */ 00206 void BSP_LCD_SetTextColor(uint16_t Color) 00207 { 00208 DrawProp.TextColor = Color; 00209 } 00210 00211 /** 00212 * @brief Sets the LCD background color. 00213 * @param Color: Background color code RGB(5-6-5) 00214 * @retval None 00215 */ 00216 void BSP_LCD_SetBackColor(uint16_t Color) 00217 { 00218 DrawProp.BackColor = Color; 00219 } 00220 00221 /** 00222 * @brief Sets the LCD text font. 00223 * @param fonts: Font to be used 00224 * @retval None 00225 */ 00226 void BSP_LCD_SetFont(sFONT *pFonts) 00227 { 00228 DrawProp.pFont = pFonts; 00229 } 00230 00231 /** 00232 * @brief Gets the LCD text font. 00233 * @param None 00234 * @retval Used font 00235 */ 00236 sFONT *BSP_LCD_GetFont(void) 00237 { 00238 return DrawProp.pFont; 00239 } 00240 00241 /** 00242 * @brief Clears the hole LCD. 00243 * @param Color: Color of the background 00244 * @retval None 00245 */ 00246 void BSP_LCD_Clear(uint16_t Color) 00247 { 00248 uint32_t counter = 0; 00249 uint32_t color_backup = DrawProp.TextColor; 00250 DrawProp.TextColor = Color; 00251 00252 for(counter = 0; counter < BSP_LCD_GetYSize(); counter++) 00253 { 00254 BSP_LCD_DrawHLine(0, counter, BSP_LCD_GetXSize()); 00255 } 00256 DrawProp.TextColor = color_backup; 00257 BSP_LCD_SetTextColor(DrawProp.TextColor); 00258 } 00259 00260 /** 00261 * @brief Clears the selected line. 00262 * @param Line: Line to be cleared 00263 * This parameter can be one of the following values: 00264 * @arg 0..9: if the Current fonts is Font16x24 00265 * @arg 0..19: if the Current fonts is Font12x12 or Font8x12 00266 * @arg 0..29: if the Current fonts is Font8x8 00267 * @retval None 00268 */ 00269 void BSP_LCD_ClearStringLine(uint16_t Line) 00270 { 00271 uint32_t color_backup = DrawProp.TextColor; 00272 DrawProp.TextColor = DrawProp.BackColor;; 00273 00274 /* Draw a rectangle with background color */ 00275 BSP_LCD_FillRect(0, (Line * DrawProp.pFont->Height), BSP_LCD_GetXSize(), DrawProp.pFont->Height); 00276 00277 DrawProp.TextColor = color_backup; 00278 BSP_LCD_SetTextColor(DrawProp.TextColor); 00279 } 00280 00281 /** 00282 * @brief Displays one character. 00283 * @param Xpos: Start column address 00284 * @param Ypos: Line where to display the character shape. 00285 * @param Ascii: Character ascii code 00286 * This parameter must be a number between Min_Data = 0x20 and Max_Data = 0x7E 00287 * @retval None 00288 */ 00289 void BSP_LCD_DisplayChar(uint16_t Xpos, uint16_t Ypos, uint8_t Ascii) 00290 { 00291 DrawChar(Xpos, Ypos, &DrawProp.pFont->table[(Ascii-' ') *\ 00292 DrawProp.pFont->Height * ((DrawProp.pFont->Width + 7) / 8)]); 00293 } 00294 00295 /** 00296 * @brief Displays characters on the LCD. 00297 * @param Xpos: X position (in pixel) 00298 * @param Ypos: Y position (in pixel) 00299 * @param Text: Pointer to string to display on LCD 00300 * @param Mode: Display mode 00301 * This parameter can be one of the following values: 00302 * @arg CENTER_MODE 00303 * @arg RIGHT_MODE 00304 * @arg LEFT_MODE 00305 * @retval None 00306 */ 00307 void BSP_LCD_DisplayStringAt(uint16_t Xpos, uint16_t Ypos, uint8_t *Text, Line_ModeTypdef Mode) 00308 { 00309 uint16_t refcolumn = 1, i = 0; 00310 uint32_t size = 0, xsize = 0; 00311 uint8_t *ptr = Text; 00312 00313 /* Get the text size */ 00314 while (*ptr++) size ++ ; 00315 00316 /* Characters number per line */ 00317 xsize = (BSP_LCD_GetXSize()/DrawProp.pFont->Width); 00318 00319 switch (Mode) 00320 { 00321 case CENTER_MODE: 00322 { 00323 refcolumn = Xpos + ((xsize - size)* DrawProp.pFont->Width) / 2; 00324 break; 00325 } 00326 case LEFT_MODE: 00327 { 00328 refcolumn = Xpos; 00329 break; 00330 } 00331 case RIGHT_MODE: 00332 { 00333 refcolumn = - Xpos + ((xsize - size)*DrawProp.pFont->Width); 00334 break; 00335 } 00336 default: 00337 { 00338 refcolumn = Xpos; 00339 break; 00340 } 00341 } 00342 00343 /* Send the string character by character on lCD */ 00344 while ((*Text != 0) & (((BSP_LCD_GetXSize() - (i*DrawProp.pFont->Width)) & 0xFFFF) >= DrawProp.pFont->Width)) 00345 { 00346 /* Display one character on LCD */ 00347 BSP_LCD_DisplayChar(refcolumn, Ypos, *Text); 00348 /* Decrement the column position by 16 */ 00349 refcolumn += DrawProp.pFont->Width; 00350 /* Point on the next character */ 00351 Text++; 00352 i++; 00353 } 00354 } 00355 00356 /** 00357 * @brief Displays a character on the LCD. 00358 * @param Line: Line where to display the character shape 00359 * This parameter can be one of the following values: 00360 * @arg 0..19: if the Current fonts is Font8 00361 * @arg 0..12: if the Current fonts is Font12 00362 * @arg 0...9: if the Current fonts is Font16 00363 * @arg 0...7: if the Current fonts is Font20 00364 * @arg 0...5: if the Current fonts is Font24 00365 * @param ptr: Pointer to string to display on LCD 00366 * @retval None 00367 */ 00368 void BSP_LCD_DisplayStringAtLine(uint16_t Line, uint8_t *ptr) 00369 { 00370 BSP_LCD_DisplayStringAt(0, LINE(Line), ptr, LEFT_MODE); 00371 } 00372 00373 /** 00374 * @brief Draws a pixel on LCD. 00375 * @param Xpos: X position 00376 * @param Ypos: Y position 00377 * @param RGB_Code: Pixel color in RGB mode (5-6-5) 00378 * @retval None 00379 */ 00380 void BSP_LCD_DrawPixel(uint16_t Xpos, uint16_t Ypos, uint16_t RGB_Code) 00381 { 00382 if(lcd_drv->WritePixel != NULL) 00383 { 00384 lcd_drv->WritePixel(Xpos, Ypos, RGB_Code); 00385 } 00386 } 00387 00388 /** 00389 * @brief Draws an horizontal line. 00390 * @param Xpos: X position 00391 * @param Ypos: Y position 00392 * @param Length: Line length 00393 * @retval None 00394 */ 00395 void BSP_LCD_DrawHLine(uint16_t Xpos, uint16_t Ypos, uint16_t Length) 00396 { 00397 uint32_t index = 0; 00398 00399 if(lcd_drv->DrawHLine != NULL) 00400 { 00401 lcd_drv->DrawHLine(DrawProp.TextColor, Xpos, Ypos, Length); 00402 } 00403 else 00404 { 00405 for(index = 0; index < Length; index++) 00406 { 00407 BSP_LCD_DrawPixel((Xpos + index), Ypos, DrawProp.TextColor); 00408 } 00409 } 00410 } 00411 00412 /** 00413 * @brief Draws a vertical line. 00414 * @param Xpos: X position 00415 * @param Ypos: Y position 00416 * @param Length: Line length 00417 * @retval None 00418 */ 00419 void BSP_LCD_DrawVLine(uint16_t Xpos, uint16_t Ypos, uint16_t Length) 00420 { 00421 uint32_t index = 0; 00422 00423 if(lcd_drv->DrawVLine != NULL) 00424 { 00425 lcd_drv->DrawVLine(DrawProp.TextColor, Xpos, Ypos, Length); 00426 } 00427 else 00428 { 00429 for(index = 0; index < Length; index++) 00430 { 00431 BSP_LCD_DrawPixel(Xpos, Ypos + index, DrawProp.TextColor); 00432 } 00433 } 00434 } 00435 00436 /** 00437 * @brief Draws an uni-line (between two points). 00438 * @param x1: Point 1 X position 00439 * @param y1: Point 1 Y position 00440 * @param x2: Point 2 X position 00441 * @param y2: Point 2 Y position 00442 * @retval None 00443 */ 00444 void BSP_LCD_DrawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) 00445 { 00446 int16_t deltax = 0, deltay = 0, x = 0, y = 0, xinc1 = 0, xinc2 = 0, 00447 yinc1 = 0, yinc2 = 0, den = 0, num = 0, numadd = 0, numpixels = 0, 00448 curpixel = 0; 00449 00450 deltax = ABS(x2 - x1); /* The difference between the x's */ 00451 deltay = ABS(y2 - y1); /* The difference between the y's */ 00452 x = x1; /* Start x off at the first pixel */ 00453 y = y1; /* Start y off at the first pixel */ 00454 00455 if (x2 >= x1) /* The x-values are increasing */ 00456 { 00457 xinc1 = 1; 00458 xinc2 = 1; 00459 } 00460 else /* The x-values are decreasing */ 00461 { 00462 xinc1 = -1; 00463 xinc2 = -1; 00464 } 00465 00466 if (y2 >= y1) /* The y-values are increasing */ 00467 { 00468 yinc1 = 1; 00469 yinc2 = 1; 00470 } 00471 else /* The y-values are decreasing */ 00472 { 00473 yinc1 = -1; 00474 yinc2 = -1; 00475 } 00476 00477 if (deltax >= deltay) /* There is at least one x-value for every y-value */ 00478 { 00479 xinc1 = 0; /* Don't change the x when numerator >= denominator */ 00480 yinc2 = 0; /* Don't change the y for every iteration */ 00481 den = deltax; 00482 num = deltax / 2; 00483 numadd = deltay; 00484 numpixels = deltax; /* There are more x-values than y-values */ 00485 } 00486 else /* There is at least one y-value for every x-value */ 00487 { 00488 xinc2 = 0; /* Don't change the x for every iteration */ 00489 yinc1 = 0; /* Don't change the y when numerator >= denominator */ 00490 den = deltay; 00491 num = deltay / 2; 00492 numadd = deltax; 00493 numpixels = deltay; /* There are more y-values than x-values */ 00494 } 00495 00496 for (curpixel = 0; curpixel <= numpixels; curpixel++) 00497 { 00498 BSP_LCD_DrawPixel(x, y, DrawProp.TextColor); /* Draw the current pixel */ 00499 num += numadd; /* Increase the numerator by the top of the fraction */ 00500 if (num >= den) /* Check if numerator >= denominator */ 00501 { 00502 num -= den; /* Calculate the new numerator value */ 00503 x += xinc1; /* Change the x as appropriate */ 00504 y += yinc1; /* Change the y as appropriate */ 00505 } 00506 x += xinc2; /* Change the x as appropriate */ 00507 y += yinc2; /* Change the y as appropriate */ 00508 } 00509 } 00510 00511 /** 00512 * @brief Draws a rectangle. 00513 * @param Xpos: X position 00514 * @param Ypos: Y position 00515 * @param Width: Rectangle width 00516 * @param Height: Rectangle height 00517 * @retval None 00518 */ 00519 void BSP_LCD_DrawRect(uint16_t Xpos, uint16_t Ypos, uint16_t Width, uint16_t Height) 00520 { 00521 /* Draw horizontal lines */ 00522 BSP_LCD_DrawHLine(Xpos, Ypos, Width); 00523 BSP_LCD_DrawHLine(Xpos, (Ypos+ Height), Width); 00524 00525 /* Draw vertical lines */ 00526 BSP_LCD_DrawVLine(Xpos, Ypos, Height); 00527 BSP_LCD_DrawVLine((Xpos + Width), Ypos, Height); 00528 } 00529 00530 /** 00531 * @brief Draws a circle. 00532 * @param Xpos: X position 00533 * @param Ypos: Y position 00534 * @param Radius: Circle radius 00535 * @retval None 00536 */ 00537 void BSP_LCD_DrawCircle(uint16_t Xpos, uint16_t Ypos, uint16_t Radius) 00538 { 00539 int32_t D; /* Decision Variable */ 00540 uint32_t CurX; /* Current X Value */ 00541 uint32_t CurY; /* Current Y Value */ 00542 00543 D = 3 - (Radius << 1); 00544 CurX = 0; 00545 CurY = Radius; 00546 00547 while (CurX <= CurY) 00548 { 00549 BSP_LCD_DrawPixel((Xpos + CurX), (Ypos - CurY), DrawProp.TextColor); 00550 00551 BSP_LCD_DrawPixel((Xpos - CurX), (Ypos - CurY), DrawProp.TextColor); 00552 00553 BSP_LCD_DrawPixel((Xpos + CurY), (Ypos - CurX), DrawProp.TextColor); 00554 00555 BSP_LCD_DrawPixel((Xpos - CurY), (Ypos - CurX), DrawProp.TextColor); 00556 00557 BSP_LCD_DrawPixel((Xpos + CurX), (Ypos + CurY), DrawProp.TextColor); 00558 00559 BSP_LCD_DrawPixel((Xpos - CurX), (Ypos + CurY), DrawProp.TextColor); 00560 00561 BSP_LCD_DrawPixel((Xpos + CurY), (Ypos + CurX), DrawProp.TextColor); 00562 00563 BSP_LCD_DrawPixel((Xpos - CurY), (Ypos + CurX), DrawProp.TextColor); 00564 00565 /* Initialize the font */ 00566 BSP_LCD_SetFont(&LCD_DEFAULT_FONT); 00567 00568 if (D < 0) 00569 { 00570 D += (CurX << 2) + 6; 00571 } 00572 else 00573 { 00574 D += ((CurX - CurY) << 2) + 10; 00575 CurY--; 00576 } 00577 CurX++; 00578 } 00579 } 00580 00581 /** 00582 * @brief Draws an poly-line (between many points). 00583 * @param Points: Pointer to the points array 00584 * @param PointCount: Number of points 00585 * @retval None 00586 */ 00587 void BSP_LCD_DrawPolygon(pPoint Points, uint16_t PointCount) 00588 { 00589 int16_t X = 0, Y = 0; 00590 00591 if(PointCount < 2) 00592 { 00593 return; 00594 } 00595 00596 BSP_LCD_DrawLine(Points->X, Points->Y, (Points+PointCount-1)->X, (Points+PointCount-1)->Y); 00597 00598 while(--PointCount) 00599 { 00600 X = Points->X; 00601 Y = Points->Y; 00602 Points++; 00603 BSP_LCD_DrawLine(X, Y, Points->X, Points->Y); 00604 } 00605 } 00606 00607 /** 00608 * @brief Draws an ellipse on LCD. 00609 * @param Xpos: X position 00610 * @param Ypos: Y position 00611 * @param XRadius: Ellipse X radius 00612 * @param YRadius: Ellipse Y radius 00613 * @retval None 00614 */ 00615 void BSP_LCD_DrawEllipse(int Xpos, int Ypos, int XRadius, int YRadius) 00616 { 00617 int x = 0, y = -YRadius, err = 2-2*XRadius, e2; 00618 float K = 0, rad1 = 0, rad2 = 0; 00619 00620 rad1 = XRadius; 00621 rad2 = YRadius; 00622 00623 K = (float)(rad2/rad1); 00624 00625 do { 00626 BSP_LCD_DrawPixel((Xpos-(uint16_t)(x/K)), (Ypos+y), DrawProp.TextColor); 00627 BSP_LCD_DrawPixel((Xpos+(uint16_t)(x/K)), (Ypos+y), DrawProp.TextColor); 00628 BSP_LCD_DrawPixel((Xpos+(uint16_t)(x/K)), (Ypos-y), DrawProp.TextColor); 00629 BSP_LCD_DrawPixel((Xpos-(uint16_t)(x/K)), (Ypos-y), DrawProp.TextColor); 00630 00631 e2 = err; 00632 if (e2 <= x) { 00633 err += ++x*2+1; 00634 if (-y == x && e2 <= y) e2 = 0; 00635 } 00636 if (e2 > y) err += ++y*2+1; 00637 } 00638 while (y <= 0); 00639 } 00640 00641 /** 00642 * @brief Draws a full rectangle. 00643 * @param Xpos: X position 00644 * @param Ypos: Y position 00645 * @param Width: Rectangle width 00646 * @param Height: Rectangle height 00647 * @retval None 00648 */ 00649 void BSP_LCD_FillRect(uint16_t Xpos, uint16_t Ypos, uint16_t Width, uint16_t Height) 00650 { 00651 BSP_LCD_SetTextColor(DrawProp.TextColor); 00652 do 00653 { 00654 BSP_LCD_DrawHLine(Xpos, Ypos++, Width); 00655 } 00656 while(Height--); 00657 } 00658 00659 /** 00660 * @brief Draws a full circle. 00661 * @param Xpos: X position 00662 * @param Ypos: Y position 00663 * @param Radius: Circle radius 00664 * @retval None 00665 */ 00666 void BSP_LCD_FillCircle(uint16_t Xpos, uint16_t Ypos, uint16_t Radius) 00667 { 00668 int32_t D; /* Decision Variable */ 00669 uint32_t CurX; /* Current X Value */ 00670 uint32_t CurY; /* Current Y Value */ 00671 00672 D = 3 - (Radius << 1); 00673 00674 CurX = 0; 00675 CurY = Radius; 00676 00677 BSP_LCD_SetTextColor(DrawProp.TextColor); 00678 00679 while (CurX <= CurY) 00680 { 00681 if(CurY > 0) 00682 { 00683 BSP_LCD_DrawHLine(Xpos - CurY, Ypos + CurX, 2*CurY); 00684 BSP_LCD_DrawHLine(Xpos - CurY, Ypos - CurX, 2*CurY); 00685 } 00686 00687 if(CurX > 0) 00688 { 00689 BSP_LCD_DrawHLine(Xpos - CurX, Ypos - CurY, 2*CurX); 00690 BSP_LCD_DrawHLine(Xpos - CurX, Ypos + CurY, 2*CurX); 00691 } 00692 if (D < 0) 00693 { 00694 D += (CurX << 2) + 6; 00695 } 00696 else 00697 { 00698 D += ((CurX - CurY) << 2) + 10; 00699 CurY--; 00700 } 00701 CurX++; 00702 } 00703 00704 BSP_LCD_SetTextColor(DrawProp.TextColor); 00705 BSP_LCD_DrawCircle(Xpos, Ypos, Radius); 00706 } 00707 00708 /** 00709 * @brief Draws a full poly-line (between many points). 00710 * @param Points: Pointer to the points array 00711 * @param PointCount: Number of points 00712 * @retval None 00713 */ 00714 void BSP_LCD_FillPolygon(pPoint Points, uint16_t PointCount) 00715 { 00716 00717 int16_t X = 0, Y = 0, X2 = 0, Y2 = 0, X_center = 0, Y_center = 0, X_first = 0, Y_first = 0, pixelX = 0, pixelY = 0, counter = 0; 00718 uint16_t IMAGE_LEFT = 0, IMAGE_RIGHT = 0, IMAGE_TOP = 0, IMAGE_BOTTOM = 0; 00719 00720 IMAGE_LEFT = IMAGE_RIGHT = Points->X; 00721 IMAGE_TOP= IMAGE_BOTTOM = Points->Y; 00722 00723 for(counter = 1; counter < PointCount; counter++) 00724 { 00725 pixelX = POLY_X(counter); 00726 if(pixelX < IMAGE_LEFT) 00727 { 00728 IMAGE_LEFT = pixelX; 00729 } 00730 if(pixelX > IMAGE_RIGHT) 00731 { 00732 IMAGE_RIGHT = pixelX; 00733 } 00734 00735 pixelY = POLY_Y(counter); 00736 if(pixelY < IMAGE_TOP) 00737 { 00738 IMAGE_TOP = pixelY; 00739 } 00740 if(pixelY > IMAGE_BOTTOM) 00741 { 00742 IMAGE_BOTTOM = pixelY; 00743 } 00744 } 00745 00746 if(PointCount < 2) 00747 { 00748 return; 00749 } 00750 00751 X_center = (IMAGE_LEFT + IMAGE_RIGHT)/2; 00752 Y_center = (IMAGE_BOTTOM + IMAGE_TOP)/2; 00753 00754 X_first = Points->X; 00755 Y_first = Points->Y; 00756 00757 while(--PointCount) 00758 { 00759 X = Points->X; 00760 Y = Points->Y; 00761 Points++; 00762 X2 = Points->X; 00763 Y2 = Points->Y; 00764 00765 FillTriangle(X, X2, X_center, Y, Y2, Y_center); 00766 FillTriangle(X, X_center, X2, Y, Y_center, Y2); 00767 FillTriangle(X_center, X2, X, Y_center, Y2, Y); 00768 } 00769 00770 FillTriangle(X_first, X2, X_center, Y_first, Y2, Y_center); 00771 FillTriangle(X_first, X_center, X2, Y_first, Y_center, Y2); 00772 FillTriangle(X_center, X2, X_first, Y_center, Y2, Y_first); 00773 } 00774 00775 /** 00776 * @brief Draws a full ellipse. 00777 * @param Xpos: X position 00778 * @param Ypos: Y position 00779 * @param XRadius: Ellipse X radius 00780 * @param YRadius: Ellipse Y radius 00781 * @retval None 00782 */ 00783 void BSP_LCD_FillEllipse(int Xpos, int Ypos, int XRadius, int YRadius) 00784 { 00785 int x = 0, y = -YRadius, err = 2-2*XRadius, e2; 00786 float K = 0, rad1 = 0, rad2 = 0; 00787 00788 rad1 = XRadius; 00789 rad2 = YRadius; 00790 00791 K = (float)(rad2/rad1); 00792 00793 do 00794 { 00795 BSP_LCD_DrawHLine((Xpos-(uint16_t)(x/K)), (Ypos+y), (2*(uint16_t)(x/K) + 1)); 00796 BSP_LCD_DrawHLine((Xpos-(uint16_t)(x/K)), (Ypos-y), (2*(uint16_t)(x/K) + 1)); 00797 00798 e2 = err; 00799 if (e2 <= x) 00800 { 00801 err += ++x*2+1; 00802 if (-y == x && e2 <= y) e2 = 0; 00803 } 00804 if (e2 > y) err += ++y*2+1; 00805 } 00806 while (y <= 0); 00807 } 00808 00809 /** 00810 * @brief Enables the display. 00811 * @param None 00812 * @retval None 00813 */ 00814 void BSP_LCD_DisplayOn(void) 00815 { 00816 lcd_drv->DisplayOn(); 00817 } 00818 00819 /** 00820 * @brief Disables the display. 00821 * @param None 00822 * @retval None 00823 */ 00824 void BSP_LCD_DisplayOff(void) 00825 { 00826 lcd_drv->DisplayOff(); 00827 } 00828 00829 /****************************************************************************** 00830 Static Function 00831 *******************************************************************************/ 00832 00833 /** 00834 * @brief Draws a character on LCD. 00835 * @param Xpos: Line where to display the character shape 00836 * @param Ypos: Start column address 00837 * @param c: Pointer to the character data 00838 * @retval None 00839 */ 00840 static void DrawChar(uint16_t Xpos, uint16_t Ypos, const uint8_t *c) 00841 { 00842 uint32_t i = 0, j = 0; 00843 uint16_t height, width; 00844 uint8_t offset; 00845 uint8_t *pchar; 00846 uint32_t line; 00847 00848 height = DrawProp.pFont->Height; 00849 width = DrawProp.pFont->Width; 00850 00851 offset = 8 *((width + 7)/8) - width ; 00852 00853 for(i = 0; i < height; i++) 00854 { 00855 pchar = ((uint8_t *)c + (width + 7)/8 * i); 00856 00857 switch(((width + 7)/8)) 00858 { 00859 case 1: 00860 line = pchar[0]; 00861 break; 00862 00863 case 2: 00864 line = (pchar[0]<< 8) | pchar[1]; 00865 break; 00866 00867 case 3: 00868 default: 00869 line = (pchar[0]<< 16) | (pchar[1]<< 8) | pchar[2]; 00870 break; 00871 } 00872 00873 for (j = 0; j < width; j++) 00874 { 00875 if(line & (1 << (width- j + offset- 1))) 00876 { 00877 BSP_LCD_DrawPixel((Xpos + j), Ypos, DrawProp.TextColor); 00878 } 00879 else 00880 { 00881 BSP_LCD_DrawPixel((Xpos + j), Ypos, DrawProp.BackColor); 00882 } 00883 } 00884 Ypos++; 00885 } 00886 } 00887 00888 /** 00889 * @brief Fills a triangle (between 3 points). 00890 * @param Points: Pointer to the points array 00891 * @param x1: Point 1 X position 00892 * @param y1: Point 1 Y position 00893 * @param x2: Point 2 X position 00894 * @param y2: Point 2 Y position 00895 * @param x3: Point 3 X position 00896 * @param y3: Point 3 Y position 00897 * @retval None 00898 */ 00899 static void FillTriangle(uint16_t x1, uint16_t x2, uint16_t x3, uint16_t y1, uint16_t y2, uint16_t y3) 00900 { 00901 int16_t deltax = 0, deltay = 0, x = 0, y = 0, xinc1 = 0, xinc2 = 0, 00902 yinc1 = 0, yinc2 = 0, den = 0, num = 0, numadd = 0, numpixels = 0, 00903 curpixel = 0; 00904 00905 deltax = ABS(x2 - x1); /* The difference between the x's */ 00906 deltay = ABS(y2 - y1); /* The difference between the y's */ 00907 x = x1; /* Start x off at the first pixel */ 00908 y = y1; /* Start y off at the first pixel */ 00909 00910 if (x2 >= x1) /* The x-values are increasing */ 00911 { 00912 xinc1 = 1; 00913 xinc2 = 1; 00914 } 00915 else /* The x-values are decreasing */ 00916 { 00917 xinc1 = -1; 00918 xinc2 = -1; 00919 } 00920 00921 if (y2 >= y1) /* The y-values are increasing */ 00922 { 00923 yinc1 = 1; 00924 yinc2 = 1; 00925 } 00926 else /* The y-values are decreasing */ 00927 { 00928 yinc1 = -1; 00929 yinc2 = -1; 00930 } 00931 00932 if (deltax >= deltay) /* There is at least one x-value for every y-value */ 00933 { 00934 xinc1 = 0; /* Don't change the x when numerator >= denominator */ 00935 yinc2 = 0; /* Don't change the y for every iteration */ 00936 den = deltax; 00937 num = deltax / 2; 00938 numadd = deltay; 00939 numpixels = deltax; /* There are more x-values than y-values */ 00940 } 00941 else /* There is at least one y-value for every x-value */ 00942 { 00943 xinc2 = 0; /* Don't change the x for every iteration */ 00944 yinc1 = 0; /* Don't change the y when numerator >= denominator */ 00945 den = deltay; 00946 num = deltay / 2; 00947 numadd = deltax; 00948 numpixels = deltay; /* There are more y-values than x-values */ 00949 } 00950 00951 for (curpixel = 0; curpixel <= numpixels; curpixel++) 00952 { 00953 BSP_LCD_DrawLine(x, y, x3, y3); 00954 00955 num += numadd; /* Increase the numerator by the top of the fraction */ 00956 if (num >= den) /* Check if numerator >= denominator */ 00957 { 00958 num -= den; /* Calculate the new numerator value */ 00959 x += xinc1; /* Change the x as appropriate */ 00960 y += yinc1; /* Change the y as appropriate */ 00961 } 00962 x += xinc2; /* Change the x as appropriate */ 00963 y += yinc2; /* Change the y as appropriate */ 00964 } 00965 } 00966 00967 /** 00968 * @} 00969 */ 00970 00971 /** 00972 * @} 00973 */ 00974 00975 /** 00976 * @} 00977 */ 00978 00979 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Tue Jul 12 2022 20:10:41 by
