Greetings;
I'm working on this project with a bunch of sensors, which need to be periodically calibrated, data entry being done on a TFT touch sreen.
The thing is working, but I have a few style questions, related to strings and buttons:
Button Definitions:
Adafruit_GFX_Button buttons[21];
/* create 21 buttons, in classic candybar phone style */
char buttonlabels[21][5] = {"EC", "RTD", "pH",
"cal", "Min", "low",
"hig", "tst", "CLR",
"1", "2", "3",
"4", "5", "6",
"7", "8", "9",
".", "0", "RTN"
};
uint16_t buttoncolors[21] = {ILI9341_GREEN, ILI9341_GREEN , ILI9341_GREEN,
ILI9341_GREEN, ILI9341_GREEN , ILI9341_GREEN,
ILI9341_GREEN, ILI9341_YELLOW , ILI9341_DARKGREY,
ILI9341_BLUE, ILI9341_BLUE, ILI9341_BLUE,
ILI9341_BLUE, ILI9341_BLUE, ILI9341_BLUE,
ILI9341_BLUE, ILI9341_BLUE, ILI9341_BLUE,
ILI9341_DARKCYAN, ILI9341_BLUE, ILI9341_RED,
}; //
Here, I draw the buttons:
// create buttons
for (uint8_t row = 0; row < 7; row++) {
for (uint8_t col = 0; col < 3; col++) {
buttons[col + row * 3].initButton(&tft, BUTTON_X + col * (BUTTON_W + BUTTON_SPACING_X),
BUTTON_Y + row * (BUTTON_H + BUTTON_SPACING_Y), // x, y, w, h, outline, fill, text
BUTTON_W, BUTTON_H, ILI9341_WHITE, buttoncolors[col + row * 3], ILI9341_WHITE,
buttonlabels[col + row * 3], BUTTON_TEXTSIZE);
buttons[col + row * 3].drawButton();
}
}
Which nicely puts displays all the characters defined above.
Now, when I push a button:
// now we can ask the buttons if their state has changed
for (uint8_t b = 0; b < 21; b++) {
if (buttons[b].justReleased()) {
Serial.print("Released: "); Serial.println(b);
buttons[b].drawButton(); // draw normal
}
if (buttons[b].justPressed()) {
buttons[b].drawButton(true); // draw invert!
//-----------------------------------------------------------------------------
switch (b) { // switch case based on the BUTTON PUSHED
case 0: // Pushed EC ---> Display "100:"
Serial.println("EC"); //
textfield[textfield_i] = '1';
textfield_i++;
textfield[textfield_i] = '0';
textfield_i++;
textfield[textfield_i] = '0';
textfield_i++;
textfield[textfield_i] = ':';
textfield_i++;
textfield[textfield_i] = 0; // zero terminate
break; // exits the switch case.
case 1: // Pushed RTD ---> Display "102:"
Serial.println("RTD"); //
textfield[textfield_i] = '1';
textfield_i++;
textfield[textfield_i] = '0';
textfield_i++;
textfield[textfield_i] = '2';
textfield_i++;
textfield[textfield_i] = ':';
textfield_i++;
textfield[textfield_i] = 0; // zero terminate
break; // exits the switch case.
case 2: // Pushed pH ---> Display "99:"
Serial.println("pH"); //
textfield[textfield_i] = '9';
textfield_i++;
textfield[textfield_i] = '9';
textfield_i++;
textfield[textfield_i] = ':';
textfield_i++;
textfield[textfield_i] = 0; // zero terminate
break; // exits the switch case.
// other cases removed for clarity
case 9 ... 19: //digits and decimal point
if (textfield_i < TEXT_LEN) {
textfield[textfield_i] = buttonlabels[b][0];
textfield_i++;
textfield[textfield_i] = 0; // zero terminate
}
break;
case 20: //Complete command, send to EZO
Serial.print("Entering Data: ");
status(textfield);
channel = atoi(strtok(textfield, ":")); // Let's parse the string at each colon, extracting the EZO address
Serial.print(channel); Serial.print(":");
cmd = strtok(NULL, ":"); // Let's parse the string at each colon, extract the command --------------> will it work for stuff other than r??
Serial.println(cmd);
I2C_call(); // send to I2C
textfield_i = 0; //reset textfield string
textfield[textfield_i] = '\0'; // for next command after RTN
// update the current text field
//Serial.println(textfield);
tft.setCursor(TEXT_X + 2, TEXT_Y + 10);
tft.setTextColor(TEXT_TCOLOR, ILI9341_BLACK);
tft.setTextSize(TEXT_TSIZE);
tft.print(" ");
break;
}
// update the current text field
//Serial.println(textfield);
tft.setCursor(TEXT_X + 2, TEXT_Y + 10);
tft.setTextColor(TEXT_TCOLOR, ILI9341_BLACK);
tft.setTextSize(TEXT_TSIZE);
tft.print(textfield);
//------------------------------------------------------------------------------
delay(100); // UI debouncing
}
}
I write some contents (not the same as the button text) into a char array, textfield.
Notice, that I am writing every single character, one at a time and then incrementing the index pointer.
There must be a more elegant way of doing this...???
I tried various"100"; followed by incrementing the index 4 times. didn't work
I tried '100:'; followed by index+4. didn't work
As an aside: If I wanted to print the label of the pressed button (variable length labels) into my textfield, is there a way to do that? case 9 ...19,
case 9 ... 19: //digits and decimal point
if (textfield_i < TEXT_LEN) {
textfield[textfield_i] = buttonlabels[b][0];
textfield_i++;
textfield[textfield_i] = 0; // zero terminate
}
break;
used for the digits 0-9 uses this, but only manages to print the first (and only) digit.
Thanks for your help.
Cheers;