lcd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <sys/mman.h>
  7. #include <sys/ioctl.h>
  8. #include <linux/fb.h>
  9. #include <linux/spi/spidev.h>
  10. #include "gpio.h"
  11. #include "lcd.h"
  12. #include "elog.h"
  13. //#define USE_FB
  14. #define BK_GPIO 98 //PD2
  15. #define DC_GPIO 99 //PD3, 96+3=99
  16. #define RST_GPIO 100 //PD4, 96+4=100
  17. #if 1
  18. #define LOGD log_d
  19. #define LOGE log_e
  20. #else
  21. #define LOGD printf
  22. #define LOGE printf
  23. #endif
  24. #define BIT(x) (1<<x)
  25. #define SWAP16(a) ((uint16_t)((a<<8)|(a>>8)))
  26. #ifdef USE_FB
  27. #define DEV_PATH "/dev/fb0"
  28. typedef struct fb_fix_screeninfo fb_finfo_t;
  29. typedef struct fb_var_screeninfo fb_vinfo_t;
  30. #else
  31. #define DEV_PATH "/dev/spidev1.0"
  32. #endif
  33. typedef struct {
  34. int x;
  35. int y;
  36. int w;
  37. int h;
  38. }lcd_rect_t;
  39. typedef struct {
  40. int width;
  41. int height;
  42. int color;
  43. int fd;
  44. #ifdef USE_FB
  45. uint16_t *buf;
  46. #else
  47. int io;
  48. uint8_t *buf;
  49. #endif
  50. int buflen;
  51. }lcd_handle_t;
  52. static lcd_handle_t lcdHandle={0};
  53. static int ssd1309_init(lcd_handle_t *h);
  54. static void ssd1309_refresh(lcd_handle_t *h);
  55. static int lcd_hw_init(lcd_handle_t *h)
  56. {
  57. h->fd = open(DEV_PATH, O_RDWR);
  58. if (h->fd < 0) {
  59. LOGE("___ open %s failed\n", DEV_PATH);
  60. return -1;
  61. }
  62. #ifdef USE_FB
  63. fb_vinfo_t vinfo;
  64. if (ioctl(h->fd, FBIOGET_VSCREENINFO, &vinfo)) {
  65. LOGE("___ get screen info failed\n");
  66. return -1;
  67. }
  68. h->buflen = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
  69. h->buf = (uint16_t *)mmap(0, h->buflen, PROT_READ | PROT_WRITE, MAP_SHARED, h->fd, 0);
  70. if ((int)h->buf == -1) {
  71. LOGE("Error: failed to map framebuffer device to memory\n");
  72. return -1;
  73. }
  74. #else
  75. h->io = gpio_init(DC_GPIO, GPIO_OUT);
  76. h->buflen = h->width*h->height/8;
  77. h->buf = (uint8_t*)calloc(1, h->buflen);
  78. if (!h->buf) {
  79. LOGE("malloc buffer falied\n");
  80. return -1;
  81. }
  82. #if 0
  83. uint8_t mode = SPI_MODE_0;
  84. if (ioctl(fd, SPI_IOC_WR_MODE, &mode) == -1) {
  85. LOGE("Can't set SPI mode\n");
  86. return -1;
  87. }
  88. uint32_t speed = 8000000;
  89. if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) {
  90. LOGE("Can't set max speed\n");
  91. return -1;
  92. }
  93. #endif
  94. ssd1309_init(h);
  95. #endif
  96. return 0;
  97. }
  98. static int lcd_hw_write(lcd_handle_t *h, void *data, int len)
  99. {
  100. int r;
  101. struct spi_ioc_transfer tr = {
  102. .tx_buf = (unsigned long)data,
  103. .rx_buf = 0,
  104. .len = len,
  105. .speed_hz = 8000000,
  106. .bits_per_word = 8,
  107. };
  108. r = ioctl(h->fd, SPI_IOC_MESSAGE(1), &tr);
  109. if (r < 1) {
  110. LOGE("Can't send spi message");
  111. }
  112. return 0;
  113. }
  114. static void lcd_write_reg(lcd_handle_t *h, uint8_t reg)
  115. {
  116. gpio_set(h->io, 0);
  117. lcd_hw_write(h, &reg, 1);
  118. }
  119. static int ssd1309_reset(void)
  120. {
  121. int fd = gpio_init(RST_GPIO, GPIO_OUT);
  122. gpio_set(fd, 0);
  123. usleep(100*1000);
  124. gpio_set(fd, 1);
  125. gpio_deinit(fd);
  126. return 0;
  127. }
  128. static void ssd1309_on(lcd_handle_t *h, int flag)
  129. {
  130. if(flag) {
  131. lcd_write_reg(h,0x8D);
  132. lcd_write_reg(h,0x14);
  133. lcd_write_reg(h,0xAF);
  134. }
  135. else {
  136. lcd_write_reg(h,0x8D);
  137. lcd_write_reg(h,0x10);
  138. lcd_write_reg(h,0xAE);
  139. }
  140. }
  141. static int ssd1309_init(lcd_handle_t *h)
  142. {
  143. ssd1309_reset();
  144. lcd_write_reg(h,0xFD);
  145. lcd_write_reg(h,0x12);
  146. lcd_write_reg(h,0xAE);//--turn off oled panel
  147. lcd_write_reg(h,0xd5);//--set display clock divide ratio/oscillator frequency
  148. lcd_write_reg(h,0xA0);
  149. lcd_write_reg(h,0xA8);//--set multiplex ratio(1 to 64)
  150. lcd_write_reg(h,0x3f);//--1/64 duty
  151. lcd_write_reg(h,0xD3);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
  152. lcd_write_reg(h,0x00);//-not offset
  153. lcd_write_reg(h,0x40);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)
  154. lcd_write_reg(h,0xA1);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常
  155. lcd_write_reg(h,0xC8);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常
  156. lcd_write_reg(h,0xDA);//--set com pins hardware configuration
  157. lcd_write_reg(h,0x12);
  158. lcd_write_reg(h,0x81);//--set contrast control register
  159. lcd_write_reg(h,0x7F);// Set SEG Output Current Brightness
  160. lcd_write_reg(h,0xD9);//--set pre-charge period
  161. lcd_write_reg(h,0x82);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
  162. lcd_write_reg(h,0xDB);//--set vcomh
  163. lcd_write_reg(h,0x34);//Set VCOM Deselect Level
  164. lcd_write_reg(h,0xA4);// Disable Entire Display On (0xa4/0xa5)
  165. lcd_write_reg(h,0xA6);// Disable Inverse Display On (0xa6/a7)
  166. ssd1309_refresh(h);
  167. ssd1309_on(h,1);
  168. return 0;
  169. }
  170. static void ssd1309_refresh(lcd_handle_t *h)
  171. {
  172. int i,n,offset;
  173. for(i=0;i<8;i++) {
  174. lcd_write_reg(h,0xb0+i); //设置行起始地址
  175. lcd_write_reg(h,0x00); //设置低列起始地址
  176. lcd_write_reg(h,0x10); //设置高列起始地址
  177. gpio_set(h->io, 1);
  178. offset = i*128;
  179. for(n=0;n<128;n++) {
  180. lcd_hw_write(h, &h->buf[offset+n], 1);
  181. }
  182. }
  183. }
  184. static int lcd_hw_deinit(lcd_handle_t *h)
  185. {
  186. #ifdef USE_FB
  187. munmap(h->buf, h->buflen);
  188. #else
  189. gpio_deinit(h->io);
  190. free(h->buf);
  191. #endif
  192. close(h->fd);
  193. return 0;
  194. }
  195. static inline int set_point(lcd_handle_t *h, int x, int y, int data)
  196. {
  197. #ifdef USE_FB
  198. h->buf[y*h->width+x] = (uint16_t)data;
  199. #else
  200. uint8_t pos,bit;
  201. pos = y / 8; /* get y page */
  202. bit = y % 8; /* get y point */
  203. if (data) { /* if 1 */
  204. h->buf[pos*h->width+x] |= BIT(bit); /* set 1 */
  205. }
  206. else {
  207. h->buf[pos*h->width+x] &= ~BIT(bit); /* set 0 */
  208. }
  209. #endif
  210. return 0;
  211. }
  212. static int get_color_bits(int color)
  213. {
  214. if(color==COLOR_WB) {
  215. return 1;
  216. }
  217. else if(color==COLOR_RGB8) {
  218. return 8;
  219. }
  220. else if(color==COLOR_RGB565) {
  221. return 16;
  222. }
  223. else {
  224. return 32;
  225. }
  226. }
  227. ////////////////////////////////////////////////////////////
  228. void lcd_init(int width, int height, int color)
  229. {
  230. lcd_handle_t *h=&lcdHandle;
  231. h->width = width;
  232. h->height = height;
  233. h->color = color;
  234. lcd_hw_init(h);
  235. }
  236. void lcd_deinit(void)
  237. {
  238. lcd_handle_t *h=&lcdHandle;
  239. lcd_hw_deinit(h);
  240. }
  241. void lcd_rotate(int angle)
  242. {
  243. lcd_handle_t *h=&lcdHandle;
  244. }
  245. void lcd_set_backlight(int on)
  246. {
  247. lcd_handle_t *h=&lcdHandle;
  248. }
  249. void lcd_fill(int color)
  250. {
  251. lcd_handle_t *h=&lcdHandle;
  252. lcd_fill_rect(0, 0, h->width-1, h->height-1, color);
  253. }
  254. void lcd_draw_line(int x1, int y1, int x2, int y2, int color)
  255. {
  256. int incx = 0, incy = 0;
  257. int delta_x = 0, delta_y = 0;
  258. int distance = 0;
  259. int t = 0;
  260. int x = 0, y = 0;
  261. int x_temp = 0, y_temp = 0;
  262. lcd_handle_t *h=&lcdHandle;
  263. /* 画斜线(Bresenham算法) */
  264. delta_x = x2 - x1;
  265. delta_y = y2 - y1;
  266. if(delta_x > 0) {
  267. //斜线(从左到右)
  268. incx = 1;
  269. }
  270. else if(delta_x == 0) {
  271. //垂直斜线(竖线)
  272. incx = 0;
  273. }
  274. else {
  275. //斜线(从右到左)
  276. incx = -1;
  277. delta_x = -delta_x;
  278. }
  279. if(delta_y > 0) {
  280. //斜线(从左到右)
  281. incy = 1;
  282. }
  283. else if(delta_y == 0) {
  284. //水平斜线(水平线)
  285. incy = 0;
  286. }
  287. else {
  288. //斜线(从右到左)
  289. incy = -1;
  290. delta_y = -delta_y;
  291. }
  292. /* 计算画笔打点距离(取两个间距中的最大值) */
  293. if(delta_x > delta_y) {
  294. distance = delta_x;
  295. }
  296. else {
  297. distance = delta_y;
  298. }
  299. /* 开始打点 */
  300. x = x1;
  301. y = y1;
  302. //第一个点无效,所以t的次数加一
  303. for(t = 0; t <= distance + 1;t++) {
  304. set_point(h, x, y, color);
  305. /* 判断离实际值最近的像素点 */
  306. x_temp += delta_x;
  307. if(x_temp > distance) {
  308. //x方向越界,减去距离值,为下一次检测做准备
  309. x_temp -= distance;
  310. //在x方向递增打点
  311. x += incx;
  312. }
  313. y_temp += delta_y;
  314. if(y_temp > distance) {
  315. //y方向越界,减去距离值,为下一次检测做准备
  316. y_temp -= distance;
  317. //在y方向递增打点
  318. y += incy;
  319. }
  320. }
  321. }
  322. void lcd_draw_rect(int x, int y, int w, int h, int color)
  323. {
  324. int x2,y2;
  325. x2 = x + w;
  326. y2 = y + h;
  327. lcd_draw_line(x,y,x2,y,color);
  328. lcd_draw_line(x,y,x,y2,color);
  329. lcd_draw_line(x,y2,x2,y2,color);
  330. lcd_draw_line(x,y,x2,y2,color);
  331. }
  332. void lcd_fill_rect(int x, int y, int w, int h, int color)
  333. {
  334. int i,j;
  335. lcd_handle_t *h2=&lcdHandle;
  336. for(i=x; i<x+w; i++) {
  337. for(j=y; j<y+h; j++) {
  338. set_point(h2, i, j, color);
  339. }
  340. }
  341. }
  342. void lcd_refresh(void)
  343. {
  344. lcd_handle_t *h=&lcdHandle;
  345. #ifndef USE_FB
  346. ssd1309_refresh(h);
  347. #endif
  348. }
  349. int lcd_draw_char(lcd_handle_t *hd, int x, int y, int w, int h, uint8_t *c, int font, int color, int bgcolor, int vert)
  350. {
  351. uint8_t tmp;
  352. font_data_t fdata;
  353. int i,j,x0=x,y0=y,x1=x+w,y1=y+h;
  354. fdata = font_data(font, c);
  355. for(i=0; i<fdata.dlen; i++) { /*遍历打印所有像素点到LCD */
  356. tmp = fdata.data[i];
  357. for(j=0; j<8; j++) {
  358. //if((x<x1 && x<hd->width) && (y<y1 && y<hd->height))
  359. {
  360. if(tmp&0x01) {
  361. set_point(hd, x, y, color);
  362. }
  363. else {
  364. set_point(hd, x, y, bgcolor);
  365. }
  366. }
  367. tmp >>= 1;
  368. y++;
  369. }
  370. #if 0
  371. if((y-y0)>=fdata.height) {
  372. x++;
  373. y = y0;
  374. }
  375. #else
  376. x++;
  377. if((font!=FONT_08) && ((x-x0)>=fdata.width)) {
  378. x = x0;
  379. y0 += 8;
  380. }
  381. y = y0;
  382. #endif
  383. }
  384. return fdata.width;
  385. }
  386. void lcd_draw_string(int x, int y, int w, int h, char *str, int font, int color, int bgcolor, int hori, int vert)
  387. {
  388. int r,xmax=x+w;
  389. uint8_t *pstr=(uint8_t*)str;
  390. lcd_handle_t *h2=&lcdHandle;
  391. while(*pstr) { /* 判断是不是非法字符! */
  392. r = lcd_draw_char(h2, x, y, w, h, pstr, font, color, bgcolor, vert);
  393. x += r;
  394. pstr = font_next(pstr);
  395. if(!pstr || x>=xmax) {
  396. break;
  397. }
  398. }
  399. }