lcd.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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, int rotate)
  55. {
  56. lcd_handle_t *h=&lcdHandle;
  57. h->width = width;
  58. h->height = height;
  59. h->rotate = rotate;
  60. st77xx_init(h);
  61. lcd_draw_string(0, 60, LCD_WIDTH, LCD_HEIGHT/10, "SmartPDU", FONT_12, COLOR_WHITE, COLOR_BLACK, HORIZONTAL_CENTER, VERTICAL_CENTER);
  62. }
  63. void lcd_deinit(void)
  64. {
  65. lcd_handle_t *h=&lcdHandle;
  66. //lcd_hw_deinit(h);
  67. }
  68. void lcd_rotate(int rotate)
  69. {
  70. lcd_handle_t *h=&lcdHandle;
  71. if(rotate==0 || rotate==180) {
  72. h->rotate = rotate;
  73. }
  74. }
  75. int lcd_get_rotate(void)
  76. {
  77. lcd_handle_t *h=&lcdHandle;
  78. return h->rotate;
  79. }
  80. void lcd_set_backlight(int on)
  81. {
  82. lcd_handle_t *h=&lcdHandle;
  83. }
  84. void lcd_set_win(int x, int y, int w, int h)
  85. {
  86. }
  87. void lcd_fill(uint32_t color)
  88. {
  89. lcd_handle_t *h=&lcdHandle;
  90. lcd_fill_rect(0, 0, h->width, h->height, color);
  91. }
  92. void lcd_draw_line(int x1, int y1, int x2, int y2, uint32_t color)
  93. {
  94. int incx = 0, incy = 0;
  95. int delta_x = 0, delta_y = 0;
  96. int distance = 0;
  97. int t = 0;
  98. int x = 0, y = 0;
  99. int x_temp = 0, y_temp = 0;
  100. lcd_handle_t *h=&lcdHandle;
  101. /* 画斜线(Bresenham算法) */
  102. delta_x = x2 - x1;
  103. delta_y = y2 - y1;
  104. if(delta_x > 0) {
  105. //斜线(从左到右)
  106. incx = 1;
  107. }
  108. else if(delta_x == 0) {
  109. //垂直斜线(竖线)
  110. incx = 0;
  111. }
  112. else {
  113. //斜线(从右到左)
  114. incx = -1;
  115. delta_x = -delta_x;
  116. }
  117. if(delta_y > 0) {
  118. //斜线(从左到右)
  119. incy = 1;
  120. }
  121. else if(delta_y == 0) {
  122. //水平斜线(水平线)
  123. incy = 0;
  124. }
  125. else {
  126. //斜线(从右到左)
  127. incy = -1;
  128. delta_y = -delta_y;
  129. }
  130. /* 计算画笔打点距离(取两个间距中的最大值) */
  131. if(delta_x > delta_y) {
  132. distance = delta_x;
  133. }
  134. else {
  135. distance = delta_y;
  136. }
  137. /* 开始打点 */
  138. x = x1;
  139. y = y1;
  140. //第一个点无效,所以t的次数加一
  141. for(t = 0; t <= distance + 1;t++) {
  142. set_point(h, x, y, color);
  143. /* 判断离实际值最近的像素点 */
  144. x_temp += delta_x;
  145. if(x_temp > distance) {
  146. //x方向越界,减去距离值,为下一次检测做准备
  147. x_temp -= distance;
  148. //在x方向递增打点
  149. x += incx;
  150. }
  151. y_temp += delta_y;
  152. if(y_temp > distance) {
  153. //y方向越界,减去距离值,为下一次检测做准备
  154. y_temp -= distance;
  155. //在y方向递增打点
  156. y += incy;
  157. }
  158. }
  159. }
  160. void lcd_draw_rect(int x, int y, int w, int h, uint32_t color)
  161. {
  162. int x2,y2;
  163. x2 = x + w;
  164. y2 = y + h;
  165. lcd_draw_line(x,y,x2,y,color);
  166. lcd_draw_line(x,y,x,y2,color);
  167. lcd_draw_line(x,y2,x2,y2,color);
  168. lcd_draw_line(x,y,x2,y2,color);
  169. }
  170. void lcd_fill_rect(int x, int y, int w, int h, uint32_t color)
  171. {
  172. int i,j;
  173. lcd_handle_t *h2=&lcdHandle;
  174. for(i=x; i<x+w; i++) {
  175. for(j=y; j<y+h; j++) {
  176. set_point(h2, i, j, color);
  177. }
  178. }
  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. int x2=x0+w,y2=y0+h;
  198. fdata = font_data(font, c);
  199. if(h>fdata.height) {
  200. if(vert==VERTICAL_CENTER) {
  201. y1 += (h-fdata.height)/2;
  202. }
  203. else if (vert==VERTICAL_BOTTOM) {
  204. y1 += h-fdata.height;
  205. }
  206. y = y1;
  207. }
  208. #if 0
  209. for(i=0; i<fdata.dlen; i++) {
  210. tmp = fdata.data[i];
  211. for(j=0; j<8; j++) {
  212. if((x<x2 && x<hd->width) && (y<y2 && y<hd->height))
  213. {
  214. if(tmp&0x01) {
  215. set_point(hd, x, y, color);
  216. }
  217. else {
  218. set_point(hd, x, y, bgcolor);
  219. }
  220. }
  221. tmp >>= 1;
  222. y++;
  223. if((y-y1)>=fdata.height) {
  224. y=y1; x++;
  225. break;
  226. }
  227. }
  228. }
  229. #else
  230. set_color(hd, bgcolor, fdata.width*h);
  231. for(i=0; i<fdata.dlen; i++) {
  232. tmp = fdata.data[i];
  233. for(j=0; j<8; j++) {
  234. if((x<x2 && x<hd->width) && (y<y2 && y<hd->height))
  235. {
  236. int dx=x-x0,dy=y-y0;
  237. if(tmp&0x01) {
  238. hd->buf[dy*fdata.width+dx] = color;
  239. }
  240. }
  241. tmp >>= 1;
  242. y++;
  243. if((y-y1)>=fdata.height) {
  244. y=y1; x++;
  245. break;
  246. }
  247. }
  248. }
  249. st77xx_refresh(hd, x0, y0, fdata.width, h, hd->buf, fdata.width*h*2);
  250. #endif
  251. return fdata.width;
  252. }
  253. 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)
  254. {
  255. int r,sw,xmax=x+w;
  256. lcd_rect_t rect;
  257. uint8_t *pstr=(uint8_t*)str;
  258. lcd_handle_t *hd=&lcdHandle;
  259. sw = font_width_str(font, pstr);
  260. if(sw>w) {
  261. sw = w;
  262. }
  263. if(hori==HORIZONTAL_LEFT) {
  264. rect.x = x;
  265. }
  266. else if(hori==HORIZONTAL_RIGHT) {
  267. rect.x = x+w-sw;
  268. }
  269. else if(hori==HORIZONTAL_CENTER) {
  270. rect.x = x+(w-sw)/2;
  271. }
  272. else {
  273. return;
  274. }
  275. if(rect.x+sw>=hd->width) {
  276. rect.w = hd->width-rect.x;
  277. }
  278. else {
  279. rect.w = sw;
  280. }
  281. rect.y = y;
  282. rect.h = h;
  283. while(*pstr) {
  284. r = lcd_draw_char(hd, rect.x, rect.y, rect.w, rect.h, pstr, font, color, bgcolor, vert);
  285. rect.x += r;
  286. if(!pstr || x>=xmax) {
  287. break;
  288. }
  289. pstr = font_next(pstr);
  290. }
  291. }