lcd.c 7.6 KB

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