I'd like to add a method that replaces the entire image buffer into the Adafruit_SSD1306 library. I haven't worked with dynamic memory handling for ages and I'm not sure how to just simply overwrite its pointer to point to a different memory address where my newBuffer is.

static uint8_t buffer[SSD1306_LCDHEIGHT * SSD1306_LCDWIDTH / 8] = {}

void Adafruit_SSD1306::setBuffer(uint8_t * newBuffer) {
  buffer = newBuffer; // not sure which operator to use

Update: Passing by reference wouldn't work. The question is: how to overwrite by copying? Would it work well with memcpy?

void Adafruit_SSD1306::setBuffer(uint8_t * newBuffer) {
  memcpy(newBuffer, buffer, (SSD1306_LCDWIDTH * SSD1306_LCDHEIGHT / 8));

Update: The code above doesn't seem to making anything. This one neither:

void Adafruit_SSD1306::setBuffer(uint8_t * newBuffer) {
  for(int16_t i = 0; i < SSD1306_LCDWIDTH * SSD1306_LCDHEIGHT / 8; i++) {
    buffer[i] = 255; // experimenting

Update: setPixel doesn't work either. - Needed display.display(); :)

  • Do you want to copy the contents of newBuffer in buffer or rewrite buffer to be uint8_t* which you can change? – Maximilian Gerhardt Apr 1 '18 at 17:12
  • (I'm feeding the newBuffer from network and) I wan't a solution that replaces the image buffer the fastest. I don't mind making some changes in the library as long as it's easy. :) – haxpanel Apr 1 '18 at 17:14
  • Does copying have big overhead compared to passing the value by reference? – haxpanel Apr 1 '18 at 17:17
  • I've just realised that passing by reference might not work as the buffer is in a specific location in the memory, can't be elsewhere. – haxpanel Apr 1 '18 at 17:35
  • 1
    Even when done properly, changing the code to point to a new buffer location won't free the old buffer. Arduinos tend to have limited RAM, so you probably want to copy the contents into the existing static allocation, not abandon a big chunk of memory. – Chris Stratton Apr 1 '18 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.