Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- //#include <iostream>
- // select the pins used on the LCD panel
- LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
- // define some values used by the panel and buttons
- int lcd_key = 0;
- int adc_key_in = 0;
- const int btnRIGHT = 0;
- const int btnUP = 1;
- const int btnDOWN = 2;
- const int btnLEFT = 3;
- const int btnSELECT = 4;
- const int btnNONE = 5;
- using namespace std;
- class Noh;
- void context_menu(Noh* option);
- class MenuOption{
- private:
- public:
- char name[16]; // favor não usar o último caractere
- bool leads_to_another_menu;
- Noh* data;
- void select();
- MenuOption(){};
- MenuOption(char* new_name){
- int i = 0;
- bool string_end = false;
- for (int i = 0; i < 15; i++){
- if (string_end == false){
- if (new_name[i] == '\0'){
- string_end = true;
- }
- name[i] = new_name[i];
- }
- else{
- name[i] = ' ';
- }
- }
- name[16] = '\0';
- };
- ~MenuOption(){};
- };
- void MenuOption::select(){
- if (this->leads_to_another_menu){
- context_menu(data);
- }
- }
- class Noh
- {
- private:
- void* data; // o ponteiro do tipo void pode ser usado pra qualquer propósito
- public:
- Noh* next;
- Noh* previous;
- MenuOption* asMenu(){return (MenuOption*)(data);}; // retorna data como um ponteiro para MenuOption
- //Skill* asSkill(){return (Skill*)(data);}; // retorna data como um ponteiro para Skill
- Noh(){
- next = NULL;
- previous = NULL;
- data = NULL;
- };
- Noh(void* new_data){
- data = new_data;
- next = NULL;
- previous = NULL;
- };
- ~Noh(){
- // remove ponteiros que apontam pra este objeto
- if (this->next->previous == this){
- this->next->previous = NULL;
- }
- if (this->previous->next == this){
- this->previous->next = NULL;
- }
- delete (Noh*)(data);
- };
- };
- int read_LCD_buttons()
- {
- adc_key_in = analogRead(0); // read the value from the sensor
- if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
- /*// For V1.1 us this threshold
- if (adc_key_in < 50) return btnRIGHT;
- if (adc_key_in < 250) return btnUP;
- if (adc_key_in < 450) return btnDOWN;
- if (adc_key_in < 650) return btnLEFT;
- if (adc_key_in < 850) return btnSELECT;*/
- // For V1.0 comment the other threshold and use the one below:
- if (adc_key_in < 50) return btnRIGHT;
- if (adc_key_in < 195) return btnUP;
- if (adc_key_in < 380) return btnDOWN;
- if (adc_key_in < 555) return btnLEFT;
- if (adc_key_in < 790) return btnSELECT;
- /*
- char choice;
- cin >> choice;
- if (choice == 'u'){
- return btnUP;
- }
- else if (choice == 'd'){
- return btnDOWN;
- }
- else if (choice == 'l'){
- return btnLEFT;
- }
- else if (choice == 'r'){
- return btnRIGHT;
- }
- else if (choice == 's'){
- return btnSELECT;
- }*/
- return btnNONE; // when all others fail, return this...
- }
- void print_menu(Noh* option){
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print(option->asMenu()->name);
- lcd.setCursor(15, 0);
- lcd.print((char)(127)); // setinha
- if (option->next != NULL){
- lcd.setCursor(0, 1);
- lcd.print(option->next->asMenu()->name);
- }
- /*
- cout << endl << endl << endl << endl << endl << endl << endl << (option->asMenu()->name);
- cout << (char)(0b01111111) << endl; // setinha
- if (option->next != NULL){
- cout << (option->next->asMenu()->name) << endl;
- }*/
- }
- void context_menu(Noh* option){
- bool in_context_page = true;
- Noh* last_page = option;
- print_menu(option);
- while (in_context_page){
- switch (read_LCD_buttons()){
- case btnNONE:
- break;
- case btnUP:
- // volta para a opção anterior
- if (option->previous != NULL){
- option = option->previous;
- }
- print_menu(option);
- while (read_LCD_buttons() == btnUP){
- delay(50);
- }
- break;
- case btnDOWN:
- // vai para a próxima opção
- if (option->next != NULL){
- option = option->next;
- }
- print_menu(option);
- while (read_LCD_buttons() == btnDOWN){
- delay(50);
- }
- break;
- case btnLEFT:
- // retorna para a página anterior
- in_context_page = false;
- delay(50);
- break;
- default:
- // seleciona a opção atual
- while (read_LCD_buttons() == btnRIGHT || read_LCD_buttons() == btnSELECT){
- delay(50);
- }
- last_page = option;
- option->asMenu()->select();
- while (read_LCD_buttons() == btnLEFT){
- delay(50);
- }
- option = last_page;
- print_menu(option);
- }
- }
- }
- void add_menu_option(Noh* node_above, Noh* node_to_be_inserted){
- if (node_above->previous == NULL && node_above->next == NULL){ // transforma o menu em uma lista circular
- node_above->previous = node_above;
- node_above->next = node_above;
- }
- node_to_be_inserted->previous = node_above;
- node_to_be_inserted->next = node_above->next;
- node_above->next = node_to_be_inserted;
- node_to_be_inserted->next->previous = node_to_be_inserted;
- }
- void add_submenu(Noh* node_father, Noh* node_to_be_inserted){
- node_father->asMenu()->leads_to_another_menu = true;
- node_father->asMenu()->data = node_to_be_inserted;
- }
- Noh* first_page = new Noh((void*)(new MenuOption((char*)"Trains")));
- void setup(){
- lcd.begin(16, 2);
- Noh* aux = new Noh(new MenuOption((char*)"I like them!"));
- add_submenu(first_page, aux);
- Noh* aux2 = new Noh(new MenuOption((char*)"..."));
- add_menu_option(aux, aux2);
- aux2 = new Noh(new MenuOption((char*)"Fuooon"));
- add_menu_option(aux, aux2);
- aux = new Noh(new MenuOption((char*)"Muffin"));
- add_menu_option(first_page, aux);
- aux2 = new Noh(new MenuOption((char*)"I wanna die"));
- add_submenu(aux, aux2);
- aux = new Noh(new MenuOption((char*)":D"));
- add_menu_option(aux2, aux);
- aux = new Noh(new MenuOption((char*)"Stegosaurus"));
- add_menu_option(first_page, aux);
- aux = new Noh(new MenuOption((char*)"Real person"));
- add_menu_option(first_page, aux);
- aux2 = new Noh(new MenuOption((char*)"Skateboards"));
- add_submenu(aux, aux2);
- }
- void loop(){
- context_menu(first_page);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement