Overall question regarding displaying a bitmap stored in PROGMEM indirectly.
Situation:
As an example, I would have a character that can move up down left and right, 4 bitmaps defined in progmem.
But this character can be happy or sad, so I have two sets of 4 bitmaps.
What I want to achieve is set the character to be either happy or sad and the bitmaps to follow accodingly.
What I considered or tried:
I can hard code this in, eveytime I draw a bitmap I have a switch/case which selects the correct bitmap.
However, as the actual code is far more complex (bigger set and more bitmaps) this gets quite big and messy.
I feel this should not be the way to do this.
I have defined variabels outside PROGMEM and copied the bitmaps for whatever set is active.
This works -> but as before, I have quite a few bitmaps and lose massive amounts of RAM
Basically this is not an option due to RAM limitations
What I want to achieve but cant seem to figure out:
I would want to just set a pointer to the active set of bitmaps, this way I do not have to buffer anything and just "lose" the RAM for the pointers (which should not be a lot anyways right?)
So I'd have 4 pointer and I would change these pointers on the fly based on the status (happy or sad)
What I can't seem to figure out is how to point to a bitmap in PROGMEM, the output is basically a mess
I'm made a simple scetch which should show happy and sad twice, but clearly I'm not doing this right as the 2nd line of icons are just junk.
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <avr/pgmspace.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET 4 // 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);
static const unsigned char PROGMEM happy[] =
{ B00000000, B11000000,
B00000001, B11000000,
B00000001, B11000000,
B00000011, B11100000,
B11110011, B11100000,
B11111110, B11111000,
B01111110, B11111111,
B00110011, B10011111,
B00011111, B11111100,
B00001101, B01110000,
B00011011, B10100000,
B00111111, B11100000,
B00111111, B11110000,
B01111100, B11110000,
B01110000, B01110000,
B00000000, B00110000
};
static const unsigned char PROGMEM sad[] PROGMEM = {
B00000000, B11000000,
B00000000, B11000000,
B00000000, B11000000,
B00000000, B11100000,
B00000000, B11100000,
B00000000, B11111000,
B00000000, B11111111,
B00000000, B10011111,
B00000000, B11111100,
B00000000, B01110000,
B00000000, B10100000,
B00000000, B11100000,
B00000000, B11110000,
B00000000, B11110000,
B00000000, B01110000,
B00000000, B00110000
};
unsigned char* bmp;
void setup() {
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
delay(1000);
Serial.println(F("Started"));
display.clearDisplay();
//first draw the bitmaps to see if those are OK
display.drawBitmap(0,0,happy, 16, 16, 1);
display.drawBitmap(30,0,sad, 16, 16, 1);
//And then to try and duplicate the same thing by using a pointer to these PROGMEM bitmaps
*bmp = &happy;
display.drawBitmap(0,30,bmp, 16, 16, 1);
*bmp = &sad;
display.drawBitmap(30,30,bmp, 16, 16, 1);
display.display();
Serial.println(F("Final"));
}
void loop() {
}