Skip to content

Commit 0de2f56

Browse files
committed
-Added typedef dfunc_t for user display function
-Added void display_d(dfunc_t dfunc) which is a variant of the display() that calls a user function after each row -changed private data to protected to more easily derive and modify.
1 parent 35ad544 commit 0de2f56

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

Adafruit_SSD1306.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656

5757
// SOME DEFINES AND STATIC VARIABLES USED INTERNALLY -----------------------
5858

59+
typedef void (* dfunc_t)(void);//void function pointer
60+
5961
#if defined(I2C_BUFFER_LENGTH)
6062
#define WIRE_MAX min(256, I2C_BUFFER_LENGTH) ///< Particle or similar Wire lib
6163
#elif defined(BUFFER_LENGTH)
@@ -113,6 +115,7 @@
113115
// so other I2C device types still work). All of these are encapsulated
114116
// in the TRANSACTION_* macros.
115117

118+
116119
// Check first if Wire, then hardware SPI, then soft SPI:
117120
#define TRANSACTION_START \
118121
if (wire) { \
@@ -970,6 +973,71 @@ void Adafruit_SSD1306::display(void) {
970973
#endif
971974
}
972975

976+
/*!
977+
@brief display with added call to user function pointer
978+
@param dfunc pointer to users display function
979+
980+
@return None (void).
981+
*/
982+
void Adafruit_SSD1306::display_d(dfunc_t dfunc){
983+
TRANSACTION_START
984+
static const uint8_t PROGMEM dlist1[] = {
985+
SSD1306_PAGEADDR,
986+
0, // Page start address
987+
0xFF, // Page end (not really, but works here)
988+
SSD1306_COLUMNADDR, 0}; // Column start address
989+
ssd1306_commandList(dlist1, sizeof(dlist1));
990+
ssd1306_command1(WIDTH - 1); // Column end address
991+
992+
#if defined(ESP8266)
993+
// ESP8266 needs a periodic yield() call to avoid watchdog reset.
994+
// With the limited size of SSD1306 displays, and the fast bitrate
995+
// being used (1 MHz or more), I think one yield() immediately before
996+
// a screen write and one immediately after should cover it. But if
997+
// not, if this becomes a problem, yields() might be added in the
998+
// 32-byte transfer condition below.
999+
yield();
1000+
#endif
1001+
uint16_t count = WIDTH * ((HEIGHT + 7) / 8);
1002+
uint8_t *ptr = buffer;
1003+
if (wire) { // I2C
1004+
wire->beginTransmission(i2caddr);
1005+
WIRE_WRITE((uint8_t)0x40);
1006+
uint16_t bytesOut = 1;
1007+
while (count--) {
1008+
if (bytesOut >= WIRE_MAX) {
1009+
wire->endTransmission();
1010+
wire->beginTransmission(i2caddr);
1011+
WIRE_WRITE((uint8_t)0x40);
1012+
bytesOut = 1;
1013+
}
1014+
WIRE_WRITE(*ptr++);
1015+
bytesOut++;
1016+
//every line give the dfunc a chance to run
1017+
if(dfunc !=NULL){
1018+
if(!(count % WIDTH)){
1019+
wire->endTransmission();
1020+
TRANSACTION_END
1021+
dfunc();
1022+
TRANSACTION_START
1023+
wire->beginTransmission(i2caddr);
1024+
WIRE_WRITE((uint8_t)0x40);
1025+
bytesOut = 1;
1026+
}
1027+
}
1028+
}
1029+
wire->endTransmission();
1030+
} else { // SPI
1031+
SSD1306_MODE_DATA
1032+
while (count--)
1033+
SPIwrite(*ptr++);
1034+
}
1035+
TRANSACTION_END
1036+
#if defined(ESP8266)
1037+
yield();
1038+
#endif
1039+
}
1040+
9731041
// SCROLLING FUNCTIONS -----------------------------------------------------
9741042

9751043
/*!

Adafruit_SSD1306.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ typedef class HardwareSPI SPIClass;
4040
#include <SPI.h>
4141
#include <Wire.h>
4242

43+
typedef void (* dfunc_t)(void);
44+
4345
#if defined(__AVR__)
4446
typedef volatile uint8_t PortReg;
4547
typedef uint8_t PortMask;
@@ -145,6 +147,7 @@ class Adafruit_SSD1306 : public Adafruit_GFX {
145147
bool begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC, uint8_t i2caddr = 0,
146148
bool reset = true, bool periphBegin = true);
147149
void display(void);
150+
void display_d(dfunc_t dfunc);
148151
void clearDisplay(void);
149152
void invertDisplay(bool i);
150153
void dim(bool dim);
@@ -160,7 +163,7 @@ class Adafruit_SSD1306 : public Adafruit_GFX {
160163
bool getPixel(int16_t x, int16_t y);
161164
uint8_t *getBuffer(void);
162165

163-
private:
166+
protected:
164167
inline void SPIwrite(uint8_t d) __attribute__((always_inline));
165168
void drawFastHLineInternal(int16_t x, int16_t y, int16_t w, uint16_t color);
166169
void drawFastVLineInternal(int16_t x, int16_t y, int16_t h, uint16_t color);

0 commit comments

Comments
 (0)