Advertisement
Space-G

Menu de contexto

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