From c6ec8cf012270c3100d076b37931a02da7752fe7 Mon Sep 17 00:00:00 2001
From: Jake Read <jake.read@cba.mit.edu>
Date: Thu, 18 Nov 2021 11:04:41 -0500
Subject: [PATCH] new bus-drop code

---
 firmware/stepper-drop/src/main.cpp     |  3 --
 firmware/stepper-drop/src/osape-d51    |  2 +-
 firmware/stepper-drop/src/syserror.cpp | 69 +++++++++++++++++++-------
 firmware/stepper-drop/src/syserror.h   |  5 ++
 4 files changed, 58 insertions(+), 21 deletions(-)

diff --git a/firmware/stepper-drop/src/main.cpp b/firmware/stepper-drop/src/main.cpp
index 77804e1..ee450ca 100644
--- a/firmware/stepper-drop/src/main.cpp
+++ b/firmware/stepper-drop/src/main.cpp
@@ -151,7 +151,6 @@ void setup() {
   ERRLIGHT_SETUP;
   CLKLIGHT_SETUP;
   DEBUG1PIN_SETUP;
-  
   // limit switch 
   //limitSetup();
   // osap
@@ -183,14 +182,12 @@ uint8_t testRx[256];
 void loop() {
   osapLoop();
   stepper_hw->dacRefresh();
-  /*
   // still not sure: this registers as "made" on init (?) 
   if(limitIsMade()){
     ERRLIGHT_ON;
   } else {
     ERRLIGHT_OFF;
   }
-  */
 } // end loop 
 
 
diff --git a/firmware/stepper-drop/src/osape-d51 b/firmware/stepper-drop/src/osape-d51
index eeabe1a..50426e4 160000
--- a/firmware/stepper-drop/src/osape-d51
+++ b/firmware/stepper-drop/src/osape-d51
@@ -1 +1 @@
-Subproject commit eeabe1a173dc1c7270b0099a7a1a0e0e7735e1be
+Subproject commit 50426e4ff771eeb2c94180e59a05258d2daf24ff
diff --git a/firmware/stepper-drop/src/syserror.cpp b/firmware/stepper-drop/src/syserror.cpp
index 6ec72cb..a650704 100644
--- a/firmware/stepper-drop/src/syserror.cpp
+++ b/firmware/stepper-drop/src/syserror.cpp
@@ -1,28 +1,55 @@
 #include "syserror.h"
+#include "indicators.h"
 #include "osape-d51/osape/osap/ts.h"
 #include "osape-d51/vertices/ucBusDrop.h"
 #include "osape-d51/osape/utils/cobs.h"
 #include "config.h"
-#include "indicators.h"
+
 
 uint8_t errBuf[1028];
 uint8_t errEncoded[1028];
 
-/*
-boolean writeString(unsigned char* dest, uint16_t* dptr, String msg){
-  uint16_t len = msg.length();
-  dest[(*dptr) ++] = TS_STRING_KEY;
-  writeLenBytes(dest, dptr, len);
-  msg.getBytes(dest, len + 1);
-  return true;
+volatile unsigned long errLightLastOn = 0;
+volatile unsigned long errLightOnTime = 0;
+volatile boolean errLightBlink = true;
+volatile boolean errLightOn = false;
+
+void sysErrLightFlash(uint8_t level){
+  ERRLIGHT_ON;
+  errLightOn = true;
+  errLightLastOn = millis();
+  switch(level){
+    case 0:
+      errLightBlink = false;
+      break;
+    case 1:
+      errLightOnTime = 5000;
+      break;
+    case 2: 
+      errLightOnTime = 1000;
+      break;
+    case 3: 
+      errLightOnTime = 250;
+      break;
+  }
+}
+
+void sysErrLightCheck(void){
+  if(errLightOn && errLightBlink){
+    if(errLightLastOn + errLightOnTime < millis()){
+      ERRLIGHT_OFF;
+      errLightOn = false;
+    }
+  }
 }
 
-boolean writeLenBytes(unsigned char* dest, uint16_t* dptr, uint16_t len){
-  dest[(*dptr) ++] = len;
-  dest[(*dptr) ++] = (len >> 8) & 255;
-  return true;
+#ifdef UCBUS_IS_DROP 
+
+void sysError(String msg){
+  // noop 
 }
-*/
+
+#else 
 
 // config-your-own-ll-escape-hatch
 void sysError(String msg){
@@ -38,11 +65,18 @@ void sysError(String msg){
   errBuf[6] = (len >> 24) & 255;
   msg.getBytes(&(errBuf[7]), len + 1);
   size_t ecl = cobsEncode(errBuf, len + 7, errEncoded);
-  errEncoded[ecl] = 0; // add delimiter 
-  // escape: 
-  Serial.write(errEncoded, ecl + 1);
+  errEncoded[ecl] = 0;
+  // direct escape 
+  //if(Serial.availableForWrite() > (int64_t)ecl){
+    Serial.write(errEncoded, ecl + 1);
+    //Serial.flush();
+  //} else {
+  //  ERRLIGHT_ON;
+  //}
 }
 
+#endif 
+
 void logPacket(uint8_t* pck, uint16_t len){
   String errmsg;
   errmsg.reserve(1024);
@@ -53,4 +87,5 @@ void logPacket(uint8_t* pck, uint16_t len){
     errmsg += ", ";
   }
   sysError(errmsg);
-}
\ No newline at end of file
+}
+
diff --git a/firmware/stepper-drop/src/syserror.h b/firmware/stepper-drop/src/syserror.h
index 3645dbc..489173a 100644
--- a/firmware/stepper-drop/src/syserror.h
+++ b/firmware/stepper-drop/src/syserror.h
@@ -7,4 +7,9 @@ void sysError(String msg);
 void logPacket(uint8_t* pck, uint16_t len);
 //void sysError(uint8_t* bytes, uint16_t len);
 
+void sysErrLightFlash(uint8_t level);
+void sysErrLightCheck(void);
+
+#define ERROR(level, msg) sysErrLightFlash(level); sysError(msg)
+
 #endif
-- 
GitLab