lcd.c 11 KB

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