#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
// The shield uses the I2C SCL and SDA pins. On classic Arduinos
// this is Analog 4 and 5 so you can't use those for analogRead() anymore
// However, you can connect other I2C sensors to the I2C bus and share
// the I2C bus.
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
uint8_t i=0;
bool UP=0;
bool DOWN=0;
bool LEFT=0;
bool RIGHT=0;
bool SELECT=0;
int waitforinput=1;
// These #defines make it easy to set the backlight color
#define RED 0x1
#define YELLOW 0x3
#define GREEN 0x2
#define TEAL 0x6
#define BLUE 0x4
#define VIOLET 0x5
#define WHITE 0x7
//Define X and Y index
#define Y 0
#define X 1
enum TYPE
{
INT,
TEMP,
PH
};
//TYPE type=MENU;
class menu
{
public:
int 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;
menu(char *myname="", byte y=0, byte x=0, byte m_color=0, int c_type=0)
{
strcpy(name, myname);
color=m_color;
type=c_type;
menulist=0;
previous_menu=0;
value=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;
}
};
byte backChar[8] = {
0b11111,
0b11100,
0b11110,
0b10111,
0b10011,
0b00011,
0b00011,
0b00000
};
menu MAIN("MAIN",3,2,TEAL);
menu menu1("EC:",0,0,RED,PH), menu2("m2",2,3,RED), menu3("menu 3",1,0), menu4("menu 4",1,0), menu5("menu 5",1,1),menu6("menu 6",1,1);
double v1=21.59;
char buffer[5];
menu* current_menu=&MAIN;
byte current_index[2]={
0,0};
byte index_offset=1;
menu* current_index_menu=current_menu->menulist[current_index[Y]][current_index[X]];
void setup(){
menu1.value=&v1;
Serial.begin(9600);
Serial.println("starting");
lcd.begin(16,2);
lcd.setBacklight(WHITE);
lcd.createChar(0, backChar);
lcd.setCursor(-2,0);
lcd.print("hello");
delay(1000);
lcd.clear();
menu2.previous_menu=&MAIN;
menu2.menulist[0][0]=&MAIN;
menu5.previous_menu=&menu2;
menu5.menulist[0][0]=&menu2;
menu6.previous_menu=&menu2;
menu6.menulist[0][0]=&menu2;
menu2.menulist[0][1]=&menu5;
menu2.menulist[0][2]=&menu6;
menu2.menulist[1][1]=&menu3;
menu2.menulist[1][2]=&menu4;
menu2.menulist[1][0]=&menu3;
MAIN.menulist[0][0]=&menu1;
MAIN.menulist[0][1]=&menu2;
MAIN.menulist[1][0]=&menu3;
MAIN.menulist[2][0]=&menu4;
MAIN.previous_menu=&MAIN;
current_index_menu=current_menu->menulist[current_index[Y]][current_index[X]];
MAIN.value=0;
if (menu3.menulist==0)
{
Serial.println("yes");
}
else
Serial.println("no");
Serial.println((int)&MAIN.value);
Serial.println(*(double*) current_menu->menulist[current_index[Y]][current_index[X]]->value);
Serial.println(*(double*) current_index_menu->value);
Serial.println(strlen(dtostrf(*(double*) current_index_menu->value,3,current_index_menu->type,buffer)));
//Serial.println(strlendtostrf(*(double*) current_index_menu->value,3,2,buffer));
//Serial.println(strlen(current_menu->menulist[0][0]->name));
display_menu();
}
void loop(){
//current_index_menu=current_menu->menulist[current_index[Y]][current_index[X]];
//Serial.println(waitforinput);
uint8_t buttons = lcd.readButtons();
if (waitforinput>1) waitforinput-=1;
else{
if (buttons) {
current_index_menu=current_menu->menulist[current_index[Y]][current_index[X]];
waitforinput=2;
lcd.clear();
if (buttons & BUTTON_UP) {
UP=true;
if (current_index[Y]==0)
{
for (i=0; current_menu->menulist[current_index[Y]+1][current_index[X]]!=0; i++)
{
current_index[Y]=i; //replace with find 0 index
index_offset=i;
}
}
else {
if (index_offset-current_index[Y]==1) index_offset--;
current_index[Y]--;
}
}
if (buttons & BUTTON_DOWN) {
DOWN=true;
if (current_menu->menulist[current_index[Y]+1]==0)
{
index_offset=1;
current_index[Y]=0;
}
else {
if (current_index[Y]==index_offset) index_offset++;
current_index[Y]++;
}
}
if (buttons & BUTTON_LEFT) {
LEFT=true;
if (current_index[X]==0)
{
for (i=0; current_menu->menulist[current_index[Y]][current_index[X]+1]!=0; i++)
current_index[X]=i;
}
else current_index[X]--;
}
if (buttons & BUTTON_RIGHT) {
RIGHT=true;
if (current_menu->menulist[current_index[Y]][current_index[X]+1]==0)
current_index[X]=0;
else
current_index[X]+=1;
}
if (buttons & BUTTON_SELECT) {
SELECT=true;
waitforinput=3;
if (current_index_menu->previous_menu==0)
current_index_menu->previous_menu=current_menu;
if (current_index_menu->color==0)
current_index_menu->color=current_menu->color;
lcd.setBacklight(current_index_menu->color);
current_menu=current_index_menu;
current_index[X]=0;
current_index[Y]=0;
//Serial.println(current_menu->previous_menu->name);
}
Serial.println(index_offset);
display_menu();
}
}
//display_menu();
lcd.setCursor(15,0);
lcd.print(current_index[Y]);
lcd.setCursor(15,1);
lcd.print(current_index[X]);
}
void display_menu()
{
for (int i=index_offset-1; i<=index_offset; i++)
{
//display_selection();
byte xpos=2;
for(int p=0; (current_menu->menulist[i][p])!=0; p++)
{
current_index_menu=current_menu->menulist[i][p];
//Serial.println(p);
//Serial.print(" ");
// delay(1000);
//lcd.setCursor(xpos);
lcd.setCursor(xpos,1-(index_offset-i));
if (current_index_menu!=current_menu)
if (current_index_menu==current_menu->previous_menu)
{
lcd.setCursor(0,0);
lcd.write((uint8_t)0);
if (current_index[Y]==i && current_index[X]==p)
{
lcd.setCursor(0,1);
lcd.print("^");
}
xpos+=2;
}
else
{
if ((current_index_menu->menulist!=0) && (current_index[Y]==i && current_index[X]==p))
{
lcd.setCursor(xpos-1,1-(index_offset-i));
lcd.print(">");
lcd.print(current_index_menu->name);
lcd.print("<");
}
else
lcd.print(current_index_menu->name);
xpos+=strlen(current_index_menu->name);
if (current_index_menu->value!=0)
{
if (current_index[Y]==i && current_index[X]==p)
{
lcd.setCursor(xpos-1,1-(index_offset-i));
lcd.print(">");
}
lcd.setCursor(xpos,1-(index_offset-i));
lcd.print (*(double*)(current_index_menu->value),current_index_menu->type);
//lcd.print("<");
xpos+=strlen(dtostrf(*(double*) current_index_menu->value,3,current_index_menu->type,buffer));
if (current_index[Y]==i && current_index[X]==p)
{
lcd.setCursor(xpos,1-(index_offset-i));
lcd.print("<");
}
}
xpos++;
}
//Serial.println();
}
}
}