lcd.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #include "gd32f30x_libopt.h"
  2. #include "lcd.h"
  3. #include "font.h"
  4. #define BIT(x) (1<<x)
  5. #define SWAP16(a) ((uint16_t)((a<<8)|(a>>8)))
  6. typedef struct {
  7. int x;
  8. int y;
  9. int w;
  10. int h;
  11. }lcd_rect_t;
  12. static lcd_handle_t lcdHandle={0};
  13. extern int st77xx_init(lcd_handle_t *h);
  14. extern int st77xx_deinit(lcd_handle_t *h);
  15. extern void st77xx_set_win(lcd_handle_t *hd, uint16_t x, uint16_t y, uint16_t w, uint16_t h);
  16. extern void st77xx_refresh(lcd_handle_t *hd, uint16_t x, uint16_t y, uint16_t w, uint16_t h, void *data, uint32_t len);
  17. extern void st77xx_rotate(lcd_handle_t *h, int rotate);
  18. extern void st77xx_draw_pixel(lcd_handle_t *hd, uint16_t x, uint16_t y, uint32_t color);
  19. extern void st77xx_fill(lcd_handle_t *h, uint32_t color);
  20. extern void st77xx_fill_rect(lcd_handle_t *hd, uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint32_t color);
  21. static inline int set_point(lcd_handle_t *h, int x, int y, uint32_t data)
  22. {
  23. #if 0
  24. int pos,bit;
  25. if(h->rotate==180) {
  26. x = h->width-1-x;
  27. y = h->height-1-y;
  28. }
  29. pos = y / 8; /* get y page */
  30. bit = y % 8; /* get y point */
  31. if (data) { /* if 1 */
  32. h->buf[pos*h->width+x] |= BIT(bit); /* set 1 */
  33. }
  34. else {
  35. h->buf[pos*h->width+x] &= ~BIT(bit); /* set 0 */
  36. }
  37. #else
  38. st77xx_draw_pixel(h,x,y,data);
  39. #endif
  40. return 0;
  41. }
  42. static int get_color_bits(int color)
  43. {
  44. if(color==COLOR_WB) {
  45. return 1;
  46. }
  47. else if(color==COLOR_RGB8) {
  48. return 8;
  49. }
  50. else if(color==COLOR_RGB565) {
  51. return 16;
  52. }
  53. else {
  54. return 32;
  55. }
  56. }
  57. ////////////////////////////////////////////////////////////
  58. void lcd_init(int width, int height, int rotate)
  59. {
  60. lcd_handle_t *h=&lcdHandle;
  61. h->width = width;
  62. h->height = height;
  63. h->rotate = rotate;
  64. st77xx_init(h);
  65. }
  66. void lcd_deinit(void)
  67. {
  68. lcd_handle_t *h=&lcdHandle;
  69. //lcd_hw_deinit(h);
  70. }
  71. void lcd_rotate(int rotate)
  72. {
  73. lcd_handle_t *h=&lcdHandle;
  74. h->rotate = rotate;
  75. st77xx_rotate(h, rotate);
  76. }
  77. int lcd_get_rotate(void)
  78. {
  79. lcd_handle_t *h=&lcdHandle;
  80. return h->rotate;
  81. }
  82. void lcd_set_backlight(int on)
  83. {
  84. lcd_handle_t *h=&lcdHandle;
  85. }
  86. void lcd_set_win(int x, int y, int w, int h)
  87. {
  88. }
  89. void lcd_fill(uint32_t color)
  90. {
  91. lcd_handle_t *h=&lcdHandle;
  92. st77xx_fill(h, color);
  93. }
  94. void lcd_draw_line(int x1, int y1, int x2, int y2, uint32_t color)
  95. {
  96. int incx = 0, incy = 0;
  97. int delta_x = 0, delta_y = 0;
  98. int distance = 0;
  99. int t = 0;
  100. int x = 0, y = 0;
  101. int x_temp = 0, y_temp = 0;
  102. lcd_handle_t *h=&lcdHandle;
  103. /* 画斜线(Bresenham算法) */
  104. delta_x = x2 - x1;
  105. delta_y = y2 - y1;
  106. if(delta_x > 0) {
  107. //斜线(从左到右)
  108. incx = 1;
  109. }
  110. else if(delta_x == 0) {
  111. //垂直斜线(竖线)
  112. incx = 0;
  113. }
  114. else {
  115. //斜线(从右到左)
  116. incx = -1;
  117. delta_x = -delta_x;
  118. }
  119. if(delta_y > 0) {
  120. //斜线(从左到右)
  121. incy = 1;
  122. }
  123. else if(delta_y == 0) {
  124. //水平斜线(水平线)
  125. incy = 0;
  126. }
  127. else {
  128. //斜线(从右到左)
  129. incy = -1;
  130. delta_y = -delta_y;
  131. }
  132. /* 计算画笔打点距离(取两个间距中的最大值) */
  133. if(delta_x > delta_y) {
  134. distance = delta_x;
  135. }
  136. else {
  137. distance = delta_y;
  138. }
  139. /* 开始打点 */
  140. x = x1;
  141. y = y1;
  142. //第一个点无效,所以t的次数加一
  143. for(t = 0; t <= distance + 1;t++) {
  144. set_point(h, x, y, color);
  145. /* 判断离实际值最近的像素点 */
  146. x_temp += delta_x;
  147. if(x_temp > distance) {
  148. //x方向越界,减去距离值,为下一次检测做准备
  149. x_temp -= distance;
  150. //在x方向递增打点
  151. x += incx;
  152. }
  153. y_temp += delta_y;
  154. if(y_temp > distance) {
  155. //y方向越界,减去距离值,为下一次检测做准备
  156. y_temp -= distance;
  157. //在y方向递增打点
  158. y += incy;
  159. }
  160. }
  161. }
  162. void lcd_draw_rect(int x, int y, int w, int h, uint32_t color)
  163. {
  164. int x2,y2;
  165. x2 = x + w;
  166. y2 = y + h;
  167. lcd_draw_line(x,y,x2,y,color);
  168. lcd_draw_line(x,y,x,y2,color);
  169. lcd_draw_line(x,y2,x2,y2,color);
  170. lcd_draw_line(x,y,x2,y2,color);
  171. }
  172. void lcd_fill_rect(int x, int y, int w, int h, uint32_t color)
  173. {
  174. lcd_handle_t *hd=&lcdHandle;
  175. if(w==0 || h==0) {
  176. return;
  177. }
  178. st77xx_fill_rect(hd, x, y, w, h, color);
  179. }
  180. void lcd_refresh(void)
  181. {
  182. lcd_handle_t *h=&lcdHandle;
  183. }
  184. static void set_color(lcd_handle_t *h, uint32_t color, int cnt)
  185. {
  186. int i,xcnt=(cnt>h->buflen/2)?h->buflen/2:cnt;
  187. for(i=0; i<xcnt; i++) {
  188. h->buf[i] = color;
  189. }
  190. }
  191. int lcd_draw_char(lcd_handle_t *hd, int x, int y, int w, int h, uint8_t *c, int font, uint32_t color, uint32_t bgcolor, int vert)
  192. {
  193. uint8_t tmp;
  194. font_data_t fdata;
  195. int i,j,x0=x,y0=y;
  196. int x1=x0,y1=y0;
  197. fdata = font_data(font, c);
  198. if(h>fdata.height) {
  199. if(vert==VERTICAL_CENTER) {
  200. y1 += (h-fdata.height)/2;
  201. }
  202. else if (vert==VERTICAL_BOTTOM) {
  203. y1 += h-fdata.height;
  204. }
  205. y = y1;
  206. }
  207. int x2=x0+fdata.width,y2=y0+h;
  208. #if 1
  209. set_color(hd, bgcolor, h*fdata.width);
  210. for(i=0; i<fdata.dlen; i++) {
  211. tmp = fdata.data[i];
  212. for(j=0; j<8; j++) {
  213. if((x<x2 && x<hd->width) && (y<y2 && y<hd->height)) {
  214. int dx=x-x0,dy=y-y0;
  215. if(tmp&0x01) {
  216. hd->buf[dy*fdata.width+dx] = color;
  217. }
  218. }
  219. tmp >>= 1;
  220. y++;
  221. if((y-y1)>=fdata.height) {
  222. y=y1; x++;
  223. break;
  224. }
  225. }
  226. }
  227. st77xx_refresh(hd, x0, y0, fdata.width, h, hd->buf, fdata.width*h*2);
  228. #else
  229. for(i=0; i<fdata.dlen; i++) {
  230. tmp = fdata.data[i];
  231. for(j=0; j<8; j++) {
  232. if((x<x2 && x<hd->width) && (y<y2 && y<hd->height)) {
  233. if(tmp&0x01) {
  234. set_point(hd, x, y, color);
  235. }
  236. else {
  237. set_point(hd, x, y, bgcolor);
  238. }
  239. }
  240. tmp >>= 1;
  241. y++;
  242. if((y-y1)>=fdata.height) {
  243. y=y1; x++;
  244. break;
  245. }
  246. }
  247. }
  248. #endif
  249. return fdata.width;
  250. }
  251. void lcd_draw_string(int x, int y, int w, int h, char *str, int font, uint32_t color, uint32_t bgcolor, int hori, int vert)
  252. {
  253. int r,sw,xmax=x+w;
  254. lcd_rect_t rect;
  255. uint8_t *pstr=(uint8_t*)str;
  256. lcd_handle_t *hd=&lcdHandle;
  257. sw = font_width_str(font, pstr);
  258. if(sw>w) {
  259. sw = w;
  260. }
  261. if(hori==HORIZONTAL_LEFT) {
  262. rect.x = x;
  263. }
  264. else if(hori==HORIZONTAL_RIGHT) {
  265. rect.x = x+w-sw;
  266. }
  267. else if(hori==HORIZONTAL_CENTER) {
  268. rect.x = x+(w-sw)/2;
  269. }
  270. else {
  271. return;
  272. }
  273. if(rect.x+sw>=w) {
  274. rect.w = w-rect.x;
  275. }
  276. else {
  277. rect.w = sw;
  278. }
  279. rect.y = y;
  280. rect.h = h;
  281. lcd_fill_rect(x, y, rect.x-x, h, bgcolor);
  282. while(*pstr) {
  283. r = lcd_draw_char(hd, rect.x, rect.y, rect.w, rect.h, pstr, font, color, bgcolor, vert);
  284. if(!pstr || x>=xmax) {
  285. break;
  286. }
  287. rect.x += r;
  288. pstr = font_next(pstr);
  289. }
  290. lcd_fill_rect(rect.x, y, x+w-rect.x, h, bgcolor);
  291. //底部
  292. }