lcd.c 11 KB

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