Advertisement
Space-G

HABEMOS GUI

Dec 4th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.32 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. const int btnRIGHT = 0;
  13. const int btnUP = 1;
  14. const int btnDOWN = 2;
  15. const int btnLEFT = 3;
  16. const int btnSELECT = 4;
  17. const int btnNONE = 5;
  18.  
  19. using namespace std;
  20.  
  21. class Noh;
  22. void context_menu(Noh* option);
  23.  
  24. class MenuOption{
  25. private:
  26.  
  27. public:
  28.     char name[16]; // favor não usar o último caractere
  29.     bool leads_to_another_menu;
  30.     Noh* data;
  31.     void select();
  32.     MenuOption(){};
  33.     MenuOption(char* new_name){
  34.         int i = 0;
  35.         for (int i = 0; i < 15; i++){
  36.             name[i] = new_name[i];
  37.         }
  38.         name[16] = '\0';
  39.     };
  40.     ~MenuOption(){};
  41. };
  42.  
  43. void MenuOption::select(){
  44.     if (this->leads_to_another_menu){
  45.         context_menu(data);
  46.     }
  47. }
  48.  
  49. class Noh
  50. {
  51. private:
  52.     void* data; // o ponteiro do tipo void pode ser usado pra qualquer propósito
  53. public:
  54.     Noh* next;
  55.     Noh* previous;
  56.     MenuOption* asMenu(){return (MenuOption*)(data);}; // retorna data como um ponteiro para MenuOption
  57.     //Skill* asSkill(){return (Skill*)(data);}; // retorna data como um ponteiro para Skill
  58.     Noh(){
  59.         next = NULL;
  60.         previous = NULL;
  61.         data = NULL;
  62.     };
  63.     Noh(void* new_data){
  64.         data = new_data;
  65.         next = NULL;
  66.         previous = NULL;
  67.     };
  68.     ~Noh(){
  69.         // remove ponteiros que apontam pra este objeto
  70.         if (this->next->previous == this){
  71.             this->next->previous = NULL;
  72.         }
  73.         if (this->previous->next == this){
  74.             this->previous->next = NULL;
  75.         }
  76.  
  77.         delete (Noh*)(data);
  78.     };
  79.  
  80. };
  81.  
  82.  
  83. int read_LCD_buttons()
  84. {/*
  85.     adc_key_in = analogRead(0);      // read the value from the sensor
  86.     if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
  87.     // For V1.1 us this threshold
  88.     if (adc_key_in < 50)   return btnRIGHT;
  89.     if (adc_key_in < 250)  return btnUP;
  90.     if (adc_key_in < 450)  return btnDOWN;
  91.     if (adc_key_in < 650)  return btnLEFT;
  92.     if (adc_key_in < 850)  return btnSELECT;
  93.     // For V1.0 comment the other threshold and use the one below:
  94. /*
  95.     if (adc_key_in < 50)   return btnRIGHT;
  96.     if (adc_key_in < 195)  return btnUP;
  97.     if (adc_key_in < 380)  return btnDOWN;
  98.     if (adc_key_in < 555)  return btnLEFT;
  99.      if (adc_key_in < 790)  return btnSELECT;
  100. */
  101.     char choice;
  102.     cin >> choice;
  103.     if (choice == 'u'){
  104.         return btnUP;
  105.     }
  106.     else if (choice == 'd'){
  107.         return btnDOWN;
  108.     }
  109.     else if (choice == 'l'){
  110.         return btnLEFT;
  111.     }
  112.     else if (choice == 'r'){
  113.         return btnRIGHT;
  114.     }
  115.     else if (choice == 's'){
  116.         return btnSELECT;
  117.     }
  118.     return btnNONE;  // when all others fail, return this...
  119. }
  120.  
  121. void print_menu(Noh* option){
  122.  
  123.     /*
  124.     lcd.setCursor(0, 0);
  125.     lcd.print(option->asMenu()->name);
  126.     lcd.setCursor(15, 0);
  127.     lcd.print(01111111, BIN); // setinha
  128.     lcd.setCursor(0, 1);
  129.     lcd.print(option->next->asMenu()->name);
  130.     */
  131.  
  132.     cout << endl << endl << endl << endl << endl << endl << endl << (option->asMenu()->name);
  133.     cout << (char)(0b01111111) << endl; // setinha
  134.     if (option->next != NULL){
  135.         cout << (option->next->asMenu()->name) << endl;
  136.     }
  137. }
  138.  
  139. void context_menu(Noh* option){
  140.     bool in_context_page = true;
  141.     Noh* last_page = option;
  142.     print_menu(option);
  143.     while (in_context_page){
  144.         switch (read_LCD_buttons()){
  145.             case btnNONE:
  146.                 break;
  147.             case btnUP:
  148.                 // volta para a opção anterior
  149.                 if (option->previous != NULL){
  150.                     option = option->previous;
  151.                 }
  152.                 print_menu(option);
  153.                 break;
  154.             case btnDOWN:
  155.                 // vai para a próxima opção
  156.                 if (option->next != NULL){
  157.                     option = option->next;
  158.                 }
  159.                 print_menu(option);
  160.                 break;
  161.             case btnLEFT:
  162.                 // retorna para a página anterior
  163.                 in_context_page = false;
  164.                 break;
  165.             default:
  166.                 // seleciona a opção atual
  167.                 option->asMenu()->select();
  168.                 print_menu(option);
  169.         }
  170.     }
  171. }
  172.  
  173. void add_menu_option(Noh* node_above, Noh* node_to_be_inserted){
  174.     if (node_above->previous == NULL && node_above->next == NULL){ // transforma o menu em uma lista circular
  175.         node_above->previous = node_above;
  176.         node_above->next = node_above;
  177.     }
  178.     node_to_be_inserted->previous = node_above;
  179.     node_to_be_inserted->next = node_above->next;
  180.     node_above->next = node_to_be_inserted;
  181.     node_to_be_inserted->next->previous = node_to_be_inserted;
  182. }
  183.  
  184. void add_submenu(Noh* node_father, Noh* node_to_be_inserted){
  185.     node_father->asMenu()->leads_to_another_menu = true;
  186.     node_father->asMenu()->data = node_to_be_inserted;
  187. }
  188.  
  189. int main(){
  190.     Noh* first_page = new Noh((void*)(new MenuOption((char*)"Trains")));
  191.     Noh* aux = new Noh(new MenuOption((char*)"I like them!"));
  192.     add_submenu(first_page, aux);
  193.     Noh* aux2 = new Noh(new MenuOption((char*)"..."));
  194.     add_menu_option(aux, aux2);
  195.     aux2 = new Noh(new MenuOption((char*)"Fuooon"));
  196.     add_menu_option(aux, aux2);
  197.     aux = new Noh(new MenuOption((char*)"Muffin"));
  198.     add_menu_option(first_page, aux);
  199.     aux2 = new Noh(new MenuOption((char*)"I wanna die"));
  200.     add_submenu(aux, aux2);
  201.     aux = new Noh(new MenuOption((char*)":D"));
  202.     add_menu_option(aux2, aux);
  203.     aux = new Noh(new MenuOption((char*)"Stegosaurus"));
  204.     add_menu_option(first_page, aux);
  205.     aux = new Noh(new MenuOption((char*)"Real person"));
  206.     add_menu_option(first_page, aux);
  207.     aux2 = new Noh(new MenuOption((char*)"Skateboards"));
  208.     add_submenu(aux, aux2);
  209.     context_menu(first_page);
  210.     return 0;
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement