lcd.c 11 KB

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