Skip to content

Commit 0a7ef5a

Browse files
Sweep of various things. ESP32 I2C clock, add getPixel(), etc.
1 parent e0ecb72 commit 0a7ef5a

File tree

4 files changed

+87
-44
lines changed

4 files changed

+87
-44
lines changed

Adafruit_SSD1306.cpp

Lines changed: 68 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@
4444
#define WIRE_WRITE wire->send
4545
#endif
4646

47-
#if defined(__AVR__) || defined(ESP32)
47+
#if defined(__AVR__)
4848
#define WIRECLK 400000L
49+
#elif defined(ESP32)
50+
#define WIRECLK 800000L
4951
#else
5052
#define WIRECLK 1000000L
5153
#endif
@@ -74,62 +76,68 @@
7476
#define SPI_TRANSACTION_END SSD1306_DESELECT
7577
#endif
7678

77-
#define TRANSACTION_START \
79+
#if ARDUINO >= 157
80+
#define TRANSACTION_START \
7881
if(wire) wire->setClock(WIRECLK); \
7982
else { SPI_TRANSACTION_START }
80-
81-
#define TRANSACTION_END \
83+
#define TRANSACTION_END \
8284
if(wire) wire->setClock(restoreClk); \
8385
else { SPI_TRANSACTION_END }
86+
#else
87+
#define TRANSACTION_START if(spi) { SPI_TRANSACTION_START }
88+
#define TRANSACTION_END if(spi) { SPI_TRANSACTION_END }
89+
#endif
8490

8591
// CONSTRUCTORS, DESTRUCTOR ------------------------------------------------
8692

8793
// New constructor for 'soft' SPI
8894
Adafruit_SSD1306::Adafruit_SSD1306(uint8_t w, uint8_t h,
89-
int8_t MOSI, int8_t SCLK, int8_t DC, int8_t RST, int8_t CS) :
90-
Adafruit_GFX(w, h), mosiPin(MOSI), clkPin(SCLK), dcPin(DC),
91-
rstPin(RST), csPin(CS), spi(NULL), wire(NULL), buffer(NULL) {
95+
int8_t mosi_pin, int8_t sclk_pin, int8_t dc_pin, int8_t rst_pin,
96+
int8_t cs_pin) : Adafruit_GFX(w, h), mosiPin(mosi_pin), clkPin(sclk_pin),
97+
dcPin(dc_pin), rstPin(rst_pin), csPin(cs_pin), spi(NULL), wire(NULL),
98+
buffer(NULL) {
9299
}
93100

94101
// New constructor for hardware SPI
95-
Adafruit_SSD1306::Adafruit_SSD1306(uint8_t w, uint8_t h,
96-
SPIClass *spi, int8_t DC, int8_t RST, int8_t CS, uint32_t bitrate) :
102+
Adafruit_SSD1306::Adafruit_SSD1306(uint8_t w, uint8_t h, SPIClass *spi,
103+
int8_t dc_pin, int8_t rst_pin, int8_t cs_pin, uint32_t bitrate) :
97104
Adafruit_GFX(w, h), spi(spi ? spi : &SPI), wire(NULL),
98-
mosiPin(-1), clkPin(-1), dcPin(DC), rstPin(RST), csPin(CS), buffer(NULL) {
105+
mosiPin(-1), clkPin(-1), dcPin(dc_pin), rstPin(rst_pin), csPin(cs_pin),
106+
buffer(NULL) {
99107
#ifdef SPI_HAS_TRANSACTION
100108
spiSettings = SPISettings(bitrate, MSBFIRST, SPI_MODE0);
101109
#endif
102110
}
103111

104112
// New constructor for I2C
105-
Adafruit_SSD1306::Adafruit_SSD1306(
106-
uint8_t w, uint8_t h, TwoWire *twi, int8_t RST, uint32_t res) :
107-
Adafruit_GFX(w, h), wire(twi ? twi : &Wire), restoreClk(res), spi(NULL),
108-
rstPin(RST), mosiPin(-1), clkPin(-1), dcPin(-1), csPin(-1), buffer(NULL) {
113+
Adafruit_SSD1306::Adafruit_SSD1306(uint8_t w, uint8_t h, TwoWire *twi,
114+
int8_t rst_pin, uint32_t res) : Adafruit_GFX(w, h),
115+
wire(twi ? twi : &Wire), restoreClk(res), spi(NULL), rstPin(rst_pin),
116+
mosiPin(-1), clkPin(-1), dcPin(-1), csPin(-1), buffer(NULL) {
109117
}
110118

111119
// Old constructor for 'soft' SPI (deprecated)
112-
Adafruit_SSD1306::Adafruit_SSD1306(
113-
int8_t MOSI, int8_t SCLK, int8_t DC, int8_t RST, int8_t CS) :
120+
Adafruit_SSD1306::Adafruit_SSD1306(int8_t mosi_pin, int8_t sclk_pin,
121+
int8_t dc_pin, int8_t rst_pin, int8_t cs_pin) :
114122
Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT),
115-
mosiPin(MOSI), clkPin(SCLK), dcPin(DC), rstPin(RST), csPin(CS),
116-
spi(NULL), wire(NULL), buffer(NULL) {
123+
mosiPin(mosi_pin), clkPin(sclk_pin), dcPin(dc_pin), rstPin(rst_pin),
124+
csPin(cs_pin), spi(NULL), wire(NULL), buffer(NULL) {
117125
}
118126

119127
// Old constructor for hardware SPI (deprecated)
120-
Adafruit_SSD1306::Adafruit_SSD1306(int8_t DC, int8_t RST, int8_t CS) :
121-
Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT),
122-
mosiPin(-1), clkPin(-1), dcPin(DC), rstPin(RST), csPin(CS),
128+
Adafruit_SSD1306::Adafruit_SSD1306(int8_t dc_pin, int8_t rst_pin,
129+
int8_t cs_pin) : Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT),
130+
mosiPin(-1), clkPin(-1), dcPin(dc_pin), rstPin(rst_pin), csPin(cs_pin),
123131
spi(&SPI), wire(NULL), buffer(NULL) {
124132
#ifdef SPI_HAS_TRANSACTION
125133
spiSettings = SPISettings(8000000, MSBFIRST, SPI_MODE0);
126134
#endif
127135
}
128136

129137
// Old constructor for I2C (deprecated)
130-
Adafruit_SSD1306::Adafruit_SSD1306(int8_t RST) :
138+
Adafruit_SSD1306::Adafruit_SSD1306(int8_t rst_pin) :
131139
Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT),
132-
rstPin(RST), mosiPin(-1), clkPin(-1), dcPin(-1), csPin(-1),
140+
rstPin(rst_pin), mosiPin(-1), clkPin(-1), dcPin(-1), csPin(-1),
133141
spi(NULL), wire(&Wire), buffer(NULL) {
134142
}
135143

@@ -203,7 +211,7 @@ void Adafruit_SSD1306::ssd1306_commandList(const uint8_t *c, uint8_t n) {
203211

204212
// ALLOCATE & INIT DISPLAY -------------------------------------------------
205213

206-
bool Adafruit_SSD1306::begin(uint8_t vcs, uint8_t addr, bool reset) {
214+
boolean Adafruit_SSD1306::begin(uint8_t vcs, uint8_t addr, boolean reset) {
207215

208216
if((!buffer) && !(buffer = (uint8_t *)malloc(WIDTH * ((HEIGHT + 7) / 8))))
209217
return false;
@@ -418,7 +426,7 @@ void Adafruit_SSD1306::drawFastHLineInternal(
418426

419427
void Adafruit_SSD1306::drawFastVLine(
420428
int16_t x, int16_t y, int16_t h, uint16_t color) {
421-
bool bSwap = false;
429+
boolean bSwap = false;
422430
switch(rotation) {
423431
case 1:
424432
// 90 degree rotation, swap x & y for rotation,
@@ -530,6 +538,32 @@ void Adafruit_SSD1306::drawFastVLineInternal(
530538
} // endif x in bounds
531539
}
532540

541+
boolean Adafruit_SSD1306::getPixel(int16_t x, int16_t y) {
542+
if((x >= 0) && (x < width()) && (y >= 0) && (y < height())) {
543+
// Pixel is in-bounds. Rotate coordinates if needed.
544+
switch(getRotation()) {
545+
case 1:
546+
ssd1306_swap(x, y);
547+
x = WIDTH - x - 1;
548+
break;
549+
case 2:
550+
x = WIDTH - x - 1;
551+
y = HEIGHT - y - 1;
552+
break;
553+
case 3:
554+
ssd1306_swap(x, y);
555+
y = HEIGHT - y - 1;
556+
break;
557+
}
558+
return (buffer[x + (y / 8) * WIDTH] & (1 << (y & 7)));
559+
}
560+
return false; // Pixel out of bounds
561+
}
562+
563+
uint8_t *Adafruit_SSD1306::getBuffer(void) {
564+
return buffer;
565+
}
566+
533567
// REFRESH DISPLAY ---------------------------------------------------------
534568

535569
// Push data currently in RAM to SSD1306 display
@@ -544,6 +578,9 @@ void Adafruit_SSD1306::display(void) {
544578
ssd1306_commandList(dlist1, sizeof(dlist1));
545579
ssd1306_command(WIDTH - 1); // Column end address
546580

581+
#if defined(ESP8266)
582+
yield();
583+
#endif
547584
uint16_t count = WIDTH * ((HEIGHT + 7) / 8);
548585
uint8_t *ptr = buffer;
549586
if(wire) { // I2C
@@ -573,7 +610,7 @@ void Adafruit_SSD1306::display(void) {
573610
// startscrollright
574611
// Activate a right handed scroll for rows start through stop
575612
// Hint, the display is 16 rows tall. To scroll the whole display, run:
576-
// display.scrollright(0x00, 0x0F)
613+
// display.startscrollright(0x00, 0x0F)
577614
void Adafruit_SSD1306::startscrollright(uint8_t start, uint8_t stop){
578615
TRANSACTION_START
579616
static const uint8_t PROGMEM scrollList1a[] = {
@@ -592,9 +629,9 @@ void Adafruit_SSD1306::startscrollright(uint8_t start, uint8_t stop){
592629
}
593630

594631
// startscrollleft
595-
// Activate a right handed scroll for rows start through stop
632+
// Activate a left handed scroll for rows start through stop
596633
// Hint, the display is 16 rows tall. To scroll the whole display, run:
597-
// display.scrollright(0x00, 0x0F)
634+
// display.startscrollleft(0x00, 0x0F)
598635
void Adafruit_SSD1306::startscrollleft(uint8_t start, uint8_t stop){
599636
TRANSACTION_START
600637
static const uint8_t PROGMEM scrollList2a[] = {
@@ -615,7 +652,7 @@ void Adafruit_SSD1306::startscrollleft(uint8_t start, uint8_t stop){
615652
// startscrolldiagright
616653
// Activate a diagonal scroll for rows start through stop
617654
// Hint, the display is 16 rows tall. To scroll the whole display, run:
618-
// display.scrollright(0x00, 0x0F)
655+
// display.startscrolldiagright(0x00, 0x0F)
619656
void Adafruit_SSD1306::startscrolldiagright(uint8_t start, uint8_t stop){
620657
TRANSACTION_START
621658
static const uint8_t PROGMEM scrollList3a[] = {
@@ -640,7 +677,7 @@ void Adafruit_SSD1306::startscrolldiagright(uint8_t start, uint8_t stop){
640677
// startscrolldiagleft
641678
// Activate a diagonal scroll for rows start through stop
642679
// Hint, the display is 16 rows tall. To scroll the whole display, run:
643-
// display.scrollright(0x00, 0x0F)
680+
// display.startscrolldiagleft(0x00, 0x0F)
644681
void Adafruit_SSD1306::startscrolldiagleft(uint8_t start, uint8_t stop){
645682
TRANSACTION_START
646683
static const uint8_t PROGMEM scrollList4a[] = {
@@ -670,7 +707,7 @@ void Adafruit_SSD1306::stopscroll(void){
670707

671708
// OTHER HARDWARE SETTINGS -------------------------------------------------
672709

673-
void Adafruit_SSD1306::invertDisplay(bool i) {
710+
void Adafruit_SSD1306::invertDisplay(boolean i) {
674711
TRANSACTION_START
675712
ssd1306_command(i ? SSD1306_INVERTDISPLAY : SSD1306_NORMALDISPLAY);
676713
TRANSACTION_END

Adafruit_SSD1306.h

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,25 +104,26 @@
104104
class Adafruit_SSD1306 : public Adafruit_GFX {
105105
public:
106106
// NEW CONSTRUCTORS -- recommended for new projects
107-
Adafruit_SSD1306(uint8_t w, uint8_t h,
108-
int8_t MOSI, int8_t SCLK, int8_t DC, int8_t RST, int8_t CS);
107+
Adafruit_SSD1306(uint8_t w, uint8_t h, int8_t mosi_pin, int8_t sclk_pin,
108+
int8_t dc_pin, int8_t rst_pin, int8_t cs_pin);
109109
Adafruit_SSD1306(uint8_t w, uint8_t h, SPIClass *spi,
110-
int8_t DC, int8_t RST, int8_t CS, uint32_t bitrate=8000000UL);
111-
Adafruit_SSD1306(uint8_t w, uint8_t h, TwoWire *twi=&Wire, int8_t RST=-1,
112-
uint32_t res=100000L);
110+
int8_t dc_pin, int8_t rst_pin, int8_t cs_pin, uint32_t bitrate=8000000UL);
111+
Adafruit_SSD1306(uint8_t w, uint8_t h, TwoWire *twi=&Wire,
112+
int8_t rst_pin=-1, uint32_t res=100000L);
113113

114114
// DEPRECATED CONSTRUCTORS - for back compatibility, avoid in new projects
115-
Adafruit_SSD1306(int8_t MOSI, int8_t SCLK, int8_t DC, int8_t RST, int8_t CS);
116-
Adafruit_SSD1306(int8_t DC, int8_t RST, int8_t CS);
117-
Adafruit_SSD1306(int8_t RST = -1);
115+
Adafruit_SSD1306(int8_t mosi_pin, int8_t sclk_pin, int8_t dc_pin,
116+
int8_t rst_pin, int8_t cs_pin);
117+
Adafruit_SSD1306(int8_t dc_pin, int8_t rst_pin, int8_t cs_pin);
118+
Adafruit_SSD1306(int8_t rst_pin = -1);
118119

119120
~Adafruit_SSD1306(void);
120121

121-
bool begin(uint8_t switchvcc=SSD1306_SWITCHCAPVCC,
122-
uint8_t i2caddr=0, bool reset=true);
122+
boolean begin(uint8_t switchvcc=SSD1306_SWITCHCAPVCC,
123+
uint8_t i2caddr=0, boolean reset=true);
123124
void display(void);
124125
void clearDisplay(void);
125-
void invertDisplay(bool i);
126+
void invertDisplay(boolean i);
126127
void dim(boolean dim);
127128
void drawPixel(int16_t x, int16_t y, uint16_t color);
128129
virtual void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
@@ -134,6 +135,9 @@ class Adafruit_SSD1306 : public Adafruit_GFX {
134135
void stopscroll(void);
135136
void ssd1306_command(uint8_t c);
136137
void ssd1306_commandList(const uint8_t *c, uint8_t n);
138+
boolean getPixel(int16_t x, int16_t y);
139+
uint8_t *getBuffer(void);
140+
137141

138142
private:
139143
inline void SPIwrite(uint8_t d) __attribute__((always_inline));
@@ -154,7 +158,9 @@ class Adafruit_SSD1306 : public Adafruit_GFX {
154158
#ifdef SPI_HAS_TRANSACTION
155159
SPISettings spiSettings;
156160
#endif
161+
#if ARDUINO >= 157
157162
uint32_t restoreClk; // Wire speed following SSD1306 transfers
163+
#endif
158164
};
159165

160166
#endif // _Adafruit_SSD1306_H_

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ ESP8266 | X | | | change OLED_RESET to
3434
ESP32 | X | | |
3535
ATSAM3X8E | X | | |
3636
ATSAM21D | X | | |
37+
Intel Curie @ 32MHz| X | | |
3738
ATtiny85 @ 16MHz | | X | |
3839
ATtiny85 @ 8MHz | | X | |
3940
WICED | | X | |
40-
Intel Curie @ 32MHz| | | X |
4141
STM32F2 | | | X |
4242

4343
* ATmega328 @ 16MHz : Arduino UNO, Adafruit Pro Trinket 5V, Adafruit Metro 328, Adafruit Metro Mini

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Adafruit SSD1306
2-
version=1.2.3
2+
version=1.2.4
33
author=Adafruit
44
maintainer=Adafruit <[email protected]>
55
sentence=SSD1306 oled driver library for monochrome 128x64 and 128x32 displays

0 commit comments

Comments
 (0)