From 1c23a613af673e3f53059c48fd545c8a4d418b90 Mon Sep 17 00:00:00 2001
From: Jake Read <jake.read@cba.mit.edu>
Date: Wed, 16 Oct 2019 11:43:29 -0400
Subject: [PATCH] lsq update cycles

---
 hunks/calibrations/lsq.js | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)

diff --git a/hunks/calibrations/lsq.js b/hunks/calibrations/lsq.js
index e66aea8..c8ccf65 100644
--- a/hunks/calibrations/lsq.js
+++ b/hunks/calibrations/lsq.js
@@ -23,6 +23,7 @@ export default function LoadcellCalibration() {
   let loadReading = new Output('number', 'prediction', this)
   this.outputs.push(loadReading)
 
+  let isOkToCalc = false
   // admit unwillingness to write nice array wrappers,
   // do that locally ..
   let xStateArray = new State('string', 'csv: readings: x', '0, 250, 500')
@@ -33,39 +34,47 @@ export default function LoadcellCalibration() {
     let exes = xStateArray.value.split(',')
     let whys = yStateArray.value.split(',')
     // haha, sorry
-    for(let x in exes){
+    for (let x in exes) {
       exes[x] = parseInt(exes[x])
     }
-    for(let y in whys){
+    for (let y in whys) {
       whys[y] = parseFloat(whys[y])
     }
-    if(exes.length === whys.length && exes.length > 2){
+    if (exes.length === whys.length && exes.length > 2) {
       // we gucc 2 lsq
       let lsqr = smallmath.lsq(exes, whys)
       m = lsqr.m
       b = lsqr.b
-      if(b >= 0){
+      isOkToCalc = true
+      if (b >= 0) {
         return `${m}x + ${b}`
       } else {
         return `${m}x ${b}`
       }
     } else {
-      return 'bad inputs...'
+      isOkToCalc = false
+      return 'bad inputs ...'
     }
   }
   let result = new State('string', 'calibration result', writeCalExpression())
   this.states.push(xStateArray, yStateArray, result)
-
-  let isOkToCalc = false
+  xStateArray.onChange = (value) => {
+    xStateArray.set(value)
+    result.set(writeCalExpression())
+  }
+  yStateArray.onChange = (value) => {
+    yStateArray.set(value)
+    result.set(writeCalExpression())
+  }
 
   this.init = () => {
     result.set(writeCalExpression())
   }
 
   this.loop = () => {
-    if(loadCellInput.io()){
-      if(isOkToCalc){
-        if(!loadReading.io()){
+    if (loadCellInput.io()) {
+      if (isOkToCalc) {
+        if (!loadReading.io()) {
           // proceeeed
         }
       } else {
-- 
GitLab