ucg_dev_ic_ili9163.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. ucg_dev_ic_ili9163.c
  3. Specific code for the ili9163 controller (TFT displays)
  4. Code is for 128x128 display which is shifted by 32 pixel within the controller RAM
  5. Universal uC Color Graphics Library
  6. Copyright (c) 2015, olikraus@gmail.com
  7. All rights reserved.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. * Redistributions of source code must retain the above copyright notice, this list
  11. of conditions and the following disclaimer.
  12. * Redistributions in binary form must reproduce the above copyright notice, this
  13. list of conditions and the following disclaimer in the documentation and/or other
  14. materials provided with the distribution.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  16. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  17. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  20. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  25. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  27. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include "ucg.h"
  30. const ucg_pgm_uint8_t ucg_ili9163_set_pos_seq[] =
  31. {
  32. UCG_CS(0), /* enable chip */
  33. UCG_C11( 0x036, 0x008),
  34. UCG_C10(0x02a), UCG_VARX(0,0x00, 0), UCG_VARX(0,0x0ff, 0), UCG_D2(0x000, 0x07f), /* set x position */
  35. UCG_C10(0x02b), UCG_VARY(0,0x00, 0), UCG_VARY(0,0x0ff, 0), UCG_D2(0x000, 0x0a1), /* set y position */
  36. UCG_C10(0x02c), /* write to RAM */
  37. UCG_DATA(), /* change to data mode */
  38. UCG_END()
  39. };
  40. const ucg_pgm_uint8_t ucg_ili9163_set_pos_dir0_seq[] =
  41. {
  42. UCG_CS(0), /* enable chip */
  43. /* 0x008 horizontal increment (dir = 0) */
  44. /* 0x008 vertical increment (dir = 1) */
  45. /* 0x048 horizontal deccrement (dir = 2) */
  46. /* 0x088 vertical deccrement (dir = 3) */
  47. UCG_C11( 0x036, 0x008),
  48. UCG_C10(0x02a), UCG_VARX(0,0x00, 0), UCG_VARX(0,0x0ff, 0), UCG_D2(0x000, 0x083), /* set x position */
  49. UCG_C10(0x02b), UCG_VARY(0,0x00, 0), UCG_VARY(0,0x0ff, 0), UCG_D2(0x000, 0x0a1), /* set y position */
  50. UCG_C10(0x02c), /* write to RAM */
  51. UCG_DATA(), /* change to data mode */
  52. UCG_END()
  53. };
  54. const ucg_pgm_uint8_t ucg_ili9163_set_pos_dir1_seq[] =
  55. {
  56. UCG_CS(0), /* enable chip */
  57. /* 0x008 horizontal increment (dir = 0) */
  58. /* 0x008 vertical increment (dir = 1) */
  59. /* 0x048 horizontal deccrement (dir = 2) */
  60. /* 0x088 vertical deccrement (dir = 3) */
  61. UCG_C11( 0x036, 0x008),
  62. UCG_C10(0x02a), UCG_VARX(0,0x00, 0), UCG_VARX(0,0x0ff, 0), UCG_VARX(0,0x00, 0), UCG_VARX(0,0x0ff, 0), /* set x position */
  63. UCG_C10(0x02b), UCG_VARY(0,0x00, 0), UCG_VARY(0,0x0ff, 0), UCG_D2(0x000, 0x0a1), /* set y position */
  64. UCG_C10(0x02c), /* write to RAM */
  65. UCG_DATA(), /* change to data mode */
  66. UCG_END()
  67. };
  68. const ucg_pgm_uint8_t ucg_ili9163_set_pos_dir2_seq[] =
  69. {
  70. UCG_CS(0), /* enable chip */
  71. /* 0x008 horizontal increment (dir = 0) */
  72. /* 0x008 vertical increment (dir = 1) */
  73. /* 0x048 horizontal deccrement (dir = 2) */
  74. /* 0x088 vertical deccrement (dir = 3) */
  75. UCG_C11( 0x036, 0x048),
  76. UCG_C11( 0x036, 0x048), /* it seems that this command needs to be sent twice */
  77. UCG_C10(0x02a), UCG_VARX(0,0x00, 0), UCG_VARX(0,0x0ff, 0), UCG_D2(0x000, 0x083), /* set x position */
  78. UCG_C10(0x02b), UCG_VARY(0,0x00, 0), UCG_VARY(0,0x0ff, 0), UCG_D2(0x000, 0x0a1), /* set y position */
  79. UCG_C10(0x02c), /* write to RAM */
  80. UCG_DATA(), /* change to data mode */
  81. UCG_END()
  82. };
  83. const ucg_pgm_uint8_t ucg_ili9163_set_pos_dir3_seq[] =
  84. {
  85. UCG_CS(0), /* enable chip */
  86. /* 0x008 horizontal increment (dir = 0) */
  87. /* 0x008 vertical increment (dir = 1) */
  88. /* 0x0c8 horizontal deccrement (dir = 2) */
  89. /* 0x0c8 vertical deccrement (dir = 3) */
  90. UCG_C11( 0x036, 0x088),
  91. UCG_C11( 0x036, 0x088), /* it seems that this command needs to be sent twice */
  92. UCG_C10(0x02a), UCG_VARX(0,0x00, 0), UCG_VARX(0,0x0ff, 0), UCG_VARX(0,0x00, 0), UCG_VARX(0,0x0ff, 0), /* set x position */
  93. UCG_C10(0x02b), UCG_VARY(0,0x00, 0), UCG_VARY(0,0x0ff, 0), UCG_D2(0x000, 0x0a1), /* set y position */
  94. UCG_C10(0x02c), /* write to RAM */
  95. UCG_DATA(), /* change to data mode */
  96. UCG_END()
  97. };
  98. ucg_int_t ucg_handle_ili9163_l90fx(ucg_t *ucg)
  99. {
  100. uint8_t c[3];
  101. ucg_int_t tmp;
  102. if ( ucg_clip_l90fx(ucg) != 0 )
  103. {
  104. switch(ucg->arg.dir)
  105. {
  106. case 0:
  107. ucg->arg.pixel.pos.y += 32;
  108. ucg_com_SendCmdSeq(ucg, ucg_ili9163_set_pos_dir0_seq);
  109. ucg->arg.pixel.pos.y -= 32;
  110. break;
  111. case 1:
  112. ucg->arg.pixel.pos.y += 32;
  113. ucg_com_SendCmdSeq(ucg, ucg_ili9163_set_pos_dir1_seq);
  114. ucg->arg.pixel.pos.y -= 32;
  115. break;
  116. case 2:
  117. tmp = ucg->arg.pixel.pos.x;
  118. ucg->arg.pixel.pos.x = 127-tmp;
  119. ucg->arg.pixel.pos.y += 32;
  120. ucg_com_SendCmdSeq(ucg, ucg_ili9163_set_pos_dir2_seq);
  121. ucg->arg.pixel.pos.y -= 32;
  122. ucg->arg.pixel.pos.x = tmp;
  123. break;
  124. case 3:
  125. default:
  126. tmp = ucg->arg.pixel.pos.y;
  127. ucg->arg.pixel.pos.y = 127-tmp;
  128. //ucg->arg.pixel.pos.y += 32;
  129. ucg_com_SendCmdSeq(ucg, ucg_ili9163_set_pos_dir3_seq);
  130. ucg->arg.pixel.pos.y = tmp;
  131. break;
  132. }
  133. c[0] = ucg->arg.pixel.rgb.color[0];
  134. c[1] = ucg->arg.pixel.rgb.color[1];
  135. c[2] = ucg->arg.pixel.rgb.color[2];
  136. ucg_com_SendRepeat3Bytes(ucg, ucg->arg.len, c);
  137. ucg_com_SetCSLineStatus(ucg, 1); /* disable chip */
  138. return 1;
  139. }
  140. return 0;
  141. }
  142. /*
  143. L2TC (Glyph Output)
  144. */
  145. /* with CmdDataSequence */
  146. ucg_int_t ucg_handle_ili9163_l90tc(ucg_t *ucg)
  147. {
  148. if ( ucg_clip_l90tc(ucg) != 0 )
  149. {
  150. uint8_t buf[16];
  151. ucg_int_t dx, dy;
  152. ucg_int_t i;
  153. unsigned char pixmap;
  154. uint8_t bitcnt;
  155. ucg_com_SetCSLineStatus(ucg, 0); /* enable chip */
  156. ucg->arg.pixel.pos.y += 32;
  157. ucg_com_SendCmdSeq(ucg, ucg_ili9163_set_pos_seq);
  158. buf[0] = 0x001; // change to 0 (cmd mode)
  159. buf[1] = 0x02a; // set x
  160. buf[2] = 0x002; // change to 1 (arg mode)
  161. buf[3] = 0x000; // upper part x
  162. buf[4] = 0x000; // no change
  163. buf[5] = 0x000; // will be overwritten by x value
  164. buf[6] = 0x001; // change to 0 (cmd mode)
  165. buf[7] = 0x02c; // write data
  166. buf[8] = 0x002; // change to 1 (data mode)
  167. buf[9] = 0x000; // red value
  168. buf[10] = 0x000; // no change
  169. buf[11] = 0x000; // green value
  170. buf[12] = 0x000; // no change
  171. buf[13] = 0x000; // blue value
  172. switch(ucg->arg.dir)
  173. {
  174. case 0:
  175. dx = 1; dy = 0;
  176. buf[1] = 0x02a; // set x
  177. break;
  178. case 1:
  179. dx = 0; dy = 1;
  180. buf[1] = 0x02b; // set y
  181. break;
  182. case 2:
  183. dx = -1; dy = 0;
  184. buf[1] = 0x02a; // set x
  185. break;
  186. case 3:
  187. default:
  188. dx = 0; dy = -1;
  189. buf[1] = 0x02b; // set y
  190. break;
  191. }
  192. pixmap = ucg_pgm_read(ucg->arg.bitmap);
  193. bitcnt = ucg->arg.pixel_skip;
  194. pixmap <<= bitcnt;
  195. buf[9] = ucg->arg.pixel.rgb.color[0];
  196. buf[11] = ucg->arg.pixel.rgb.color[1];
  197. buf[13] = ucg->arg.pixel.rgb.color[2];
  198. //ucg_com_SetCSLineStatus(ucg, 0); /* enable chip */
  199. for( i = 0; i < ucg->arg.len; i++ )
  200. {
  201. if ( (pixmap & 128) != 0 )
  202. {
  203. if ( (ucg->arg.dir&1) == 0 )
  204. {
  205. buf[5] = ucg->arg.pixel.pos.x;
  206. }
  207. else
  208. {
  209. buf[3] = ucg->arg.pixel.pos.y>>8;
  210. buf[5] = ucg->arg.pixel.pos.y&255;
  211. }
  212. ucg_com_SendCmdDataSequence(ucg, 7, buf, 0);
  213. }
  214. pixmap<<=1;
  215. ucg->arg.pixel.pos.x+=dx;
  216. ucg->arg.pixel.pos.y+=dy;
  217. bitcnt++;
  218. if ( bitcnt >= 8 )
  219. {
  220. ucg->arg.bitmap++;
  221. pixmap = ucg_pgm_read(ucg->arg.bitmap);
  222. bitcnt = 0;
  223. }
  224. }
  225. ucg_com_SetCSLineStatus(ucg, 1); /* disable chip */
  226. return 1;
  227. }
  228. return 0;
  229. }
  230. ucg_int_t ucg_handle_ili9163_l90se(ucg_t *ucg)
  231. {
  232. uint8_t i;
  233. uint8_t c[3];
  234. ucg_int_t tmp;
  235. /* Setup ccs for l90se. This will be updated by ucg_clip_l90se if required */
  236. for ( i = 0; i < 3; i++ )
  237. {
  238. ucg_ccs_init(ucg->arg.ccs_line+i, ucg->arg.rgb[0].color[i], ucg->arg.rgb[1].color[i], ucg->arg.len);
  239. }
  240. /* check if the line is visible */
  241. if ( ucg_clip_l90se(ucg) != 0 )
  242. {
  243. ucg_int_t i;
  244. switch(ucg->arg.dir)
  245. {
  246. case 0:
  247. ucg->arg.pixel.pos.y += 32;
  248. ucg_com_SendCmdSeq(ucg, ucg_ili9163_set_pos_dir0_seq);
  249. ucg->arg.pixel.pos.y -= 32;
  250. break;
  251. case 1:
  252. ucg->arg.pixel.pos.y += 32;
  253. ucg_com_SendCmdSeq(ucg, ucg_ili9163_set_pos_dir1_seq);
  254. ucg->arg.pixel.pos.y -= 32;
  255. break;
  256. case 2:
  257. tmp = ucg->arg.pixel.pos.x;
  258. ucg->arg.pixel.pos.x = 127-tmp;
  259. ucg->arg.pixel.pos.y += 32;
  260. ucg_com_SendCmdSeq(ucg, ucg_ili9163_set_pos_dir2_seq);
  261. ucg->arg.pixel.pos.y -= 32;
  262. ucg->arg.pixel.pos.x = tmp;
  263. break;
  264. case 3:
  265. default:
  266. tmp = ucg->arg.pixel.pos.y;
  267. ucg->arg.pixel.pos.y = 127-tmp;
  268. ucg_com_SendCmdSeq(ucg, ucg_ili9163_set_pos_dir3_seq);
  269. ucg->arg.pixel.pos.y = tmp;
  270. break;
  271. }
  272. for( i = 0; i < ucg->arg.len; i++ )
  273. {
  274. c[0] = ucg->arg.ccs_line[0].current;
  275. c[1] = ucg->arg.ccs_line[1].current;
  276. c[2] = ucg->arg.ccs_line[2].current;
  277. ucg_com_SendRepeat3Bytes(ucg, 1, c);
  278. ucg_ccs_step(ucg->arg.ccs_line+0);
  279. ucg_ccs_step(ucg->arg.ccs_line+1);
  280. ucg_ccs_step(ucg->arg.ccs_line+2);
  281. }
  282. ucg_com_SetCSLineStatus(ucg, 1); /* disable chip */
  283. return 1;
  284. }
  285. return 0;
  286. }
  287. static const ucg_pgm_uint8_t ucg_ili9163_power_down_seq[] = {
  288. UCG_CS(0), /* enable chip */
  289. UCG_C10(0x010), /* sleep in */
  290. UCG_C10(0x28), /* display off */
  291. UCG_CS(1), /* disable chip */
  292. UCG_END(), /* end of sequence */
  293. };
  294. ucg_int_t ucg_dev_ic_ili9163_18(ucg_t *ucg, ucg_int_t msg, void *data)
  295. {
  296. switch(msg)
  297. {
  298. case UCG_MSG_DEV_POWER_UP:
  299. /* setup com interface and provide information on the clock speed */
  300. /* of the serial and parallel interface. Values are nanoseconds. */
  301. return ucg_com_PowerUp(ucg, 100, 66);
  302. case UCG_MSG_DEV_POWER_DOWN:
  303. ucg_com_SendCmdSeq(ucg, ucg_ili9163_power_down_seq);
  304. return 1;
  305. case UCG_MSG_GET_DIMENSION:
  306. ((ucg_wh_t *)data)->w = 128;
  307. ((ucg_wh_t *)data)->h = 128;
  308. return 1;
  309. case UCG_MSG_DRAW_PIXEL:
  310. if ( ucg_clip_is_pixel_visible(ucg) !=0 )
  311. {
  312. uint8_t c[3];
  313. ucg->arg.pixel.pos.y += 32;
  314. ucg_com_SendCmdSeq(ucg, ucg_ili9163_set_pos_seq);
  315. ucg->arg.pixel.pos.y -= 32;
  316. c[0] = ucg->arg.pixel.rgb.color[0];
  317. c[1] = ucg->arg.pixel.rgb.color[1];
  318. c[2] = ucg->arg.pixel.rgb.color[2];
  319. ucg_com_SendRepeat3Bytes(ucg, 1, c);
  320. ucg_com_SetCSLineStatus(ucg, 1); /* disable chip */
  321. }
  322. return 1;
  323. case UCG_MSG_DRAW_L90FX:
  324. //ucg_handle_l90fx(ucg, ucg_dev_ic_ili9163_18);
  325. ucg_handle_ili9163_l90fx(ucg);
  326. return 1;
  327. #ifdef UCG_MSG_DRAW_L90TC
  328. case UCG_MSG_DRAW_L90TC:
  329. //ucg_handle_l90tc(ucg, ucg_dev_ic_ili9163_18);
  330. ucg_handle_ili9163_l90tc(ucg);
  331. return 1;
  332. #endif /* UCG_MSG_DRAW_L90TC */
  333. #ifdef UCG_MSG_DRAW_L90BF
  334. case UCG_MSG_DRAW_L90BF:
  335. ucg_handle_l90bf(ucg, ucg_dev_ic_ili9163_18);
  336. return 1;
  337. #endif /* UCG_MSG_DRAW_L90BF */
  338. /* msg UCG_MSG_DRAW_L90SE is handled by ucg_dev_default_cb */
  339. /*
  340. case UCG_MSG_DRAW_L90SE:
  341. return ucg->ext_cb(ucg, msg, data);
  342. */
  343. }
  344. return ucg_dev_default_cb(ucg, msg, data);
  345. }
  346. ucg_int_t ucg_ext_ili9163_18(ucg_t *ucg, ucg_int_t msg, void *data)
  347. {
  348. switch(msg)
  349. {
  350. case UCG_MSG_DRAW_L90SE:
  351. //ucg_handle_l90se(ucg, ucg_dev_ic_ili9163_18);
  352. ucg_handle_ili9163_l90se(ucg);
  353. break;
  354. }
  355. return 1;
  356. }