Skip to content
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

Add setBuffer method for static buffers and/or multiple displays sharing a single buffer #149

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
30 changes: 26 additions & 4 deletions Adafruit_SSD1306.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,7 @@ Adafruit_SSD1306::Adafruit_SSD1306(int8_t rst_pin) :
@brief Destructor for Adafruit_SSD1306 object.
*/
Adafruit_SSD1306::~Adafruit_SSD1306(void) {
if(buffer) {
free(buffer);
buffer = NULL;
}
deallocateBuffer();
}

// LOW-LEVEL UTILS ---------------------------------------------------------
Expand Down Expand Up @@ -581,6 +578,17 @@ boolean Adafruit_SSD1306::begin(uint8_t vcs, uint8_t addr, boolean reset,
return true; // Success
}

/*!
@brief If this display owns a display buffer, deallocate it.
@return None (void).
*/
void Adafruit_SSD1306::deallocateBuffer(void) {
if(buffer && bufferPolicy == Allocated) {
free(buffer);
buffer = NULL;
}
}

// DRAWING FUNCTIONS -------------------------------------------------------

/*!
Expand Down Expand Up @@ -874,6 +882,20 @@ uint8_t *Adafruit_SSD1306::getBuffer(void) {
return buffer;
}

/*!
@brief Set the display buffer to be used.
If a buffer was allocated before, it is freed.
@return None (void).
@param buffer
A pointer to the new display buffer. Must be at least
`⌈WIDTH * HEIGHT / 8⌉` bytes in size.
*/
void Adafruit_SSD1306::setBuffer(uint8_t *buffer) {
deallocateBuffer();
this->bufferPolicy = Borrowed;
this->buffer = buffer;
}

// REFRESH DISPLAY ---------------------------------------------------------

/*!
Expand Down
6 changes: 6 additions & 0 deletions Adafruit_SSD1306.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ class Adafruit_SSD1306 : public Adafruit_GFX {
void ssd1306_command(uint8_t c);
boolean getPixel(int16_t x, int16_t y);
uint8_t *getBuffer(void);
void setBuffer(uint8_t *buffer);

private:
inline void SPIwrite(uint8_t d) __attribute__((always_inline));
Expand All @@ -157,10 +158,12 @@ class Adafruit_SSD1306 : public Adafruit_GFX {
uint16_t color);
void ssd1306_command1(uint8_t c);
void ssd1306_commandList(const uint8_t *c, uint8_t n);
void deallocateBuffer(void);

SPIClass *spi;
TwoWire *wire;
uint8_t *buffer;
enum : bool { Borrowed, Allocated } bufferPolicy = Allocated;
int8_t i2caddr, vccstate, page_end;
int8_t mosiPin , clkPin , dcPin , csPin, rstPin;
#ifdef HAVE_PORTREG
Expand All @@ -176,4 +179,7 @@ class Adafruit_SSD1306 : public Adafruit_GFX {
#endif
};

/// Macro to determine whether this version supports `setBuffer`.
#define ADAFRUIT_SSD1306_HAS_SETBUFFER 1

#endif // _Adafruit_SSD1306_H_
Loading