lcd_drv.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include "lcd_drv.h"
  2. #include "io.h"
  3. #include "elog.h"
  4. #include <sys/mman.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <unistd.h>
  8. #include <linux/fb.h>
  9. #include <fcntl.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <sys/ioctl.h>
  13. #define BACKLIGHT_GPIO_NUM 98
  14. static int fd_fb = 0 ;
  15. static struct fb_var_screeninfo var;
  16. static unsigned char *fb_base;
  17. static unsigned int line_width;
  18. static unsigned int pixel_width;
  19. static int screen_size;
  20. /// @brief 初始化LCD
  21. /// @param
  22. /// @return
  23. int lcd_init(void)
  24. {
  25. //打开背光
  26. //初始化IO
  27. io_init(BACKLIGHT_GPIO_NUM);
  28. //设置输出
  29. io_set_dir(BACKLIGHT_GPIO_NUM,1);
  30. //拉高
  31. io_set_value(BACKLIGHT_GPIO_NUM,1);
  32. fd_fb = open("/dev/fb0", O_RDWR);
  33. if (fd_fb < 0) {
  34. log_e("Can't open /dev/fb0\n");
  35. return -1;
  36. }
  37. if(ioctl(fd_fb, FBIOGET_VSCREENINFO, &var)) {
  38. log_e("Can't get var\n");
  39. return -1;
  40. }
  41. line_width = var.xres * var.bits_per_pixel / 8; // 一行多少byte
  42. pixel_width = var.bits_per_pixel / 8; // 一个像素多少byte
  43. screen_size = var.xres * var.yres * var.bits_per_pixel / 8; // 一屏多少byte
  44. fb_base = (unsigned char*)mmap(NULL, screen_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0);
  45. if (fb_base == (unsigned char*)MAP_FAILED) {
  46. log_e("Can't mmap\n");
  47. return -1;
  48. }
  49. //清屏为黑色 0x00 黑色 0xFF 白色
  50. memset(fb_base, 0x00, screen_size);
  51. return 0;
  52. }
  53. /// @brief 释放LCD接口
  54. /// @param
  55. /// @return
  56. int lcd_deinit(void)
  57. {
  58. munmap(fb_base , screen_size);
  59. close(fd_fb);
  60. io_set_dir(BACKLIGHT_GPIO_NUM,0);
  61. io_set_value(BACKLIGHT_GPIO_NUM,0);
  62. io_deinit(BACKLIGHT_GPIO_NUM);
  63. return 0;
  64. }
  65. /// @brief 画点
  66. /// @param x
  67. /// @param y
  68. /// @param color
  69. void lcd_put_pixel(int x, int y, unsigned int color)
  70. {
  71. unsigned char *pen_8 = fb_base + y * line_width + x * pixel_width;
  72. unsigned short *pen_16;
  73. unsigned int *pen_32;
  74. unsigned int red, green, blue;
  75. pen_16 = (unsigned short *)pen_8;
  76. pen_32 = (unsigned int *)pen_8;
  77. switch(var.bits_per_pixel) { /* bpp 像素深度 */
  78. case 8:
  79. {
  80. *pen_8 = color;
  81. break;
  82. }
  83. case 16:
  84. {
  85. /* RGB565 */
  86. red = (color >> 16) & 0xFF;
  87. green = (color >> 8) & 0xFF;
  88. blue = (color >> 0) & 0xFF;
  89. color = ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3);
  90. *pen_16 = color;
  91. break;
  92. }
  93. case 32:
  94. {
  95. /* RGB888 */
  96. *pen_32 = color;
  97. break;
  98. }
  99. default:
  100. {
  101. log_e("Can't support %d bpp\n", var.bits_per_pixel);
  102. break;
  103. }
  104. }
  105. }
  106. #if 0
  107. /// @brief 绘制字符 在LCD指定位置上显示一个8*16的字符
  108. /// @param x
  109. /// @param y
  110. /// @param c
  111. /// @param color
  112. void lcd_put_ascii(int x, int y, unsigned char c, int color)
  113. {
  114. unsigned char *dots = (unsigned char *)&fontdata_8x16[c*16];
  115. int i, b;
  116. unsigned char byte;
  117. for (i = 0; i < 16; i++)
  118. {
  119. byte = dots[i];
  120. for (b = 7; b >= 0; b--)
  121. {
  122. if (byte & (1<<b))
  123. {
  124. /* show */
  125. lcd_put_pixel(x+7-b, y+i, color); /* 白 */
  126. }
  127. else
  128. {
  129. /* hide */
  130. lcd_put_pixel(x+7-b, y+i, 0); /* 黑 */
  131. }
  132. }
  133. }
  134. }
  135. /// @brief 在指定位置显示字符串
  136. /// @param x
  137. /// @param y
  138. /// @param string
  139. /// @param color
  140. void lcd_show_string(int x, int y, unsigned char *string, int color)
  141. {
  142. int string_len = strlen(string);
  143. int string_x = x;
  144. int string_y = y;
  145. for(int i = 0; i < string_len; i++)
  146. {
  147. if(string[i] == '\n')
  148. {
  149. string_y += 18;
  150. string_x = 0;
  151. if(string_y > var.yres)
  152. {
  153. return ;
  154. }
  155. }
  156. else
  157. {
  158. lcd_put_ascii(string_x, string_y, string[i], color);
  159. string_x += 8;
  160. if(string_x > var.xres)
  161. {
  162. string_x = 0;
  163. string_y += 18;
  164. if(string_y > var.yres)
  165. {
  166. return ;
  167. }
  168. }
  169. }
  170. }
  171. }
  172. /// @brief 显示中文
  173. /// @param x
  174. /// @param y
  175. /// @param str
  176. /// @param color
  177. void lcd_put_chinese(int x, int y, unsigned char *str, unsigned int color)
  178. {
  179. unsigned int area = str[0] - 0xA1;
  180. unsigned int where = str[1] - 0xA1;
  181. unsigned char *dots = hzkmem + (area * 94 + where)*32;
  182. unsigned char byte;
  183. int i, j, b;
  184. for (i = 0; i < 16; i++)
  185. for (j = 0; j < 2; j++)
  186. {
  187. byte = dots[i*2 + j];
  188. for (b = 7; b >=0; b--)
  189. {
  190. if (byte & (1<<b))
  191. {
  192. /* show */
  193. lcd_put_pixel(x+j*8+7-b, y+i, color); /* 白 */
  194. }
  195. else
  196. {
  197. /* hide */
  198. lcd_put_pixel(x+j*8+7-b, y+i, 0); /* 黑 */
  199. }
  200. }
  201. }
  202. }
  203. #endif