ucg_dev_ic_st7735.c 11 KB

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