Retrieving Array from EEPROM for OLED

Hello,

I am attempting to store multiple arrays of bitmap data, to display on an OLED, on an external EEPROM to save memory for my sketch. I'm not having any trouble getting the data on the EEPROM, the trouble starts when I try to populate an array in the program.

I'm using the Adafruit SSD1306 library, which uses PROGMEM for the bitmap array. The reason for that is a little advanced for my understanding, but I do know that using PROGMEM requires a const variable. Because of that, I'm unable to populate the array from the EEPROM with a for loop. At least that's what I'm thinking.

I'm hoping someone can advise me about retrieving bitmap data from the EEPROM and displaying it on an OLED.

Here is my current unsuccessful code. Excuse the messy formatting:

#include <EEPROM.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


#define LOGO_HEIGHT   64
#define LOGO_WIDTH    128

int eeAddress = 0;

const byte PROGMEM logo_bmp[1024];  // I can not populate this array from the EEPROM
 
void setup() {
  Serial.begin(9600);
  for (int i=0;i<1024;i++){
EEPROM.get(eeAddress, logo_bmp[i]);
eeAddress++;

}
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

  testdrawbitmap();
}

void loop() {
  // put your main code here, to run repeatedly:
}

void testdrawbitmap(void) {
  display.clearDisplay();
//delay(3000);
  display.drawBitmap(
    (display.width()  - LOGO_WIDTH ) / 2,
    (display.height() - LOGO_HEIGHT) / 2,
    logo_bmp, LOGO_WIDTH, LOGO_HEIGHT, 1);
  display.display();
  delay(1000);
}

If I populate the array beforehand and get rid of the for loop in setup, the image displays fine.

I would appreciate any suggestions.

Ok after some more research, I found a solution.

first I saw someone had added a couple of functions to the Adafruit GFX library which uses RAM instead of flash memory. Apparently, it worked for them but didn't for me.

What ended up working for me is a library called OLED_Display_SSD1306 by AbdulBasitKhatri. It uses RAM so I only get 32X32 pixels at a time. I'll have to position 8 little images in order to fill my 64X128 pixel display. Not ideal, but at least I can store a bunch on an external EEPROM now.

This is for use with the Arduino's internal flash memory. It is not relevant for data stored on external memory of any type.

If the bitmap is declared as const, then it is assumed to be in program memory. Declaring the bitmap array without const will assume it is stored in ram, but depending on the arduino you are using there may not be sufficient ram for both a bitmap array and the display buffer.

An alternative method is to write the data from the external EEPROM directly to the display buffer.

Yeah, that's why I was looking for an alternative method. I want to save as much memory as possible for the program.

I'm using the Pro Micro. The method I came up with is working out for this project. But I'll look into writing directly to the display buffer in the future. That sounds pretty good. Thanks for the info.

If anyone is having a similar problem and wants to try this method, here is a simple function to display the 32X32 pixel images on your screen in order. This is for a 128X64 pixel display.

void eePic(int addr){
  int v = 0;
  int h = 0;

  for (int k=0;k<8;k++){
for (int i=0;i<128;i++){
EEPROM.get(addr, bitmap[i]);
addr++;
}
if(k==4){
  v=32;
  h=0;
}
OLED_Display_SSD1306_drawBitmap(h, v, bitmap, 32, 32, 1);
OLED_Display_SSD1306_display(); 
h=h+32;
}
}

Just call it with eePic(0); replace the 0 with the starting eeprom address.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.