From 4dae5384f60d032418a57918f5463781da16de09 Mon Sep 17 00:00:00 2001
From: spencerkee <spencerkee@users.noreply.github.com>
Date: Mon, 24 Mar 2025 19:54:12 +0000
Subject: [PATCH] Get it working by removing the threshold and adding
 everything

---
 app.js | 203 +++++++++++++++++++++++++++++++++------------------------
 1 file changed, 117 insertions(+), 86 deletions(-)

diff --git a/app.js b/app.js
index 94f05ee..0694787 100644
--- a/app.js
+++ b/app.js
@@ -73,18 +73,81 @@ const recordingButtons = [
   // recordStart,
 ]
 
+// TODO fix weird indexes
 const codeDataButtonIndexes = [
   [14, 19], // Neutral
-  [14, 19], // Up
-  [14, 19], // Down
-  [15, 19], // Left
-  [15, 19], // Right
+  [0, 0], // Up
+  [0, 0], // Down
+  [14, 19], // Left
+  [14, 19], // Right
   [0, 5],   // A
   [0, 5],   // B
   // [0, 5],   // Select
   // [0, 5],   // Start
 ]
 
+let codeDataButtonSubstrings = [
+  // Indexes 15-19 inclusive for neutral input
+  [
+    // "OONYS",
+    // "OOQRS",
+    // "OPQRS",
+    // "OOQ/S",
+    // "OOI3S",
+    // "OPQ/S",
+    // "OOZ6S",
+    // "OOPOS",
+    // "OPNYS",
+    // "OOO5S",
+    // "OOI3S",
+    // "OOO5S",
+  ], // Neutral
+  [], // Up
+  [], // Down
+  [
+    // "OP3%S",
+    // "OP9OS",
+    // "OOP8S",
+    // "OOI3S",
+    // "OP9+S",
+    // "OP8$S",
+    // "OP8$S",
+    // "OP98S",
+  ], // Left
+  [
+    // "OO$:S",
+    // "OPJMS",
+    // "OOZ6S",
+    // "OPZ6S",
+    // "OPCHS",
+    // "OPD S",
+    // "OOD S",
+    // "OPGTS",
+    // "OO PS",
+    // "OOYJS",
+    // "OOFAS",
+    // "OPH0S",
+    // "OPDGS",
+    // "OPDKS",
+    // "OPXWS",
+    // "OPU*S",
+    // "OOGTS",
+    // "OODKS",
+  ], // Right
+  // Indexes 0-5 inclusive
+  [
+    // "A7H",
+    // "ANH",
+    // "A8+",
+    // "A8H",
+    // "A9H",
+    // "AO+",
+  ], // A
+  [], // B
+  // [], // Select
+  // [], // Start
+]
+
 let recordingButtonIsHeldArr = [
   false, // recordNetural
   false, // recordUp
@@ -138,9 +201,9 @@ recordingButtons.forEach((button, index) => {
     const sum = counts.reduce((partialSum, a) => partialSum + a, 0);
     let threshold = 0;
     if (index === 0) {
-      threshold = sum * 0.90;
+      threshold = sum * 1.00;
     } else {
-      threshold = sum * 0.40;
+      threshold = sum * 1.00;
     }
     // Get sorted list of recordedData for this button
     let sortedRecordedData = Object.entries(relevantRecordedData).sort((a, b) => b[1].count - a[1].count);
@@ -154,28 +217,56 @@ recordingButtons.forEach((button, index) => {
         codeDataButtonIndexes[index][1]
       ), data[0], data[1], getButtonReadouts(data[0])];
     });
-
     let filteredAnnotatedVals = annotatedVals.filter((relevantSlice, codeData, count, readouts) => {
-      if (codeDataButtonSubstrings[index].includes(relevantSlice)) {
-        console.log(`Found ${relevantSlice} in codeDataButtonSubstrings[${index}]`);
-        return false;
-      }
-      for (let i = 0; i < codeDataButtonSubstrings[index].length; i++) {
-        // Skip self.
-        if (i === index) {
-          continue
+      /* 
+      index=4 means that the user clicked the "Right+" button meaning they're calibrating the right input.
+      relevantSlice="qwer", the substring that we just recorded
+      codeDataButtonSubstrings is the array of arrays of button substrings
+      codeDataButtonIndexes is the array of arrays of button indexes, i.e. 14-19 corresponds to neutral, left, and right.
+      This is a filter block,
+        we're first getting our start and stop indices from codeDataButtonIndexes[index]
+        then we get the codeDataButtonIndexes indicies that are the same as our start and stop indices from codeDataButtonIndexes like 0, 3, 4
+        then look through codeDataButtonSubstrings[0, 3, 4] and return false if relevantSlice is found in any of them
+      otherwise return true
+      */
+      let ourStartStop = codeDataButtonIndexes[index]; // [14, 19]
+      let matchingStartStopIndexes = [];
+      for (let i = 0; i < codeDataButtonIndexes.length; i++) {
+        if (codeDataButtonIndexes[i] === ourStartStop) {
+          matchingStartStopIndexes.push(i);
         }
-        let sliceStartStop = codeDataButtonIndexes[i];
-        // Check if indexes are irrelevant.
-        if (!sliceStartStop === codeDataButtonIndexes[index]) {
-          continue;
-        }
-        // Check if relevantSlice is in found for another button
-        if (codeDataButtonSubstrings[index].includes(relevantSlice)) {
-          console.log(`Found ${relevantSlice} in codeDataButtonSubstrings[${i}]`);
+      }
+
+      matchingStartStopIndexes.forEach(buttonIndex => {
+        if (codeDataButtonSubstrings[buttonIndex].includes(relevantSlice)) {
+          console.log(`Found ${relevantSlice} in codeDataButtonSubstrings[${buttonIndex}], skipping`);
           return false;
         }
-      }
+      });
+
+      // // Check to see if we're already using this substring in the same button.
+      // if (codeDataButtonSubstrings[index].includes(relevantSlice)) {
+      //   console.log(`Found ${relevantSlice} in codeDataButtonSubstrings[${index}]`);
+      //   return false;
+      // }
+      // // Check to see if we're already using this substring in a different button.
+      // let allSubstringsForCurrentButton = codeDataButtonSubstrings[index];
+      // for (let i = 0; i < allSubstringsForCurrentButton.length; i++) {
+      //   // Skip self.
+      //   if (i === index) {
+      //     continue
+      //   }
+      //   let sliceStartStop = codeDataButtonIndexes[i];
+      //   // Check if indexes are irrelevant.
+      //   if (!sliceStartStop === codeDataButtonIndexes[index]) {
+      //     continue;
+      //   }
+      //   // Check if relevantSlice is in found for another button
+      //   if (codeDataButtonSubstrings[index].includes(relevantSlice)) {
+      //     console.log(`Found ${relevantSlice} in codeDataButtonSubstrings[${i}]`);
+      //     return false;
+      //   }
+      // }
       return true;
     });
     // Create accumulator and iterate over sortedRecordedData and filter out values
@@ -193,6 +284,8 @@ recordingButtons.forEach((button, index) => {
 
     // debugger;
     // let stringBestValues = bestValues.map(a => a[0]);
+    // TODO bestValues can contain duplicates, removing here
+    bestValues = bestValues.filter((value, index, self) => self.indexOf(value) === index);
     bestValues.forEach(relevantSlice => {
       console.log(`Adding ${relevantSlice} to codeDataButtonSubstrings[${index}]`);
       codeDataButtonSubstrings[index].push(relevantSlice);
@@ -390,68 +483,6 @@ navigator.mediaDevices
     requestAnimationFrame(tick);
   });
 
-let codeDataButtonSubstrings = [
-  // Indexes 15-19 inclusive for neutral input
-  [
-    "OONYS",
-    "OOQRS",
-    "OPQRS",
-    "OOQ/S",
-    "OOI3S",
-    "OPQ/S",
-    "OOZ6S",
-    "OOPOS",
-    "OPNYS",
-    "OOO5S",
-    "OOI3S",
-    "OOO5S",
-  ], // Neutral
-  [], // Up
-  [], // Down
-  [
-    // "OP3%S",
-    // "OP9OS",
-    // "OOP8S",
-    // "OOI3S",
-    // "OP9+S",
-    // "OP8$S",
-    // "OP8$S",
-    // "OP98S",
-  ], // Left
-  [
-    // "OO$:S",
-    // "OPJMS",
-    // "OOZ6S",
-    // "OPZ6S",
-    // "OPCHS",
-    // "OPD S",
-    // "OOD S",
-    // "OPGTS",
-    // "OO PS",
-    // "OOYJS",
-    // "OOFAS",
-    // "OPH0S",
-    // "OPDGS",
-    // "OPDKS",
-    // "OPXWS",
-    // "OPU*S",
-    // "OOGTS",
-    // "OODKS",
-  ], // Right
-  // Indexes 0-5 inclusive
-  [
-    // "A7H",
-    // "ANH",
-    // "A8+",
-    // "A8H",
-    // "A9H",
-    // "AO+",
-  ], // A
-  [], // B
-  // [], // Select
-  // [], // Start
-]
-
 // Returns an object which contains booleans for each button
 function getButtonReadouts(codeData) {
   let localQr = {
-- 
GitLab