Advertisement
Space-G

Untitled

Dec 4th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.70 KB | None | 0 0
  1. //#include <LiquidCrystal.h>
  2. #include <iostream>
  3.  
  4. // select the pins used on the LCD panel
  5. /*
  6. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
  7.  
  8. // define some values used by the panel and buttons
  9. int lcd_key     = 0;
  10. int adc_key_in  = 0;
  11. */
  12. #define btnRIGHT  0
  13. #define btnUP     1
  14. #define btnDOWN   2
  15. #define btnLEFT   3
  16. #define btnSELECT 4
  17. #define btnNONE   5
  18.  
  19. using namespace std;
  20.  
  21. class Noh;
  22.  
  23. class MenuOption{
  24. private:
  25.  
  26. public:
  27.     char name[16]; // favor não usar o último caractere
  28.     bool leads_to_another_menu;
  29.     Noh* data;
  30.     MenuOption(){};
  31.     MenuOption(char* new_name){
  32.         for (int i = 0; i < 15; i++){
  33.             name[i] = new_name[i];
  34.         }
  35.         name[16] = '\0';
  36.     };
  37.     ~MenuOption(){};
  38. };
  39.  
  40. class Noh
  41. {
  42. private:
  43.     void* data; // o ponteiro do tipo void pode ser usado pra qualquer propósito
  44. public:
  45.     Noh* next;
  46.     Noh* previous;
  47.     MenuOption* asMenu(){return (MenuOption*)(data);}; // retorna data como um ponteiro para MenuOption
  48.     //Skill* asSkill(){return (Skill*)(data);}; // retorna data como um ponteiro para Skill
  49.     Noh(){
  50.         next = NULL;
  51.         previous = NULL;
  52.         data = NULL;
  53.     };
  54.     Noh(void* new_data){
  55.         data = new_data;
  56.         next = NULL;
  57.         previous = NULL;
  58.     };
  59.     ~Noh(){
  60.         // remove ponteiros que apontam pra este objeto
  61.         if (this->next->previous == this){
  62.             this->next->previous = NULL;
  63.         }
  64.         if (this->previous->next == this){
  65.             this->previous->next = NULL;
  66.         }
  67.  
  68.         delete (Noh*)(data);
  69.     };
  70.  
  71. };
  72.  
  73.  
  74. int read_LCD_buttons()
  75. {/*
  76.     adc_key_in = analogRead(0);      // read the value from the sensor
  77.     // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
  78.     // we add approx 50 to those values and check to see if we are close
  79.     if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
  80.     // For V1.1 us this threshold
  81.     if (adc_key_in < 50)   return btnRIGHT;
  82.     if (adc_key_in < 250)  return btnUP;
  83.     if (adc_key_in < 450)  return btnDOWN;
  84.     if (adc_key_in < 650)  return btnLEFT;
  85.     if (adc_key_in < 850)  return btnSELECT;
  86.     // For V1.0 comment the other threshold and use the one below:
  87. /*
  88.     if (adc_key_in < 50)   return btnRIGHT;
  89.     if (adc_key_in < 195)  return btnUP;
  90.     if (adc_key_in < 380)  return btnDOWN;
  91.     if (adc_key_in < 555)  return btnLEFT;
  92.      if (adc_key_in < 790)  return btnSELECT;
  93. */
  94.     char choice;
  95.     cin >> choice;
  96.     if (choice == 'u'){
  97.         return btnUP;
  98.     }
  99.     else if (choice == 'd'){
  100.         return btnDOWN;
  101.     }
  102.     else if (choice == 'l'){
  103.         return btnLEFT;
  104.     }
  105.     else if (choice == 'r'){
  106.         return btnRIGHT;
  107.     }
  108.     else if (choice == 's'){
  109.         return btnSELECT;
  110.     }
  111.     return btnNONE;  // when all others fail, return this...
  112. }
  113.  
  114. void print_menu(Noh* option){
  115.     cout << (option->asMenu()->name);
  116.     cout << (char)(0b01111111); // setinha
  117.     if (option->next != NULL){
  118.         cout << (option->next->asMenu()->name) << endl << endl;
  119.     }
  120. }
  121.  
  122. Noh* context_menu(Noh* option){
  123.     bool in_context_page = true;
  124.     Noh* last_page = option;
  125.     if (option->asMenu()->leads_to_another_menu){ // se é apenas navegação, e não uma alteração
  126.         while (in_context_page){
  127.             switch ((int)read_LCD_buttons){
  128.                 case btnNONE:
  129.                     break;
  130.                 case btnUP:
  131.                     // volta para a opção anterior
  132.                     option = option->previous;
  133.                     print_menu(option);
  134.                     break;
  135.                 case btnDOWN:
  136.                     // vai para a próxima opção
  137.                     option = option->next;
  138.                     print_menu(option);
  139.                     break;
  140.                 case btnLEFT:
  141.                     // retorna para a página anterior
  142.                     in_context_page = false;
  143.                     break;
  144.                 default:
  145.                     // seleciona a opção atual
  146.                     option = context_menu((Noh*)(option->asMenu()->data));
  147.                     print_menu(option);
  148.             }
  149.         }
  150.         return last_page;
  151.     }
  152. }
  153.  
  154. int main(){
  155.     Noh* first_page = new Noh((void*)(new MenuOption()));
  156.     return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement