| 1 | /* |
| 2 | |
| 3 | u8x8_d_uc1617.c |
| 4 | |
| 5 | Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/) |
| 6 | |
| 7 | Copyright (c) 2017, olikraus@gmail.com |
| 8 | All rights reserved. |
| 9 | |
| 10 | Redistribution and use in source and binary forms, with or without modification, |
| 11 | are permitted provided that the following conditions are met: |
| 12 | |
| 13 | * Redistributions of source code must retain the above copyright notice, this list |
| 14 | of conditions and the following disclaimer. |
| 15 | |
| 16 | * Redistributions in binary form must reproduce the above copyright notice, this |
| 17 | list of conditions and the following disclaimer in the documentation and/or other |
| 18 | materials provided with the distribution. |
| 19 | |
| 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 21 | CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 22 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 24 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 25 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 27 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 28 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 29 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 30 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 31 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 32 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | |
| 34 | |
| 35 | */ |
| 36 | #include "u8x8.h" |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | static const uint8_t u8x8_d_uc1617_powersave0_seq[] = { |
| 43 | U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */ |
| 44 | //U8X8_C(0x0ad), /* display enable BW Mode*/ |
| 45 | U8X8_C(0x0af), /* display enable GS Mode*/ |
| 46 | U8X8_END_TRANSFER(), /* disable chip */ |
| 47 | U8X8_END() /* end of sequence */ |
| 48 | }; |
| 49 | |
| 50 | static const uint8_t u8x8_d_uc1617_powersave1_seq[] = { |
| 51 | U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */ |
| 52 | U8X8_C(0x0ac), /* display off, enter sleep mode */ |
| 53 | U8X8_END_TRANSFER(), /* disable chip */ |
| 54 | U8X8_END() /* end of sequence */ |
| 55 | }; |
| 56 | |
| 57 | static const uint8_t u8x8_d_uc1617_flip0_seq[] = { |
| 58 | U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */ |
| 59 | U8X8_C(0x0c0), /* LCD Mapping */ |
| 60 | U8X8_END_TRANSFER(), /* disable chip */ |
| 61 | U8X8_END() /* end of sequence */ |
| 62 | }; |
| 63 | |
| 64 | static const uint8_t u8x8_d_uc1617_flip1_seq[] = { |
| 65 | U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */ |
| 66 | U8X8_C(0x0c6), /* LCD Mapping */ |
| 67 | U8X8_END_TRANSFER(), /* disable chip */ |
| 68 | U8X8_END() /* end of sequence */ |
| 69 | }; |
| 70 | |
| 71 | |
| 72 | //static uint8_t u8x8_upscale_4bit(uint8_t x) U8X8_NOINLINE; |
| 73 | static uint8_t u8x8_upscale_4bit(uint8_t x) |
| 74 | { |
| 75 | uint8_t y = x; |
| 76 | y |= (y << 4); // x = (x | (x << S[2])) & B[2]; |
| 77 | y &= 0x0f; |
| 78 | y |= (y << 2); // x = (x | (x << S[1])) & B[1]; |
| 79 | y &= 0x33; |
| 80 | y |= (y << 1); // x = (x | (x << S[0])) & B[0]; |
| 81 | y &= 0x55; |
| 82 | |
| 83 | y |= (y << 1); // z = x | (y << 1); |
| 84 | return y; |
| 85 | } |
| 86 | |
| 87 | static uint8_t u8x8_uc1617_tile_half_buffer[8]; |
| 88 | |
| 89 | static uint8_t *u8x8_convert_tile_for_uc1617_lower4bit(uint8_t *t) |
| 90 | { |
| 91 | uint8_t i; |
| 92 | uint8_t *pbuf = u8x8_uc1617_tile_half_buffer; |
| 93 | |
| 94 | for( i = 0; i < 8; i++ ) |
| 95 | { |
| 96 | *pbuf++ = u8x8_upscale_4bit(*t++); |
| 97 | } |
| 98 | return u8x8_uc1617_tile_half_buffer; |
| 99 | } |
| 100 | |
| 101 | static uint8_t *u8x8_convert_tile_for_uc1617_upper4bit(uint8_t *t) |
| 102 | { |
| 103 | uint8_t i; |
| 104 | uint8_t *pbuf = u8x8_uc1617_tile_half_buffer; |
| 105 | |
| 106 | for( i = 0; i < 8; i++ ) |
| 107 | { |
| 108 | *pbuf++ = u8x8_upscale_4bit((*t++)>>4); |
| 109 | } |
| 110 | return u8x8_uc1617_tile_half_buffer; |
| 111 | } |
| 112 | |
| 113 | #ifdef NOT_USED |
| 114 | static uint8_t *u8x8_convert_tile_for_uc1617(uint8_t *t) |
| 115 | { |
| 116 | uint8_t i; |
| 117 | uint16_t r; |
| 118 | static uint8_t buf[16]; |
| 119 | uint8_t *pbuf = buf; |
| 120 | |
| 121 | for( i = 0; i < 8; i++ ) |
| 122 | { |
| 123 | r = u8x8_upscale_byte(*t++); |
| 124 | *pbuf = r & 255; |
| 125 | r >>= 8; |
| 126 | pbuf+=8; |
| 127 | *pbuf = r; |
| 128 | pbuf-=7; |
| 129 | } |
| 130 | return buf; |
| 131 | } |
| 132 | #endif |
| 133 | |
| 134 | |
| 135 | uint8_t u8x8_d_uc1617_common(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) |
| 136 | { |
| 137 | uint8_t x, y, c, a; |
| 138 | uint8_t *ptr; |
| 139 | switch(msg) |
| 140 | { |
| 141 | case U8X8_MSG_DISPLAY_DRAW_TILE: |
| 142 | u8x8_cad_StartTransfer(u8x8); |
| 143 | |
| 144 | |
| 145 | |
| 146 | y = ((u8x8_tile_t *)arg_ptr)->y_pos; |
| 147 | y*=2; |
| 148 | |
| 149 | x = ((u8x8_tile_t *)arg_ptr)->x_pos; |
| 150 | x *= 8; |
| 151 | x += u8x8->x_offset; |
| 152 | u8x8_cad_SendCmd(u8x8, 0x060 | (x&15)); |
| 153 | u8x8_cad_SendCmd(u8x8, 0x070 | (x>>4)); |
| 154 | u8x8_cad_SendCmd(u8x8, 0x00 | (y)); |
| 155 | |
| 156 | #ifdef NOT_REQUIRED |
| 157 | u8x8_cad_SendCmd(u8x8, 0xf8 ); /* disable window */ |
| 158 | u8x8_cad_SendCmd(u8x8, 0xf4 ); /* page start */ |
| 159 | u8x8_cad_SendCmd(u8x8, y ); |
| 160 | u8x8_cad_SendCmd(u8x8, 0xf5 ); /* x start */ |
| 161 | u8x8_cad_SendCmd(u8x8, x ); |
| 162 | u8x8_cad_SendCmd(u8x8, 0xf6 ); /* page end */ |
| 163 | u8x8_cad_SendCmd(u8x8, y ); |
| 164 | u8x8_cad_SendCmd(u8x8, 0xf7 ); /* x end */ |
| 165 | u8x8_cad_SendCmd(u8x8, 127 ); |
| 166 | u8x8_cad_SendCmd(u8x8, 0xf9 ); /* enable window */ |
| 167 | #endif |
| 168 | |
| 169 | a = arg_int; |
| 170 | do |
| 171 | { |
| 172 | c = ((u8x8_tile_t *)arg_ptr)->cnt; |
| 173 | ptr = ((u8x8_tile_t *)arg_ptr)->tile_ptr; |
| 174 | do |
| 175 | { |
| 176 | u8x8_cad_SendData(u8x8, 8, u8x8_convert_tile_for_uc1617_lower4bit(ptr)); |
| 177 | ptr += 8; |
| 178 | x += 8; |
| 179 | c--; |
| 180 | } while( c > 0 ); |
| 181 | a--; |
| 182 | } while( a > 0 ); |
| 183 | |
| 184 | x = ((u8x8_tile_t *)arg_ptr)->x_pos; |
| 185 | x *= 8; |
| 186 | x += u8x8->x_offset; |
| 187 | u8x8_cad_SendCmd(u8x8, 0x060 | (x&15)); |
| 188 | u8x8_cad_SendCmd(u8x8, 0x070 | (x>>4)); |
| 189 | u8x8_cad_SendCmd(u8x8, 0x00 | (y+1)); |
| 190 | a = arg_int; |
| 191 | do |
| 192 | { |
| 193 | c = ((u8x8_tile_t *)arg_ptr)->cnt; |
| 194 | ptr = ((u8x8_tile_t *)arg_ptr)->tile_ptr; |
| 195 | do |
| 196 | { |
| 197 | u8x8_cad_SendData(u8x8, 8, u8x8_convert_tile_for_uc1617_upper4bit(ptr)); |
| 198 | ptr += 8; |
| 199 | x += 8; |
| 200 | c--; |
| 201 | } while( c > 0 ); |
| 202 | a--; |
| 203 | } while( a > 0 ); |
| 204 | |
| 205 | u8x8_cad_EndTransfer(u8x8); |
| 206 | break; |
| 207 | case U8X8_MSG_DISPLAY_SET_POWER_SAVE: |
| 208 | if ( arg_int == 0 ) |
| 209 | u8x8_cad_SendSequence(u8x8, u8x8_d_uc1617_powersave0_seq); |
| 210 | else |
| 211 | u8x8_cad_SendSequence(u8x8, u8x8_d_uc1617_powersave1_seq); |
| 212 | break; |
| 213 | case U8X8_MSG_DISPLAY_SET_FLIP_MODE: |
| 214 | if ( arg_int == 0 ) |
| 215 | { |
| 216 | u8x8_cad_SendSequence(u8x8, u8x8_d_uc1617_flip0_seq); |
| 217 | u8x8->x_offset = u8x8->display_info->default_x_offset; |
| 218 | } |
| 219 | else |
| 220 | { |
| 221 | u8x8_cad_SendSequence(u8x8, u8x8_d_uc1617_flip1_seq); |
| 222 | u8x8->x_offset = u8x8->display_info->flipmode_x_offset; |
| 223 | } |
| 224 | break; |
| 225 | #ifdef U8X8_WITH_SET_CONTRAST |
| 226 | case U8X8_MSG_DISPLAY_SET_CONTRAST: |
| 227 | u8x8_cad_StartTransfer(u8x8); |
| 228 | u8x8_cad_SendCmd(u8x8, 0x081 ); |
| 229 | u8x8_cad_SendArg(u8x8, arg_int ); /* uc1617 has range from 0 to 255 */ |
| 230 | u8x8_cad_EndTransfer(u8x8); |
| 231 | break; |
| 232 | #endif |
| 233 | default: |
| 234 | return 0; |
| 235 | } |
| 236 | return 1; |
| 237 | } |
| 238 | |
| 239 | /*================================================*/ |
| 240 | /* JLX128128 */ |
| 241 | |
| 242 | static const uint8_t u8x8_d_uc1617_jlx128128_init_seq[] = { |
| 243 | |
| 244 | U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */ |
| 245 | |
| 246 | U8X8_C(0x0e2), /* reset */ |
| 247 | U8X8_DLY(10), |
| 248 | |
| 249 | //U8X8_D1(0x0ff), |
| 250 | U8X8_C(0x027), /* temperature compensation */ |
| 251 | U8X8_C(0x02b), /* panel loading: 13-18nF */ |
| 252 | |
| 253 | U8X8_C(0x02f), /* internal pump control */ |
| 254 | U8X8_C(0x0eb), /* bias=1/11 */ |
| 255 | U8X8_CA(0x081, 0x028), /* set contrast */ |
| 256 | //U8X8_C(0x0a9), /* used in display datasheet, but cmd not described in controller datasheet */ |
| 257 | |
| 258 | U8X8_CA(0x0f1, 0x07f), /* set COM end */ |
| 259 | U8X8_CA(0x0f2, 0x000), /* display line start */ |
| 260 | U8X8_CA(0x0f3, 127), /* display line end */ |
| 261 | U8X8_C(0x0a3), /* line rate */ |
| 262 | |
| 263 | U8X8_C(0x0d3), /* */ |
| 264 | U8X8_C(0x0d7), /* */ |
| 265 | |
| 266 | |
| 267 | //U8X8_C(0x0a5), /* all pixel on */ |
| 268 | |
| 269 | //U8X8_C(0x0d1), /* display pattern */ |
| 270 | U8X8_C(0x08b), /* auto increment */ |
| 271 | U8X8_C(0x0c0), /* LCD Mapping */ |
| 272 | |
| 273 | //U8X8_C(0x0ad), /* display enable BW Mode*/ |
| 274 | //U8X8_C(0x0af), /* display enable GS Mode*/ |
| 275 | |
| 276 | U8X8_END_TRANSFER(), /* disable chip */ |
| 277 | U8X8_END() /* end of sequence */ |
| 278 | }; |
| 279 | |
| 280 | static const u8x8_display_info_t u8x8_uc1617_128x128_display_info = |
| 281 | { |
| 282 | /* chip_enable_level = */ 0, |
| 283 | /* chip_disable_level = */ 1, |
| 284 | |
| 285 | /* post_chip_enable_wait_ns = */ 10, /* uc1617 datasheet, page 54, actually 5 */ |
| 286 | /* pre_chip_disable_wait_ns = */ 10, /* uc1617 datasheet, page 54, actually 5 */ |
| 287 | /* reset_pulse_width_ms = */ 10, |
| 288 | /* post_reset_wait_ms = */ 20, /* uc1617 datasheet, page 56 */ |
| 289 | /* sda_setup_time_ns = */ 24, /* uc1617 datasheet, page 54 */ |
| 290 | /* sck_pulse_width_ns = */ 45, /* half of cycle time uc1617 datasheet, page 54*/ |
| 291 | /* sck_clock_hz = */ 8000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */ |
| 292 | /* spi_mode = */ 0, /* active high, rising edge */ |
| 293 | /* i2c_bus_clock_100kHz = */ 4, |
| 294 | /* data_setup_time_ns = */ 30, /* uc1617 datasheet, page 52 */ |
| 295 | /* write_pulse_width_ns = */ 65, /* uc1617 datasheet, page 52 */ |
| 296 | /* tile_width = */ 16, /* width of 16*8=128 pixel */ |
| 297 | /* tile_height = */ 16, |
| 298 | /* default_x_offset = */ 0, |
| 299 | /* flipmode_x_offset = */ 0, |
| 300 | /* pixel_width = */ 128, |
| 301 | /* pixel_height = */ 128 |
| 302 | }; |
| 303 | |
| 304 | uint8_t u8x8_d_uc1617_jlx128128(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) |
| 305 | { |
| 306 | /* call common procedure first and handle messages there */ |
| 307 | if ( u8x8_d_uc1617_common(u8x8, msg, arg_int, arg_ptr) == 0 ) |
| 308 | { |
| 309 | /* msg not handled, then try here */ |
| 310 | switch(msg) |
| 311 | { |
| 312 | case U8X8_MSG_DISPLAY_SETUP_MEMORY: |
| 313 | u8x8_d_helper_display_setup_memory(u8x8, &u8x8_uc1617_128x128_display_info); |
| 314 | break; |
| 315 | case U8X8_MSG_DISPLAY_INIT: |
| 316 | u8x8_d_helper_display_init(u8x8); |
| 317 | u8x8_cad_SendSequence(u8x8, u8x8_d_uc1617_jlx128128_init_seq); |
| 318 | break; |
| 319 | default: |
| 320 | return 0; /* msg unknown */ |
| 321 | } |
| 322 | } |
| 323 | return 1; |
| 324 | } |