u8x8_d_st7586s_jlx320160.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. u8x8_d_st7586s_jlx320160.c
  3. Display: 320x160 pixel
  4. ST7586s: 384 x 160 x 2
  5. takeover from https://github.com/olikraus/u8g2/issues/1183
  6. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  7. Copyright (c) 2023, olikraus@gmail.com
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without modification,
  10. are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice, this list
  12. of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright notice, this
  14. list of conditions and the following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  17. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  18. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  21. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  26. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  28. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "u8g2.h"
  31. static const uint8_t u8x8_d_st7586s_sleep_on[] = {
  32. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  33. U8X8_C(0x010), /* set power save mode */
  34. U8X8_END_TRANSFER(), /* disable chip */
  35. U8X8_END() /* end of sequence */
  36. };
  37. static const uint8_t u8x8_d_st7586s_sleep_off[] = {
  38. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  39. U8X8_C(0x011), //Sleep out
  40. U8X8_DLY(50), /* delay 50 ms */
  41. U8X8_END_TRANSFER(), /* disable chip */
  42. U8X8_END() /* end of sequence */
  43. };
  44. static const uint8_t u8x8_d_st7586s_jlx320160_flip0_seq[] = {
  45. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  46. U8X8_C(0x036), /* Scan Direction Setting */
  47. U8X8_A(0x000), /* COM159 -> COM0 SEG383 -> SEG0 */
  48. U8X8_C(0x037), /* Start line 0 */
  49. U8X8_A(0x000),
  50. U8X8_END_TRANSFER(), /* disable chip */
  51. U8X8_END() /* end of sequence */
  52. };
  53. /* Flip Mode is not supported, use u8g2 rotation instead */
  54. static const uint8_t u8x8_d_st7586s_jlx320160_flip1_seq[] = {
  55. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  56. U8X8_C(0x036), /* Scan Direction Setting */
  57. U8X8_A(0x000), /* COM0 -> COM159 SEG0 -> SEG383 */
  58. U8X8_C(0x037), /* Start line 0 */
  59. U8X8_A(0x000),
  60. U8X8_END_TRANSFER(), /* disable chip */
  61. U8X8_END() /* end of sequence */
  62. };
  63. static const uint8_t u8x8_d_st7586s_jlx320160_init_seq[] = {
  64. U8X8_START_TRANSFER(),/* enable chip */
  65. U8X8_END_TRANSFER(),/* disable chip */
  66. // U8G_ESC_RST(1), /* hardware reset */
  67. U8X8_DLY(60), /* Delay 60 ms */
  68. U8X8_START_TRANSFER(),/* enable chip */
  69. U8X8_C(0x001), // Soft reset
  70. U8X8_DLY(60), // Delay 120 ms
  71. U8X8_C(0x011), // Sleep Out
  72. U8X8_C(0x028), // Display OFF
  73. U8X8_DLY(25), // Delay 50 ms
  74. U8X8_CAA(0x0C0,70,1),// Vop
  75. U8X8_CA(0x0C3,0x002), // BIAS: 0 = 1/14, 2 = 1/12
  76. U8X8_CA(0x0C4,0x007), // Booster = x8
  77. U8X8_CA(0x0D0,0x01D), // Enable Analog Circuit
  78. U8X8_CA(0x0B3,0x000), // Set FOSC divider
  79. U8X8_CA(0x0B5,0x000), // N-Line = 0
  80. U8X8_C(0x039), // 0x39 Monochrome mode. 0x38 - gray Mode
  81. U8X8_C(0x03A), // Enable DDRAM Interface
  82. U8X8_A(0x002), // monochrome and 4-level
  83. U8X8_C(0x036), // Scan Direction Setting
  84. U8X8_A(0x000), // COM:C159->C0 SEG: SEG383->SEG0
  85. U8X8_C(0x0B1), // First output COM
  86. U8X8_A(0x000), //
  87. U8X8_C(0x0B0), // Duty Setting (num rows - 1)
  88. U8X8_A(0x09F),
  89. U8X8_C(0x020), // Display inversion off
  90. U8X8_C(0x02A), // Column Address Setting
  91. U8X8_A(0), // COL0 -> COL127
  92. U8X8_A(0), //
  93. U8X8_A(0), //
  94. U8X8_A(112-1), // 112*3=336 pixels
  95. U8X8_C(0x02B), // Row Address Setting
  96. U8X8_A(0x000), // ROW0 -> ROW159
  97. U8X8_A(0x000), //
  98. U8X8_A(0x000), //
  99. U8X8_A(0x09F), // 160 pixels
  100. U8X8_C(0x029), // Display ON
  101. U8X8_END_TRANSFER(),/* disable chip */
  102. U8X8_END() /* end of sequence */
  103. };
  104. static const u8x8_display_info_t u8x8_st7586s_jlx320160_display_info =
  105. {
  106. /* chip_enable_level = */ 0,
  107. /* chip_disable_level = */ 1,
  108. /* post_chip_enable_wait_ns = */ 5,
  109. /* pre_chip_disable_wait_ns = */ 5,
  110. /* reset_pulse_width_ms = */ 1,
  111. /* post_reset_wait_ms = */ 6,
  112. /* sda_setup_time_ns = */ 20,
  113. /* sck_pulse_width_ns = */ 100, /* datasheet ST7586S */
  114. /* sck_clock_hz = */ 8000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  115. /* ST7586+Atmega128RFA1 works with 8MHz */
  116. /* spi_mode = */ 3, /* active high, rising edge */
  117. /* i2c_bus_clock_100kHz = */ 4,
  118. /* data_setup_time_ns = */ 20, /* datasheet suggests min 20 */
  119. /* write_pulse_width_ns = */ 40,
  120. /* tile_width = */ 42,
  121. /* tile_height = */ 20,
  122. /* default_x_offset = */ 0, /* abused as flag to know if we are flipped */
  123. /* flipmode_x_offset = */ 1, /* as pixel order different for normal/flipped */
  124. /* pixel_width = */ 320,
  125. /* pixel_height = */ 160
  126. };
  127. /* https://github.com/olikraus/u8g2/issues/2186 */
  128. uint8_t u8x8_d_st7586s_jlx320160(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  129. {
  130. uint16_t c;
  131. uint8_t *ptr;
  132. uint8_t i, byte;
  133. uint32_t input;
  134. uint8_t output[8];
  135. // uint8_t output[4];
  136. switch (msg) {
  137. case U8X8_MSG_DISPLAY_DRAW_TILE:
  138. u8x8_cad_StartTransfer(u8x8); // OK Start transfer
  139. u8x8_cad_SendCmd(u8x8, 0x02B); /* Row Address Setting */
  140. u8x8_cad_SendArg(u8x8, 0x000);
  141. u8x8_cad_SendArg(u8x8, 0x008 * ((u8x8_tile_t *)arg_ptr)->y_pos);
  142. u8x8_cad_SendArg(u8x8, 0x000);
  143. // u8x8_cad_SendArg(u8x8, 0x09F); // should set end row based on display dimensions
  144. u8x8_cad_SendArg(u8x8, u8x8->display_info->pixel_height - 1); /* should this be u8x8->display_info->pixel_height - 1 */
  145. u8x8_cad_SendCmd(u8x8, 0x02C); /* cmd write display data to ram */
  146. c = ((u8x8_tile_t *) arg_ptr)->cnt; //
  147. c *= 8;
  148. ptr = ((u8x8_tile_t *) arg_ptr)->tile_ptr; //
  149. while (c > 0)
  150. {
  151. input = (((uint32_t)ptr[0] << 16) | ((uint32_t)ptr[1] << 8) | (uint32_t)ptr[2]);
  152. for (i=0; i<8; i++)
  153. {
  154. byte = 0;
  155. if (input & 0x800000) // if bit 23
  156. byte = byte | 0xC0; //set pixel 1
  157. if (input & 0x400000) // if bit 22
  158. byte = byte | 0x18; //set pixel 2
  159. if (input & 0x200000) // if bit 22
  160. byte = byte | 0x3; //set pixel 3
  161. output[i] = byte;
  162. input <<= 3;
  163. }
  164. u8x8_cad_SendData(u8x8, 8, output);
  165. ptr += 3;
  166. c -= 3;
  167. }
  168. u8x8_cad_EndTransfer(u8x8);
  169. break;
  170. case U8X8_MSG_DISPLAY_INIT:
  171. u8x8_d_helper_display_init(u8x8);
  172. u8x8_cad_SendSequence(u8x8, u8x8_d_st7586s_jlx320160_init_seq);
  173. break;
  174. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  175. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_st7586s_jlx320160_display_info);
  176. break;
  177. case U8X8_MSG_DISPLAY_SET_FLIP_MODE:
  178. if ( arg_int == 0 )
  179. {
  180. u8x8_cad_SendSequence(u8x8, u8x8_d_st7586s_jlx320160_flip0_seq);
  181. u8x8->x_offset = u8x8->display_info->default_x_offset;
  182. }
  183. else
  184. {
  185. u8x8_cad_SendSequence(u8x8, u8x8_d_st7586s_jlx320160_flip1_seq);
  186. u8x8->x_offset = u8x8->display_info->flipmode_x_offset;
  187. }
  188. break;
  189. case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
  190. if (arg_int == 0)
  191. u8x8_cad_SendSequence(u8x8, u8x8_d_st7586s_sleep_off);
  192. else
  193. u8x8_cad_SendSequence(u8x8, u8x8_d_st7586s_sleep_on);
  194. break;
  195. #ifdef U8X8_WITH_SET_CONTRAST
  196. case U8X8_MSG_DISPLAY_SET_CONTRAST:
  197. u8x8_cad_StartTransfer(u8x8);
  198. u8x8_cad_SendCmd(u8x8, 0x0C0);
  199. u8x8_cad_SendArg(u8x8, arg_int);
  200. u8x8_cad_SendArg(u8x8, 1);
  201. u8x8_cad_EndTransfer(u8x8);
  202. break;
  203. #endif
  204. default:
  205. return 0;
  206. }
  207. return 1;
  208. }