Advertisement
Space-G

GUI Funcionando Arduino

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