u8x8_d_ssd1306_96x40.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. u8x8_d_ssd1306_96x40.c
  3. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  4. Copyright (c) 2016, 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. takeover from 96x16 OLED
  27. 21 Sep 2022: Also added the 96x39 variant here....
  28. */
  29. #include "u8x8.h"
  30. static const uint8_t u8x8_d_ssd1306_96x40_powersave0_seq[] = {
  31. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  32. U8X8_C(0x0af), /* display on */
  33. U8X8_END_TRANSFER(), /* disable chip */
  34. U8X8_END() /* end of sequence */
  35. };
  36. static const uint8_t u8x8_d_ssd1306_96x40_powersave1_seq[] = {
  37. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  38. U8X8_C(0x0ae), /* display off */
  39. U8X8_END_TRANSFER(), /* disable chip */
  40. U8X8_END() /* end of sequence */
  41. };
  42. static const uint8_t u8x8_d_ssd1306_96x40_flip0_seq[] = {
  43. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  44. U8X8_C(0x0a1), /* segment remap a0/a1*/
  45. U8X8_C(0x0c8), /* c0: scan dir normal, c8: reverse */
  46. U8X8_END_TRANSFER(), /* disable chip */
  47. U8X8_END() /* end of sequence */
  48. };
  49. static const uint8_t u8x8_d_ssd1306_96x40_flip1_seq[] = {
  50. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  51. U8X8_C(0x0a0), /* segment remap a0/a1*/
  52. U8X8_C(0x0c0), /* c0: scan dir normal, c8: reverse */
  53. U8X8_END_TRANSFER(), /* disable chip */
  54. U8X8_END() /* end of sequence */
  55. };
  56. static uint8_t u8x8_d_ssd1306_96x40_generic(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  57. {
  58. uint8_t x, c;
  59. uint8_t *ptr;
  60. switch(msg)
  61. {
  62. /* handled by the calling function
  63. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  64. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_ssd1306_96x40_display_info);
  65. break;
  66. case U8X8_MSG_DISPLAY_INIT:
  67. u8x8_d_helper_display_init(u8x8);
  68. u8x8_cad_SendSequence(u8x8, u8x8_d_ssd1306_96x40_er_init_seq);
  69. break;
  70. */
  71. case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
  72. if ( arg_int == 0 )
  73. u8x8_cad_SendSequence(u8x8, u8x8_d_ssd1306_96x40_powersave0_seq);
  74. else
  75. u8x8_cad_SendSequence(u8x8, u8x8_d_ssd1306_96x40_powersave1_seq);
  76. break;
  77. case U8X8_MSG_DISPLAY_SET_FLIP_MODE:
  78. if ( arg_int == 0 )
  79. {
  80. u8x8_cad_SendSequence(u8x8, u8x8_d_ssd1306_96x40_flip0_seq);
  81. u8x8->x_offset = u8x8->display_info->default_x_offset;
  82. }
  83. else
  84. {
  85. u8x8_cad_SendSequence(u8x8, u8x8_d_ssd1306_96x40_flip1_seq);
  86. u8x8->x_offset = u8x8->display_info->flipmode_x_offset;
  87. }
  88. break;
  89. #ifdef U8X8_WITH_SET_CONTRAST
  90. case U8X8_MSG_DISPLAY_SET_CONTRAST:
  91. u8x8_cad_StartTransfer(u8x8);
  92. u8x8_cad_SendCmd(u8x8, 0x081 );
  93. u8x8_cad_SendArg(u8x8, arg_int ); /* ssd1306 has range from 0 to 255 */
  94. u8x8_cad_EndTransfer(u8x8);
  95. break;
  96. #endif
  97. case U8X8_MSG_DISPLAY_DRAW_TILE:
  98. u8x8_cad_StartTransfer(u8x8);
  99. x = ((u8x8_tile_t *)arg_ptr)->x_pos;
  100. x *= 8;
  101. x += u8x8->x_offset;
  102. u8x8_cad_SendCmd(u8x8, 0x010 | (x>>4) );
  103. u8x8_cad_SendCmd(u8x8, 0x000 | ((x&15)));
  104. u8x8_cad_SendCmd(u8x8, 0x0b0 | (((u8x8_tile_t *)arg_ptr)->y_pos));
  105. do
  106. {
  107. c = ((u8x8_tile_t *)arg_ptr)->cnt;
  108. ptr = ((u8x8_tile_t *)arg_ptr)->tile_ptr;
  109. u8x8_cad_SendData(u8x8, c*8, ptr); /* note: SendData can not handle more than 255 bytes */
  110. arg_int--;
  111. } while( arg_int > 0 );
  112. u8x8_cad_EndTransfer(u8x8);
  113. break;
  114. default:
  115. return 0;
  116. }
  117. return 1;
  118. }
  119. /*===============================================*/
  120. /* https://github.com/olikraus/u8g2/issues/1812 */
  121. static const uint8_t u8x8_d_ssd1306_96x40_init_seq[] = {
  122. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  123. U8X8_C(0x0ae), /* display off */
  124. U8X8_CA(0x0d5, 0x080), /* clock divide ratio (0x00=1) and oscillator frequency (0x8) */
  125. U8X8_CA(0x0a8, 0x027), /* multiplex ratio */
  126. U8X8_CA(0x0d3, 0x000), /* display offset */
  127. U8X8_C(0x040), /* set display start line to 0 */
  128. U8X8_CA(0x08d, 0x014), /* [2] charge pump setting (p62): 0x014 enable, 0x010 disable, 0.66 OLED 0x14*/
  129. U8X8_CA(0x020, 0x000), /* horizontal addressing mode */
  130. U8X8_C(0x0a1), /* segment remap a0/a1, 0.66 OLED */
  131. U8X8_C(0x0c8), /* c0: scan dir normal, c8: reverse, 0.66 OLED */
  132. // Flipmode
  133. // U8X8_C(0x0a0), /* segment remap a0/a1*/
  134. // U8X8_C(0x0c0), /* c0: scan dir normal, c8: reverse */
  135. U8X8_CA(0x0da, 0x002), /* com pin HW config, sequential com pin config (bit 4), disable left/right remap (bit 5), 0.66 OLED */
  136. U8X8_CA(0x081, 0x0af), /* [2] set contrast control */
  137. U8X8_CA(0x0d9, 0x0f1), /* [2] pre-charge period 0x0f1 */
  138. U8X8_CA(0x0db, 0x020), /* vcomh deselect level */
  139. U8X8_C(0x02e), /* Deactivate scroll */
  140. U8X8_C(0x0a4), /* output ram to display */
  141. U8X8_C(0x0a6), /* none inverted normal display mode */
  142. U8X8_END_TRANSFER(), /* disable chip */
  143. U8X8_END() /* end of sequence */
  144. };
  145. static const u8x8_display_info_t u8x8_ssd1306_96x40_display_info =
  146. {
  147. /* chip_enable_level = */ 0,
  148. /* chip_disable_level = */ 1,
  149. /* post_chip_enable_wait_ns = */ 20,
  150. /* pre_chip_disable_wait_ns = */ 10,
  151. /* reset_pulse_width_ms = */ 100, /* SSD1306: 3 us */
  152. /* post_reset_wait_ms = */ 100, /* OLEDs need much longer setup time */
  153. /* sda_setup_time_ns = */ 50, /* SSD1306: 15ns, but cycle time is 100ns, so use 100/2 */
  154. /* sck_pulse_width_ns = */ 50, /* SSD1306: 20ns, but cycle time is 100ns, so use 100/2, AVR: below 70: 8 MHz, >= 70 --> 4MHz clock */
  155. /* sck_clock_hz = */ 8000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns, increased to 8MHz (issue 215) */
  156. /* spi_mode = */ 0, /* active high, rising edge */
  157. /* i2c_bus_clock_100kHz = */ 4,
  158. /* data_setup_time_ns = */ 40,
  159. /* write_pulse_width_ns = */ 150, /* SSD1306: cycle time is 300ns, so use 300/2 = 150 */
  160. /* tile_width = */ 12,
  161. /* tile_height = */ 5,
  162. /* default_x_offset = */ 0,
  163. /* flipmode_x_offset = */ 32,
  164. /* pixel_width = */ 96,
  165. /* pixel_height = */ 40
  166. };
  167. uint8_t u8x8_d_ssd1306_96x40(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  168. {
  169. if ( msg == U8X8_MSG_DISPLAY_SETUP_MEMORY )
  170. {
  171. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_ssd1306_96x40_display_info);
  172. return 1;
  173. }
  174. else if ( msg == U8X8_MSG_DISPLAY_INIT )
  175. {
  176. u8x8_d_helper_display_init(u8x8);
  177. u8x8_cad_SendSequence(u8x8, u8x8_d_ssd1306_96x40_init_seq);
  178. return 1;
  179. }
  180. return u8x8_d_ssd1306_96x40_generic(u8x8, msg, arg_int, arg_ptr);
  181. }
  182. /*===============================================*/
  183. /* 96x39 OLED */
  184. /* https://github.com/olikraus/u8g2/issues/1959 */
  185. static const uint8_t u8x8_d_ssd1306_96x39_init_seq[] = {
  186. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  187. U8X8_C(0x0ae), /* display off */
  188. U8X8_CA(0x0d5, 0x080), /* clock divide ratio (0x00=1) and oscillator frequency (0x8) */
  189. U8X8_CA(0x0a8, 0x027), /* multiplex ratio */
  190. U8X8_CA(0x0d3, 0), /* display offset, issue 1959 */
  191. U8X8_C(0x040), /* set display start line to 0 */
  192. U8X8_CA(0x08d, 0x014), /* [2] charge pump setting (p62): 0x014 enable, 0x010 disable, 0.66 OLED 0x14*/
  193. U8X8_CA(0x020, 0x000), /* horizontal addressing mode */
  194. U8X8_C(0x0a1), /* segment remap a0/a1, 0.66 OLED */
  195. U8X8_C(0x0c8), /* c0: scan dir normal, c8: reverse, 0.66 OLED */
  196. // Flipmode
  197. // U8X8_C(0x0a0), /* segment remap a0/a1*/
  198. // U8X8_C(0x0c0), /* c0: scan dir normal, c8: reverse */
  199. U8X8_CA(0x0da, 0x010), /* com pin HW config, sequential com pin config (bit 4), disable left/right remap (bit 5), issue 1959: 0x010 */
  200. U8X8_CA(0x081, 0x0af), /* [2] set contrast control */
  201. U8X8_CA(0x0d9, 0x0f1), /* [2] pre-charge period 0x0f1 */
  202. U8X8_CA(0x0db, 0x020), /* vcomh deselect level */
  203. U8X8_C(0x02e), /* Deactivate scroll */
  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 uint8_t u8x8_d_ssd1306_96x39_flip0_seq[] = {
  210. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  211. U8X8_C(0x0a1), /* segment remap a0/a1*/
  212. U8X8_C(0x0c8), /* c0: scan dir normal, c8: reverse */
  213. U8X8_C(0x07f), /* set display start line to -1 (not exactly sure why...) */
  214. U8X8_END_TRANSFER(), /* disable chip */
  215. U8X8_END() /* end of sequence */
  216. };
  217. static const uint8_t u8x8_d_ssd1306_96x39_flip1_seq[] = {
  218. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  219. U8X8_C(0x0a0), /* segment remap a0/a1*/
  220. U8X8_C(0x0c0), /* c0: scan dir normal, c8: reverse */
  221. U8X8_C(0x040), /* set display start line to 0 */
  222. U8X8_END_TRANSFER(), /* disable chip */
  223. U8X8_END() /* end of sequence */
  224. };
  225. static const u8x8_display_info_t u8x8_ssd1306_96x39_display_info =
  226. {
  227. /* chip_enable_level = */ 0,
  228. /* chip_disable_level = */ 1,
  229. /* post_chip_enable_wait_ns = */ 20,
  230. /* pre_chip_disable_wait_ns = */ 10,
  231. /* reset_pulse_width_ms = */ 100, /* SSD1306: 3 us */
  232. /* post_reset_wait_ms = */ 100, /* OLEDs need much longer setup time */
  233. /* sda_setup_time_ns = */ 50, /* SSD1306: 15ns, but cycle time is 100ns, so use 100/2 */
  234. /* sck_pulse_width_ns = */ 50, /* SSD1306: 20ns, but cycle time is 100ns, so use 100/2, AVR: below 70: 8 MHz, >= 70 --> 4MHz clock */
  235. /* sck_clock_hz = */ 8000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns, increased to 8MHz (issue 215) */
  236. /* spi_mode = */ 0, /* active high, rising edge */
  237. /* i2c_bus_clock_100kHz = */ 4,
  238. /* data_setup_time_ns = */ 40,
  239. /* write_pulse_width_ns = */ 150, /* SSD1306: cycle time is 300ns, so use 300/2 = 150 */
  240. /* tile_width = */ 12,
  241. /* tile_height = */ 5,
  242. /* default_x_offset = */ 32,
  243. /* flipmode_x_offset = */ 0,
  244. /* pixel_width = */ 96,
  245. /* pixel_height = */ 39
  246. };
  247. uint8_t u8x8_d_ssd1306_96x39(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  248. {
  249. if ( msg == U8X8_MSG_DISPLAY_SETUP_MEMORY )
  250. {
  251. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_ssd1306_96x39_display_info);
  252. return 1;
  253. }
  254. else if ( msg == U8X8_MSG_DISPLAY_INIT )
  255. {
  256. u8x8_d_helper_display_init(u8x8);
  257. u8x8_cad_SendSequence(u8x8, u8x8_d_ssd1306_96x39_init_seq);
  258. return 1;
  259. }
  260. else if ( msg == U8X8_MSG_DISPLAY_SET_FLIP_MODE )
  261. {
  262. if ( arg_int == 0 )
  263. {
  264. u8x8_cad_SendSequence(u8x8, u8x8_d_ssd1306_96x39_flip0_seq);
  265. u8x8->x_offset = u8x8->display_info->default_x_offset;
  266. }
  267. else
  268. {
  269. u8x8_cad_SendSequence(u8x8, u8x8_d_ssd1306_96x39_flip1_seq);
  270. u8x8->x_offset = u8x8->display_info->flipmode_x_offset;
  271. }
  272. }
  273. return u8x8_d_ssd1306_96x40_generic(u8x8, msg, arg_int, arg_ptr);
  274. }
  275. /*===============================================*/