lcd.c 9.8 KB

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