GUI_Paint.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. /******************************************************************************
  2. * | File : GUI_Paint.c
  3. * | Author : Waveshare electronics
  4. * | Function : Achieve drawing: draw points, lines, boxes, circles and
  5. * their size, solid dotted line, solid rectangle hollow
  6. * rectangle, solid circle hollow circle.
  7. * | Info :
  8. * Achieve display characters: Display a single character, string, number
  9. * Achieve time display: adaptive size display time minutes and seconds
  10. *----------------
  11. * | This version: V3.0
  12. * | Date : 2019-04-18
  13. * | Info :
  14. * -----------------------------------------------------------------------------
  15. * V3.0(2019-04-18):
  16. * 1.Change:
  17. * Paint_DrawPoint(..., DOT_STYLE DOT_STYLE)
  18. * => Paint_DrawPoint(..., DOT_STYLE Dot_Style)
  19. * Paint_DrawLine(..., LINE_STYLE Line_Style, DOT_PIXEL Dot_Pixel)
  20. * => Paint_DrawLine(..., DOT_PIXEL Line_width, LINE_STYLE Line_Style)
  21. * Paint_DrawRectangle(..., DRAW_FILL Filled, DOT_PIXEL Dot_Pixel)
  22. * => Paint_DrawRectangle(..., DOT_PIXEL Line_width, DRAW_FILL Draw_Fill)
  23. * Paint_DrawCircle(..., DRAW_FILL Draw_Fill, DOT_PIXEL Dot_Pixel)
  24. * => Paint_DrawCircle(..., DOT_PIXEL Line_width, DRAW_FILL Draw_Filll)
  25. *
  26. * -----------------------------------------------------------------------------
  27. * V2.0(2018-11-15):
  28. * 1.add: Paint_NewImage()
  29. * Create an image's properties
  30. * 2.add: Paint_SelectImage()
  31. * Select the picture to be drawn
  32. * 3.add: Paint_SetRotate()
  33. * Set the direction of the cache
  34. * 4.add: Paint_RotateImage()
  35. * Can flip the picture, Support 0-360 degrees,
  36. * but only 90.180.270 rotation is better
  37. * 4.add: Paint_SetMirroring()
  38. * Can Mirroring the picture, horizontal, vertical, origin
  39. * 5.add: Paint_DrawString_CN()
  40. * Can display Chinese(GB1312)
  41. *
  42. * -----------------------------------------------------------------------------
  43. * V1.0(2018-07-17):
  44. * Create library
  45. *
  46. * Permission is hereby granted, free of charge, to any person obtaining a copy
  47. * of this software and associated documnetation files (the "Software"), to deal
  48. * in the Software without restriction, including without limitation the rights
  49. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  50. * copies of the Software, and to permit persons to whom the Software is
  51. * furished to do so, subject to the following conditions:
  52. *
  53. * The above copyright notice and this permission notice shall be included in
  54. * all copies or substantial portions of the Software.
  55. *
  56. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  57. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  58. * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  59. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  60. * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  61. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  62. * THE SOFTWARE.
  63. *
  64. ******************************************************************************/
  65. #include "GUI_Paint.h"
  66. #include "DEV_Config.h"
  67. #include "Debug.h"
  68. #include <stdio.h>
  69. #include <stdint.h>
  70. #include <stdlib.h>
  71. #include <string.h> //memset()
  72. #include <math.h>
  73. PAINT Paint;
  74. /******************************************************************************
  75. function: Create Image
  76. parameter:
  77. image : Pointer to the image cache
  78. width : The width of the picture
  79. Height : The height of the picture
  80. Color : Whether the picture is inverted
  81. ******************************************************************************/
  82. void Paint_NewImage(UBYTE *image, UWORD Width, UWORD Height, UWORD Rotate, UWORD Color)
  83. {
  84. Paint.Image = NULL;
  85. Paint.Image = image;
  86. Paint.WidthMemory = Width;
  87. Paint.HeightMemory = Height;
  88. Paint.Color = Color;
  89. Paint.Scale = 2;
  90. Paint.WidthByte = (Width % 8 == 0)? (Width / 8 ): (Width / 8 + 1);
  91. Paint.HeightByte = Height;
  92. // printf("WidthByte = %d, HeightByte = %d\r\n", Paint.WidthByte, Paint.HeightByte);
  93. // printf(" EPD_WIDTH / 8 = %d\r\n", 122 / 8);
  94. Paint.Rotate = Rotate;
  95. Paint.Mirror = MIRROR_NONE;
  96. if(Rotate == ROTATE_0 || Rotate == ROTATE_180) {
  97. Paint.Width = Width;
  98. Paint.Height = Height;
  99. } else {
  100. Paint.Width = Height;
  101. Paint.Height = Width;
  102. }
  103. }
  104. /******************************************************************************
  105. function: Select Image
  106. parameter:
  107. image : Pointer to the image cache
  108. ******************************************************************************/
  109. void Paint_SelectImage(UBYTE *image)
  110. {
  111. Paint.Image = image;
  112. }
  113. /******************************************************************************
  114. function: Select Image Rotate
  115. parameter:
  116. Rotate : 0,90,180,270
  117. ******************************************************************************/
  118. void Paint_SetRotate(UWORD Rotate)
  119. {
  120. if(Rotate == ROTATE_0 || Rotate == ROTATE_90 || Rotate == ROTATE_180 || Rotate == ROTATE_270) {
  121. Debug("Set image Rotate %d\r\n", Rotate);
  122. Paint.Rotate = Rotate;
  123. } else {
  124. Debug("rotate = 0, 90, 180, 270\r\n");
  125. }
  126. }
  127. void Paint_SetScale(UBYTE scale)
  128. {
  129. if(scale == 2){
  130. Paint.Scale = scale;
  131. Paint.WidthByte = (Paint.WidthMemory % 8 == 0)? (Paint.WidthMemory / 8 ): (Paint.WidthMemory / 8 + 1);
  132. }else if(scale == 4){
  133. Paint.Scale = scale;
  134. Paint.WidthByte = (Paint.WidthMemory % 4 == 0)? (Paint.WidthMemory / 4 ): (Paint.WidthMemory / 4 + 1);
  135. }else{
  136. Debug("Set Scale Input parameter error\r\n");
  137. Debug("Scale Only support: 2 4 \r\n");
  138. }
  139. }
  140. /******************************************************************************
  141. function: Select Image mirror
  142. parameter:
  143. mirror :Not mirror,Horizontal mirror,Vertical mirror,Origin mirror
  144. ******************************************************************************/
  145. void Paint_SetMirroring(UBYTE mirror)
  146. {
  147. if(mirror == MIRROR_NONE || mirror == MIRROR_HORIZONTAL ||
  148. mirror == MIRROR_VERTICAL || mirror == MIRROR_ORIGIN) {
  149. Debug("mirror image x:%s, y:%s\r\n",(mirror & 0x01)? "mirror":"none", ((mirror >> 1) & 0x01)? "mirror":"none");
  150. Paint.Mirror = mirror;
  151. } else {
  152. Debug("mirror should be MIRROR_NONE, MIRROR_HORIZONTAL, \
  153. MIRROR_VERTICAL or MIRROR_ORIGIN\r\n");
  154. }
  155. }
  156. /******************************************************************************
  157. function: Draw Pixels
  158. parameter:
  159. Xpoint : At point X
  160. Ypoint : At point Y
  161. Color : Painted colors
  162. ******************************************************************************/
  163. void Paint_SetPixel(UWORD Xpoint, UWORD Ypoint, UWORD Color)
  164. {
  165. if(Xpoint > Paint.Width || Ypoint > Paint.Height){
  166. Debug("Exceeding display boundaries\r\n");
  167. return;
  168. }
  169. UWORD X, Y;
  170. switch(Paint.Rotate) {
  171. case 0:
  172. X = Xpoint;
  173. Y = Ypoint;
  174. break;
  175. case 90:
  176. X = Paint.WidthMemory - Ypoint - 1;
  177. Y = Xpoint;
  178. break;
  179. case 180:
  180. X = Paint.WidthMemory - Xpoint - 1;
  181. Y = Paint.HeightMemory - Ypoint - 1;
  182. break;
  183. case 270:
  184. X = Ypoint;
  185. Y = Paint.HeightMemory - Xpoint - 1;
  186. break;
  187. default:
  188. return;
  189. }
  190. switch(Paint.Mirror) {
  191. case MIRROR_NONE:
  192. break;
  193. case MIRROR_HORIZONTAL:
  194. X = Paint.WidthMemory - X - 1;
  195. break;
  196. case MIRROR_VERTICAL:
  197. Y = Paint.HeightMemory - Y - 1;
  198. break;
  199. case MIRROR_ORIGIN:
  200. X = Paint.WidthMemory - X - 1;
  201. Y = Paint.HeightMemory - Y - 1;
  202. break;
  203. default:
  204. return;
  205. }
  206. if(X > Paint.WidthMemory || Y > Paint.HeightMemory){
  207. Debug("Exceeding display boundaries\r\n");
  208. return;
  209. }
  210. if(Paint.Scale == 2){
  211. UDOUBLE Addr = X / 8 + Y * Paint.WidthByte;
  212. UBYTE Rdata = Paint.Image[Addr];
  213. if(Color == BLACK)
  214. Paint.Image[Addr] = Rdata & ~(0x80 >> (X % 8));
  215. else
  216. Paint.Image[Addr] = Rdata | (0x80 >> (X % 8));
  217. }else if(Paint.Scale == 4){
  218. UDOUBLE Addr = X / 4 + Y * Paint.WidthByte;
  219. Color = Color % 4;//Guaranteed color scale is 4 --- 0~3
  220. UBYTE Rdata = Paint.Image[Addr];
  221. Rdata = Rdata & (~(0xC0 >> ((X % 4)*2)));
  222. Paint.Image[Addr] = Rdata | ((Color << 6) >> ((X % 4)*2));
  223. }
  224. }
  225. /******************************************************************************
  226. function: Clear the color of the picture
  227. parameter:
  228. Color : Painted colors
  229. ******************************************************************************/
  230. void Paint_Clear(UWORD Color)
  231. {
  232. for (UWORD Y = 0; Y < Paint.HeightByte; Y++) {
  233. for (UWORD X = 0; X < Paint.WidthByte; X++ ) {//8 pixel = 1 byte
  234. UDOUBLE Addr = X + Y*Paint.WidthByte;
  235. Paint.Image[Addr] = Color;
  236. }
  237. }
  238. }
  239. /******************************************************************************
  240. function: Clear the color of a window
  241. parameter:
  242. Xstart : x starting point
  243. Ystart : Y starting point
  244. Xend : x end point
  245. Yend : y end point
  246. Color : Painted colors
  247. ******************************************************************************/
  248. void Paint_ClearWindows(UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend, UWORD Color)
  249. {
  250. UWORD X, Y;
  251. for (Y = Ystart; Y < Yend; Y++) {
  252. for (X = Xstart; X < Xend; X++) {//8 pixel = 1 byte
  253. Paint_SetPixel(X, Y, Color);
  254. }
  255. }
  256. }
  257. /******************************************************************************
  258. function: Draw Point(Xpoint, Ypoint) Fill the color
  259. parameter:
  260. Xpoint : The Xpoint coordinate of the point
  261. Ypoint : The Ypoint coordinate of the point
  262. Color : Painted color
  263. Dot_Pixel : point size
  264. Dot_Style : point Style
  265. ******************************************************************************/
  266. void Paint_DrawPoint(UWORD Xpoint, UWORD Ypoint, UWORD Color,
  267. DOT_PIXEL Dot_Pixel, DOT_STYLE Dot_Style)
  268. {
  269. if (Xpoint > Paint.Width || Ypoint > Paint.Height) {
  270. Debug("Paint_DrawPoint Input exceeds the normal display range\r\n");
  271. printf("Xpoint = %d , Paint.Width = %d \r\n ",Xpoint,Paint.Width);
  272. printf("Ypoint = %d , Paint.Height = %d \r\n ",Ypoint,Paint.Height);
  273. return;
  274. }
  275. int16_t XDir_Num , YDir_Num;
  276. if (Dot_Style == DOT_FILL_AROUND) {
  277. for (XDir_Num = 0; XDir_Num < 2 * Dot_Pixel - 1; XDir_Num++) {
  278. for (YDir_Num = 0; YDir_Num < 2 * Dot_Pixel - 1; YDir_Num++) {
  279. if(Xpoint + XDir_Num - Dot_Pixel < 0 || Ypoint + YDir_Num - Dot_Pixel < 0)
  280. break;
  281. // printf("x = %d, y = %d\r\n", Xpoint + XDir_Num - Dot_Pixel, Ypoint + YDir_Num - Dot_Pixel);
  282. Paint_SetPixel(Xpoint + XDir_Num - Dot_Pixel, Ypoint + YDir_Num - Dot_Pixel, Color);
  283. }
  284. }
  285. } else {
  286. for (XDir_Num = 0; XDir_Num < Dot_Pixel; XDir_Num++) {
  287. for (YDir_Num = 0; YDir_Num < Dot_Pixel; YDir_Num++) {
  288. Paint_SetPixel(Xpoint + XDir_Num - 1, Ypoint + YDir_Num - 1, Color);
  289. }
  290. }
  291. }
  292. }
  293. /******************************************************************************
  294. function: Draw a line of arbitrary slope
  295. parameter:
  296. Xstart :Starting Xpoint point coordinates
  297. Ystart :Starting Xpoint point coordinates
  298. Xend :End point Xpoint coordinate
  299. Yend :End point Ypoint coordinate
  300. Color :The color of the line segment
  301. Line_width : Line width
  302. Line_Style: Solid and dotted lines
  303. ******************************************************************************/
  304. void Paint_DrawLine(UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend,
  305. UWORD Color, DOT_PIXEL Line_width, LINE_STYLE Line_Style)
  306. {
  307. if (Xstart > Paint.Width || Ystart > Paint.Height ||
  308. Xend > Paint.Width || Yend > Paint.Height) {
  309. Debug("Paint_DrawLine Input exceeds the normal display range\r\n");
  310. return;
  311. }
  312. UWORD Xpoint = Xstart;
  313. UWORD Ypoint = Ystart;
  314. int dx = (int)Xend - (int)Xstart >= 0 ? Xend - Xstart : Xstart - Xend;
  315. int dy = (int)Yend - (int)Ystart <= 0 ? Yend - Ystart : Ystart - Yend;
  316. // Increment direction, 1 is positive, -1 is counter;
  317. int XAddway = Xstart < Xend ? 1 : -1;
  318. int YAddway = Ystart < Yend ? 1 : -1;
  319. //Cumulative error
  320. int Esp = dx + dy;
  321. char Dotted_Len = 0;
  322. for (;;) {
  323. Dotted_Len++;
  324. //Painted dotted line, 2 point is really virtual
  325. if (Line_Style == LINE_STYLE_DOTTED && Dotted_Len % 3 == 0) {
  326. //Debug("LINE_DOTTED\r\n");
  327. if(Color)
  328. Paint_DrawPoint(Xpoint, Ypoint, BLACK, Line_width, DOT_STYLE_DFT);
  329. else
  330. Paint_DrawPoint(Xpoint, Ypoint, WHITE, Line_width, DOT_STYLE_DFT);
  331. Dotted_Len = 0;
  332. } else {
  333. Paint_DrawPoint(Xpoint, Ypoint, Color, Line_width, DOT_STYLE_DFT);
  334. }
  335. if (2 * Esp >= dy) {
  336. if (Xpoint == Xend)
  337. break;
  338. Esp += dy;
  339. Xpoint += XAddway;
  340. }
  341. if (2 * Esp <= dx) {
  342. if (Ypoint == Yend)
  343. break;
  344. Esp += dx;
  345. Ypoint += YAddway;
  346. }
  347. }
  348. }
  349. /******************************************************************************
  350. function: Draw a rectangle
  351. parameter:
  352. Xstart :Rectangular Starting Xpoint point coordinates
  353. Ystart :Rectangular Starting Xpoint point coordinates
  354. Xend :Rectangular End point Xpoint coordinate
  355. Yend :Rectangular End point Ypoint coordinate
  356. Color :The color of the Rectangular segment
  357. Line_width: Line width
  358. Draw_Fill : Whether to fill the inside of the rectangle
  359. ******************************************************************************/
  360. void Paint_DrawRectangle(UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend,
  361. UWORD Color, DOT_PIXEL Line_width, DRAW_FILL Draw_Fill)
  362. {
  363. if (Xstart > Paint.Width || Ystart > Paint.Height ||
  364. Xend > Paint.Width || Yend > Paint.Height) {
  365. Debug("Input exceeds the normal display range\r\n");
  366. return;
  367. }
  368. if (Draw_Fill) {
  369. UWORD Ypoint;
  370. for(Ypoint = Ystart; Ypoint < Yend; Ypoint++) {
  371. Paint_DrawLine(Xstart, Ypoint, Xend, Ypoint, Color , Line_width, LINE_STYLE_SOLID);
  372. }
  373. } else {
  374. Paint_DrawLine(Xstart, Ystart, Xend, Ystart, Color, Line_width, LINE_STYLE_SOLID);
  375. Paint_DrawLine(Xstart, Ystart, Xstart, Yend, Color, Line_width, LINE_STYLE_SOLID);
  376. Paint_DrawLine(Xend, Yend, Xend, Ystart, Color, Line_width, LINE_STYLE_SOLID);
  377. Paint_DrawLine(Xend, Yend, Xstart, Yend, Color, Line_width, LINE_STYLE_SOLID);
  378. }
  379. }
  380. /******************************************************************************
  381. function: Use the 8-point method to draw a circle of the
  382. specified size at the specified position->
  383. parameter:
  384. X_Center :Center X coordinate
  385. Y_Center :Center Y coordinate
  386. Radius :circle Radius
  387. Color :The color of the :circle segment
  388. Line_width: Line width
  389. Draw_Fill : Whether to fill the inside of the Circle
  390. ******************************************************************************/
  391. void Paint_DrawCircle(UWORD X_Center, UWORD Y_Center, UWORD Radius,
  392. UWORD Color, DOT_PIXEL Line_width, DRAW_FILL Draw_Fill)
  393. {
  394. if (X_Center > Paint.Width || Y_Center >= Paint.Height) {
  395. Debug("Paint_DrawCircle Input exceeds the normal display range\r\n");
  396. return;
  397. }
  398. //Draw a circle from(0, R) as a starting point
  399. int16_t XCurrent, YCurrent;
  400. XCurrent = 0;
  401. YCurrent = Radius;
  402. //Cumulative error,judge the next point of the logo
  403. int16_t Esp = 3 - (Radius << 1 );
  404. int16_t sCountY;
  405. if (Draw_Fill == DRAW_FILL_FULL) {
  406. while (XCurrent <= YCurrent ) { //Realistic circles
  407. for (sCountY = XCurrent; sCountY <= YCurrent; sCountY ++ ) {
  408. Paint_DrawPoint(X_Center + XCurrent, Y_Center + sCountY, Color, DOT_PIXEL_DFT, DOT_STYLE_DFT);//1
  409. Paint_DrawPoint(X_Center - XCurrent, Y_Center + sCountY, Color, DOT_PIXEL_DFT, DOT_STYLE_DFT);//2
  410. Paint_DrawPoint(X_Center - sCountY, Y_Center + XCurrent, Color, DOT_PIXEL_DFT, DOT_STYLE_DFT);//3
  411. Paint_DrawPoint(X_Center - sCountY, Y_Center - XCurrent, Color, DOT_PIXEL_DFT, DOT_STYLE_DFT);//4
  412. Paint_DrawPoint(X_Center - XCurrent, Y_Center - sCountY, Color, DOT_PIXEL_DFT, DOT_STYLE_DFT);//5
  413. Paint_DrawPoint(X_Center + XCurrent, Y_Center - sCountY, Color, DOT_PIXEL_DFT, DOT_STYLE_DFT);//6
  414. Paint_DrawPoint(X_Center + sCountY, Y_Center - XCurrent, Color, DOT_PIXEL_DFT, DOT_STYLE_DFT);//7
  415. Paint_DrawPoint(X_Center + sCountY, Y_Center + XCurrent, Color, DOT_PIXEL_DFT, DOT_STYLE_DFT);
  416. }
  417. if (Esp < 0 )
  418. Esp += 4 * XCurrent + 6;
  419. else {
  420. Esp += 10 + 4 * (XCurrent - YCurrent );
  421. YCurrent --;
  422. }
  423. XCurrent ++;
  424. }
  425. } else { //Draw a hollow circle
  426. while (XCurrent <= YCurrent ) {
  427. Paint_DrawPoint(X_Center + XCurrent, Y_Center + YCurrent, Color, Line_width, DOT_STYLE_DFT);//1
  428. Paint_DrawPoint(X_Center - XCurrent, Y_Center + YCurrent, Color, Line_width, DOT_STYLE_DFT);//2
  429. Paint_DrawPoint(X_Center - YCurrent, Y_Center + XCurrent, Color, Line_width, DOT_STYLE_DFT);//3
  430. Paint_DrawPoint(X_Center - YCurrent, Y_Center - XCurrent, Color, Line_width, DOT_STYLE_DFT);//4
  431. Paint_DrawPoint(X_Center - XCurrent, Y_Center - YCurrent, Color, Line_width, DOT_STYLE_DFT);//5
  432. Paint_DrawPoint(X_Center + XCurrent, Y_Center - YCurrent, Color, Line_width, DOT_STYLE_DFT);//6
  433. Paint_DrawPoint(X_Center + YCurrent, Y_Center - XCurrent, Color, Line_width, DOT_STYLE_DFT);//7
  434. Paint_DrawPoint(X_Center + YCurrent, Y_Center + XCurrent, Color, Line_width, DOT_STYLE_DFT);//0
  435. if (Esp < 0 )
  436. Esp += 4 * XCurrent + 6;
  437. else {
  438. Esp += 10 + 4 * (XCurrent - YCurrent );
  439. YCurrent --;
  440. }
  441. XCurrent ++;
  442. }
  443. }
  444. }
  445. /******************************************************************************
  446. function: Show English characters
  447. parameter:
  448. Xpoint :X coordinate
  449. Ypoint :Y coordinate
  450. Acsii_Char :To display the English characters
  451. Font :A structure pointer that displays a character size
  452. Color_Foreground : Select the foreground color
  453. Color_Background : Select the background color
  454. ******************************************************************************/
  455. void Paint_DrawChar(UWORD Xpoint, UWORD Ypoint, const char Acsii_Char,
  456. sFONT* Font, UWORD Color_Foreground, UWORD Color_Background)
  457. {
  458. UWORD Page, Column;
  459. if (Xpoint > Paint.Width || Ypoint > Paint.Height) {
  460. //Debug("Paint_DrawChar Input exceeds the normal display range\r\n");
  461. return;
  462. }
  463. uint32_t Char_Offset = (Acsii_Char - ' ') * Font->Height * (Font->Width / 8 + (Font->Width % 8 ? 1 : 0));
  464. const unsigned char *ptr = &Font->table[Char_Offset];
  465. for ( Page = 0; Page < Font->Height; Page ++ ) {
  466. for ( Column = 0; Column < Font->Width; Column ++ ) {
  467. //To determine whether the font background color and screen background color is consistent
  468. if (FONT_BACKGROUND == Color_Background) { //this process is to speed up the scan
  469. if (pgm_read_byte(ptr) & (0x80 >> (Column % 8)))
  470. Paint_SetPixel (Xpoint + Column, Ypoint + Page, Color_Foreground );
  471. } else {
  472. if (pgm_read_byte(ptr) & (0x80 >> (Column % 8))) {
  473. Paint_SetPixel (Xpoint + Column, Ypoint + Page, Color_Foreground );
  474. } else {
  475. Paint_SetPixel (Xpoint + Column, Ypoint + Page, Color_Background );
  476. }
  477. }
  478. //One pixel is 8 bits
  479. if (Column % 8 == 7) {
  480. ptr++;
  481. }
  482. }/* Write a line */
  483. if (Font->Width % 8 != 0) {
  484. ptr++;
  485. }
  486. }/* Write all */
  487. }
  488. /******************************************************************************
  489. function: Display the string
  490. parameter:
  491. Xstart :X coordinate
  492. Ystart :Y coordinate
  493. pString :The first address of the English string to be displayed
  494. Font :A structure pointer that displays a character size
  495. Color_Foreground : Select the foreground color
  496. Color_Background : Select the background color
  497. ******************************************************************************/
  498. void Paint_DrawString_EN(UWORD Xstart, UWORD Ystart, const char * pString,
  499. sFONT* Font, UWORD Color_Foreground, UWORD Color_Background)
  500. {
  501. UWORD Xpoint = Xstart;
  502. UWORD Ypoint = Ystart;
  503. if (Xstart > Paint.Width || Ystart > Paint.Height) {
  504. Debug("Paint_DrawString_EN Input exceeds the normal display range\r\n");
  505. return;
  506. }
  507. while (* pString != '\0') {
  508. //if X direction filled , reposition to(Xstart,Ypoint),Ypoint is Y direction plus the Height of the character
  509. if ((Xpoint + Font->Width ) > Paint.Width ) {
  510. Xpoint = Xstart;
  511. Ypoint += Font->Height;
  512. }
  513. // If the Y direction is full, reposition to(Xstart, Ystart)
  514. if ((Ypoint + Font->Height ) > Paint.Height ) {
  515. Xpoint = Xstart;
  516. Ypoint = Ystart;
  517. }
  518. Paint_DrawChar(Xpoint, Ypoint, * pString, Font, Color_Background, Color_Foreground);
  519. //The next character of the address
  520. pString ++;
  521. //The next word of the abscissa increases the font of the broadband
  522. Xpoint += Font->Width;
  523. }
  524. }
  525. /******************************************************************************
  526. function: Display the string
  527. parameter:
  528. Xstart :X coordinate
  529. Ystart :Y coordinate
  530. pString :The first address of the Chinese string and English
  531. string to be displayed
  532. Font :A structure pointer that displays a character size
  533. Color_Foreground : Select the foreground color
  534. Color_Background : Select the background color
  535. ******************************************************************************/
  536. void Paint_DrawString_CN(UWORD Xstart, UWORD Ystart, const char * pString, cFONT* font, UWORD Color_Background, UWORD Color_Foreground)
  537. {
  538. const unsigned char* p_text = pString;
  539. int refcolumn = Xstart;
  540. int i, j, Num;
  541. /* Send the string character by character on EPD */
  542. while (*p_text != 0) {
  543. if (*p_text < 0x7F) { //ASCII
  544. for (Num = 0; Num < font->size ; Num++) {
  545. if (*p_text == pgm_read_byte(&font->table[Num].index[0])) {
  546. const char* ptr = &font->table[Num].matrix[0];
  547. for (j = 0; j < font->Height; j++) {
  548. for (i = 0; i < font->Width; i++) {
  549. if (pgm_read_byte(ptr) & (0x80 >> (i % 8))) {
  550. Paint_SetPixel(refcolumn + i,Ystart + j, Color_Foreground);
  551. }
  552. if (i % 8 == 7) {
  553. ptr++;
  554. }
  555. }
  556. if (font->Width % 8 != 0) {
  557. ptr++;
  558. }
  559. }
  560. break;
  561. }
  562. }
  563. /* Point on the next character */
  564. p_text += 1;
  565. /* Decrement the column position by 16 */
  566. refcolumn += font->ASCII_Width;
  567. } else { //中文
  568. for (Num = 0; Num < font->size ; Num++) {
  569. if ((*p_text == pgm_read_byte(&font->table[Num].index[0])) && (*(p_text + 1) == pgm_read_byte(&font->table[Num].index[1])) && (*(p_text + 2) == pgm_read_byte(&font->table[Num].index[2]))) {
  570. const char* ptr = &font->table[Num].matrix[0];
  571. for (j = 0; j < font->Height; j++) {
  572. for (i = 0; i < font->Width; i++) {
  573. if (pgm_read_byte(ptr) & (0x80 >> (i % 8))) {
  574. Paint_SetPixel(refcolumn + i,Ystart + j, Color_Foreground);
  575. }
  576. if (i % 8 == 7) {
  577. ptr++;
  578. }
  579. }
  580. if (font->Width % 8 != 0) {
  581. ptr++;
  582. }
  583. }
  584. break;
  585. }
  586. }
  587. /* Point on the next character */
  588. p_text += 3;
  589. /* Decrement the column position by 16 */
  590. refcolumn += font->Width;
  591. }
  592. }
  593. }
  594. /******************************************************************************
  595. function: Display nummber
  596. parameter:
  597. Xstart :X coordinate
  598. Ystart : Y coordinate
  599. Number : The number displayed
  600. Font :A structure pointer that displays a character size
  601. Digit : Fractional width
  602. Color_Foreground : Select the foreground color
  603. Color_Background : Select the background color
  604. ******************************************************************************/
  605. #define ARRAY_LEN 255
  606. void Paint_DrawNum(UWORD Xpoint, UWORD Ypoint,const char * Number,
  607. sFONT* Font, UWORD Digit,UWORD Color_Foreground, UWORD Color_Background)
  608. {
  609. uint8_t Str_Array[ARRAY_LEN] = {0};
  610. uint8_t *pStr = Str_Array;
  611. uint8_t i, len = 0;
  612. int16_t arr[3] = {0, 0, 0};
  613. int16_t *p = arr;
  614. if (Xpoint > Paint.Width || Ypoint > Paint.Height) {
  615. Debug("Paint_DisNum Input exceeds the normal display range\r\n");
  616. return;
  617. }
  618. while(Number[len] != '\0') {
  619. len++; //get total length
  620. (*p)++; //get the integer part length
  621. if(Number[len] == '.') {
  622. arr[2] = 1;
  623. arr[0]--;
  624. p++; //get fractional part length
  625. }
  626. }
  627. if(Digit > 0) {
  628. if(Digit <= arr[1]) {
  629. for(i=0; i<=len-(arr[1]-Digit); i++) //cut some Number
  630. Str_Array[i] = Number[i];
  631. }
  632. else {
  633. for(i=0; i<=len+Digit-arr[1]; i++) {
  634. if(i == len && arr[2] == 0)
  635. Str_Array[i] = '.';
  636. else if(i >= len) //add '0'
  637. Str_Array[i] = '0';
  638. else
  639. Str_Array[i] = Number[i];
  640. }
  641. }
  642. }
  643. else
  644. for(i=0; i<=len-arr[1]-arr[2]; i++) {
  645. Str_Array[i] = Number[i];
  646. }
  647. //show
  648. Paint_DrawString_EN(Xpoint, Ypoint, (const char*)pStr, Font, Color_Background, Color_Foreground);
  649. }
  650. /******************************************************************************
  651. function: Display time
  652. parameter:
  653. Xstart :X coordinate
  654. Ystart : Y coordinate
  655. pTime : Time-related structures
  656. Font :A structure pointer that displays a character size
  657. Color_Foreground : Select the foreground color
  658. Color_Background : Select the background color
  659. ******************************************************************************/
  660. void Paint_DrawTime(UWORD Xstart, UWORD Ystart, PAINT_TIME *pTime, sFONT* Font,
  661. UWORD Color_Foreground, UWORD Color_Background)
  662. {
  663. uint8_t value[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
  664. UWORD Dx = Font->Width;
  665. //Write data into the cache
  666. Paint_DrawChar(Xstart , Ystart, value[pTime->Hour / 10], Font, Color_Background, Color_Foreground);
  667. Paint_DrawChar(Xstart + Dx , Ystart, value[pTime->Hour % 10], Font, Color_Background, Color_Foreground);
  668. Paint_DrawChar(Xstart + Dx + Dx / 4 + Dx / 2 , Ystart, ':' , Font, Color_Background, Color_Foreground);
  669. Paint_DrawChar(Xstart + Dx * 2 + Dx / 2 , Ystart, value[pTime->Min / 10] , Font, Color_Background, Color_Foreground);
  670. Paint_DrawChar(Xstart + Dx * 3 + Dx / 2 , Ystart, value[pTime->Min % 10] , Font, Color_Background, Color_Foreground);
  671. Paint_DrawChar(Xstart + Dx * 4 + Dx / 2 - Dx / 4, Ystart, ':' , Font, Color_Background, Color_Foreground);
  672. Paint_DrawChar(Xstart + Dx * 5 , Ystart, value[pTime->Sec / 10] , Font, Color_Background, Color_Foreground);
  673. Paint_DrawChar(Xstart + Dx * 6 , Ystart, value[pTime->Sec % 10] , Font, Color_Background, Color_Foreground);
  674. }
  675. /******************************************************************************
  676. function: Display monochrome bitmap
  677. parameter:
  678. image_buffer :A picture data converted to a bitmap
  679. info:
  680. Use a computer to convert the image into a corresponding array,
  681. and then embed the array directly into Imagedata.cpp as a .c file.
  682. ******************************************************************************/
  683. void Paint_DrawBitMap(const unsigned char* image_buffer)
  684. {
  685. UWORD x, y;
  686. UDOUBLE Addr = 0;
  687. for (y = 0; y < Paint.HeightByte; y++) {
  688. for (x = 0; x < Paint.WidthByte; x++) {//8 pixel = 1 byte
  689. Addr = x + y * Paint.WidthByte;
  690. Paint.Image[Addr] = (unsigned char)image_buffer[Addr];
  691. }
  692. }
  693. }
  694. ///******************************************************************************
  695. //function: SDisplay half of monochrome bitmap
  696. //parameter:
  697. // Region : 1 Upper half
  698. // 2 Lower half
  699. //info:
  700. //******************************************************************************/
  701. //void Paint_DrawBitMap_Half(const unsigned char* image_buffer, UBYTE Region)
  702. //{
  703. // UWORD x, y;
  704. // UDOUBLE Addr = 0;
  705. //
  706. // if(Region == 1){
  707. // for (y = 0; y < Paint.HeightByte; y++) {
  708. // for (x = 0; x < Paint.WidthByte; x++) {//8 pixel = 1 byte
  709. // Addr = x + y * Paint.WidthByte;
  710. // Paint.Image[Addr] = (unsigned char)image_buffer[Addr];
  711. // }
  712. // }
  713. // }else{
  714. // for (y = 0; y < Paint.HeightByte; y++) {
  715. // for (x = 0; x < Paint.WidthByte; x++) {//8 pixel = 1 byte
  716. // Addr = x + y * Paint.WidthByte ;
  717. // Paint.Image[Addr] = \
  718. // (unsigned char)image_buffer[Addr+ (Paint.HeightByte)*Paint.WidthByte];
  719. // }
  720. // }
  721. // }
  722. //}
  723. ///******************************************************************************
  724. //function: SDisplay half of monochrome bitmap
  725. //parameter:
  726. // Region : 1 Upper half
  727. // 2 Lower half
  728. //info:
  729. //******************************************************************************/
  730. //void Paint_DrawBitMap_OneQuarter(const unsigned char* image_buffer, UBYTE Region)
  731. //{
  732. // UWORD x, y;
  733. // UDOUBLE Addr = 0;
  734. //
  735. // if(Region == 1){
  736. // for (y = 0; y < Paint.HeightByte; y++) {
  737. // for (x = 0; x < Paint.WidthByte; x++) {//8 pixel = 1 byte
  738. // Addr = x + y * Paint.WidthByte;
  739. // Paint.Image[Addr] = (unsigned char)image_buffer[Addr];
  740. // }
  741. // }
  742. // }else if(Region == 2){
  743. // for (y = 0; y < Paint.HeightByte; y++) {
  744. // for (x = 0; x < Paint.WidthByte; x++) {//8 pixel = 1 byte
  745. // Addr = x + y * Paint.WidthByte ;
  746. // Paint.Image[Addr] = \
  747. // (unsigned char)image_buffer[Addr+ (Paint.HeightByte)*Paint.WidthByte];
  748. // }
  749. // }
  750. // }else if(Region == 3){
  751. // for (y = 0; y < Paint.HeightByte; y++) {
  752. // for (x = 0; x < Paint.WidthByte; x++) {//8 pixel = 1 byte
  753. // Addr = x + y * Paint.WidthByte ;
  754. // Paint.Image[Addr] = \
  755. // (unsigned char)image_buffer[Addr+ (Paint.HeightByte)*Paint.WidthByte*2];
  756. // }
  757. // }
  758. // }else if(Region == 4){
  759. // for (y = 0; y < Paint.HeightByte; y++) {
  760. // for (x = 0; x < Paint.WidthByte; x++) {//8 pixel = 1 byte
  761. // Addr = x + y * Paint.WidthByte ;
  762. // Paint.Image[Addr] = \
  763. // (unsigned char)image_buffer[Addr+ (Paint.HeightByte)*Paint.WidthByte*3];
  764. // }
  765. // }
  766. // }
  767. //}
  768. void Paint_DrawBitMap_Block(const unsigned char* image_buffer, UBYTE Region)
  769. {
  770. UWORD x, y;
  771. UDOUBLE Addr = 0;
  772. for (y = 0; y < Paint.HeightByte; y++) {
  773. for (x = 0; x < Paint.WidthByte; x++) {//8 pixel = 1 byte
  774. Addr = x + y * Paint.WidthByte ;
  775. Paint.Image[Addr] = \
  776. (unsigned char)image_buffer[Addr+ (Paint.HeightByte)*Paint.WidthByte*(Region - 1)];
  777. }
  778. }
  779. }