Advertisement
solielios

tank.js (0206)

Feb 6th, 2025
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 🟢 Initialize Tippy.js tooltips for UI hints
  2. tippy('[title]', {
  3.     theme: 'custom',
  4.     arrow: false,
  5.     followCursor: true,
  6.     delay: 100,
  7.     placement: 'top',
  8.     zIndex: 9999999999,
  9.     maxWidth: 400,
  10.     content(reference) {
  11.         const title = reference.getAttribute('title');
  12.         reference.removeAttribute('title'); // Remove default browser tooltips
  13.         return title;
  14.     },
  15. });
  16.  
  17. // 🟢 Firebase Configuration
  18. const firebaseConfig = {
  19.     apiKey: "AIzaSyAjG_MOsClpLU7N7o-D1PkI08_tFO84w-c",
  20.     authDomain: "facerecognition-tank.firebaseapp.com",
  21.     databaseURL: "https://facerecognition-tank-default-rtdb.firebaseio.com",
  22.     projectId: "facerecognition-tank",
  23.     storageBucket: "facerecognition-tank.appspot.com",
  24.     messagingSenderId: "253443593668",
  25.     appId: "1:253443593668:web:735fa5d04d8138cb3d4091",
  26.     measurementId: "G-SQ0XBX3MN2"
  27. };
  28.  
  29. // 🟢 Initialize Firebase
  30. firebase.initializeApp(firebaseConfig);
  31. const auth = firebase.auth();
  32. const database = firebase.database();
  33.  
  34. // Authentication Handling
  35. auth.onAuthStateChanged((user) => {
  36.     if (!user) {
  37.         alert("Please log in to access.");
  38.         window.location.href = "index.html";
  39.     } else {
  40.         loggedOutContent.style.display = "none";
  41.         loggedInContent.style.display = "block";
  42.         loggedInContent.parentElement.style.height = "80px";
  43.     }
  44. });
  45.  
  46. const loggedOutContent = document.getElementById("logged-out-content");
  47. const loggedInContent = document.getElementById("logged-in-content");
  48. const logoutButton = document.getElementById("logout-btn");
  49.  
  50. logoutButton.addEventListener("click", () => {
  51.     auth.signOut()
  52.         .then(() => {
  53.             alert("You have logged out.");
  54.             window.location.href = "index.html";
  55.         })
  56.         .catch((error) => {
  57.             alert("Logout error: " + error.message);
  58.         });
  59. });
  60.  
  61. // 🟢 UI Elements for Sensor Data
  62. const totalSumDisplay = document.getElementById("total-sum-display");
  63. const distanceDisplay = document.getElementById("distance-display");
  64. const speedDisplay = document.getElementById("speed-display"); // New speed display element
  65.  
  66. // 🟢 Tracks active commands (Ensures only one per node)
  67. const activeCommands = { movement: 0, servo: 0, stepper: 0, laser: 0 };
  68.  
  69. // 🟢 Map keys and buttons to their respective nodes
  70. const keyMappings = {
  71.     "w": { value: 1, node: "movement", button: "btn-up" },
  72.     "a": { value: 2, node: "movement", button: "btn-left" },
  73.     "s": { value: 3, node: "movement", button: "btn-down" },
  74.     "d": { value: 4, node: "movement", button: "btn-right" },
  75.     "+": { value: 7, node: "movement", button: "btn-speed-up" }, // Speed Up
  76.     "-": { value: 6, node: "movement", button: "btn-speed-down" }, // Speed Down
  77.     "j": { value: 64, node: "servo", button: "btn-servo-left" },
  78.     "l": { value: 128, node: "servo", button: "btn-servo-right" },
  79.     "r": { value: 16, node: "stepper", button: "btn-stepper-left" },
  80.     "t": { value: 48, node: "stepper", button: "btn-stepper-right" },
  81.     " ": { value: 8, node: "laser", button: "btn-fire" }
  82. };
  83.  
  84. // 🟢 Remove "active" class from all buttons in the same node
  85. function clearActiveButtons(node) {
  86.     Object.values(keyMappings).forEach(mapping => {
  87.         if (mapping.node === node) {
  88.             document.getElementById(mapping.button)?.classList.remove("active-btn");
  89.         }
  90.     });
  91. }
  92.  
  93. // 🟢 Toggle a command ON/OFF (Ensures only one active per node)
  94. function toggleCommand(key) {
  95.     if (!keyMappings[key]) return;
  96.  
  97.     const { value, node, button } = keyMappings[key];
  98.  
  99.     clearActiveButtons(node); // Remove active class from other buttons in the same node
  100.  
  101.     // Toggle the command
  102.     if (activeCommands[node] === value) {
  103.         activeCommands[node] = 0; // Turn OFF
  104.     } else {
  105.         activeCommands[node] = value; // Turn ON
  106.         document.getElementById(button)?.classList.add("active-btn");
  107.     }
  108.  
  109.     updateCommand(node); // Update Firebase
  110. }
  111.  
  112. // 🟢 Send updated command to Firebase
  113. function updateCommand(node) {
  114.     database.ref(`tank/${node}`).set({ key: activeCommands[node] });
  115.     updateTotalSum(); // Update sum after change
  116. }
  117.  
  118. // 🟢 Calculate and update the total sum
  119. function updateTotalSum() {
  120.     let totalSum = Object.values(activeCommands).reduce((acc, val) => acc + val, 0);
  121.    
  122.     database.ref("Node/RX").set({ value: totalSum }); // Update Firebase
  123.     totalSumDisplay.innerText = totalSum; // Update UI
  124. }
  125.  
  126. // 🟢 Keyboard Control: Toggle command when key is pressed
  127. document.addEventListener("keydown", (event) => toggleCommand(event.key.toLowerCase()));
  128.  
  129. // 🟢 Button Click Handling
  130. document.querySelectorAll("button").forEach(button => {
  131.     button.addEventListener("click", event => {
  132.         const buttonMapping = {
  133.             "btn-up": "w",
  134.             "btn-left": "a",
  135.             "btn-down": "s",
  136.             "btn-right": "d",
  137.             "btn-speed-up": "+",
  138.             "btn-speed-down": "-",
  139.             "btn-servo-left": "j",
  140.             "btn-servo-right": "l",
  141.             "btn-stepper-left": "r",
  142.             "btn-stepper-right": "t",
  143.             "btn-fire": " "
  144.         };
  145.         const key = buttonMapping[event.target.id];
  146.         if (key) toggleCommand(key);
  147.     });
  148. });
  149.  
  150. // 🟢 Fetch Sensor Data from Firebase (A & B -> Distance, C -> Speed in HEX)
  151. function updateSensorData() {
  152.     database.ref("Node/TX").on("value", (snapshot) => {
  153.         const data = snapshot.val();
  154.         if (data) {
  155.             const distanceA = data.A;
  156.             const distanceB = data.B;
  157.             const speed = data.C;
  158.  
  159.             // Display distance (concatenated as two hex values)
  160.             distanceDisplay.innerText = `A: ${distanceA}, B: ${distanceB}`;
  161.  
  162.             // Display speed as hex
  163.             speedDisplay.innerText = `${speed}`;
  164.         }
  165.     });
  166. }
  167.  
  168. updateSensorData(); // Run sensor update function
  169.  
  170. // 🟢 Sync Total Sum with Firebase in Real-time
  171. database.ref("Node/RX").on("value", (snapshot) => {
  172.     if (snapshot.val()) {
  173.         totalSumDisplay.innerText = snapshot.val().value;
  174.     }
  175. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement