Skip to content

allow SPI display usage without CS pin #376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,11 @@ or for the SH1106:
#include <SPI.h>
#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
Expand Down
28 changes: 18 additions & 10 deletions src/SH1106Spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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 ();
Expand Down Expand Up @@ -105,27 +108,27 @@ 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
for (uint8_t y=0; y<displayHeight/8; y++) {
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
Expand All @@ -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);
}
};

Expand Down
28 changes: 18 additions & 10 deletions src/SSD1306Spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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 ();
Expand Down Expand Up @@ -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);
Expand All @@ -136,27 +139,32 @@ 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; i<displayBufferSize; i++) {
SPI.transfer(buffer[i]);
yield();
}
digitalWrite(_cs, HIGH);
set_CS(HIGH);
#endif
}

private:
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);
}
};

Expand Down