I have this code I have been working on for over 6 months. Some of you have seen previous iterations and helped me through some of my struggles. The code works fine, right now I am still trying to learn from it, and optimize it a little bit more.
One of the things that stuck out to me, and this is small mind you, but I am looking for more "precious" bits...
I have a button class, in the definition one of the variables is:
const char *text;
My understanding is this is a string pointer.
The constructor is the following
void initButton(int16_t xPos, uint8_t yPos, uint8_t butWidth, uint8_t butHeight, uint16_t buttonColor, uint16_t textColor, uint8_t textSize, const char *butText) {
x = xPos;
y = yPos;
width = butWidth;
height = butHeight;
text = butText;
bcolor = buttonColor;
tcolor = textColor;
tsize = textSize;
render();
}
There is a reference to the pointer? I'm really trying to figure out how this all works. Anyway, what I want to be able to do is save a couple of bits...
There are a number of buttons that have the same "text" in the button, I would like to use a string array and point to it in the initButton since this button appears in 4 different parts of my code. I figure it would save space to use a pointer instead of the string in every instance.
btn[4].initButton(90, 200, 140, 25, ILI9341_BLUE, ILI9341_WHITE, 2, " Main Menu");
I think to create the string array it would be:
char mainMenu[11] = {" Main Menu"};
But not sure where to go from there...
There are 4 buttons with " Main Menu", 4 buttons with "+" and 4 buttons with "-" I would like to apply this to. The other buttons need to be unaffected.
And here is the entire button class:
int8_t blockWidth = 20; // block size
int8_t blockHeight = 20;
int16_t blockX = 0, blockY = 0; // block position (pixels)
class ScreenPoint {
public:
int16_t x;
int16_t y;
// default constructor
ScreenPoint() {
}
ScreenPoint(int16_t xIn, int16_t yIn) {
x = xIn;
y = yIn;
}
};
//Touch Screen
ScreenPoint getScreenCoords(int16_t x, int16_t y) {
int16_t xCoord = round((x * xCalM) + xCalC);
int16_t yCoord = round((y * yCalM) + yCalC);
if (xCoord < 0) xCoord = 0;
if (xCoord >= tft.width()) xCoord = tft.width() - 1;
if (yCoord < 0) yCoord = 0;
if (yCoord >= tft.height()) yCoord = tft.height() - 1;
return (ScreenPoint(xCoord, yCoord));
}
ScreenPoint sp;
class Button {
public:
int16_t x;
uint8_t y;
uint8_t width;
uint8_t height;
uint16_t bcolor;
uint16_t tcolor;
uint8_t tsize;
const char *text;
Button() {
}
void initButton(int16_t xPos, uint8_t yPos, uint8_t butWidth, uint8_t butHeight, uint16_t buttonColor, uint16_t textColor, uint8_t textSize, const char *butText) {
x = xPos;
y = yPos;
width = butWidth;
height = butHeight;
text = butText;
bcolor = buttonColor;
tcolor = textColor;
tsize = textSize;
render();
}
void render() {
tft.fillRect(x, y, width, height, bcolor);
tft.setCursor(x + 5, y + 5);
tft.setTextSize(tsize);
tft.setTextColor(tcolor);
tft.print(text);
}
bool isClicked(ScreenPoint sp) {
return ((sp.x >= x) && (sp.x <= (x + width)) && (sp.y >= y) && (sp.y <= (y + height)));
}
};
Button btn[19]; // Array for touch screen buttons. Buttons are initialized on the pages they appear
Someone may say, "post your entire code". It is on the longer side and would take time for someone to navigate it, especially since my arrangement of it still needs work.