Can this be done more simpler or not

I want to move a bitmap accress a 128x64 OLED.
Came up this solution but it takes almost half the programspace.

/*********
  Rui Santos
  Complete project details at https://randomnerdtutorials.com  
*********/

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

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

const unsigned char NaN[] PROGMEM = {
  // 'SRD-MC, 64x64px
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x11, 0xe0, 0x03, 0xbe, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xe0,
  0x03, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x3d, 0x80, 0x03, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00,
  0x0d, 0xf0, 0x00, 0x00, 0x00, 0x00, 0xcf, 0x00, 0x3e, 0xf0, 0x00, 0x00, 0x00, 0x03, 0xef, 0x00,
  0x7c, 0x7e, 0x00, 0x00, 0x00, 0x07, 0xc7, 0xc0, 0x38, 0x38, 0x00, 0x00, 0x00, 0x03, 0x01, 0x80,
  0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x07, 0x80, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x07, 0x03, 0x83, 0xf0, 0x3e, 0x00, 0x00, 0x00, 0x0b, 0x8f, 0xe3, 0xfc, 0x7f, 0xc0, 0x00,
  0x00, 0x03, 0x9f, 0xf3, 0xfe, 0x7f, 0xe0, 0x00, 0x00, 0x01, 0xde, 0xf3, 0xff, 0x7f, 0xf0, 0x00,
  0x00, 0x00, 0xec, 0x03, 0x8f, 0x70, 0xf0, 0x00, 0x00, 0x00, 0x76, 0x03, 0x87, 0x70, 0x70, 0x00,
  0x00, 0x00, 0x73, 0xe3, 0x8f, 0x70, 0x70, 0x00, 0x00, 0x00, 0x39, 0xf3, 0xfe, 0x70, 0x70, 0x00,
  0x00, 0x00, 0x5c, 0xfb, 0xfc, 0x70, 0x70, 0x00, 0x00, 0x00, 0x2f, 0x3b, 0xfc, 0x70, 0xf0, 0x00,
  0x00, 0x00, 0x37, 0xbb, 0x9e, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x19, 0xd3, 0x8f, 0x7f, 0xe0, 0x00,
  0x00, 0x00, 0x0c, 0xe3, 0x8f, 0x7f, 0xc0, 0x00, 0x00, 0x00, 0x07, 0x79, 0x07, 0x3f, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x80, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x03, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x08, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x3f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xe0, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0,
  0x03, 0xde, 0x00, 0x00, 0x00, 0x00, 0x33, 0xe0, 0x03, 0xec, 0x00, 0x00, 0x00, 0x00, 0x39, 0xc0,
  0x01, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x0e, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x5e, 0x00,
  0x3f, 0x78, 0x00, 0x00, 0x00, 0x01, 0xef, 0x00, 0x3e, 0x3e, 0x00, 0x00, 0x00, 0x07, 0xe7, 0x00,
  0x18, 0x18, 0x00, 0x00, 0x00, 0x03, 0x83, 0xc0, 0x00, 0x18, 0x00, 0x00, 0x00, 0x03, 0x01, 0x00,
  0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

void setup() {
  Serial.begin(115200);

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;)
      ;
  }
  delay(2000);  // Pause for 2 seconds

  // Clear the buffer.
  display.clearDisplay();

  // Draw bitmap on the screen
  // display.drawBitmap(0, 0, image_data_Saraarray, 128, 64, 1);
  display.drawBitmap(0, 0, NaN, 64, 64, 1);
  display.display();
}

void loop() {
  for (int i = 0; i <= 127; i++) {
    display.clearDisplay();
    display.drawBitmap(i, 0, NaN, 64, 64, 1);
    display.display();
  }
}

Is there a way to create smaller code??

Here is a link to the docs, seems you have the option to scroll left, right or diagonal. Not only will this be a lot faster but also save on resources.

https://adafruit.github.io/Adafruit_SSD1306/html/class_adafruit___s_s_d1306.html

1 Like

Your image included a lot of empty space and four redundant pieces.
image

Try using these two images.

1

2

// '1', 13x10px
const unsigned char epd_bitmap_1 [] PROGMEM = {
	0x00, 0x38, 0x0f, 0x78, 0x0f, 0xb0, 0x07, 0xc0, 0x3b, 0xc0, 0xfd, 0xe0, 0xf8, 0xf8, 0x60, 0x60, 
	0x00, 0x60, 0x00, 0x40
};
// '2', 41x28px
const unsigned char epd_bitmap_2 [] PROGMEM = {
	0x07, 0x80, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x02, 0x00, 0x00, 
	0x00, 0x00, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x38, 0x1c, 0x1f, 0x81, 0xf0, 0x00, 0x5c, 0x7f, 0x1f, 0xe3, 0xfe, 0x00, 0x1c, 0xff, 0x9f, 0xf3, 
	0xff, 0x00, 0x0e, 0xf7, 0x9f, 0xfb, 0xff, 0x80, 0x07, 0x60, 0x1c, 0x7b, 0x87, 0x80, 0x03, 0xb0, 
	0x1c, 0x3b, 0x83, 0x80, 0x03, 0x9f, 0x1c, 0x7b, 0x83, 0x80, 0x01, 0xcf, 0x9f, 0xf3, 0x83, 0x80, 
	0x02, 0xe7, 0xdf, 0xe3, 0x83, 0x80, 0x01, 0x79, 0xdf, 0xe3, 0x87, 0x80, 0x01, 0xbd, 0xdc, 0xf3, 
	0xff, 0x80, 0x00, 0xce, 0x9c, 0x7b, 0xff, 0x00, 0x00, 0x67, 0x1c, 0x7b, 0xfe, 0x00, 0x00, 0x3b, 
	0xc8, 0x39, 0xf8, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x40, 0x00, 0x00, 0x00, 0x01, 0xff, 
	0xc0, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00
};

// Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 240)
const int epd_bitmap_allArray_LEN = 2;
const unsigned char* epd_bitmap_allArray[2] = {
	epd_bitmap_1,
	epd_bitmap_2
};

Place the big one "in the middle" and place the small one at four places around the edge... move all the same way the original is moved.

  Rui Santos
  Complete project details at https://randomnerdtutorials.com
*********/

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

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// '1', 13x10px
const unsigned char small [] PROGMEM = {
  0x00, 0x38, 0x0f, 0x78, 0x0f, 0xb0, 0x07, 0xc0, 0x3b, 0xc0, 0xfd, 0xe0, 0xf8, 0xf8, 0x60, 0x60,
  0x00, 0x60, 0x00, 0x40
};
// '2', 41x28px
const unsigned char big [] PROGMEM = {
  0x07, 0x80, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x02, 0x00, 0x00,
  0x00, 0x00, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x00,
  0x00, 0x00, 0x00, 0x00, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x38, 0x1c, 0x1f, 0x81, 0xf0, 0x00, 0x5c, 0x7f, 0x1f, 0xe3, 0xfe, 0x00, 0x1c, 0xff, 0x9f, 0xf3,
  0xff, 0x00, 0x0e, 0xf7, 0x9f, 0xfb, 0xff, 0x80, 0x07, 0x60, 0x1c, 0x7b, 0x87, 0x80, 0x03, 0xb0,
  0x1c, 0x3b, 0x83, 0x80, 0x03, 0x9f, 0x1c, 0x7b, 0x83, 0x80, 0x01, 0xcf, 0x9f, 0xf3, 0x83, 0x80,
  0x02, 0xe7, 0xdf, 0xe3, 0x83, 0x80, 0x01, 0x79, 0xdf, 0xe3, 0x87, 0x80, 0x01, 0xbd, 0xdc, 0xf3,
  0xff, 0x80, 0x00, 0xce, 0x9c, 0x7b, 0xff, 0x00, 0x00, 0x67, 0x1c, 0x7b, 0xfe, 0x00, 0x00, 0x3b,
  0xc8, 0x39, 0xf8, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x40, 0x00, 0x00, 0x00, 0x01, 0xff,
  0xc0, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00
};

void setup() {
  Serial.begin(115200);

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;)
      ;
  }

  // Clear the buffer.
  display.clearDisplay();

  // Draw bitmap on the screen
  display.drawBitmap(30, 10, small, 13, 10, 1);
  display.drawBitmap(80, 10, small, 13, 10, 1);
  display.drawBitmap(30, 40, small, 13, 10, 1);
  display.drawBitmap(80, 40, small, 13, 10, 1);
  display.drawBitmap(41, 17, big, 41, 28, 1);
  display.display();

  delay(2000);
}

void loop() {

  for (int i = 0; i <= 70; i++) {
    display.clearDisplay();
    display.drawBitmap(i, 10, small, 13, 10, 1);
    display.drawBitmap(i+45, 10, small, 13, 10, 1);
    display.drawBitmap(i, 40, small, 13, 10, 1);
    display.drawBitmap(i+45, 40, small, 13, 10, 1);
    display.drawBitmap(i+10, 17, big, 41, 28, 1);
    display.display();
  }
}

[edit]
I found only a 1% saving.

1 image...

Sketch uses 14568 bytes (47%) of program storage space. Maximum is 30720 bytes.
Global variables use 523 bytes (25%) of dynamic memory, leaving 1525 bytes for local variables. Maximum is 2048 bytes.

Two images...

Sketch uses 14282 bytes (46%) of program storage space. Maximum is 30720 bytes.
Global variables use 523 bytes (25%) of dynamic memory, leaving 1525 bytes for local variables. Maximum is 2048 bytes.

As I am not a fulltime programmer this doesnt tell me anything. Programming wiht basic knowledge by try and error

Part of learning to program is using functions, do a search on c/c++ functions and try and learn the basics.

You are using an Adafruit library, #include <Adafruit_SSD1306.h> , which contains functions to help you draw so that you don't have to create the functions yourself which makes using your display much easier.

An example of a library function is display.clearDisplay() which is the third one listed in the link I provided. So you are already using the library functions you just don't know what they all are or what some of them mean.

You have the following code

 for (int i = 0; i <= 127; i++) {
    display.clearDisplay();
    display.drawBitmap(i, 0, NaN, 64, 64, 1);
    display.display();
  }

which you can probably replace with one line which should be much faster and smoother.

display.startscrollright(0,127);

I am not familiar with your particular display so you might have to experiment a little, there is also a scroll left and a scroll diagonal.

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