Menu class:
class menu
{
public:
int type;
// typename type;
char name[10];
byte color;
byte xpos;// position on the screen
byte ypos;
//byte index[2]; //position in the current menu index
menu*** menulist; //indexed array pointer to group members
menu* previous_menu; //pointer to previous menu
void *value;
void (*function) ();
template <typename Type>
Type Value(Type)
{
//void *ptr;
Type* ptr=static_cast<Type*>(value);
return *ptr;
// switch (typeof(type))
// {
// case int:
// Type* ptr= *static_cast<int*>(*value);
// return *( static_cast<int *>(value));
// //return static_cast<int>(*value);
// break;
// return *ptr;
// }
}
menu(char *myname = "", byte y = 0, byte x = 0, int c_type = 0, byte m_color = 0)
{
strcpy(name, myname);
color = m_color;
type = c_type;
menulist = 0;
previous_menu = 0;
value = 0;
function = 0;
if (y > 0)
{
menulist = new menu**[y + 1];
menulist[y] = 0;
for (byte i = 0; i < y; i++)
{
menulist[i] = new menu*[x + 1];
for (byte p = 0; p <= x; p++)
menulist[i][p] = 0;
}
}
}
~menu() {
for (int i = 0; menulist[i] != 0; ++i) {
delete [] menulist[i];
}
delete [] menulist;
}
};
Time menu instantiation:
int second_set = 9; //second();
int minute_set = 9; //minute();
int hour_set = 9;//hour();
int day_set = 9;//day();
int month_set = 9;//month();
int year_set = 99;//year();
menu SET_TIME("Time/Date", 2, 4, 0), set_second(":"), set_minute(":"), set_hour("Time:", 0, 0, 0), set_year("/"), set_month("/"), set_day("DATE: ");
void setup()
{
set_hour.value = &hour_set;
set_minute.value = &minute_set;
set_second.value = &second_set;
set_day.value = &day_set;
set_month.value = &month_set;
set_year.value = &year_set;
SET_TIME.menulist[0][0] = &OPTIONS;
SET_TIME.previous_menu = &OPTIONS;
SET_TIME.menulist[0][1] = &set_hour;
SET_TIME.menulist[0][2] = &set_minute;
SET_TIME.menulist[0][3] = &set_second;
SET_TIME.menulist[1][0] = &set_day;
SET_TIME.menulist[1][1] = &set_month;
SET_TIME.menulist[1][2] = &set_year;
SET_TIME.function=SetRTCTime;
}
When select is pressed call the current menu's function
void loop() {
if (buttons & BUTTON_SELECT) {
Serial.println("select pressed");
if (current_menu->function!=0)
{
Serial.println("Calling "+(String)current_menu->name +"'s function");// Serial.print(current_menu->name);
(*(current_menu->function))();
}
else Serial.println("no function detected");
}
SET_TIME's function pointer is set to this function:
void SetRTCTime()
{
Serial.println("calling setrtc function");
setTime(hour_set,minute_set,second_set,day_set,month_set,year_set);
RTC.set(now());
setSyncProvider(RTC.get); // the function to get the time from the RTC
if(timeStatus()!= timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time to "+(String)hour_set+":"+(String)minute_set+":"+(String)second_set);
}
Here is a function to change the value a menu object is pointing at:
void change_value(void *ivalue, int type, int sign)
{
int exponent = int(2 * (float(holdtimer) / 20.0));
switch (type)
{
case 0:
{
int* nvalue = static_cast<int*>(ivalue);
*nvalue += (int)sign * pow(10, exponent);
//Serial.print("Exponent value: "); Serial.println(pow(10,exponent));
break;
}
default:
{
double* pvalue = static_cast<double*>(ivalue);
if (type == 1)
*pvalue += (double)0.1 * sign * pow(10, exponent);
if (type == 2)
*pvalue += (double)0.01 * sign * pow(10, exponent);
break;
}
}
}
But it requires me to use ints or an enum to store the type, I would rather just store the type int itself in each menu object. Then I can use that as an argument to get the type that needs to be returned by the template function Value() inside the menu class I am trying to write. The only way I can think of "storing" the type like int is to use a pointer to the int class itself, but this obviously needs to be a void pointer. If there is another way please give me an example instead.