Advertisement
Space-G

Untitled

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