I'm trying to make use of a 2 line LCD by cycling the content of one of the lines every X iterations. While I could just increase a counter, and check if it's divisible by some number, then display the content based on it, I figured it would be easier to just have an array that had what I wanted to display, then cycle through that.
In this case, I'm using TinyGPS++ as a library, so I'm trying to cycle LAT, LNG, and Time.
Note, I'm not a C programmer by trade... so my (bad) knowledge comes from things like PHP. What seems like it should be doable, doesn't seem to be obvious.
int n = 0;
int display = 0;
char* displayTitle[] = {"TME=","LAT=","LNG="};
char* displayInfo[] = {"gps.time.value()","gps.location.lat()","gps.location.lng()"};
snip
if (gps.time.isUpdated()) {
lcd.setCursor (0,0);
if (display == 500) {
lcd.print(displayTitle[n]);
lcd.setCursor(4,0);
lcd.print(displayInfo[n]);
display=0;
if (n < 2) {
n++;
} else {
n = 0;
}
} else {
display++;
}
Obviously "char*" is wrong, since that's just setting them as strings. Is there any way to have those evaluated at the time they're displayed, so they display the current value? I know "&" can be used to do this in some languages, but I can't seem to figure out how to do this in this case... I get a compile error.
GPS_Display:21: error: invalid cast from type 'uint32_t (TinyGPSTime::*)()' to type 'uint32_t'
GPS_Display:21: error: invalid cast from type 'double (TinyGPSLocation::*)()' to type 'double'
GPS_Display:21: error: invalid cast from type 'double (TinyGPSLocation::*)()' to type 'double'
Because, that doesn't work either (same error). =/
What? No... that is not what I said at all???
The three functions:
TinyGPSTime::value
TinyGPSLocation::lat
TinyGPSLocation::lng
Do they all have the same prototype? As in all take the same parameters ( or none ) and return the same type ( or none ).
Don't just reply yes or no, give details please ( what is/are the prototype(s) ).
If they are the same, you can change your typedef to that prototype.
Because, that doesn't work either (same error). =/
What? No... that is not what I said at all???
The three functions:
TinyGPSTime::value
TinyGPSLocation::lat
TinyGPSLocation::lng
Do they all have the same prototype? As in all take the same parameters ( or none ) and return the same type ( or none ).
Don't just reply yes or no, give details please ( what is/are the prototype(s) ).
If they are the same, you can change your typedef to that prototype.
Sorry, I guess I didn't understand what you said.
Sadly, no, TinyGPSTime::value returns uint32_t, the other two TinyGPSLocation::lat and TinyGPSLocation:lng are both double's.
char displayTitle[][5] = {"TME=","LAT=","LNG="};
int (*ptr[3])();
void setup() {
int i;
Serial.begin(115200);
ptr[0] = &func1; // Initialize the array...
ptr[1] = &func2;
ptr[2] = &func3;
for (i = 0; i < (sizeof(ptr) / sizeof(ptr[0])); i++)
(*ptr[i])();
}
void loop() {
}
int func1()
{
Serial.println("In func1()");
return 1;
}
int func2()
{
Serial.println("In func2()");
return 2;
}
int func3()
{
Serial.println("In func3()");
return 3;
}
You can figure out complex definition like:
int (*ptr[3])();
using Purdum's Right-Left Rule. Go to the identifier (ptr) and look to the right. You see [3]. So far: "ptr is an array of three...". Now look left, you see an asterisk (*), so now it's "ptr is an array of three pointers pointing to...". Now look right and you see the two parentheses, so "ptr is an array of three pointers pointing to functions that return...". Look back left, you see int, so the complete definition becomes: "ptr is an array of three pointers pointing to functions that return int.", which is what I think you are asking for.
So, uhh... why would I want to convert uint32_t to uint32_t, or rather, why would I need to?
Becuase "uint32_t" is not the same as " * *uint32_t (TinyGPSTime::*)()* *
"
As the three functions have different prototypes, they cannot go in an array.
econjack's method is sound, however it will not work due to the problem I've pointed out above.
You can have arrays of functions, and pointers to functions. But you really don't want to. Your problem can be solved in much more straightforward ways.
for (i = 0; i < (sizeof(ptr) / sizeof(ptr[0])); i++)
(*ptr[i])();
}
void loop() {
}
int func1()
{
Serial.println("In func1()");
return 1;
}
int func2()
{
Serial.println("In func2()");
return 2;
}
int func3()
{
Serial.println("In func3()");
return 3;
}
You can figure out complex definition like:
int (*ptr[3])();
using Purdum's Right-Left Rule. Go to the identifier (*ptr*) and look to the right. You see [3]. So far: "*ptr* is an array of three...". Now look left, you see an asterisk (*), so now it's "*ptr* is an array of three pointers pointing to...". Now look right and you see the two parentheses, so "*ptr* is an array of three pointers pointing to functions that return...". Look back left, you see *int*, so the complete definition becomes: "*ptr* is an array of three pointers pointing to functions that return *int*.", which is what I think you are asking for.
Extremely helpful, alas, same issue. Errors that I'm trying to convert... and as I mentioned before, sadly, one returns uint32_t, and two return double. =(
michinyon:
You can have arrays of functions, and pointers to functions. But you really don't want to. Your problem can be solved in much more straightforward ways.