u8x8_d_ssd1316.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. u8x8_d_ssd1316.c
  3. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  4. Copyright (c) 2019, 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. SSD1316: 128x39 OLED
  27. https://github.com/olikraus/u8g2/issues/919
  28. */
  29. #include "u8x8.h"
  30. static const uint8_t u8x8_d_ssd1316_128x32_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_ssd1316_128x32_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_ssd1316_128x32_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(0x0c0), /* 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_ssd1316_128x32_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(0x0c8), /* c0: scan dir normal, c8: reverse */
  53. U8X8_END_TRANSFER(), /* disable chip */
  54. U8X8_END() /* end of sequence */
  55. };
  56. /*===================================================*/
  57. static uint8_t u8x8_d_ssd1316_generic(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  58. {
  59. uint8_t x, c;
  60. uint8_t *ptr;
  61. switch(msg)
  62. {
  63. /* handled by the calling function
  64. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  65. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_ssd1316_128x32_display_info);
  66. break;
  67. */
  68. /* handled by the calling function
  69. case U8X8_MSG_DISPLAY_INIT:
  70. u8x8_d_helper_display_init(u8x8);
  71. u8x8_cad_SendSequence(u8x8, u8x8_d_ssd1316_128x32_init_seq);
  72. break;
  73. */
  74. case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
  75. if ( arg_int == 0 )
  76. u8x8_cad_SendSequence(u8x8, u8x8_d_ssd1316_128x32_powersave0_seq);
  77. else
  78. u8x8_cad_SendSequence(u8x8, u8x8_d_ssd1316_128x32_powersave1_seq);
  79. break;
  80. case U8X8_MSG_DISPLAY_SET_FLIP_MODE:
  81. if ( arg_int == 0 )
  82. {
  83. u8x8_cad_SendSequence(u8x8, u8x8_d_ssd1316_128x32_flip0_seq);
  84. u8x8->x_offset = u8x8->display_info->default_x_offset;
  85. }
  86. else
  87. {
  88. u8x8_cad_SendSequence(u8x8, u8x8_d_ssd1316_128x32_flip1_seq);
  89. u8x8->x_offset = u8x8->display_info->flipmode_x_offset;
  90. }
  91. break;
  92. #ifdef U8X8_WITH_SET_CONTRAST
  93. case U8X8_MSG_DISPLAY_SET_CONTRAST:
  94. u8x8_cad_StartTransfer(u8x8);
  95. u8x8_cad_SendCmd(u8x8, 0x081 );
  96. u8x8_cad_SendArg(u8x8, arg_int ); /* ssd1306 has range from 0 to 255 */
  97. u8x8_cad_EndTransfer(u8x8);
  98. break;
  99. #endif
  100. case U8X8_MSG_DISPLAY_DRAW_TILE:
  101. x = ((u8x8_tile_t *)arg_ptr)->x_pos;
  102. x *= 8;
  103. x += u8x8->x_offset;
  104. u8x8_cad_StartTransfer(u8x8);
  105. u8x8_cad_SendCmd(u8x8, 0x010 | (x>>4) );
  106. u8x8_cad_SendCmd(u8x8, 0x000 | ((x&15))); /* probably wrong, should be SendCmd */
  107. u8x8_cad_SendCmd(u8x8, 0x0b0 | (((u8x8_tile_t *)arg_ptr)->y_pos)); /* probably wrong, should be SendCmd */
  108. do
  109. {
  110. c = ((u8x8_tile_t *)arg_ptr)->cnt;
  111. ptr = ((u8x8_tile_t *)arg_ptr)->tile_ptr;
  112. u8x8_cad_SendData(u8x8, c*8, ptr); /* note: SendData can not handle more than 255 bytes */
  113. arg_int--;
  114. } while( arg_int > 0 );
  115. u8x8_cad_EndTransfer(u8x8);
  116. break;
  117. default:
  118. return 0;
  119. }
  120. return 1;
  121. }
  122. /*===================================================*/
  123. /* QT-2832TSWUG02/ZJY-2832TSWZG02 */
  124. static const uint8_t u8x8_d_ssd1316_128x32_init_seq[] = {
  125. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  126. U8X8_C(0x0ae), /* display off */
  127. U8X8_C(0x040), /* start line */
  128. U8X8_CA(0x081, 0x045), /* QG-2832TSWZG02 datasheet */
  129. U8X8_C(0x0a6), /* none inverted normal display mode */
  130. U8X8_CA(0x0a8, 0x01f), /* multiplex ratio, duty = 1/32 */
  131. U8X8_C(0x0a1), /* segment remap a0/a1*/
  132. U8X8_C(0x0c0), /* c0: scan dir normal, c8: reverse */
  133. U8X8_CA(0x0d3, 0x000), /* display offset */
  134. U8X8_CA(0x0d5, 0x080), /* clock divide ratio (0x00=1) and oscillator frequency (0x8) */
  135. U8X8_CA(0x0d9, 0x022), /* [2] pre-charge period 0x022/f1*/
  136. U8X8_CA(0x0da, 0x012), /* com pin HW config, sequential com pin config (bit 4), disable left/right remap (bit 5) */
  137. U8X8_CA(0x0db, 0x020), /* vcomh deselect level */
  138. U8X8_CA(0x08d, 0x015), /* [2] charge pump setting (p62): 0x014 enable, 0x010 disable, */
  139. //U8X8_CA(0x0a2, 0x000), /* set display start line to 0 */
  140. //U8X8_CA(0x020, 0x000), /* horizontal addressing mode */
  141. // Flipmode
  142. //U8X8_C(0x0a1), /* segment remap a0/a1*/
  143. //U8X8_C(0x0c0), /* c0: scan dir normal, c8: reverse */
  144. U8X8_C(0x02e), /* Deactivate scroll */
  145. //U8X8_C(0x0a4), /* output ram to display */
  146. U8X8_END_TRANSFER(), /* disable chip */
  147. U8X8_END() /* end of sequence */
  148. };
  149. static const u8x8_display_info_t u8x8_ssd1316_128x32_display_info =
  150. {
  151. /* chip_enable_level = */ 0,
  152. /* chip_disable_level = */ 1,
  153. /* post_chip_enable_wait_ns = */ 20,
  154. /* pre_chip_disable_wait_ns = */ 10,
  155. /* reset_pulse_width_ms = */ 100, /* reset time */
  156. /* post_reset_wait_ms = */ 100, /* reset delay */
  157. /* sda_setup_time_ns = */ 50, /* SSD1306: 15ns, but cycle time is 100ns, so use 100/2 */
  158. /* sck_pulse_width_ns = */ 50, /* SSD1306: 20ns, but cycle time is 100ns, so use 100/2, AVR: below 70: 8 MHz, >= 70 --> 4MHz clock */
  159. /* sck_clock_hz = */ 8000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  160. /* spi_mode = */ 0, /* active high, rising edge */
  161. /* i2c_bus_clock_100kHz = */ 4,
  162. /* data_setup_time_ns = */ 40,
  163. /* write_pulse_width_ns = */ 150, /* SSD1306: cycle time is 300ns, so use 300/2 = 150 */
  164. /* tile_width = */ 16,
  165. /* tile_hight = */ 4,
  166. /* default_x_offset = */ 0,
  167. /* flipmode_x_offset = */ 0,
  168. /* pixel_width = */ 128,
  169. /* pixel_height = */ 32
  170. };
  171. uint8_t u8x8_d_ssd1316_128x32(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  172. {
  173. if ( u8x8_d_ssd1316_generic(u8x8, msg, arg_int, arg_ptr) != 0 )
  174. return 1;
  175. switch(msg)
  176. {
  177. case U8X8_MSG_DISPLAY_INIT:
  178. u8x8_d_helper_display_init(u8x8);
  179. u8x8_cad_SendSequence(u8x8, u8x8_d_ssd1316_128x32_init_seq);
  180. break;
  181. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  182. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_ssd1316_128x32_display_info);
  183. break;
  184. default:
  185. return 0;
  186. }
  187. return 1;
  188. }