|
13 | 13 | Bodmer: Added RPi 16 bit display support |
14 | 14 | ****************************************************/ |
15 | 15 |
|
| 16 | + |
16 | 17 | #include "TFT_eSPI.h" |
17 | 18 |
|
18 | 19 | #if defined (ESP32) |
@@ -85,7 +86,7 @@ inline void TFT_eSPI::spi_end_read(void){ |
85 | 86 | #if !defined(ESP32_PARALLEL) |
86 | 87 | spi.setFrequency(SPI_FREQUENCY); |
87 | 88 | #endif |
88 | | - CS_H; |
| 89 | + if(!inTransaction) CS_H; |
89 | 90 | #endif |
90 | 91 | #ifdef ESP8266 |
91 | 92 | SPI1U = SPI1U_WRITE; |
@@ -342,17 +343,6 @@ void TFT_eSPI::init(uint8_t tc) |
342 | 343 | writecommand(TFT_SWRST); // Software reset |
343 | 344 | #endif |
344 | 345 |
|
345 | | -#if defined (TFT_BL) && defined (TFT_BACKLIGHT_ON) |
346 | | - digitalWrite(TFT_BL, TFT_BACKLIGHT_ON); |
347 | | - pinMode(TFT_BL, OUTPUT); |
348 | | -#else |
349 | | - #if defined (TFT_BL) && defined (M5STACK) |
350 | | - // Turn on the back-light LED |
351 | | - digitalWrite(TFT_BL, HIGH); |
352 | | - pinMode(TFT_BL, OUTPUT); |
353 | | - #endif |
354 | | -#endif |
355 | | - |
356 | 346 | spi_end(); |
357 | 347 |
|
358 | 348 | delay(150); // Wait for reset to complete |
@@ -407,6 +397,17 @@ void TFT_eSPI::init(uint8_t tc) |
407 | 397 | spi_end(); |
408 | 398 |
|
409 | 399 | setRotation(rotation); |
| 400 | + |
| 401 | +#if defined (TFT_BL) && defined (TFT_BACKLIGHT_ON) |
| 402 | + digitalWrite(TFT_BL, TFT_BACKLIGHT_ON); |
| 403 | + pinMode(TFT_BL, OUTPUT); |
| 404 | +#else |
| 405 | + #if defined (TFT_BL) && defined (M5STACK) |
| 406 | + // Turn on the back-light LED |
| 407 | + digitalWrite(TFT_BL, HIGH); |
| 408 | + pinMode(TFT_BL, OUTPUT); |
| 409 | + #endif |
| 410 | +#endif |
410 | 411 | } |
411 | 412 |
|
412 | 413 |
|
@@ -2324,7 +2325,7 @@ int16_t TFT_eSPI::textWidth(const char *string, uint8_t font) |
2324 | 2325 | { |
2325 | 2326 | while (*string) |
2326 | 2327 | { |
2327 | | - uniCode = *(string++); |
| 2328 | + uniCode = decodeUTF8(*string++); |
2328 | 2329 | if ((uniCode >= (uint8_t)pgm_read_byte(&gfxFont->first)) && (uniCode <= (uint8_t)pgm_read_byte(&gfxFont->last ))) |
2329 | 2330 | { |
2330 | 2331 | uniCode -= pgm_read_byte(&gfxFont->first); |
@@ -3895,6 +3896,95 @@ void TFT_eSPI::invertDisplay(boolean i) |
3895 | 3896 | } |
3896 | 3897 |
|
3897 | 3898 |
|
| 3899 | +/*************************************************************************************** |
| 3900 | +** Function name: decodeUTF8 |
| 3901 | +** Description: Serial UTF-8 decoder with fall-back to extended ASCII |
| 3902 | +*************************************************************************************x*/ |
| 3903 | +#define DECODE_UTF8 // Test only, comment out to stop decoding |
| 3904 | +uint16_t TFT_eSPI::decodeUTF8(uint8_t c) |
| 3905 | +{ |
| 3906 | +#ifdef DECODE_UTF8 |
| 3907 | + // 7 bit Unicode Code Point |
| 3908 | + if ((c & 0x80) == 0x00) { |
| 3909 | + decoderState = 0; |
| 3910 | + return (uint16_t)c; |
| 3911 | + } |
| 3912 | + |
| 3913 | + if (decoderState == 0) |
| 3914 | + { |
| 3915 | + // 11 bit Unicode Code Point |
| 3916 | + if ((c & 0xE0) == 0xC0) |
| 3917 | + { |
| 3918 | + decoderBuffer = ((c & 0x1F)<<6); |
| 3919 | + decoderState = 1; |
| 3920 | + return 0; |
| 3921 | + } |
| 3922 | + |
| 3923 | + // 16 bit Unicode Code Point |
| 3924 | + if ((c & 0xF0) == 0xE0) |
| 3925 | + { |
| 3926 | + decoderBuffer = ((c & 0x0F)<<12); |
| 3927 | + decoderState = 2; |
| 3928 | + return 0; |
| 3929 | + } |
| 3930 | + // 21 bit Unicode Code Point not supported so fall-back to extended ASCII |
| 3931 | + if ((c & 0xF8) == 0xF0) return (uint16_t)c; |
| 3932 | + } |
| 3933 | + else |
| 3934 | + { |
| 3935 | + if (decoderState == 2) |
| 3936 | + { |
| 3937 | + decoderBuffer |= ((c & 0x3F)<<6); |
| 3938 | + decoderState--; |
| 3939 | + return 0; |
| 3940 | + } |
| 3941 | + else |
| 3942 | + { |
| 3943 | + decoderBuffer |= (c & 0x3F); |
| 3944 | + decoderState = 0; |
| 3945 | + return decoderBuffer; |
| 3946 | + } |
| 3947 | + } |
| 3948 | + |
| 3949 | + decoderState = 0; |
| 3950 | +#endif |
| 3951 | + |
| 3952 | + return (uint16_t)c; // fall-back to extended ASCII |
| 3953 | +} |
| 3954 | + |
| 3955 | + |
| 3956 | +/*************************************************************************************** |
| 3957 | +** Function name: decodeUTF8 |
| 3958 | +** Description: Line buffer UTF-8 decoder with fall-back to extended ASCII |
| 3959 | +*************************************************************************************x*/ |
| 3960 | +uint16_t TFT_eSPI::decodeUTF8(uint8_t *buf, uint16_t *index, uint16_t remaining) |
| 3961 | +{ |
| 3962 | + byte c = buf[(*index)++]; |
| 3963 | + //Serial.print("Byte from string = 0x"); Serial.println(c, HEX); |
| 3964 | + |
| 3965 | +#ifdef DECODE_UTF8 |
| 3966 | + // 7 bit Unicode |
| 3967 | + if ((c & 0x80) == 0x00) return c; |
| 3968 | + |
| 3969 | + // 11 bit Unicode |
| 3970 | + if (((c & 0xE0) == 0xC0) && (remaining > 1)) |
| 3971 | + return ((c & 0x1F)<<6) | (buf[(*index)++]&0x3F); |
| 3972 | + |
| 3973 | + // 16 bit Unicode |
| 3974 | + if (((c & 0xF0) == 0xE0) && (remaining > 2)) |
| 3975 | + { |
| 3976 | + c = ((c & 0x0F)<<12) | ((buf[(*index)++]&0x3F)<<6); |
| 3977 | + return c | ((buf[(*index)++]&0x3F)); |
| 3978 | + } |
| 3979 | + |
| 3980 | + // 21 bit Unicode not supported so fall-back to extended ASCII |
| 3981 | + // if ((c & 0xF8) == 0xF0) return c; |
| 3982 | +#endif |
| 3983 | + |
| 3984 | + return c; // fall-back to extended ASCII |
| 3985 | +} |
| 3986 | + |
| 3987 | + |
3898 | 3988 | /*************************************************************************************** |
3899 | 3989 | ** Function name: write |
3900 | 3990 | ** Description: draw characters piped through serial stream |
@@ -4008,6 +4098,8 @@ size_t TFT_eSPI::write(uint8_t utf8) |
4008 | 4098 | } // Custom GFX font |
4009 | 4099 | else |
4010 | 4100 | { |
| 4101 | + uniCode = (uint8_t)decodeUTF8(utf8); |
| 4102 | + if (!uniCode) return 1; |
4011 | 4103 |
|
4012 | 4104 | if(utf8 == '\n') { |
4013 | 4105 | cursor_x = 0; |
@@ -4046,14 +4138,17 @@ size_t TFT_eSPI::write(uint8_t utf8) |
4046 | 4138 | ** Function name: drawChar |
4047 | 4139 | ** Description: draw a Unicode onto the screen |
4048 | 4140 | ***************************************************************************************/ |
4049 | | -int16_t TFT_eSPI::drawChar(uint16_t uniCode, int32_t x, int32_t y) |
| 4141 | +int16_t TFT_eSPI::drawChar(uint16_t utf8, int32_t x, int32_t y) |
4050 | 4142 | { |
4051 | | - return drawChar(uniCode, x, y, textfont); |
| 4143 | + return drawChar(utf8, x, y, textfont); |
4052 | 4144 | } |
4053 | 4145 |
|
4054 | | -int16_t TFT_eSPI::drawChar(uint16_t uniCode, int32_t x, int32_t y, uint8_t font) |
| 4146 | +int16_t TFT_eSPI::drawChar(uint16_t utf8, int32_t x, int32_t y, uint8_t font) |
4055 | 4147 | { |
4056 | 4148 |
|
| 4149 | + uint16_t uniCode = decodeUTF8(utf8); |
| 4150 | + if (!uniCode) return 0; |
| 4151 | + |
4057 | 4152 | if (font==1) |
4058 | 4153 | { |
4059 | 4154 | #ifdef LOAD_GLCD |
@@ -4458,7 +4553,12 @@ int16_t TFT_eSPI::drawString(const char *string, int32_t poX, int32_t poY, uint8 |
4458 | 4553 | { |
4459 | 4554 | cheight = (glyph_ab + glyph_bb) * textsize; |
4460 | 4555 | // Get the offset for the first character only to allow for negative offsets |
4461 | | - uint8_t c2 = *string; |
| 4556 | + uint8_t c2 = 0; |
| 4557 | + uint16_t len = strlen(string); |
| 4558 | + uint16_t n = 0; |
| 4559 | + |
| 4560 | + while (n < len && c2 == 0) c2 = decodeUTF8((uint8_t*)string, &n, len - n); |
| 4561 | + |
4462 | 4562 | if((c2 >= pgm_read_byte(&gfxFont->first)) && (c2 <= pgm_read_byte(&gfxFont->last) )) |
4463 | 4563 | { |
4464 | 4564 | c2 -= pgm_read_byte(&gfxFont->first); |
@@ -4498,8 +4598,12 @@ int16_t TFT_eSPI::drawString(const char *string, int32_t poX, int32_t poY, uint8 |
4498 | 4598 | } |
4499 | 4599 | else |
4500 | 4600 | #endif |
4501 | | - while (*string) sumX += drawChar(*(string++), poX+sumX, poY, font); |
4502 | | - |
| 4601 | +Serial.print("sumX="); |
| 4602 | + while (*string) { |
| 4603 | + sumX += drawChar(*(string++), poX+sumX, poY, font); |
| 4604 | + Serial.print(sumX); |
| 4605 | + } |
| 4606 | +Serial.println(); |
4503 | 4607 | //vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv DEBUG vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv |
4504 | 4608 | // Switch on debugging for the padding areas |
4505 | 4609 | //#define PADDING_DEBUG |
|
0 commit comments