From b7946a360e1695c9c1431a7e40e078a54a38023b Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Sun, 30 May 2021 18:19:14 +0200 Subject: [PATCH] Support for SH1107 over SPI, found in M5Stick --- lib/lib_display/UDisplay/uDisplay.cpp | 84 +++++++++++++++++--------- tasmota/displaydesc/SH1107_display.ini | 11 ++++ 2 files changed, 68 insertions(+), 27 deletions(-) create mode 100644 tasmota/displaydesc/SH1107_display.ini diff --git a/lib/lib_display/UDisplay/uDisplay.cpp b/lib/lib_display/UDisplay/uDisplay.cpp index 65644eb04..1b509d3de 100755 --- a/lib/lib_display/UDisplay/uDisplay.cpp +++ b/lib/lib_display/UDisplay/uDisplay.cpp @@ -225,7 +225,7 @@ uDisplay::uDisplay(char *lp) : Renderer(800, 600) { rot_t[3] = next_hex(&lp1); break; case 'A': - if (interface == _UDSP_I2C) { + if (interface == _UDSP_I2C || bpp == 1) { saw_1 = next_hex(&lp1); i2c_page_start = next_hex(&lp1); i2c_page_end = next_hex(&lp1); @@ -381,6 +381,19 @@ uDisplay::uDisplay(char *lp) : Renderer(800, 600) { Renderer *uDisplay::Init(void) { + // for any bpp below native 16 bits, we allocate a local framebuffer to copy into + if (ep_mode || bpp < 16) { + if (framebuffer) free(framebuffer); +#ifdef ESP8266 + framebuffer = (uint8_t*)calloc((gxs * gys * bpp) / 8, 1); +#else + if (psramFound()) { + framebuffer = (uint8_t*)heap_caps_malloc((gxs * gys * bpp) / 8, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); + } else { + framebuffer = (uint8_t*)calloc((gxs * gys * bpp) / 8, 1); + } + #endif + } if (interface == _UDSP_I2C) { @@ -392,19 +405,7 @@ Renderer *uDisplay::Init(void) { wire = &Wire1; } #endif - wire->begin(i2c_sda, i2c_scl); - if (bpp < 16) { - if (framebuffer) free(framebuffer); -#ifdef ESP8266 - framebuffer = (uint8_t*)calloc((gxs * gys * bpp) / 8, 1); -#else - if (psramFound()) { - framebuffer = (uint8_t*)heap_caps_malloc((gxs * gys * bpp) / 8, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); - } else { - framebuffer = (uint8_t*)calloc((gxs * gys * bpp) / 8, 1); - } -#endif - } + wire->begin(i2c_sda, i2c_scl); // TODO: aren't I2C buses already initialized? Shouldn't this be moved to display driver? #ifdef UDSP_DEBUG Serial.printf("I2C cmds: %d\n", dsp_ncmds); @@ -419,18 +420,6 @@ Renderer *uDisplay::Init(void) { } if (interface == _UDSP_SPI) { - if (ep_mode) { - #ifdef ESP8266 - framebuffer = (uint8_t*)calloc((gxs * gys * bpp) / 8, 1); - #else - if (psramFound()) { - framebuffer = (uint8_t*)heap_caps_malloc((gxs * gys * bpp) / 8, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); - } else { - framebuffer = (uint8_t*)calloc((gxs * gys * bpp) / 8, 1); - } - #endif - } - if (bpanel >= 0) { #ifdef ESP32 ledcSetup(ESP32_PWM_CHANNEL, 4000, 8); @@ -774,9 +763,49 @@ void uDisplay::Updateframe(void) { } } #endif + } + if (interface == _UDSP_SPI) { + if (framebuffer == nullptr) { return; } + + SPI_BEGIN_TRANSACTION + SPI_CS_LOW + + // below commands are not needed for SH1107 + // spi_command(saw_1 | 0x0); // set low col = 0, 0x00 + // spi_command(i2c_page_start | 0x0); // set hi col = 0, 0x10 + // spi_command(i2c_page_end | 0x0); // set startline line #0, 0x40 + + uint8_t ys = gys >> 3; + uint8_t xs = gxs >> 3; + //uint8_t xs = 132 >> 3; + uint8_t m_row = saw_2; + uint8_t m_col = i2c_col_start; + // Serial.printf("m_row=%d m_col=%d xs=%d ys=%d\n", m_row, m_col, xs, ys); + + uint16_t p = 0; + + uint8_t i, j, k = 0; + for ( i = 0; i < ys; i++) { // i = line from 0 to ys + // send a bunch of data in one xmission + spi_command(0xB0 + i + m_row); //set page address + spi_command(m_col & 0xf); //set lower column address + spi_command(0x10 | (m_col >> 4)); //set higher column address + + for ( j = 0; j < 8; j++) { + for ( k = 0; k < xs; k++, p++) { + spi_data8(framebuffer[p]); + } + } + } + + SPI_CS_HIGH + SPI_END_TRANSACTION + + } + } void uDisplay::drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color) { @@ -982,6 +1011,7 @@ void uDisplay::setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) seta_xp2 = x1; seta_yp1 = y0; seta_yp2 = y1; + // Serial.printf("xp1=%d xp2=%d yp1=%d yp2=%d\n", seta_xp1, seta_xp2, seta_yp1, seta_yp2); } return; } @@ -1206,7 +1236,7 @@ void uDisplay::drawPixel(int16_t x, int16_t y, uint16_t color) { return; } - if (interface != _UDSP_SPI) { + if (interface != _UDSP_SPI || bpp < 16) { Renderer::drawPixel(x, y, color); return; } diff --git a/tasmota/displaydesc/SH1107_display.ini b/tasmota/displaydesc/SH1107_display.ini new file mode 100644 index 000000000..4b644715c --- /dev/null +++ b/tasmota/displaydesc/SH1107_display.ini @@ -0,0 +1,11 @@ +:H,SH1107,64,128,1,SPI,1,*,*,*,*,*,*,*,16 +:S,0,2,1,0,30,20 +:I +AE,0 +81,0 +2F,0 +:o,AE +:O,AF +:A,00,10,40,00,20 +:i,A6,A7 +#