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