Everything is working when I use a fixed size array for the caption inside the structure, but as I add more features to the program it's becoming apparent that fixed size is not the way to go.
Most of the buttons only use a few characters for a caption, but some need up to 30. So, I thought about storing the pointer in the structure instead of the actual caption.
struct Button
{
uint16_t top;
uint16_t left;
uint16_t height;
uint16_t width;
uint8_t GlyphIndex;
unsigned long BgColour;
unsigned long FgColour;
_Bool IsLabel;
_Bool Enabled;
char *Caption;
};
struct Button Buttons[55];
This compiles fine, but the button drawing is wrong. Each button now draws the text of the last one assigned.
Here's the function that sets up a button:
void ButtonSet(uint8_t BtnNo, uint16_t top, uint16_t left, uint16_t height, uint16_t width, uint8_t GlyphIndex, unsigned long BgColour, unsigned long FgColour, _Bool IsLabel, _Bool Enabled, void * caption)
{
Buttons[BtnNo].top = top;
Buttons[BtnNo].left = left;
Buttons[BtnNo].height = height;
Buttons[BtnNo].width = width;
Buttons[BtnNo].GlyphIndex = GlyphIndex;
Buttons[BtnNo].BgColour = BgColour;
Buttons[BtnNo].FgColour = FgColour;
Buttons[BtnNo].IsLabel = IsLabel;
Buttons[BtnNo].Enabled = Enabled;
strcpy(Buttons[BtnNo].Caption, caption);
}
And here's an example call to it:
ButtonSet(BtnSetup, BtnRow0Top, 380, BtnHeight, 250, 0, VGA_BLUE, VGA_YELLOW, 0, 1, "SETUP");
So it seems like the pointer to each caption is the same, hence the same caption appearing on all buttons.
There's obviously still a gap in my understanding of how pointers interact with the compiler and how it stores the strings in program memory.
Can anyone take a look please and tell me what bone-headed mistake (or assumption) I've made?