u8x8_d_sh1108.c 11 KB

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