stm322xg_eval_lcd.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. /**
  2. ******************************************************************************
  3. * @file stm322xg_eval_lcd.c
  4. * @author MCD Application Team
  5. * @brief This file includes the driver for Liquid Crystal Display (LCD) module
  6. * mounted on STM322xG-EVAL evaluation board.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * are permitted provided that the following conditions are met:
  14. * 1. Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  29. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  31. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ******************************************************************************
  35. */
  36. /* File Info : -----------------------------------------------------------------
  37. User NOTES
  38. 1. How To use this driver:
  39. --------------------------
  40. - This driver is used to drive indirectly an LCD TFT.
  41. - This driver supports the ILI9325 LCD mounted on MB785 daughter board
  42. - The ILI9325 component driver MUST be included with this driver.
  43. 2. Driver description:
  44. ---------------------
  45. + Initialization steps:
  46. o Initialize the LCD using the BSP_LCD_Init() function.
  47. + Display on LCD
  48. o Clear the hole LCD using yhe BSP_LCD_Clear() function or only one specified
  49. string line using the BSP_LCD_ClearStringLine() function.
  50. o Display a character on the specified line and column using the BSP_LCD_DisplayChar()
  51. function or a complete string line using the BSP_LCD_DisplayStringAtLine() function.
  52. o Display a string line on the specified position (x,y in pixel) and align mode
  53. using the BSP_LCD_DisplayStringAtLine() function.
  54. o Draw and fill basic shapes (dot, line, rectangle, circle, ellipse, .. bitmap, raw picture)
  55. on LCD using a set of functions.
  56. ------------------------------------------------------------------------------*/
  57. /* Includes ------------------------------------------------------------------*/
  58. #include "stm322xg_eval_lcd.h"
  59. #include "../../../Utilities/Fonts/fonts.h"
  60. #include "../../../Utilities/Fonts/font24.c"
  61. #include "../../../Utilities/Fonts/font20.c"
  62. #include "../../../Utilities/Fonts/font16.c"
  63. #include "../../../Utilities/Fonts/font12.c"
  64. #include "../../../Utilities/Fonts/font8.c"
  65. /** @addtogroup BSP
  66. * @{
  67. */
  68. /** @addtogroup STM322xG_EVAL
  69. * @{
  70. */
  71. /** @defgroup STM322xG_EVAL_LCD STM322xG EVAL LCD
  72. * @{
  73. */
  74. /** @defgroup STM322xG_EVAL_LCD_Private_Defines STM322xG EVAL LCD Private Defines
  75. * @{
  76. */
  77. #define POLY_X(Z) ((int32_t)((Points + (Z))->X))
  78. #define POLY_Y(Z) ((int32_t)((Points + (Z))->Y))
  79. /**
  80. * @}
  81. */
  82. /** @defgroup STM322xG_EVAL_LCD_Private_Macros STM322xG EVAL LCD Private Macros
  83. * @{
  84. */
  85. #define ABS(X) ((X) > 0 ? (X) : -(X))
  86. /**
  87. * @}
  88. */
  89. /** @defgroup STM322xG_EVAL_LCD_Private_Variables STM322xG EVAL LCD Private Variables
  90. * @{
  91. */
  92. LCD_DrawPropTypeDef DrawProp;
  93. static LCD_DrvTypeDef *lcd_drv;
  94. /**
  95. * @}
  96. */
  97. /** @defgroup STM322xG_EVAL_LCD_Private_FunctionPrototypes STM322xG EVAL LCD Private FunctionPrototypes
  98. * @{
  99. */
  100. static void DrawChar(uint16_t Xpos, uint16_t Ypos, const uint8_t *c);
  101. static void SetDisplayWindow(uint16_t Xpos, uint16_t Ypos, uint16_t Width, uint16_t Height);
  102. static void FillTriangle(uint16_t x1, uint16_t x2, uint16_t x3, uint16_t y1, uint16_t y2, uint16_t y3);
  103. /**
  104. * @}
  105. */
  106. /** @defgroup STM322xG_EVAL_LCD_Private_Functions STM322xG EVAL LCD Private Functions
  107. * @{
  108. */
  109. /**
  110. * @brief Initializes the LCD.
  111. * @retval LCD state
  112. */
  113. uint8_t BSP_LCD_Init(void)
  114. {
  115. uint8_t ret = LCD_ERROR;
  116. /* Default value for draw propriety */
  117. DrawProp.BackColor = 0xFFFF;
  118. DrawProp.pFont = &Font24;
  119. DrawProp.TextColor = 0x0000;
  120. if(ili9325_drv.ReadID() == ILI9325_ID)
  121. {
  122. lcd_drv = &ili9325_drv;
  123. /* LCD Init */
  124. lcd_drv->Init();
  125. /* Initialize the font */
  126. BSP_LCD_SetFont(&LCD_DEFAULT_FONT);
  127. ret = LCD_OK;
  128. }
  129. return ret;
  130. }
  131. /**
  132. * @brief Gets the LCD X size.
  133. * @retval Used LCD X size
  134. */
  135. uint32_t BSP_LCD_GetXSize(void)
  136. {
  137. return(lcd_drv->GetLcdPixelWidth());
  138. }
  139. /**
  140. * @brief Gets the LCD Y size.
  141. * @retval Used LCD Y size
  142. */
  143. uint32_t BSP_LCD_GetYSize(void)
  144. {
  145. return(lcd_drv->GetLcdPixelHeight());
  146. }
  147. /**
  148. * @brief Gets the LCD text color.
  149. * @retval Used text color.
  150. */
  151. uint16_t BSP_LCD_GetTextColor(void)
  152. {
  153. return DrawProp.TextColor;
  154. }
  155. /**
  156. * @brief Gets the LCD background color.
  157. * @retval Used background color
  158. */
  159. uint16_t BSP_LCD_GetBackColor(void)
  160. {
  161. return DrawProp.BackColor;
  162. }
  163. /**
  164. * @brief Sets the LCD text color.
  165. * @param Color: Text color code RGB(5-6-5)
  166. */
  167. void BSP_LCD_SetTextColor(uint16_t Color)
  168. {
  169. DrawProp.TextColor = Color;
  170. }
  171. /**
  172. * @brief Sets the LCD background color.
  173. * @param Color: Background color code RGB(5-6-5)
  174. */
  175. void BSP_LCD_SetBackColor(uint16_t Color)
  176. {
  177. DrawProp.BackColor = Color;
  178. }
  179. /**
  180. * @brief Sets the LCD text font.
  181. * @param fonts: Font to be used
  182. */
  183. void BSP_LCD_SetFont(sFONT *fonts)
  184. {
  185. DrawProp.pFont = fonts;
  186. }
  187. /**
  188. * @brief Gets the LCD text font.
  189. * @retval Used font
  190. */
  191. sFONT *BSP_LCD_GetFont(void)
  192. {
  193. return DrawProp.pFont;
  194. }
  195. /**
  196. * @brief Clears the hole LCD.
  197. * @param Color: Color of the background
  198. */
  199. void BSP_LCD_Clear(uint16_t Color)
  200. {
  201. uint32_t counter = 0;
  202. uint32_t color_backup = DrawProp.TextColor;
  203. DrawProp.TextColor = Color;
  204. for(counter = 0; counter < BSP_LCD_GetYSize(); counter++)
  205. {
  206. BSP_LCD_DrawHLine(0, counter, BSP_LCD_GetXSize());
  207. }
  208. DrawProp.TextColor = color_backup;
  209. BSP_LCD_SetTextColor(DrawProp.TextColor);
  210. }
  211. /**
  212. * @brief Clears the selected line.
  213. * @param Line: Line to be cleared
  214. * This parameter can be one of the following values:
  215. * @arg 0..9: if the Current fonts is Font16x24
  216. * @arg 0..19: if the Current fonts is Font12x12 or Font8x12
  217. * @arg 0..29: if the Current fonts is Font8x8
  218. */
  219. void BSP_LCD_ClearStringLine(uint16_t Line)
  220. {
  221. uint32_t color_backup = DrawProp.TextColor;
  222. DrawProp.TextColor = DrawProp.BackColor;;
  223. /* Draw a rectangle with background color */
  224. BSP_LCD_FillRect(0, (Line * DrawProp.pFont->Height), BSP_LCD_GetXSize(), DrawProp.pFont->Height);
  225. DrawProp.TextColor = color_backup;
  226. BSP_LCD_SetTextColor(DrawProp.TextColor);
  227. }
  228. /**
  229. * @brief Displays one character.
  230. * @param Xpos: Start column address
  231. * @param Ypos: Line where to display the character shape.
  232. * @param Ascii: Character ascii code
  233. * This parameter must be a number between Min_Data = 0x20 and Max_Data = 0x7E
  234. */
  235. void BSP_LCD_DisplayChar(uint16_t Xpos, uint16_t Ypos, uint8_t Ascii)
  236. {
  237. DrawChar(Xpos, Ypos, &DrawProp.pFont->table[(Ascii-' ') *\
  238. DrawProp.pFont->Height * ((DrawProp.pFont->Width + 7) / 8)]);
  239. }
  240. /**
  241. * @brief Displays characters on the LCD.
  242. * @param Xpos: X position (in pixel)
  243. * @param Ypos: Y position (in pixel)
  244. * @param Text: Pointer to string to display on LCD
  245. * @param Mode: Display mode
  246. * This parameter can be one of the following values:
  247. * @arg CENTER_MODE
  248. * @arg RIGHT_MODE
  249. * @arg LEFT_MODE
  250. */
  251. void BSP_LCD_DisplayStringAt(uint16_t Xpos, uint16_t Ypos, uint8_t *Text, Line_ModeTypdef Mode)
  252. {
  253. uint16_t refcolumn = 1, i = 0;
  254. uint32_t size = 0, xsize = 0;
  255. uint8_t *ptr = Text;
  256. /* Get the text size */
  257. while (*ptr++) size ++ ;
  258. /* Characters number per line */
  259. xsize = (BSP_LCD_GetXSize()/DrawProp.pFont->Width);
  260. switch (Mode)
  261. {
  262. case CENTER_MODE:
  263. {
  264. refcolumn = Xpos + ((xsize - size)* DrawProp.pFont->Width) / 2;
  265. break;
  266. }
  267. case LEFT_MODE:
  268. {
  269. refcolumn = Xpos;
  270. break;
  271. }
  272. case RIGHT_MODE:
  273. {
  274. refcolumn = - Xpos + ((xsize - size)*DrawProp.pFont->Width);
  275. break;
  276. }
  277. default:
  278. {
  279. refcolumn = Xpos;
  280. break;
  281. }
  282. }
  283. /* Send the string character by character on lCD */
  284. while ((*Text != 0) & (((BSP_LCD_GetXSize() - (i*DrawProp.pFont->Width)) & 0xFFFF) >= DrawProp.pFont->Width))
  285. {
  286. /* Display one character on LCD */
  287. BSP_LCD_DisplayChar(refcolumn, Ypos, *Text);
  288. /* Decrement the column position by 16 */
  289. refcolumn += DrawProp.pFont->Width;
  290. /* Point on the next character */
  291. Text++;
  292. i++;
  293. }
  294. }
  295. /**
  296. * @brief Displays a character on the LCD.
  297. * @param Line: Line where to display the character shape
  298. * This parameter can be one of the following values:
  299. * @arg 0..9: if the Current fonts is Font16x24
  300. * @arg 0..19: if the Current fonts is Font12x12 or Font8x12
  301. * @arg 0..29: if the Current fonts is Font8x8
  302. * @param ptr: Pointer to string to display on LCD
  303. */
  304. void BSP_LCD_DisplayStringAtLine(uint16_t Line, uint8_t *ptr)
  305. {
  306. BSP_LCD_DisplayStringAt(0, LINE(Line), ptr, LEFT_MODE);
  307. }
  308. /**
  309. * @brief Reads an LCD pixel.
  310. * @param Xpos: X position
  311. * @param Ypos: Y position
  312. * @retval RGB pixel color
  313. */
  314. uint16_t BSP_LCD_ReadPixel(uint16_t Xpos, uint16_t Ypos)
  315. {
  316. uint16_t ret = 0;
  317. if(lcd_drv->ReadPixel != NULL)
  318. {
  319. ret = lcd_drv->ReadPixel(Xpos, Ypos);
  320. }
  321. return ret;
  322. }
  323. /**
  324. * @brief Draws a pixel on LCD.
  325. * @param Xpos: X position
  326. * @param Ypos: Y position
  327. * @param RGB_Code: Pixel color in RGB mode (5-6-5)
  328. */
  329. void BSP_LCD_DrawPixel(uint16_t Xpos, uint16_t Ypos, uint16_t RGB_Code)
  330. {
  331. if(lcd_drv->WritePixel != NULL)
  332. {
  333. lcd_drv->WritePixel(Xpos, Ypos, RGB_Code);
  334. }
  335. }
  336. /**
  337. * @brief Draws an horizontal line.
  338. * @param Xpos: X position
  339. * @param Ypos: Y position
  340. * @param Length: Line length
  341. */
  342. void BSP_LCD_DrawHLine(uint16_t Xpos, uint16_t Ypos, uint16_t Length)
  343. {
  344. uint32_t index = 0;
  345. if(lcd_drv->DrawHLine != NULL)
  346. {
  347. lcd_drv->DrawHLine(DrawProp.TextColor, Xpos, Ypos, Length);
  348. }
  349. else
  350. {
  351. for(index = 0; index < Length; index++)
  352. {
  353. BSP_LCD_DrawPixel((Xpos + index), Ypos, DrawProp.TextColor);
  354. }
  355. }
  356. }
  357. /**
  358. * @brief Draws a vertical line.
  359. * @param Xpos: X position
  360. * @param Ypos: Y position
  361. * @param Length: Line length
  362. */
  363. void BSP_LCD_DrawVLine(uint16_t Xpos, uint16_t Ypos, uint16_t Length)
  364. {
  365. uint32_t index = 0;
  366. if(lcd_drv->DrawVLine != NULL)
  367. {
  368. lcd_drv->DrawVLine(DrawProp.TextColor, Xpos, Ypos, Length);
  369. }
  370. else
  371. {
  372. for(index = 0; index < Length; index++)
  373. {
  374. BSP_LCD_DrawPixel(Xpos, Ypos + index, DrawProp.TextColor);
  375. }
  376. }
  377. }
  378. /**
  379. * @brief Draws an uni-line (between two points).
  380. * @param x1: Point 1 X position
  381. * @param y1: Point 1 Y position
  382. * @param x2: Point 2 X position
  383. * @param y2: Point 2 Y position
  384. */
  385. void BSP_LCD_DrawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
  386. {
  387. int16_t deltax = 0, deltay = 0, x = 0, y = 0, xinc1 = 0, xinc2 = 0,
  388. yinc1 = 0, yinc2 = 0, den = 0, num = 0, numadd = 0, numpixels = 0,
  389. curpixel = 0;
  390. deltax = ABS(x2 - x1); /* The difference between the x's */
  391. deltay = ABS(y2 - y1); /* The difference between the y's */
  392. x = x1; /* Start x off at the first pixel */
  393. y = y1; /* Start y off at the first pixel */
  394. if (x2 >= x1) /* The x-values are increasing */
  395. {
  396. xinc1 = 1;
  397. xinc2 = 1;
  398. }
  399. else /* The x-values are decreasing */
  400. {
  401. xinc1 = -1;
  402. xinc2 = -1;
  403. }
  404. if (y2 >= y1) /* The y-values are increasing */
  405. {
  406. yinc1 = 1;
  407. yinc2 = 1;
  408. }
  409. else /* The y-values are decreasing */
  410. {
  411. yinc1 = -1;
  412. yinc2 = -1;
  413. }
  414. if (deltax >= deltay) /* There is at least one x-value for every y-value */
  415. {
  416. xinc1 = 0; /* Don't change the x when numerator >= denominator */
  417. yinc2 = 0; /* Don't change the y for every iteration */
  418. den = deltax;
  419. num = deltax / 2;
  420. numadd = deltay;
  421. numpixels = deltax; /* There are more x-values than y-values */
  422. }
  423. else /* There is at least one y-value for every x-value */
  424. {
  425. xinc2 = 0; /* Don't change the x for every iteration */
  426. yinc1 = 0; /* Don't change the y when numerator >= denominator */
  427. den = deltay;
  428. num = deltay / 2;
  429. numadd = deltax;
  430. numpixels = deltay; /* There are more y-values than x-values */
  431. }
  432. for (curpixel = 0; curpixel <= numpixels; curpixel++)
  433. {
  434. BSP_LCD_DrawPixel(x, y, DrawProp.TextColor); /* Draw the current pixel */
  435. num += numadd; /* Increase the numerator by the top of the fraction */
  436. if (num >= den) /* Check if numerator >= denominator */
  437. {
  438. num -= den; /* Calculate the new numerator value */
  439. x += xinc1; /* Change the x as appropriate */
  440. y += yinc1; /* Change the y as appropriate */
  441. }
  442. x += xinc2; /* Change the x as appropriate */
  443. y += yinc2; /* Change the y as appropriate */
  444. }
  445. }
  446. /**
  447. * @brief Draws a rectangle.
  448. * @param Xpos: X position
  449. * @param Ypos: Y position
  450. * @param Width: Rectangle width
  451. * @param Height: Rectangle height
  452. */
  453. void BSP_LCD_DrawRect(uint16_t Xpos, uint16_t Ypos, uint16_t Width, uint16_t Height)
  454. {
  455. /* Draw horizontal lines */
  456. BSP_LCD_DrawHLine(Xpos, Ypos, Width);
  457. BSP_LCD_DrawHLine(Xpos, (Ypos+ Height), Width);
  458. /* Draw vertical lines */
  459. BSP_LCD_DrawVLine(Xpos, Ypos, Height);
  460. BSP_LCD_DrawVLine((Xpos + Width), Ypos, Height);
  461. }
  462. /**
  463. * @brief Draws a circle.
  464. * @param Xpos: X position
  465. * @param Ypos: Y position
  466. * @param Radius: Circle radius
  467. */
  468. void BSP_LCD_DrawCircle(uint16_t Xpos, uint16_t Ypos, uint16_t Radius)
  469. {
  470. int32_t D; /* Decision Variable */
  471. uint32_t CurX; /* Current X Value */
  472. uint32_t CurY; /* Current Y Value */
  473. D = 3 - (Radius << 1);
  474. CurX = 0;
  475. CurY = Radius;
  476. while (CurX <= CurY)
  477. {
  478. BSP_LCD_DrawPixel((Xpos + CurX), (Ypos - CurY), DrawProp.TextColor);
  479. BSP_LCD_DrawPixel((Xpos - CurX), (Ypos - CurY), DrawProp.TextColor);
  480. BSP_LCD_DrawPixel((Xpos + CurY), (Ypos - CurX), DrawProp.TextColor);
  481. BSP_LCD_DrawPixel((Xpos - CurY), (Ypos - CurX), DrawProp.TextColor);
  482. BSP_LCD_DrawPixel((Xpos + CurX), (Ypos + CurY), DrawProp.TextColor);
  483. BSP_LCD_DrawPixel((Xpos - CurX), (Ypos + CurY), DrawProp.TextColor);
  484. BSP_LCD_DrawPixel((Xpos + CurY), (Ypos + CurX), DrawProp.TextColor);
  485. BSP_LCD_DrawPixel((Xpos - CurY), (Ypos + CurX), DrawProp.TextColor);
  486. /* Initialize the font */
  487. BSP_LCD_SetFont(&LCD_DEFAULT_FONT);
  488. if (D < 0)
  489. {
  490. D += (CurX << 2) + 6;
  491. }
  492. else
  493. {
  494. D += ((CurX - CurY) << 2) + 10;
  495. CurY--;
  496. }
  497. CurX++;
  498. }
  499. }
  500. /**
  501. * @brief Draws an poly-line (between many points).
  502. * @param Points: Pointer to the points array
  503. * @param PointCount: Number of points
  504. */
  505. void BSP_LCD_DrawPolygon(pPoint Points, uint16_t PointCount)
  506. {
  507. int16_t X = 0, Y = 0;
  508. if(PointCount < 2)
  509. {
  510. return;
  511. }
  512. BSP_LCD_DrawLine(Points->X, Points->Y, (Points+PointCount-1)->X, (Points+PointCount-1)->Y);
  513. while(--PointCount)
  514. {
  515. X = Points->X;
  516. Y = Points->Y;
  517. Points++;
  518. BSP_LCD_DrawLine(X, Y, Points->X, Points->Y);
  519. }
  520. }
  521. /**
  522. * @brief Draws an ellipse on LCD.
  523. * @param Xpos: X position
  524. * @param Ypos: Y position
  525. * @param XRadius: Ellipse X radius
  526. * @param YRadius: Ellipse Y radius
  527. */
  528. void BSP_LCD_DrawEllipse(int Xpos, int Ypos, int XRadius, int YRadius)
  529. {
  530. int x = 0, y = -YRadius, err = 2-2*XRadius, e2;
  531. float K = 0, rad1 = 0, rad2 = 0;
  532. rad1 = XRadius;
  533. rad2 = YRadius;
  534. K = (float)(rad2/rad1);
  535. do {
  536. BSP_LCD_DrawPixel((Xpos-(uint16_t)(x/K)), (Ypos+y), DrawProp.TextColor);
  537. BSP_LCD_DrawPixel((Xpos+(uint16_t)(x/K)), (Ypos+y), DrawProp.TextColor);
  538. BSP_LCD_DrawPixel((Xpos+(uint16_t)(x/K)), (Ypos-y), DrawProp.TextColor);
  539. BSP_LCD_DrawPixel((Xpos-(uint16_t)(x/K)), (Ypos-y), DrawProp.TextColor);
  540. e2 = err;
  541. if (e2 <= x) {
  542. err += ++x*2+1;
  543. if (-y == x && e2 <= y) e2 = 0;
  544. }
  545. if (e2 > y) err += ++y*2+1;
  546. }
  547. while (y <= 0);
  548. }
  549. /**
  550. * @brief Draws a bitmap picture (16 bpp).
  551. * @param Xpos: Bmp X position in the LCD
  552. * @param Ypos: Bmp Y position in the LCD
  553. * @param pbmp: Pointer to Bmp picture address.
  554. */
  555. void BSP_LCD_DrawBitmap(uint16_t Xpos, uint16_t Ypos, uint8_t *pbmp)
  556. {
  557. uint32_t height = 0;
  558. uint32_t width = 0;
  559. /* Read bitmap width */
  560. width = pbmp[18] + (pbmp[19] << 8) + (pbmp[20] << 16) + (pbmp[21] << 24);
  561. /* Read bitmap height */
  562. height = pbmp[22] + (pbmp[23] << 8) + (pbmp[24] << 16) + (pbmp[25] << 24);
  563. SetDisplayWindow(Xpos, Ypos, width, height);
  564. if(lcd_drv->DrawBitmap != NULL)
  565. {
  566. lcd_drv->DrawBitmap(Xpos, Ypos, pbmp);
  567. }
  568. SetDisplayWindow(0, 0, BSP_LCD_GetXSize(), BSP_LCD_GetYSize());
  569. }
  570. /**
  571. * @brief Draws RGB Image (16 bpp).
  572. * @param Xpos: X position in the LCD
  573. * @param Ypos: Y position in the LCD
  574. * @param Xsize: X size in the LCD
  575. * @param Ysize: Y size in the LCD
  576. * @param pdata: Pointer to the RGB Image address.
  577. */
  578. void BSP_LCD_DrawRGBImage(uint16_t Xpos, uint16_t Ypos, uint16_t Xsize, uint16_t Ysize, uint8_t *pdata)
  579. {
  580. SetDisplayWindow(Xpos, Ypos, Xsize, Ysize);
  581. if(lcd_drv->DrawRGBImage != NULL)
  582. {
  583. lcd_drv->DrawRGBImage(Xpos, Ypos, Xsize, Ysize, pdata);
  584. }
  585. SetDisplayWindow(0, 0, BSP_LCD_GetXSize(), BSP_LCD_GetYSize());
  586. }
  587. /**
  588. * @brief Draws a full rectangle.
  589. * @param Xpos: X position
  590. * @param Ypos: Y position
  591. * @param Width: Rectangle width
  592. * @param Height: Rectangle height
  593. */
  594. void BSP_LCD_FillRect(uint16_t Xpos, uint16_t Ypos, uint16_t Width, uint16_t Height)
  595. {
  596. BSP_LCD_SetTextColor(DrawProp.TextColor);
  597. do
  598. {
  599. BSP_LCD_DrawHLine(Xpos, Ypos++, Width);
  600. }
  601. while(Height--);
  602. }
  603. /**
  604. * @brief Draws a full circle.
  605. * @param Xpos: X position
  606. * @param Ypos: Y position
  607. * @param Radius: Circle radius
  608. */
  609. void BSP_LCD_FillCircle(uint16_t Xpos, uint16_t Ypos, uint16_t Radius)
  610. {
  611. int32_t D; /* Decision Variable */
  612. uint32_t CurX; /* Current X Value */
  613. uint32_t CurY; /* Current Y Value */
  614. D = 3 - (Radius << 1);
  615. CurX = 0;
  616. CurY = Radius;
  617. BSP_LCD_SetTextColor(DrawProp.TextColor);
  618. while (CurX <= CurY)
  619. {
  620. if(CurY > 0)
  621. {
  622. BSP_LCD_DrawHLine(Xpos - CurY, Ypos + CurX, 2*CurY);
  623. BSP_LCD_DrawHLine(Xpos - CurY, Ypos - CurX, 2*CurY);
  624. }
  625. if(CurX > 0)
  626. {
  627. BSP_LCD_DrawHLine(Xpos - CurX, Ypos - CurY, 2*CurX);
  628. BSP_LCD_DrawHLine(Xpos - CurX, Ypos + CurY, 2*CurX);
  629. }
  630. if (D < 0)
  631. {
  632. D += (CurX << 2) + 6;
  633. }
  634. else
  635. {
  636. D += ((CurX - CurY) << 2) + 10;
  637. CurY--;
  638. }
  639. CurX++;
  640. }
  641. BSP_LCD_SetTextColor(DrawProp.TextColor);
  642. BSP_LCD_DrawCircle(Xpos, Ypos, Radius);
  643. }
  644. /**
  645. * @brief Draws a full poly-line (between many points).
  646. * @param Points: Pointer to the points array
  647. * @param PointCount: Number of points
  648. */
  649. void BSP_LCD_FillPolygon(pPoint Points, uint16_t PointCount)
  650. {
  651. 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;
  652. uint16_t IMAGE_LEFT = 0, IMAGE_RIGHT = 0, IMAGE_TOP = 0, IMAGE_BOTTOM = 0;
  653. IMAGE_LEFT = IMAGE_RIGHT = Points->X;
  654. IMAGE_TOP= IMAGE_BOTTOM = Points->Y;
  655. for(counter = 1; counter < PointCount; counter++)
  656. {
  657. pixelX = POLY_X(counter);
  658. if(pixelX < IMAGE_LEFT)
  659. {
  660. IMAGE_LEFT = pixelX;
  661. }
  662. if(pixelX > IMAGE_RIGHT)
  663. {
  664. IMAGE_RIGHT = pixelX;
  665. }
  666. pixelY = POLY_Y(counter);
  667. if(pixelY < IMAGE_TOP)
  668. {
  669. IMAGE_TOP = pixelY;
  670. }
  671. if(pixelY > IMAGE_BOTTOM)
  672. {
  673. IMAGE_BOTTOM = pixelY;
  674. }
  675. }
  676. if(PointCount < 2)
  677. {
  678. return;
  679. }
  680. X_center = (IMAGE_LEFT + IMAGE_RIGHT)/2;
  681. Y_center = (IMAGE_BOTTOM + IMAGE_TOP)/2;
  682. X_first = Points->X;
  683. Y_first = Points->Y;
  684. while(--PointCount)
  685. {
  686. X = Points->X;
  687. Y = Points->Y;
  688. Points++;
  689. X2 = Points->X;
  690. Y2 = Points->Y;
  691. FillTriangle(X, X2, X_center, Y, Y2, Y_center);
  692. FillTriangle(X, X_center, X2, Y, Y_center, Y2);
  693. FillTriangle(X_center, X2, X, Y_center, Y2, Y);
  694. }
  695. FillTriangle(X_first, X2, X_center, Y_first, Y2, Y_center);
  696. FillTriangle(X_first, X_center, X2, Y_first, Y_center, Y2);
  697. FillTriangle(X_center, X2, X_first, Y_center, Y2, Y_first);
  698. }
  699. /**
  700. * @brief Draws a full ellipse.
  701. * @param Xpos: X position
  702. * @param Ypos: Y position
  703. * @param XRadius: Ellipse X radius
  704. * @param YRadius: Ellipse Y radius
  705. */
  706. void BSP_LCD_FillEllipse(int Xpos, int Ypos, int XRadius, int YRadius)
  707. {
  708. int x = 0, y = -YRadius, err = 2-2*XRadius, e2;
  709. float K = 0, rad1 = 0, rad2 = 0;
  710. rad1 = XRadius;
  711. rad2 = YRadius;
  712. K = (float)(rad2/rad1);
  713. do
  714. {
  715. BSP_LCD_DrawHLine((Xpos-(uint16_t)(x/K)), (Ypos+y), (2*(uint16_t)(x/K) + 1));
  716. BSP_LCD_DrawHLine((Xpos-(uint16_t)(x/K)), (Ypos-y), (2*(uint16_t)(x/K) + 1));
  717. e2 = err;
  718. if (e2 <= x)
  719. {
  720. err += ++x*2+1;
  721. if (-y == x && e2 <= y) e2 = 0;
  722. }
  723. if (e2 > y) err += ++y*2+1;
  724. }
  725. while (y <= 0);
  726. }
  727. /**
  728. * @brief Enables the display.
  729. */
  730. void BSP_LCD_DisplayOn(void)
  731. {
  732. lcd_drv->DisplayOn();
  733. }
  734. /**
  735. * @brief Disables the display.
  736. */
  737. void BSP_LCD_DisplayOff(void)
  738. {
  739. lcd_drv->DisplayOff();
  740. }
  741. /******************************************************************************
  742. Static Function
  743. *******************************************************************************/
  744. /**
  745. * @brief Draws a character on LCD.
  746. * @param Xpos: Line where to display the character shape
  747. * @param Ypos: Start column address
  748. * @param c: Pointer to the character data
  749. */
  750. static void DrawChar(uint16_t Xpos, uint16_t Ypos, const uint8_t *c)
  751. {
  752. uint32_t i = 0, j = 0;
  753. uint16_t height, width;
  754. uint8_t offset;
  755. uint8_t *pchar;
  756. uint32_t line;
  757. height = DrawProp.pFont->Height;
  758. width = DrawProp.pFont->Width;
  759. offset = 8 *((width + 7)/8) - width ;
  760. for(i = 0; i < height; i++)
  761. {
  762. pchar = ((uint8_t *)c + (width + 7)/8 * i);
  763. switch(((width + 7)/8))
  764. {
  765. case 1:
  766. line = pchar[0];
  767. break;
  768. case 2:
  769. line = (pchar[0]<< 8) | pchar[1];
  770. break;
  771. case 3:
  772. default:
  773. line = (pchar[0]<< 16) | (pchar[1]<< 8) | pchar[2];
  774. break;
  775. }
  776. for (j = 0; j < width; j++)
  777. {
  778. if(line & (1 << (width- j + offset- 1)))
  779. {
  780. BSP_LCD_DrawPixel((Xpos + j), Ypos, DrawProp.TextColor);
  781. }
  782. else
  783. {
  784. BSP_LCD_DrawPixel((Xpos + j), Ypos, DrawProp.BackColor);
  785. }
  786. }
  787. Ypos++;
  788. }
  789. }
  790. /**
  791. * @brief Sets display window.
  792. * @param Xpos: LCD X position
  793. * @param Ypos: LCD Y position
  794. * @param Width: LCD window width
  795. * @param Height: LCD window height
  796. */
  797. static void SetDisplayWindow(uint16_t Xpos, uint16_t Ypos, uint16_t Width, uint16_t Height)
  798. {
  799. if(lcd_drv->SetDisplayWindow != NULL)
  800. {
  801. lcd_drv->SetDisplayWindow(Xpos, Ypos, Width, Height);
  802. }
  803. }
  804. /**
  805. * @brief Fills a triangle (between 3 points).
  806. * @param x1: Point 1 X position
  807. * @param y1: Point 1 Y position
  808. * @param x2: Point 2 X position
  809. * @param y2: Point 2 Y position
  810. * @param x3: Point 3 X position
  811. * @param y3: Point 3 Y position
  812. */
  813. static void FillTriangle(uint16_t x1, uint16_t x2, uint16_t x3, uint16_t y1, uint16_t y2, uint16_t y3)
  814. {
  815. int16_t deltax = 0, deltay = 0, x = 0, y = 0, xinc1 = 0, xinc2 = 0,
  816. yinc1 = 0, yinc2 = 0, den = 0, num = 0, numadd = 0, numpixels = 0,
  817. curpixel = 0;
  818. deltax = ABS(x2 - x1); /* The difference between the x's */
  819. deltay = ABS(y2 - y1); /* The difference between the y's */
  820. x = x1; /* Start x off at the first pixel */
  821. y = y1; /* Start y off at the first pixel */
  822. if (x2 >= x1) /* The x-values are increasing */
  823. {
  824. xinc1 = 1;
  825. xinc2 = 1;
  826. }
  827. else /* The x-values are decreasing */
  828. {
  829. xinc1 = -1;
  830. xinc2 = -1;
  831. }
  832. if (y2 >= y1) /* The y-values are increasing */
  833. {
  834. yinc1 = 1;
  835. yinc2 = 1;
  836. }
  837. else /* The y-values are decreasing */
  838. {
  839. yinc1 = -1;
  840. yinc2 = -1;
  841. }
  842. if (deltax >= deltay) /* There is at least one x-value for every y-value */
  843. {
  844. xinc1 = 0; /* Don't change the x when numerator >= denominator */
  845. yinc2 = 0; /* Don't change the y for every iteration */
  846. den = deltax;
  847. num = deltax / 2;
  848. numadd = deltay;
  849. numpixels = deltax; /* There are more x-values than y-values */
  850. }
  851. else /* There is at least one y-value for every x-value */
  852. {
  853. xinc2 = 0; /* Don't change the x for every iteration */
  854. yinc1 = 0; /* Don't change the y when numerator >= denominator */
  855. den = deltay;
  856. num = deltay / 2;
  857. numadd = deltax;
  858. numpixels = deltay; /* There are more y-values than x-values */
  859. }
  860. for (curpixel = 0; curpixel <= numpixels; curpixel++)
  861. {
  862. BSP_LCD_DrawLine(x, y, x3, y3);
  863. num += numadd; /* Increase the numerator by the top of the fraction */
  864. if (num >= den) /* Check if numerator >= denominator */
  865. {
  866. num -= den; /* Calculate the new numerator value */
  867. x += xinc1; /* Change the x as appropriate */
  868. y += yinc1; /* Change the y as appropriate */
  869. }
  870. x += xinc2; /* Change the x as appropriate */
  871. y += yinc2; /* Change the y as appropriate */
  872. }
  873. }
  874. /**
  875. * @}
  876. */
  877. /**
  878. * @}
  879. */
  880. /**
  881. * @}
  882. */
  883. /**
  884. * @}
  885. */
  886. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/