u8x8_d_ch1120.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. u8x8_d_ch1120.c
  3. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  4. Copyright (c) 2018, 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. /*
  29. code copyied from sh1107
  30. ch1120: 160x160 controller from Sino Wealth
  31. https://github.com/olikraus/u8g2/issues/2496
  32. */
  33. static const uint8_t u8x8_d_ch1120_noname_powersave0_seq[] = {
  34. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  35. U8X8_C(0x0af), /* display on */
  36. U8X8_END_TRANSFER(), /* disable chip */
  37. U8X8_END() /* end of sequence */
  38. };
  39. static const uint8_t u8x8_d_ch1120_noname_powersave1_seq[] = {
  40. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  41. U8X8_C(0x0ae), /* display off */
  42. U8X8_END_TRANSFER(), /* disable chip */
  43. U8X8_END() /* end of sequence */
  44. };
  45. static const uint8_t u8x8_d_ch1120_160x160_noname_powersave0_seq[] = {
  46. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  47. U8X8_C(0x0a0), /* segment remap a0/a1*/
  48. U8X8_C(0x0c0), /* c0: scan dir normal, c8: reverse */
  49. U8X8_END_TRANSFER(), /* disable chip */
  50. U8X8_END() /* end of sequence */
  51. };
  52. static const uint8_t u8x8_d_ch1120_160x160_noname_powersave1_seq[] = {
  53. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  54. U8X8_C(0x0a1), /* segment remap a0/a1*/
  55. U8X8_C(0x0c8), /* c0: scan dir normal, c8: reverse */
  56. U8X8_END_TRANSFER(), /* disable chip */
  57. U8X8_END() /* end of sequence */
  58. };
  59. static uint8_t u8x8_d_ch1120_generic(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  60. {
  61. uint8_t x, c;
  62. uint8_t *ptr;
  63. switch(msg)
  64. {
  65. /* handled by the calling function
  66. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  67. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_ch1120_64x128_noname_display_info);
  68. break;
  69. */
  70. /* handled by the calling function
  71. case U8X8_MSG_DISPLAY_INIT:
  72. u8x8_d_helper_display_init(u8x8);
  73. u8x8_cad_SendSequence(u8x8, u8x8_d_ch1120_64x128_noname_init_seq);
  74. break;
  75. */
  76. case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
  77. if ( arg_int == 0 )
  78. u8x8_cad_SendSequence(u8x8, u8x8_d_ch1120_noname_powersave0_seq);
  79. else
  80. u8x8_cad_SendSequence(u8x8, u8x8_d_ch1120_noname_powersave1_seq);
  81. break;
  82. case U8X8_MSG_DISPLAY_SET_FLIP_MODE:
  83. if ( arg_int == 0 )
  84. {
  85. u8x8_cad_SendSequence(u8x8, u8x8_d_ch1120_160x160_noname_powersave0_seq);
  86. u8x8->x_offset = u8x8->display_info->default_x_offset;
  87. }
  88. else
  89. {
  90. u8x8_cad_SendSequence(u8x8, u8x8_d_ch1120_160x160_noname_powersave1_seq);
  91. u8x8->x_offset = u8x8->display_info->flipmode_x_offset;
  92. }
  93. break;
  94. #ifdef U8X8_WITH_SET_CONTRAST
  95. case U8X8_MSG_DISPLAY_SET_CONTRAST:
  96. u8x8_cad_StartTransfer(u8x8);
  97. u8x8_cad_SendCmd(u8x8, 0x081 );
  98. u8x8_cad_SendArg(u8x8, arg_int ); /* ch1120 has range from 0 to 255 */
  99. u8x8_cad_EndTransfer(u8x8);
  100. break;
  101. #endif
  102. case U8X8_MSG_DISPLAY_DRAW_TILE:
  103. u8x8_cad_StartTransfer(u8x8);
  104. x = ((u8x8_tile_t *)arg_ptr)->x_pos;
  105. x *= 8;
  106. x += u8x8->x_offset;
  107. //u8x8_cad_SendCmd(u8x8, 0x040 ); /* set line offset to 0 */
  108. // set column address
  109. u8x8_cad_SendCmd(u8x8, 0x010 | (x >> 4));
  110. u8x8_cad_SendCmd(u8x8, 0x000 | ((x & 15)));
  111. // set page address
  112. u8x8_cad_SendCmd(u8x8, 0x0b0 ); // page cmd is a two byte command
  113. u8x8_cad_SendArg(u8x8, (((u8x8_tile_t *)arg_ptr)->y_pos));
  114. do
  115. {
  116. c = ((u8x8_tile_t *)arg_ptr)->cnt;
  117. ptr = ((u8x8_tile_t *)arg_ptr)->tile_ptr;
  118. u8x8_cad_SendData(u8x8, c*8, ptr); /* note: SendData can not handle more than 255 bytes */
  119. arg_int--;
  120. } while( arg_int > 0 );
  121. u8x8_cad_EndTransfer(u8x8);
  122. break;
  123. default:
  124. return 0;
  125. }
  126. return 1;
  127. }
  128. /*==================================================*/
  129. #ifdef NOT_YET_REQUESTED
  130. /* 160x160 OLED */
  131. static const uint8_t u8x8_d_ch1120_160x160_noname_init_seq[] = {
  132. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  133. U8X8_C(0x0ae), /* display off */
  134. U8X8_CA(0x0d5, 0x060), /* clock divide ratio and oscillator frequency */
  135. U8X8_CA(0x0a9, 0x003), /* set display resolution, 0=64x160, 1=96x160, 2=128x160, 3=160x160 */
  136. U8X8_C(0x020), /* addressing mode */
  137. U8X8_CA(0x081, 0x01f), /* set contrast control */
  138. U8X8_CA(0x0ad, 0x80), /* DC/DC control 80=Use external Vpp, 89=Use internal DC/DC*/
  139. U8X8_C(0x030), /* set discharge VSL level, 0x030..0x03f */
  140. U8X8_CA(0x0d9, 0x028), /* pre-charge period */
  141. U8X8_CA(0x0db, 0x035), /* vcomh deselect level */
  142. U8X8_CA(0x0dc, 0x035), /* VSEGM Deselect Level */
  143. U8X8_C(0x0a0), /* segment remap a0/a1*/
  144. U8X8_C(0x0c0), /* c0: scan dir normal, c8: reverse */
  145. U8X8_C(0x0a4), /* output ram to display */
  146. U8X8_C(0x0a6), /* none inverted normal display mode */
  147. U8X8_END_TRANSFER(), /* disable chip */
  148. U8X8_END() /* end of sequence */
  149. };
  150. static const u8x8_display_info_t u8x8_ch1120_160x160_noname_display_info =
  151. {
  152. /* chip_enable_level = */ 0,
  153. /* chip_disable_level = */ 1,
  154. /* post_chip_enable_wait_ns = */ 60,
  155. /* pre_chip_disable_wait_ns = */ 120,
  156. /* reset_pulse_width_ms = */ 100, /* ch1120: 3 us */
  157. /* post_reset_wait_ms = */ 100, /* sometimes OLEDs need much longer setup time */
  158. /* sda_setup_time_ns = */ 100, /* ch1120: 100ns */
  159. /* sck_pulse_width_ns = */ 100, /* ch1120: 100ns */
  160. /* sck_clock_hz = */ 4000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  161. /* spi_mode = */ 0, /* active high, rising edge */
  162. /* i2c_bus_clock_100kHz = */ 4,
  163. /* data_setup_time_ns = */ 40,
  164. /* write_pulse_width_ns = */ 150, /* ch1120: cycle time is 300ns, so use 300/2 = 150 */
  165. /* tile_width = */ 20,
  166. /* tile_height = */ 20,
  167. /* default_x_offset = */ 0,
  168. /* flipmode_x_offset = */ 0,
  169. /* pixel_width = */ 160,
  170. /* pixel_height = */ 160
  171. };
  172. uint8_t u8x8_d_ch1120_160x160(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  173. {
  174. if ( u8x8_d_ch1120_generic(u8x8, msg, arg_int, arg_ptr) != 0 )
  175. return 1;
  176. switch(msg)
  177. {
  178. case U8X8_MSG_DISPLAY_INIT:
  179. u8x8_d_helper_display_init(u8x8);
  180. u8x8_cad_SendSequence(u8x8, u8x8_d_ch1120_160x160_noname_init_seq);
  181. break;
  182. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  183. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_ch1120_160x160_noname_display_info);
  184. break;
  185. default:
  186. return 0;
  187. }
  188. return 1;
  189. }
  190. #endif
  191. /*==================================================*/
  192. /* https://github.com/olikraus/u8g2/issues/2496, 128x160 OLED */
  193. static const uint8_t u8x8_d_ch1120_128x160_noname_init_seq[] = {
  194. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  195. U8X8_C(0x0ae), /* display off */
  196. //U8X8_CAA(0x21, 0x00, 0x1f), /* Set Column Start/End Address of Display RAM, mono mode */
  197. //U8X8_CAA(0x22, 0x00, 0x4f), /* Set Row Start/End Address of Display RAM, mono mode */
  198. U8X8_CA(0x0a2, 0x000), /* display start */
  199. U8X8_CA(0x081, 0x08f), /* set contrast control */
  200. U8X8_CA(0x0ac, 0x001), /* mono mode, or 0x003???*/
  201. U8X8_CA(0x020, 0x000), /* addressing mode */
  202. U8X8_C(0x0C8), /* scan direction */
  203. U8X8_CA(0x0a3, 0x000), /* display rotation */
  204. U8X8_CA(0x0a8, 0x07f), /* multiplex ratio */
  205. U8X8_CA(0x0d3, 0x010), /* display offset */
  206. U8X8_CA(0x0d5, 0x01f), /* clock divide ratio and oscillator frequency */
  207. U8X8_CA(0x048, 0x002), /* */
  208. U8X8_CA(0x093, 0x002), /* */
  209. U8X8_CA(0x0d8, 0x001), /* */
  210. U8X8_CA(0x049, 0x006), /* */
  211. U8X8_CA(0x0d9, 0x00f), /* */
  212. U8X8_CA(0x094, 0x01f), /* */
  213. U8X8_CA(0x04b, 0x004), /* */
  214. U8X8_CA(0x0da, 0x000), /* */
  215. U8X8_CA(0x0dc, 0x040), /* VSEGM Deselect Level */
  216. U8X8_CA(0x0ad, 0x002), /* */
  217. U8X8_END_TRANSFER(), /* disable chip */
  218. U8X8_END() /* end of sequence */
  219. };
  220. static const u8x8_display_info_t u8x8_ch1120_128x160_noname_display_info =
  221. {
  222. /* chip_enable_level = */ 0,
  223. /* chip_disable_level = */ 1,
  224. /* post_chip_enable_wait_ns = */ 20,
  225. /* pre_chip_disable_wait_ns = */ 20,
  226. /* reset_pulse_width_ms = */ 100, /* ch1120: 3 us */
  227. /* post_reset_wait_ms = */ 100, /* sometimes OLEDs need much longer setup time */
  228. /* sda_setup_time_ns = */ 100, /* ch1120: 100ns */
  229. /* sck_pulse_width_ns = */ 100, /* ch1120: 100ns */
  230. /* sck_clock_hz = */ 4000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  231. /* spi_mode = */ 0, /* active high, rising edge */
  232. /* i2c_bus_clock_100kHz = */ 4,
  233. /* data_setup_time_ns = */ 40,
  234. /* write_pulse_width_ns = */ 40, /* ch1120: cycle time is 300ns, so use 300/2 = 150 */
  235. /* tile_width = */ 16,
  236. /* tile_height = */ 20,
  237. /* default_x_offset = */ 16,
  238. /* flipmode_x_offset = */ 16,
  239. /* pixel_width = */ 128,
  240. /* pixel_height = */ 160
  241. };
  242. uint8_t u8x8_d_ch1120_128x160(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  243. {
  244. if ( u8x8_d_ch1120_generic(u8x8, msg, arg_int, arg_ptr) != 0 )
  245. return 1;
  246. switch(msg)
  247. {
  248. case U8X8_MSG_DISPLAY_INIT:
  249. u8x8_d_helper_display_init(u8x8);
  250. u8x8_cad_SendSequence(u8x8, u8x8_d_ch1120_128x160_noname_init_seq);
  251. break;
  252. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  253. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_ch1120_128x160_noname_display_info);
  254. break;
  255. default:
  256. return 0;
  257. }
  258. return 1;
  259. }
  260. /*==================================================*/