SPI Display with graphics store in *.c file

Hello,

I am experimenting with a SPI display with Adafruit_GFX and Adafruit_ST7735 libraries.

Everything works as expected but when I try to use function tft.drawBitmap(x, y, image, w, h); the only way I can make it work is if I define the array containing the image in HEX directly in the main sketch.

I've found online that people put all the graphic files into a different tab with *.c extension and, in order to do so, they firstly add "#include <avr/pgmspace.h>" as first line in the *.c file and then, one after another, all the images in HEX defined as:

const unsigned char imagename1 [] PROGMEM = { ...HEX NUMBERS... };
const unsigned char imagename2 [] PROGMEM = { ...HEX NUMBERS... };

Then, in the main sketch tab, before setup, they add:

extern uint8_t imagename1;
extern uint8_t imagename2;
...

I am going crazy because, if I do so, the display shows some crazy random dots, if I simply put the HEX file in the main sketch everything work as supposed to do...

Anyone who could give me a hint on what am I doing wrong?

Many many thanks!

Alessandro

Your other .c file is declaring imagename1 and imagename2 as arrays of unsigned char.

Your main program is declaring a extern reference to those variables as if they are an usigned 8 bit value, not a pointer.

extern uint8_t *imagename1;
extern uint8_t *imagename2;
...

Firstly, it's rather unhelpful to post snippets of code; a simplified compilable example is preferred and in many cases necessary. More so when you are careless with what you write - eg. when I try to use function tft.drawBitmap(x, y, image, w, h) - I don't see a drawBitmap with only five parameters.

Anyway, I find four function prototypes for drawBitmap():

drawBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color),
drawBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color, uint16_t bg),

drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h, uint16_t color),
drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg)

You'll notice that two of the functions take a const bitmap and two take a non-const one.

In your .c file, you have correctly defined your image data variables as const and PROGMEM (so really constant).
In your .ino file (which is compiled separately) you have declared the variables as non-const. This means that when you call drawBitmap , the non-const versions of the functions are selected.

So what? Well, the const functions access the image data from program memory, but the non-const functions access the image data from RAM. So you are trying to read data only stored in program memory by reading it from RAM. It doesn't work.

Hello,

thank you very much for your replies.
I actually missed your messages and read them only today.

Sorry, I didn't know about the practice of inserting compilable examples, I will do next time!

I solved the problem thank to your advices: I just had to insert "const" in between "extern" and "uint8_t imagename1".

Thank you very much again!

Alessandro