stm32_adafruit_lcd.c 28 KB

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