diff --git a/README.md b/README.md index 98ad975..d6ac766 100644 --- a/README.md +++ b/README.md @@ -125,9 +125,11 @@ or for the SH1106: #include #include "SH1106Spi.h" -SH1106Spi display(D0, D2); // RES, DC +SH1106Spi display(D0, D2, CS); // RES, DC, CS ``` +In case the CS pin is not used (hard wired to ground), pass CS as -1. + ## API ### Display Control diff --git a/library.json b/library.json index 18f59fd..0c09cab 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "ESP8266 and ESP32 OLED driver for SSD1306 displays", - "version": "4.3.0", + "version": "4.4.0", "keywords": "ssd1306, oled, display, i2c", "description": "I2C display driver for SSD1306 OLED displays connected to ESP8266, ESP32, Mbed-OS", "license": "MIT", diff --git a/library.properties b/library.properties index 42fb10a..5e289cc 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ESP8266 and ESP32 OLED driver for SSD1306 displays -version=4.3.0 +version=4.4.0 author=ThingPulse, Fabrice Weinberg maintainer=ThingPulse sentence=I2C display driver for SSD1306 OLED displays connected to ESP8266, ESP32, Mbed-OS diff --git a/src/SH1106Spi.h b/src/SH1106Spi.h index b31dd84..2862e9f 100644 --- a/src/SH1106Spi.h +++ b/src/SH1106Spi.h @@ -41,6 +41,7 @@ class SH1106Spi : public OLEDDisplay { uint8_t _cs; public: + /* pass _cs as -1 to indicate "do not use CS pin", for cases where it is hard wired low */ SH1106Spi(uint8_t _rst, uint8_t _dc, uint8_t _cs, OLEDDISPLAY_GEOMETRY g = GEOMETRY_128_64) { setGeometry(g); @@ -51,7 +52,9 @@ class SH1106Spi : public OLEDDisplay { bool connect(){ pinMode(_dc, OUTPUT); - pinMode(_cs, OUTPUT); + if (_cs != (uint8_t) -1) { + pinMode(_cs, OUTPUT); + } pinMode(_rst, OUTPUT); SPI.begin (); @@ -105,13 +108,13 @@ class SH1106Spi : public OLEDDisplay { sendCommand(0xB0 + y); sendCommand(minBoundXp2H); sendCommand(minBoundXp2L); - digitalWrite(_cs, HIGH); + set_CS(HIGH); digitalWrite(_dc, HIGH); // data mode - digitalWrite(_cs, LOW); + set_CS(LOW); for (x = minBoundX; x <= maxBoundX; x++) { SPI.transfer(buffer[x + y * displayWidth]); } - digitalWrite(_cs, HIGH); + set_CS(HIGH); yield(); } #else @@ -119,13 +122,13 @@ class SH1106Spi : public OLEDDisplay { sendCommand(0xB0 + y); sendCommand(0x02); sendCommand(0x10); - digitalWrite(_cs, HIGH); + set_CS(HIGH); digitalWrite(_dc, HIGH); // data mode - digitalWrite(_cs, LOW); + set_CS(LOW); for( uint8_t x=0; x < displayWidth; x++) { SPI.transfer(buffer[x + y * displayWidth]); } - digitalWrite(_cs, HIGH); + set_CS(HIGH); yield(); } #endif @@ -135,12 +138,17 @@ class SH1106Spi : public OLEDDisplay { int getBufferOffset(void) { return 0; } + inline void set_CS(bool level) { + if (_cs != (uint8_t) -1) { + digitalWrite(_cs, level); + } + }; inline void sendCommand(uint8_t com) __attribute__((always_inline)){ - digitalWrite(_cs, HIGH); + set_CS(HIGH); digitalWrite(_dc, LOW); - digitalWrite(_cs, LOW); + set_CS(LOW); SPI.transfer(com); - digitalWrite(_cs, HIGH); + set_CS(HIGH); } }; diff --git a/src/SSD1306Spi.h b/src/SSD1306Spi.h index 08cb4a8..b8d7b78 100644 --- a/src/SSD1306Spi.h +++ b/src/SSD1306Spi.h @@ -47,6 +47,7 @@ class SSD1306Spi : public OLEDDisplay { uint8_t _cs; public: + /* pass _cs as -1 to indicate "do not use CS pin", for cases where it is hard wired low */ SSD1306Spi(uint8_t _rst, uint8_t _dc, uint8_t _cs, OLEDDISPLAY_GEOMETRY g = GEOMETRY_128_64) { setGeometry(g); @@ -57,7 +58,9 @@ class SSD1306Spi : public OLEDDisplay { bool connect(){ pinMode(_dc, OUTPUT); - pinMode(_cs, OUTPUT); + if (_cs != (uint8_t) -1) { + pinMode(_cs, OUTPUT); + } pinMode(_rst, OUTPUT); SPI.begin (); @@ -111,16 +114,16 @@ class SSD1306Spi : public OLEDDisplay { sendCommand(minBoundY); sendCommand(maxBoundY); - digitalWrite(_cs, HIGH); + set_CS(HIGH); digitalWrite(_dc, HIGH); // data mode - digitalWrite(_cs, LOW); + set_CS(LOW); for (y = minBoundY; y <= maxBoundY; y++) { for (x = minBoundX; x <= maxBoundX; x++) { SPI.transfer(buffer[x + y * displayWidth]); } yield(); } - digitalWrite(_cs, HIGH); + set_CS(HIGH); #else // No double buffering sendCommand(COLUMNADDR); @@ -136,14 +139,14 @@ class SSD1306Spi : public OLEDDisplay { sendCommand(0x3); } - digitalWrite(_cs, HIGH); + set_CS(HIGH); digitalWrite(_dc, HIGH); // data mode - digitalWrite(_cs, LOW); + set_CS(LOW); for (uint16_t i=0; iwrite(buffer[x + y * this->width()]); k++; - if (k == 16) { + if (k == (I2C_MAX_TRANSFER_BYTE - 1)) { _wire->endTransmission(); k = 0; } @@ -170,7 +175,7 @@ class SSD1306Wire : public OLEDDisplay { for (uint16_t i=0; i < displayBufferSize; i++) { _wire->beginTransmission(this->_address); _wire->write(0x40); - for (uint8_t x = 0; x < 16; x++) { + for (uint8_t x = 0; x < (I2C_MAX_TRANSFER_BYTE - 1); x++) { _wire->write(buffer[i]); i++; }