u8x8_d_uc1617.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. u8x8_d_uc1617.c
  3. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  4. Copyright (c) 2017, olikraus@gmail.com
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice, this list
  9. of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice, this
  11. list of conditions and the following disclaimer in the documentation and/or other
  12. materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  14. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  15. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "u8x8.h"
  28. static const uint8_t u8x8_d_uc1617_powersave0_seq[] = {
  29. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  30. //U8X8_C(0x0ad), /* display enable BW Mode*/
  31. U8X8_C(0x0af), /* display enable GS Mode*/
  32. U8X8_END_TRANSFER(), /* disable chip */
  33. U8X8_END() /* end of sequence */
  34. };
  35. static const uint8_t u8x8_d_uc1617_powersave1_seq[] = {
  36. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  37. U8X8_C(0x0ac), /* display off, enter sleep mode */
  38. U8X8_END_TRANSFER(), /* disable chip */
  39. U8X8_END() /* end of sequence */
  40. };
  41. static const uint8_t u8x8_d_uc1617_flip0_seq[] = {
  42. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  43. U8X8_C(0x0c0), /* LCD Mapping */
  44. U8X8_END_TRANSFER(), /* disable chip */
  45. U8X8_END() /* end of sequence */
  46. };
  47. static const uint8_t u8x8_d_uc1617_flip1_seq[] = {
  48. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  49. U8X8_C(0x0c6), /* LCD Mapping */
  50. U8X8_END_TRANSFER(), /* disable chip */
  51. U8X8_END() /* end of sequence */
  52. };
  53. //static uint8_t u8x8_upscale_4bit(uint8_t x) U8X8_NOINLINE;
  54. static uint8_t u8x8_upscale_4bit(uint8_t x)
  55. {
  56. uint8_t y = x;
  57. y |= (y << 4); // x = (x | (x << S[2])) & B[2];
  58. y &= 0x0f;
  59. y |= (y << 2); // x = (x | (x << S[1])) & B[1];
  60. y &= 0x33;
  61. y |= (y << 1); // x = (x | (x << S[0])) & B[0];
  62. y &= 0x55;
  63. y |= (y << 1); // z = x | (y << 1);
  64. return y;
  65. }
  66. static uint8_t u8x8_uc1617_tile_half_buffer[8];
  67. static uint8_t *u8x8_convert_tile_for_uc1617_lower4bit(uint8_t *t)
  68. {
  69. uint8_t i;
  70. uint8_t *pbuf = u8x8_uc1617_tile_half_buffer;
  71. for( i = 0; i < 8; i++ )
  72. {
  73. *pbuf++ = u8x8_upscale_4bit(*t++);
  74. }
  75. return u8x8_uc1617_tile_half_buffer;
  76. }
  77. static uint8_t *u8x8_convert_tile_for_uc1617_upper4bit(uint8_t *t)
  78. {
  79. uint8_t i;
  80. uint8_t *pbuf = u8x8_uc1617_tile_half_buffer;
  81. for( i = 0; i < 8; i++ )
  82. {
  83. *pbuf++ = u8x8_upscale_4bit((*t++)>>4);
  84. }
  85. return u8x8_uc1617_tile_half_buffer;
  86. }
  87. #ifdef NOT_USED
  88. static uint8_t *u8x8_convert_tile_for_uc1617(uint8_t *t)
  89. {
  90. uint8_t i;
  91. uint16_t r;
  92. static uint8_t buf[16];
  93. uint8_t *pbuf = buf;
  94. for( i = 0; i < 8; i++ )
  95. {
  96. r = u8x8_upscale_byte(*t++);
  97. *pbuf = r & 255;
  98. r >>= 8;
  99. pbuf+=8;
  100. *pbuf = r;
  101. pbuf-=7;
  102. }
  103. return buf;
  104. }
  105. #endif
  106. uint8_t u8x8_d_uc1617_common(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  107. {
  108. uint8_t x, y, c, a;
  109. uint8_t *ptr;
  110. switch(msg)
  111. {
  112. case U8X8_MSG_DISPLAY_DRAW_TILE:
  113. u8x8_cad_StartTransfer(u8x8);
  114. y = ((u8x8_tile_t *)arg_ptr)->y_pos;
  115. y*=2;
  116. x = ((u8x8_tile_t *)arg_ptr)->x_pos;
  117. x *= 8;
  118. x += u8x8->x_offset;
  119. u8x8_cad_SendCmd(u8x8, 0x060 | (x&15));
  120. u8x8_cad_SendCmd(u8x8, 0x070 | (x>>4));
  121. u8x8_cad_SendCmd(u8x8, 0x00 | (y));
  122. #ifdef NOT_REQUIRED
  123. u8x8_cad_SendCmd(u8x8, 0xf8 ); /* disable window */
  124. u8x8_cad_SendCmd(u8x8, 0xf4 ); /* page start */
  125. u8x8_cad_SendCmd(u8x8, y );
  126. u8x8_cad_SendCmd(u8x8, 0xf5 ); /* x start */
  127. u8x8_cad_SendCmd(u8x8, x );
  128. u8x8_cad_SendCmd(u8x8, 0xf6 ); /* page end */
  129. u8x8_cad_SendCmd(u8x8, y );
  130. u8x8_cad_SendCmd(u8x8, 0xf7 ); /* x end */
  131. u8x8_cad_SendCmd(u8x8, 127 );
  132. u8x8_cad_SendCmd(u8x8, 0xf9 ); /* enable window */
  133. #endif
  134. a = arg_int;
  135. do
  136. {
  137. c = ((u8x8_tile_t *)arg_ptr)->cnt;
  138. ptr = ((u8x8_tile_t *)arg_ptr)->tile_ptr;
  139. do
  140. {
  141. u8x8_cad_SendData(u8x8, 8, u8x8_convert_tile_for_uc1617_lower4bit(ptr));
  142. ptr += 8;
  143. x += 8;
  144. c--;
  145. } while( c > 0 );
  146. a--;
  147. } while( a > 0 );
  148. x = ((u8x8_tile_t *)arg_ptr)->x_pos;
  149. x *= 8;
  150. x += u8x8->x_offset;
  151. u8x8_cad_SendCmd(u8x8, 0x060 | (x&15));
  152. u8x8_cad_SendCmd(u8x8, 0x070 | (x>>4));
  153. u8x8_cad_SendCmd(u8x8, 0x00 | (y+1));
  154. a = arg_int;
  155. do
  156. {
  157. c = ((u8x8_tile_t *)arg_ptr)->cnt;
  158. ptr = ((u8x8_tile_t *)arg_ptr)->tile_ptr;
  159. do
  160. {
  161. u8x8_cad_SendData(u8x8, 8, u8x8_convert_tile_for_uc1617_upper4bit(ptr));
  162. ptr += 8;
  163. x += 8;
  164. c--;
  165. } while( c > 0 );
  166. a--;
  167. } while( a > 0 );
  168. u8x8_cad_EndTransfer(u8x8);
  169. break;
  170. case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
  171. if ( arg_int == 0 )
  172. u8x8_cad_SendSequence(u8x8, u8x8_d_uc1617_powersave0_seq);
  173. else
  174. u8x8_cad_SendSequence(u8x8, u8x8_d_uc1617_powersave1_seq);
  175. break;
  176. case U8X8_MSG_DISPLAY_SET_FLIP_MODE:
  177. if ( arg_int == 0 )
  178. {
  179. u8x8_cad_SendSequence(u8x8, u8x8_d_uc1617_flip0_seq);
  180. u8x8->x_offset = u8x8->display_info->default_x_offset;
  181. }
  182. else
  183. {
  184. u8x8_cad_SendSequence(u8x8, u8x8_d_uc1617_flip1_seq);
  185. u8x8->x_offset = u8x8->display_info->flipmode_x_offset;
  186. }
  187. break;
  188. #ifdef U8X8_WITH_SET_CONTRAST
  189. case U8X8_MSG_DISPLAY_SET_CONTRAST:
  190. u8x8_cad_StartTransfer(u8x8);
  191. u8x8_cad_SendCmd(u8x8, 0x081 );
  192. u8x8_cad_SendArg(u8x8, arg_int ); /* uc1617 has range from 0 to 255 */
  193. u8x8_cad_EndTransfer(u8x8);
  194. break;
  195. #endif
  196. default:
  197. return 0;
  198. }
  199. return 1;
  200. }
  201. /*================================================*/
  202. /* JLX128128 */
  203. static const uint8_t u8x8_d_uc1617_jlx128128_init_seq[] = {
  204. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  205. U8X8_C(0x0e2), /* reset */
  206. U8X8_DLY(10),
  207. //U8X8_D1(0x0ff),
  208. U8X8_C(0x027), /* temperature compensation */
  209. U8X8_C(0x02b), /* panel loading: 13-18nF */
  210. U8X8_C(0x02f), /* internal pump control */
  211. U8X8_C(0x0eb), /* bias=1/11 */
  212. U8X8_CA(0x081, 0x028), /* set contrast */
  213. //U8X8_C(0x0a9), /* used in display datasheet, but cmd not described in controller datasheet */
  214. U8X8_CA(0x0f1, 0x07f), /* set COM end */
  215. U8X8_CA(0x0f2, 0x000), /* display line start */
  216. U8X8_CA(0x0f3, 127), /* display line end */
  217. U8X8_C(0x0a3), /* line rate */
  218. U8X8_C(0x0d3), /* */
  219. U8X8_C(0x0d7), /* */
  220. //U8X8_C(0x0a5), /* all pixel on */
  221. //U8X8_C(0x0d1), /* display pattern */
  222. U8X8_C(0x08b), /* auto increment */
  223. U8X8_C(0x0c0), /* LCD Mapping */
  224. //U8X8_C(0x0ad), /* display enable BW Mode*/
  225. //U8X8_C(0x0af), /* display enable GS Mode*/
  226. U8X8_END_TRANSFER(), /* disable chip */
  227. U8X8_END() /* end of sequence */
  228. };
  229. static const u8x8_display_info_t u8x8_uc1617_128x128_display_info =
  230. {
  231. /* chip_enable_level = */ 0,
  232. /* chip_disable_level = */ 1,
  233. /* post_chip_enable_wait_ns = */ 10, /* uc1617 datasheet, page 54, actually 5 */
  234. /* pre_chip_disable_wait_ns = */ 10, /* uc1617 datasheet, page 54, actually 5 */
  235. /* reset_pulse_width_ms = */ 10,
  236. /* post_reset_wait_ms = */ 20, /* uc1617 datasheet, page 56 */
  237. /* sda_setup_time_ns = */ 24, /* uc1617 datasheet, page 54 */
  238. /* sck_pulse_width_ns = */ 45, /* half of cycle time uc1617 datasheet, page 54*/
  239. /* sck_clock_hz = */ 8000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  240. /* spi_mode = */ 0, /* active high, rising edge */
  241. /* i2c_bus_clock_100kHz = */ 4,
  242. /* data_setup_time_ns = */ 30, /* uc1617 datasheet, page 52 */
  243. /* write_pulse_width_ns = */ 65, /* uc1617 datasheet, page 52 */
  244. /* tile_width = */ 16, /* width of 16*8=128 pixel */
  245. /* tile_height = */ 16,
  246. /* default_x_offset = */ 0,
  247. /* flipmode_x_offset = */ 0,
  248. /* pixel_width = */ 128,
  249. /* pixel_height = */ 128
  250. };
  251. uint8_t u8x8_d_uc1617_jlx128128(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  252. {
  253. /* call common procedure first and handle messages there */
  254. if ( u8x8_d_uc1617_common(u8x8, msg, arg_int, arg_ptr) == 0 )
  255. {
  256. /* msg not handled, then try here */
  257. switch(msg)
  258. {
  259. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  260. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_uc1617_128x128_display_info);
  261. break;
  262. case U8X8_MSG_DISPLAY_INIT:
  263. u8x8_d_helper_display_init(u8x8);
  264. u8x8_cad_SendSequence(u8x8, u8x8_d_uc1617_jlx128128_init_seq);
  265. break;
  266. default:
  267. return 0; /* msg unknown */
  268. }
  269. }
  270. return 1;
  271. }