ucg_dev_ic_pcf8833.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. ucg_dev_ic_pcf8833.c
  3. Specific code for the pcf8833 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. #define XOFFSET 0
  30. #define WIDTH 132
  31. #define HEIGHT 132
  32. const ucg_pgm_uint8_t ucg_pcf8833_set_pos_seq[] =
  33. {
  34. UCG_CS(0), /* enable chip */
  35. UCG_C11( 0x036, 0x000),
  36. UCG_C10(0x02a), UCG_VARX(0,0x0ff, 0), UCG_A1(WIDTH-1), /* set x position */
  37. UCG_C10(0x02b), UCG_VARY(0,0x0ff, 0), UCG_A1(HEIGHT-1), /* set y position */
  38. UCG_C10(0x02c), /* write to RAM */
  39. UCG_DATA(), /* change to data mode */
  40. UCG_END()
  41. };
  42. const ucg_pgm_uint8_t ucg_pcf8833_set_pos_dir0_seq[] =
  43. {
  44. UCG_CS(0), /* enable chip */
  45. /*
  46. bit 7: my
  47. bit 6: mx
  48. bit 5: x or y increment
  49. bit 4: line order
  50. bit 3: rgb
  51. */
  52. UCG_C11( 0x036, 0x000),
  53. UCG_C10(0x02a), UCG_VARX(0,0x0ff, 0), UCG_A1(WIDTH-1), /* set x position */
  54. UCG_C10(0x02b), UCG_VARY(0,0x0ff, 0), UCG_A1(HEIGHT-1), /* set y position */
  55. UCG_C10(0x02c), /* write to RAM */
  56. UCG_DATA(), /* change to data mode */
  57. UCG_END()
  58. };
  59. const ucg_pgm_uint8_t ucg_pcf8833_set_pos_dir1_seq[] =
  60. {
  61. UCG_CS(0), /* enable chip */
  62. /*
  63. bit 7: my
  64. bit 6: mx
  65. bit 5: x or y increment
  66. bit 4: line order
  67. bit 3: rgb
  68. */
  69. UCG_C11( 0x036, 0x000),
  70. UCG_C10(0x02a), UCG_VARX(0,0x0ff, 0), UCG_VARX(0,0x0ff, 0), /* set x position */
  71. UCG_C10(0x02b), UCG_VARY(0,0x0ff, 0), UCG_A1(HEIGHT-1), /* set y position */
  72. UCG_C10(0x02c), /* write to RAM */
  73. UCG_DATA(), /* change to data mode */
  74. UCG_END()
  75. };
  76. const ucg_pgm_uint8_t ucg_pcf8833_set_pos_dir2_seq[] =
  77. {
  78. UCG_CS(0), /* enable chip */
  79. /*
  80. bit 7: my
  81. bit 6: mx
  82. bit 5: x or y increment
  83. bit 4: line order
  84. bit 3: rgb
  85. */
  86. UCG_C11( 0x036, 0x040),
  87. //UCG_C11( 0x036, 0x020), /* it seems that this command needs to be sent twice */
  88. UCG_C10(0x02a), UCG_VARX(0,0x0ff, 0), UCG_A1(WIDTH-1), /* set x position */
  89. UCG_C10(0x02b), UCG_VARY(0,0x0ff, 0), UCG_A1(HEIGHT-1), /* set y position */
  90. UCG_C10(0x02c), /* write to RAM */
  91. UCG_DATA(), /* change to data mode */
  92. UCG_END()
  93. };
  94. const ucg_pgm_uint8_t ucg_pcf8833_set_pos_dir3_seq[] =
  95. {
  96. UCG_CS(0), /* enable chip */
  97. /*
  98. bit 7: my
  99. bit 6: mx
  100. bit 5: x or y increment
  101. bit 4: line order
  102. bit 3: rgb
  103. */
  104. UCG_C11( 0x036, 0x080),
  105. //UCG_C11( 0x036, 0x0d0), /* it seems that this command needs to be sent twice */
  106. UCG_C10(0x02a), UCG_VARX(0,0x0ff, 0), UCG_VARX(0,0x0ff, 0), /* set x position */
  107. UCG_C10(0x02b), UCG_VARY(0,0x0ff, 0), UCG_A1(HEIGHT-1), /* set y position */
  108. UCG_C10(0x02c), /* write to RAM */
  109. UCG_DATA(), /* change to data mode */
  110. UCG_END()
  111. };
  112. static uint8_t ucg_pcf8833_get_color_high_byte(ucg_t *ucg)
  113. {
  114. return (ucg->arg.pixel.rgb.color[0]&0x0f8) | (((ucg->arg.pixel.rgb.color[1]) >>5));
  115. }
  116. static uint8_t ucg_pcf8833_get_color_low_byte(ucg_t *ucg)
  117. {
  118. return ((((ucg->arg.pixel.rgb.color[1]))<<3)&0x0e0) | (((ucg->arg.pixel.rgb.color[2]) >>3));
  119. }
  120. ucg_int_t ucg_handle_pcf8833_l90fx(ucg_t *ucg)
  121. {
  122. uint8_t c[3];
  123. ucg_int_t tmp;
  124. if ( ucg_clip_l90fx(ucg) != 0 )
  125. {
  126. switch(ucg->arg.dir)
  127. {
  128. case 0:
  129. ucg_com_SendCmdSeq(ucg, ucg_pcf8833_set_pos_dir0_seq);
  130. break;
  131. case 1:
  132. ucg_com_SendCmdSeq(ucg, ucg_pcf8833_set_pos_dir1_seq);
  133. break;
  134. case 2:
  135. tmp = ucg->arg.pixel.pos.x;
  136. ucg->arg.pixel.pos.x = WIDTH-1-tmp;
  137. ucg_com_SendCmdSeq(ucg, ucg_pcf8833_set_pos_dir2_seq);
  138. ucg->arg.pixel.pos.x = tmp;
  139. break;
  140. case 3:
  141. default:
  142. tmp = ucg->arg.pixel.pos.y;
  143. ucg->arg.pixel.pos.y = HEIGHT-1-tmp;
  144. ucg_com_SendCmdSeq(ucg, ucg_pcf8833_set_pos_dir3_seq);
  145. ucg->arg.pixel.pos.y = tmp;
  146. break;
  147. }
  148. c[0] = ucg_pcf8833_get_color_high_byte(ucg);
  149. c[1] = ucg_pcf8833_get_color_low_byte(ucg);
  150. ucg_com_SendRepeat2Bytes(ucg, ucg->arg.len, c);
  151. ucg_com_SetCSLineStatus(ucg, 1); /* disable chip */
  152. return 1;
  153. }
  154. return 0;
  155. }
  156. /*
  157. L2TC (Glyph Output)
  158. */
  159. /* with CmdDataSequence */
  160. ucg_int_t ucg_handle_pcf8833_l90tc(ucg_t *ucg)
  161. {
  162. if ( ucg_clip_l90tc(ucg) != 0 )
  163. {
  164. uint8_t buf[16];
  165. ucg_int_t dx, dy;
  166. ucg_int_t i;
  167. unsigned char pixmap;
  168. uint8_t bitcnt;
  169. ucg_com_SetCSLineStatus(ucg, 0); /* enable chip */
  170. ucg_com_SendCmdSeq(ucg, ucg_pcf8833_set_pos_seq);
  171. buf[0] = 0x001; // change to 0 (cmd mode)
  172. buf[1] = 0x02a; // set x
  173. buf[2] = 0x002; // change to 1 (arg mode)
  174. buf[3] = 0x000; // x/y
  175. buf[4] = 0x000; // no change
  176. buf[5] = 0x000; // end value
  177. buf[6] = 0x001; // change to 0 (cmd mode)
  178. buf[7] = 0x02c; // write data
  179. buf[8] = 0x002; // change to 1 (data mode)
  180. buf[9] = ucg_pcf8833_get_color_high_byte(ucg);
  181. buf[10] = 0x000; // no change
  182. buf[11] = ucg_pcf8833_get_color_low_byte(ucg);
  183. switch(ucg->arg.dir)
  184. {
  185. case 0:
  186. dx = 1; dy = 0;
  187. buf[1] = 0x02a; // set x
  188. buf[5] = WIDTH-1; // end value
  189. break;
  190. case 1:
  191. dx = 0; dy = 1;
  192. buf[1] = 0x02b; // set y
  193. buf[5] = HEIGHT-1; // end value
  194. break;
  195. case 2:
  196. dx = -1; dy = 0;
  197. buf[1] = 0x02a; // set x
  198. buf[5] = WIDTH-1; // end value
  199. break;
  200. case 3:
  201. default:
  202. dx = 0; dy = -1;
  203. buf[1] = 0x02b; // set y
  204. buf[5] = HEIGHT-1; // end value
  205. break;
  206. }
  207. pixmap = ucg_pgm_read(ucg->arg.bitmap);
  208. bitcnt = ucg->arg.pixel_skip;
  209. pixmap <<= bitcnt;
  210. for( i = 0; i < ucg->arg.len; i++ )
  211. {
  212. if ( (pixmap & 128) != 0 )
  213. {
  214. if ( (ucg->arg.dir&1) == 0 )
  215. {
  216. buf[3] = ucg->arg.pixel.pos.x;
  217. }
  218. else
  219. {
  220. buf[3] = ucg->arg.pixel.pos.y;
  221. }
  222. ucg_com_SendCmdDataSequence(ucg, 6, buf, 0);
  223. }
  224. pixmap<<=1;
  225. ucg->arg.pixel.pos.x+=dx;
  226. ucg->arg.pixel.pos.y+=dy;
  227. bitcnt++;
  228. if ( bitcnt >= 8 )
  229. {
  230. ucg->arg.bitmap++;
  231. pixmap = ucg_pgm_read(ucg->arg.bitmap);
  232. bitcnt = 0;
  233. }
  234. }
  235. ucg_com_SetCSLineStatus(ucg, 1); /* disable chip */
  236. return 1;
  237. }
  238. return 0;
  239. }
  240. ucg_int_t ucg_handle_pcf8833_l90se(ucg_t *ucg)
  241. {
  242. uint8_t i;
  243. uint8_t c[3];
  244. ucg_int_t tmp;
  245. /* Setup ccs for l90se. This will be updated by ucg_clip_l90se if required */
  246. for ( i = 0; i < 3; i++ )
  247. {
  248. ucg_ccs_init(ucg->arg.ccs_line+i, ucg->arg.rgb[0].color[i], ucg->arg.rgb[1].color[i], ucg->arg.len);
  249. }
  250. /* check if the line is visible */
  251. if ( ucg_clip_l90se(ucg) != 0 )
  252. {
  253. ucg_int_t i;
  254. switch(ucg->arg.dir)
  255. {
  256. case 0:
  257. ucg_com_SendCmdSeq(ucg, ucg_pcf8833_set_pos_dir0_seq);
  258. break;
  259. case 1:
  260. ucg_com_SendCmdSeq(ucg, ucg_pcf8833_set_pos_dir1_seq);
  261. break;
  262. case 2:
  263. tmp = ucg->arg.pixel.pos.x;
  264. ucg->arg.pixel.pos.x = WIDTH-1-tmp;
  265. ucg_com_SendCmdSeq(ucg, ucg_pcf8833_set_pos_dir2_seq);
  266. ucg->arg.pixel.pos.x = tmp;
  267. break;
  268. case 3:
  269. default:
  270. tmp = ucg->arg.pixel.pos.y;
  271. ucg->arg.pixel.pos.y = HEIGHT-1-tmp;
  272. ucg_com_SendCmdSeq(ucg, ucg_pcf8833_set_pos_dir3_seq);
  273. ucg->arg.pixel.pos.y = tmp;
  274. break;
  275. }
  276. for( i = 0; i < ucg->arg.len; i++ )
  277. {
  278. c[0] = (ucg->arg.ccs_line[0].current&0x0f8) | (((ucg->arg.ccs_line[1].current) >>5));
  279. c[1] = ((((ucg->arg.ccs_line[1].current))<<3)&0x0e0) | (((ucg->arg.ccs_line[2].current) >>3));
  280. ucg_com_SendRepeat2Bytes(ucg, 1, c);
  281. ucg_ccs_step(ucg->arg.ccs_line+0);
  282. ucg_ccs_step(ucg->arg.ccs_line+1);
  283. ucg_ccs_step(ucg->arg.ccs_line+2);
  284. }
  285. ucg_com_SetCSLineStatus(ucg, 1); /* disable chip */
  286. return 1;
  287. }
  288. return 0;
  289. }
  290. static const ucg_pgm_uint8_t ucg_pcf8833_power_down_seq[] = {
  291. UCG_CS(0), /* enable chip */
  292. UCG_C10(0x28), /* display off */
  293. UCG_C10(0x10), /* sleep in */
  294. UCG_CS(1), /* disable chip */
  295. UCG_END(), /* end of sequence */
  296. };
  297. ucg_int_t ucg_dev_ic_pcf8833_16(ucg_t *ucg, ucg_int_t msg, void *data)
  298. {
  299. switch(msg)
  300. {
  301. case UCG_MSG_DEV_POWER_UP:
  302. //ucg->display_offset.x = 2;
  303. //ucg->display_offset.y = 2;
  304. /* setup com interface and provide information on the clock speed */
  305. /* of the serial and parallel interface. Values are nanoseconds. */
  306. return ucg_com_PowerUp(ucg, 150, 160);
  307. case UCG_MSG_DEV_POWER_DOWN:
  308. ucg_com_SendCmdSeq(ucg, ucg_pcf8833_power_down_seq);
  309. return 1;
  310. case UCG_MSG_GET_DIMENSION:
  311. ((ucg_wh_t *)data)->w = WIDTH;
  312. ((ucg_wh_t *)data)->h = HEIGHT;
  313. return 1;
  314. case UCG_MSG_DRAW_PIXEL:
  315. if ( ucg_clip_is_pixel_visible(ucg) !=0 )
  316. {
  317. uint8_t c[3];
  318. ucg_com_SendCmdSeq(ucg, ucg_pcf8833_set_pos_seq);
  319. c[0] = ucg_pcf8833_get_color_high_byte(ucg);
  320. c[1] = ucg_pcf8833_get_color_low_byte(ucg);
  321. ucg_com_SendRepeat2Bytes(ucg, 1, c);
  322. ucg_com_SetCSLineStatus(ucg, 1); /* disable chip */
  323. }
  324. return 1;
  325. case UCG_MSG_DRAW_L90FX:
  326. //ucg_handle_l90fx(ucg, ucg_dev_ic_pcf8833_16);
  327. ucg_handle_pcf8833_l90fx(ucg);
  328. return 1;
  329. #ifdef UCG_MSG_DRAW_L90TC
  330. case UCG_MSG_DRAW_L90TC:
  331. //ucg_handle_l90tc(ucg, ucg_dev_ic_pcf8833_16);
  332. ucg_handle_pcf8833_l90tc(ucg);
  333. return 1;
  334. #endif /* UCG_MSG_DRAW_L90TC */
  335. #ifdef UCG_MSG_DRAW_L90BF
  336. case UCG_MSG_DRAW_L90BF:
  337. ucg_handle_l90bf(ucg, ucg_dev_ic_pcf8833_16);
  338. return 1;
  339. #endif /* UCG_MSG_DRAW_L90BF */
  340. /* msg UCG_MSG_DRAW_L90SE is handled by ucg_dev_default_cb */
  341. /*
  342. case UCG_MSG_DRAW_L90SE:
  343. return ucg->ext_cb(ucg, msg, data);
  344. */
  345. }
  346. return ucg_dev_default_cb(ucg, msg, data);
  347. }
  348. ucg_int_t ucg_ext_pcf8833_16(ucg_t *ucg, ucg_int_t msg, void *data)
  349. {
  350. switch(msg)
  351. {
  352. case UCG_MSG_DRAW_L90SE:
  353. //ucg_handle_l90se(ucg, ucg_dev_ic_pcf8833_16);
  354. ucg_handle_pcf8833_l90se(ucg);
  355. break;
  356. }
  357. return 1;
  358. }