MenuFunctions.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #include "MenuFunctions.h"
  2. MenuFunctions::MenuFunctions()
  3. {
  4. //Serial.println("Initializing Menu Object...");
  5. }
  6. // Function to check menu input
  7. void MenuFunctions::main()
  8. {
  9. // This is code from bodmer's keypad example
  10. uint16_t t_x = 0, t_y = 0; // To store the touch coordinates
  11. // Get the display buffer out of the way
  12. if (wifi_scan_obj.currentScanMode != WIFI_SCAN_OFF)
  13. display_obj.displayBuffer();
  14. //else
  15. // display_obj.displayBuffer(true);
  16. //Serial.println("Cycle");
  17. // Pressed will be set true is there is a valid touch on the screen
  18. //vTaskDelay(10 / portTICK_PERIOD_MS);
  19. boolean pressed = display_obj.tft.getTouch(&t_x, &t_y);
  20. //boolean pressed = false;
  21. // This is if there are scans going on
  22. if ((wifi_scan_obj.currentScanMode != WIFI_SCAN_OFF) && (pressed))
  23. //if ((wifi_scan_obj.currentScanMode != WIFI_SCAN_OFF) && (x != -1) && (y != -1))
  24. {
  25. // Stop the current scan
  26. if ((wifi_scan_obj.currentScanMode == WIFI_SCAN_PROBE) ||
  27. (wifi_scan_obj.currentScanMode == WIFI_SCAN_AP) ||
  28. (wifi_scan_obj.currentScanMode == WIFI_SCAN_ST) ||
  29. (wifi_scan_obj.currentScanMode == WIFI_SCAN_ALL) ||
  30. (wifi_scan_obj.currentScanMode == BT_SCAN_ALL) ||
  31. (wifi_scan_obj.currentScanMode == BT_SCAN_SKIMMERS))
  32. {
  33. Serial.println("Stopping scan...");
  34. wifi_scan_obj.StartScan(WIFI_SCAN_OFF);
  35. // If we don't do this, the text and button coordinates will be off
  36. display_obj.tft.init();
  37. // Take us back to the menu
  38. changeMenu(current_menu);
  39. }
  40. x = -1;
  41. y = -1;
  42. return;
  43. }
  44. // / Check if any key coordinate boxes contain the touch coordinates
  45. for (uint8_t b = 0; b < BUTTON_ARRAY_LEN; b++) {
  46. if (pressed && key[b].contains(t_x, t_y)) {
  47. key[b].press(true); // tell the button it is pressed
  48. } else {
  49. key[b].press(false); // tell the button it is NOT pressed
  50. }
  51. }
  52. // Check if any key has changed state
  53. for (uint8_t b = 0; b < BUTTON_ARRAY_LEN; b++) {
  54. display_obj.tft.setFreeFont(MENU_FONT);
  55. if (key[b].justPressed()) {
  56. key[b].drawButton2(current_menu->list->get(b).name, true); // draw invert
  57. }
  58. // If button was just release, execute the button's function
  59. if (key[b].justReleased())
  60. {
  61. key[b].drawButton2(current_menu->list->get(b).name); // draw normal
  62. //Serial.print("Executing button -> ");
  63. //Serial.println(current_menu->list->get(b).name);
  64. current_menu->list->get(b).callable();
  65. }
  66. display_obj.tft.setFreeFont(NULL);
  67. }
  68. // This is if there are any scans or sniffs going on
  69. if(digitalRead(FLASH_BUTTON) == 0)
  70. {
  71. // Stop the current scan
  72. if ((wifi_scan_obj.currentScanMode == WIFI_SCAN_PROBE) ||
  73. (wifi_scan_obj.currentScanMode == WIFI_SCAN_AP) ||
  74. (wifi_scan_obj.currentScanMode == WIFI_SCAN_ST) ||
  75. (wifi_scan_obj.currentScanMode == WIFI_SCAN_ALL) ||
  76. (wifi_scan_obj.currentScanMode == BT_SCAN_ALL) ||
  77. (wifi_scan_obj.currentScanMode == BT_SCAN_SKIMMERS))
  78. {
  79. Serial.println("Stopping scan...");
  80. wifi_scan_obj.StartScan(WIFI_SCAN_OFF);
  81. // If we don't do this, the text and button coordinates will be off
  82. display_obj.tft.init();
  83. // Take us back to the menu
  84. changeMenu(current_menu);
  85. }
  86. }
  87. //else
  88. //{
  89. // display_obj.displayBuffer();
  90. //}
  91. x = -1;
  92. y = -1;
  93. }
  94. void MenuFunctions::handlePress(boolean pressed, uint16_t t_x, uint16_t t_y)
  95. {
  96. if ((wifi_scan_obj.currentScanMode != WIFI_SCAN_OFF) && (pressed))
  97. {
  98. // Stop the current scan
  99. if ((wifi_scan_obj.currentScanMode == WIFI_SCAN_PROBE) ||
  100. (wifi_scan_obj.currentScanMode == WIFI_SCAN_AP) ||
  101. (wifi_scan_obj.currentScanMode == WIFI_SCAN_ST) ||
  102. (wifi_scan_obj.currentScanMode == WIFI_SCAN_ALL) ||
  103. (wifi_scan_obj.currentScanMode == BT_SCAN_ALL) ||
  104. (wifi_scan_obj.currentScanMode == BT_SCAN_SKIMMERS))
  105. {
  106. Serial.println("Stopping scan...");
  107. wifi_scan_obj.StartScan(WIFI_SCAN_OFF);
  108. // If we don't do this, the text and button coordinates will be off
  109. display_obj.tft.init();
  110. // Take us back to the menu
  111. changeMenu(current_menu);
  112. }
  113. return;
  114. }
  115. // / Check if any key coordinate boxes contain the touch coordinates
  116. for (uint8_t b = 0; b < BUTTON_ARRAY_LEN; b++) {
  117. if (pressed && key[b].contains(t_x, t_y)) {
  118. key[b].press(true); // tell the button it is pressed
  119. } else {
  120. key[b].press(false); // tell the button it is NOT pressed
  121. }
  122. }
  123. // Check if any key has changed state
  124. for (uint8_t b = 0; b < BUTTON_ARRAY_LEN; b++) {
  125. display_obj.tft.setFreeFont(MENU_FONT);
  126. if (key[b].justPressed()) {
  127. key[b].drawButton2(current_menu->list->get(b).name, true); // draw invert
  128. }
  129. // If button was just release, execute the button's function
  130. if (key[b].justReleased())
  131. {
  132. key[b].drawButton2(current_menu->list->get(b).name); // draw normal
  133. //Serial.print("Executing button -> ");
  134. //Serial.println(current_menu->list->get(b).name);
  135. current_menu->list->get(b).callable();
  136. }
  137. display_obj.tft.setFreeFont(NULL);
  138. }
  139. // This is if there are any scans or sniffs going on
  140. if(digitalRead(FLASH_BUTTON) == 0)
  141. {
  142. // Stop the current scan
  143. if ((wifi_scan_obj.currentScanMode == WIFI_SCAN_PROBE) ||
  144. (wifi_scan_obj.currentScanMode == WIFI_SCAN_AP) ||
  145. (wifi_scan_obj.currentScanMode == WIFI_SCAN_ST) ||
  146. (wifi_scan_obj.currentScanMode == WIFI_SCAN_ALL) ||
  147. (wifi_scan_obj.currentScanMode == BT_SCAN_ALL) ||
  148. (wifi_scan_obj.currentScanMode == BT_SCAN_SKIMMERS))
  149. {
  150. Serial.println("Stopping scan...");
  151. wifi_scan_obj.StartScan(WIFI_SCAN_OFF);
  152. // If we don't do this, the text and button coordinates will be off
  153. display_obj.tft.init();
  154. // Take us back to the menu
  155. changeMenu(current_menu);
  156. }
  157. }
  158. }
  159. // Function to build the menus
  160. void MenuFunctions::RunSetup()
  161. {
  162. mainMenu.list = new SimpleList<MenuNode>(); // Get list in first menu ready
  163. wifiMenu.list = new SimpleList<MenuNode>(); // Get list in second menu ready
  164. bluetoothMenu.list = new SimpleList<MenuNode>(); // Get list in third menu ready
  165. // Build Main Menu
  166. mainMenu.parentMenu = NULL;
  167. addNodes(&mainMenu, "WiFi", TFT_GREEN, NULL, 0, [this](){changeMenu(&wifiMenu);});
  168. addNodes(&mainMenu, "Bluetooth", TFT_CYAN, NULL, 1, [this](){changeMenu(&bluetoothMenu);});
  169. addNodes(&mainMenu, "Pee", TFT_MAGENTA, NULL, 2, NULL);
  170. // Build WiFi Menu
  171. wifiMenu.parentMenu = &mainMenu; // Main Menu is second menu parent
  172. addNodes(&wifiMenu, "Back", TFT_RED, NULL, 0, [this](){changeMenu(wifiMenu.parentMenu);});
  173. addNodes(&wifiMenu, "Probe Request Sniff", TFT_MAGENTA, NULL, 1, [this](){wifi_scan_obj.StartScan(WIFI_SCAN_PROBE);});
  174. addNodes(&wifiMenu, "Beacon Sniff", TFT_BLUE, NULL, 2, [this](){wifi_scan_obj.StartScan(WIFI_SCAN_AP);});
  175. // Build Bluetooth Menu
  176. bluetoothMenu.parentMenu = &mainMenu; // Second Menu is third menu parent
  177. addNodes(&bluetoothMenu, "Back", TFT_RED, NULL, 0, [this](){changeMenu(bluetoothMenu.parentMenu);});
  178. addNodes(&bluetoothMenu, "Bluetooth Sniffer", TFT_GREEN, NULL, 1, [this](){wifi_scan_obj.StartScan(BT_SCAN_ALL);});
  179. addNodes(&bluetoothMenu, "Detect Card Skimmers", TFT_MAGENTA, NULL, 2, [this](){wifi_scan_obj.StartScan(BT_SCAN_SKIMMERS);});
  180. // Set the current menu to the mainMenu
  181. changeMenu(&mainMenu);
  182. // Show the current menu
  183. //Serial.println("\n\nCurrent Menu Tree:");
  184. //Serial.println("-------------------------------");
  185. //showMenuList(current_menu, 0);
  186. //xTaskCreate(&MenuFunctions::getPresses, "getPresses", 2048, NULL, 5, NULL);
  187. }
  188. // Function to change menu
  189. void MenuFunctions::changeMenu(Menu* menu)
  190. {
  191. //display_obj.clearScreen();
  192. display_obj.initScrollValues();
  193. display_obj.setupScrollArea(TOP_FIXED_AREA, BOT_FIXED_AREA);
  194. display_obj.tft.init();
  195. current_menu = menu;
  196. buildButtons(menu);
  197. displayCurrentMenu();
  198. }
  199. // Function to show all MenuNodes in a Menu
  200. void MenuFunctions::showMenuList(Menu* menu, int layer)
  201. {
  202. // Iterate through all of the menu nodes in the menu
  203. for (int i = 0; i < menu->list->size(); i++)
  204. {
  205. // Depending on layer, indent
  206. for (int x = 0; x < layer * 4; x++)
  207. Serial.print(" ");
  208. Serial.print("Node: ");
  209. Serial.println(menu->list->get(i).name);
  210. // If the current menu node points to another menu, list that menu
  211. if (menu->list->get(i).childMenu != NULL)
  212. showMenuList(menu->list->get(i).childMenu, layer+1);
  213. }
  214. Serial.println();
  215. }
  216. // Function to add MenuNodes to a menu
  217. void MenuFunctions::addNodes(Menu* menu, String name, uint16_t color, Menu* child, int place, std::function<void()> callable)
  218. {
  219. TFT_eSPI_Button new_button;
  220. menu->list->add(MenuNode{name, color, child, &new_button, callable});
  221. }
  222. void MenuFunctions::buildButtons(Menu* menu)
  223. {
  224. Serial.println("Bulding buttons...");
  225. //display_obj.clearScreen();
  226. if (menu->list != NULL)
  227. {
  228. for (int i = 0; i < menu->list->size(); i++)
  229. {
  230. TFT_eSPI_Button new_button;
  231. //Serial.print("Building button -> ");
  232. //Serial.println(menu->list->get(i).name);
  233. char buf[menu->list->get(i).name.length() + 1] = {};
  234. menu->list->get(i).name.toCharArray(buf, menu->list->get(i).name.length() + 1);
  235. //for (int x = 0; x < sizeof(buf); x++)
  236. // Serial.print(buf[x]);
  237. //Serial.println();
  238. key[i].initButton(&display_obj.tft,
  239. KEY_X + 0 * (KEY_W + KEY_SPACING_X),
  240. KEY_Y + i * (KEY_H + KEY_SPACING_Y), // x, y, w, h, outline, fill, text
  241. KEY_W,
  242. KEY_H,
  243. TFT_BLACK, // Outline
  244. TFT_BLACK, // Fill
  245. menu->list->get(i).color, // Text
  246. buf,
  247. KEY_TEXTSIZE);
  248. }
  249. }
  250. }
  251. void MenuFunctions::displayCurrentMenu()
  252. {
  253. Serial.println("Displaying current menu...");
  254. display_obj.clearScreen();
  255. display_obj.tft.setTextColor(TFT_LIGHTGREY, TFT_DARKGREY);
  256. display_obj.tft.fillRect(0,0,240,16, TFT_DARKGREY);
  257. display_obj.tft.drawCentreString(" ESP32 Marauder ",120,0,2);
  258. if (current_menu->list != NULL)
  259. {
  260. display_obj.tft.setFreeFont(MENU_FONT);
  261. for (int i = 0; i < current_menu->list->size(); i++)
  262. {
  263. key[i].drawButton2(current_menu->list->get(i).name);
  264. }
  265. display_obj.tft.setFreeFont(NULL);
  266. }
  267. }