lcd.c 6.9 KB

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