lcd.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. static inline int set_point(lcd_handle_t *h, int x, int y, uint32_t data)
  22. {
  23. int pos,bit;
  24. if(h->rotate==180) {
  25. x = h->width-1-x;
  26. y = h->height-1-y;
  27. }
  28. pos = y / 8; /* get y page */
  29. bit = y % 8; /* get y point */
  30. if (data) { /* if 1 */
  31. h->buf[pos*h->width+x] |= BIT(bit); /* set 1 */
  32. }
  33. else {
  34. h->buf[pos*h->width+x] &= ~BIT(bit); /* set 0 */
  35. }
  36. return 0;
  37. }
  38. static int get_color_bits(int color)
  39. {
  40. if(color==COLOR_WB) {
  41. return 1;
  42. }
  43. else if(color==COLOR_RGB8) {
  44. return 8;
  45. }
  46. else if(color==COLOR_RGB565) {
  47. return 16;
  48. }
  49. else {
  50. return 32;
  51. }
  52. }
  53. ////////////////////////////////////////////////////////////
  54. void lcd_init(int width, int height, uint32_t color)
  55. {
  56. lcd_handle_t *h=&lcdHandle;
  57. h->width = width;
  58. h->height = height;
  59. h->color = color;
  60. h->rotate = 0;
  61. st77xx_init(h);
  62. //lcd_draw_string(0, 0, LCD_WIDTH, LCD_HEIGHT/10, "SmartPDU", FONT_12, COLOR_WHITE, COLOR_BLACK, HORIZONTAL_CENTER, VERTICAL_CENTER);
  63. //lcd_hw_init(h);
  64. }
  65. void lcd_deinit(void)
  66. {
  67. lcd_handle_t *h=&lcdHandle;
  68. //lcd_hw_deinit(h);
  69. }
  70. void lcd_rotate(int rotate)
  71. {
  72. lcd_handle_t *h=&lcdHandle;
  73. if(rotate==0 || rotate==180) {
  74. h->rotate = rotate;
  75. }
  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. lcd_fill_rect(0, 0, h->width, h->height, 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. int i,j;
  175. lcd_handle_t *h2=&lcdHandle;
  176. for(i=x; i<x+w; i++) {
  177. for(j=y; j<y+h; j++) {
  178. set_point(h2, i, j, color);
  179. }
  180. }
  181. }
  182. void lcd_refresh(void)
  183. {
  184. lcd_handle_t *h=&lcdHandle;
  185. }
  186. 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)
  187. {
  188. uint8_t tmp;
  189. font_data_t fdata;
  190. int i,j,x0=x,y0=y,x1=x+w,y1=y+h;
  191. fdata = font_data(font, c);
  192. if(h>fdata.height) {
  193. if(vert==VERTICAL_CENTER) {
  194. y0 += (h-fdata.height)/2;
  195. }
  196. else if (vert==VERTICAL_BOTTOM) {
  197. y0 += h-fdata.height;
  198. }
  199. y = y0;
  200. }
  201. #if 0
  202. for(i=0; i<fdata.dlen; i++) {
  203. tmp = fdata.data[i];
  204. for(j=0; j<8; j++) {
  205. if((x<x1 && x<hd->width) && (y<y1 && y<hd->height))
  206. {
  207. if(tmp&0x01) {
  208. set_point(hd, x, y, color);
  209. }
  210. else {
  211. set_point(hd, x, y, bgcolor);
  212. }
  213. }
  214. tmp >>= 1;
  215. y++;
  216. if((y-y0)>=fdata.height) {
  217. y=y0; x++;
  218. break;
  219. }
  220. }
  221. }
  222. #else
  223. for(i=0; i<fdata.dlen; i++) {
  224. tmp = fdata.data[i];
  225. for(j=0; j<8; j++) {
  226. if((x<x1 && x<hd->width) && (y<y1 && y<hd->height))
  227. {
  228. int x1=x-x0,y1=y-y0;
  229. if(tmp&0x01) {
  230. hd->buf[y1*fdata.width+x1] = color;
  231. }
  232. else {
  233. hd->buf[y1*fdata.width+x1] = bgcolor;
  234. }
  235. }
  236. tmp >>= 1;
  237. y++;
  238. if((y-y0)>=fdata.height) {
  239. y=y0; x++;
  240. break;
  241. }
  242. }
  243. }
  244. st77xx_refresh(hd, x0, y0, fdata.width, fdata.height, hd->buf, fdata.width*fdata.height*2);
  245. #endif
  246. return fdata.width;
  247. }
  248. 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)
  249. {
  250. int r,sw,xmax=x+w;
  251. lcd_rect_t rect;
  252. uint8_t *pstr=(uint8_t*)str;
  253. lcd_handle_t *h2=&lcdHandle;
  254. sw = font_width_str(font, pstr);
  255. if(sw>w) {
  256. sw = w;
  257. }
  258. if(hori==HORIZONTAL_LEFT) {
  259. rect.x = x;
  260. }
  261. else if(hori==HORIZONTAL_RIGHT) {
  262. rect.x = x+w-sw;
  263. }
  264. else if(hori==HORIZONTAL_CENTER) {
  265. rect.x = x+(w-sw)/2;
  266. }
  267. else {
  268. return;
  269. }
  270. if(rect.x+sw>=h2->width) {
  271. rect.w = h2->width-rect.x;
  272. }
  273. else {
  274. rect.w = sw;
  275. }
  276. rect.y = y;
  277. rect.h = h;
  278. while(*pstr) {
  279. r = lcd_draw_char(h2, rect.x, rect.y, rect.w, rect.h, pstr, font, color, bgcolor, vert);
  280. rect.x += r;
  281. pstr = font_next(pstr);
  282. if(!pstr || x>=xmax) {
  283. break;
  284. }
  285. }
  286. }