lcd.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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_refresh(lcd_handle_t *hd, uint16_t x, uint16_t y, uint16_t w, uint16_t h, void *data, uint32_t len);
  20. extern void st77xx_rotate(lcd_handle_t *h, int rotate);
  21. extern void st77xx_draw_pixel(lcd_handle_t *hd, uint16_t x, uint16_t y, uint32_t color);
  22. extern void st77xx_fill(lcd_handle_t *h, uint32_t color);
  23. extern void st77xx_fill_rect(lcd_handle_t *hd, uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint32_t color);
  24. static inline int set_point(lcd_handle_t *h, int x, int y, uint32_t data)
  25. {
  26. #if 0
  27. int pos,bit;
  28. if(h->rotate==180) {
  29. x = h->width-1-x;
  30. y = h->height-1-y;
  31. }
  32. pos = y / 8; /* get y page */
  33. bit = y % 8; /* get y point */
  34. if (data) { /* if 1 */
  35. h->buf[pos*h->width+x] |= BIT(bit); /* set 1 */
  36. }
  37. else {
  38. h->buf[pos*h->width+x] &= ~BIT(bit); /* set 0 */
  39. }
  40. #else
  41. st77xx_draw_pixel(h,x,y,data);
  42. #endif
  43. return 0;
  44. }
  45. static int get_color_bits(int color)
  46. {
  47. if(color==COLOR_WB) {
  48. return 1;
  49. }
  50. else if(color==COLOR_RGB8) {
  51. return 8;
  52. }
  53. else if(color==COLOR_RGB565) {
  54. return 16;
  55. }
  56. else {
  57. return 32;
  58. }
  59. }
  60. ////////////////////////////////////////////////////////////
  61. void lcd_init(int width, int height, int rotate)
  62. {
  63. lcd_handle_t *h=&lcdHandle;
  64. h->width = width;
  65. h->height = height;
  66. h->rotate = rotate;
  67. st77xx_init(h);
  68. }
  69. void lcd_deinit(void)
  70. {
  71. lcd_handle_t *h=&lcdHandle;
  72. //lcd_hw_deinit(h);
  73. }
  74. void lcd_rotate(int rotate)
  75. {
  76. lcd_handle_t *h=&lcdHandle;
  77. if(rotate==0 || rotate==180) {
  78. h->rotate = rotate;
  79. }
  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. st77xx_fill_rect(hd, x, y, w, h, color);
  180. }
  181. void lcd_refresh(void)
  182. {
  183. lcd_handle_t *h=&lcdHandle;
  184. }
  185. static void set_color(lcd_handle_t *h, uint32_t color, int cnt)
  186. {
  187. int i,xcnt=(cnt>h->buflen/2)?h->buflen/2:cnt;
  188. for(i=0; i<xcnt; i++) {
  189. h->buf[i] = color;
  190. }
  191. }
  192. 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)
  193. {
  194. uint8_t tmp;
  195. font_data_t fdata;
  196. int i,j,x0=x,y0=y;
  197. int x1=x0,y1=y0;
  198. int x2=x0+w,y2=y0+h;
  199. fdata = font_data(font, c);
  200. if(h>fdata.height) {
  201. if(vert==VERTICAL_CENTER) {
  202. y1 += (h-fdata.height)/2;
  203. }
  204. else if (vert==VERTICAL_BOTTOM) {
  205. y1 += h-fdata.height;
  206. }
  207. y = y1;
  208. }
  209. #if 0
  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. {
  215. if(tmp&0x01) {
  216. set_point(hd, x, y, color);
  217. }
  218. else {
  219. set_point(hd, x, y, bgcolor);
  220. }
  221. }
  222. tmp >>= 1;
  223. y++;
  224. if((y-y1)>=fdata.height) {
  225. y=y1; x++;
  226. break;
  227. }
  228. }
  229. }
  230. #else
  231. #if 0
  232. for(i=0; i<fdata.dlen; i++) {
  233. tmp = fdata.data[i];
  234. for(j=0; j<8; j++) {
  235. if((x<x2 && x<hd->width) && (y<y2 && y<hd->height))
  236. {
  237. int dx=x-x1,dy=y-y1;
  238. if(tmp&0x01) {
  239. hd->buf[dy*fdata.width+dx] = color;
  240. }
  241. }
  242. tmp >>= 1;
  243. y++;
  244. if((y-y1)>=fdata.height) {
  245. y=y1; x++;
  246. break;
  247. }
  248. }
  249. }
  250. st77xx_refresh(hd, x1, y1, fdata.width, fdata.height, hd->buf, fdata.width*fdata.height*2);
  251. #else
  252. for(i=0; i<fdata.dlen; i++) {
  253. tmp = fdata.data[i];
  254. for(j=0; j<8; j++) {
  255. if((x<x2 && x<hd->width) && (y<y2 && y<hd->height))
  256. {
  257. if(tmp&0x01) {
  258. set_point(hd, x, y, color);
  259. }
  260. else {
  261. set_point(hd, x, y, bgcolor);
  262. }
  263. }
  264. tmp >>= 1;
  265. y++;
  266. if((y-y1)>=fdata.height) {
  267. y=y1; x++;
  268. break;
  269. }
  270. }
  271. }
  272. #endif
  273. #endif
  274. return fdata.width;
  275. }
  276. 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)
  277. {
  278. int r,sw,xmax=x+w;
  279. lcd_rect_t rect;
  280. uint8_t *pstr=(uint8_t*)str;
  281. lcd_handle_t *hd=&lcdHandle;
  282. sw = font_width_str(font, pstr);
  283. if(sw>w) {
  284. sw = w;
  285. }
  286. if(hori==HORIZONTAL_LEFT) {
  287. rect.x = x;
  288. }
  289. else if(hori==HORIZONTAL_RIGHT) {
  290. rect.x = x+w-sw;
  291. }
  292. else if(hori==HORIZONTAL_CENTER) {
  293. rect.x = x+(w-sw)/2;
  294. }
  295. else {
  296. return;
  297. }
  298. if(rect.x+sw>=hd->width) {
  299. rect.w = hd->width-rect.x;
  300. }
  301. else {
  302. rect.w = sw;
  303. }
  304. rect.y = y;
  305. rect.h = h;
  306. while(*pstr) {
  307. r = lcd_draw_char(hd, rect.x, rect.y, rect.w, rect.h, pstr, font, color, bgcolor, vert);
  308. rect.x += r;
  309. if(!pstr || x>=xmax) {
  310. break;
  311. }
  312. pstr = font_next(pstr);
  313. }
  314. }