From 6dcdd6319728354985f42e0981ba1e1520f0289a Mon Sep 17 00:00:00 2001
From: Jake Read <jake.read@cba.mit.edu>
Date: Fri, 1 Apr 2022 14:04:58 -0400
Subject: [PATCH] move stepper-drop to new subs

---
 .gitmodules                                   | 12 ++++
 firmware/stepper-drop/src/config.h            | 22 ------
 firmware/stepper-drop/src/drivers/dacs.h      |  2 +-
 .../stepper-drop/src/drivers/step_a4950.h     |  2 +-
 firmware/stepper-drop/src/homing.cpp          | 12 ++--
 firmware/stepper-drop/src/homing.h            |  4 +-
 firmware/stepper-drop/src/main.cpp            | 59 ++++++++--------
 firmware/stepper-drop/src/osap_config.h       | 33 +++++++++
 .../src/{syserror.cpp => osap_debug.cpp}      | 68 +++++++++----------
 firmware/stepper-drop/src/osap_debug.h        | 34 ++++++++++
 firmware/stepper-drop/src/osape               |  1 +
 firmware/stepper-drop/src/osape_arduino       |  1 +
 firmware/stepper-drop/src/osape_ucbus         |  1 +
 firmware/stepper-drop/src/syserror.h          | 15 ----
 firmware/stepper-drop/src/ucbus_config.h      | 27 ++++++++
 firmware/stepper-drop/src/utils_samd51        |  1 +
 16 files changed, 181 insertions(+), 113 deletions(-)
 delete mode 100644 firmware/stepper-drop/src/config.h
 create mode 100644 firmware/stepper-drop/src/osap_config.h
 rename firmware/stepper-drop/src/{syserror.cpp => osap_debug.cpp} (52%)
 create mode 100644 firmware/stepper-drop/src/osap_debug.h
 create mode 160000 firmware/stepper-drop/src/osape
 create mode 160000 firmware/stepper-drop/src/osape_arduino
 create mode 160000 firmware/stepper-drop/src/osape_ucbus
 delete mode 100644 firmware/stepper-drop/src/syserror.h
 create mode 100644 firmware/stepper-drop/src/ucbus_config.h
 create mode 160000 firmware/stepper-drop/src/utils_samd51

diff --git a/.gitmodules b/.gitmodules
index 66af72e..8aae449 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -7,3 +7,15 @@
 [submodule "firmware/osape-smoothieroll-drop-stepper/src/osape-d51"]
 	path = firmware/osape-smoothieroll-drop-stepper/src/osape-d51
 	url = ssh://git@gitlab.cba.mit.edu:846/jakeread/osape-d51.git
+[submodule "firmware/stepper-drop/src/osape"]
+	path = firmware/stepper-drop/src/osape
+	url = git@github.com:jakeread/osape.git
+[submodule "firmware/stepper-drop/src/osape_ucbus"]
+	path = firmware/stepper-drop/src/osape_ucbus
+	url = git@github.com:jakeread/osape_ucbus.git
+[submodule "firmware/stepper-drop/src/osape_arduino"]
+	path = firmware/stepper-drop/src/osape_arduino
+	url = git@github.com:jakeread/osape_arduino.git
+[submodule "firmware/stepper-drop/src/utils_samd51"]
+	path = firmware/stepper-drop/src/utils_samd51
+	url = git@github.com:jakeread/utils_samd51.git
diff --git a/firmware/stepper-drop/src/config.h b/firmware/stepper-drop/src/config.h
deleted file mode 100644
index 05c5713..0000000
--- a/firmware/stepper-drop/src/config.h
+++ /dev/null
@@ -1,22 +0,0 @@
-// compile-time configur8or 
-#ifndef CONFIG_H_
-#define CONFIG_H_
-
-// for the bus: head, or drop? 
-//#define UCBUS_IS_HEAD
-// *genuine* max is 63 ! 
-#define UCBUS_MAX_DROPS 32
-#define UCBUS_IS_DROP
-// and D21 or D51?
-#define UCBUS_IS_D51
-//#define UCBUS_IS_D21 
-
-// 3, 2, or 1 MBaud: 
-#define UCBUS_BAUD 2
-
-// if you're using the 'module board' https://gitlab.cba.mit.edu/jakeread/ucbus-module
-// the first (og) revision has an SMT header, and some of the RS485 pins are varied, 
-// set this flag. otherwise, if you have thru-hole JTAG header, comment it out 
-//#define IS_OG_MODULE 
-
-#endif 
\ No newline at end of file
diff --git a/firmware/stepper-drop/src/drivers/dacs.h b/firmware/stepper-drop/src/drivers/dacs.h
index 196984e..f3ce989 100644
--- a/firmware/stepper-drop/src/drivers/dacs.h
+++ b/firmware/stepper-drop/src/drivers/dacs.h
@@ -18,7 +18,7 @@ is; no warranty is provided, and users accept all liability.
 #include <arduino.h>
 
 #include "indicators.h"
-#include "../syserror.h"
+#include "../osap_debug.h"
 
 // scrape https://github.com/adafruit/ArduinoCore-samd/blob/master/cores/arduino/wiring_analog.c
 // scrape https://github.com/adafruit/ArduinoCore-samd/blob/master/cores/arduino/startup.c (clock)
diff --git a/firmware/stepper-drop/src/drivers/step_a4950.h b/firmware/stepper-drop/src/drivers/step_a4950.h
index 1f6e606..370e0c8 100644
--- a/firmware/stepper-drop/src/drivers/step_a4950.h
+++ b/firmware/stepper-drop/src/drivers/step_a4950.h
@@ -19,7 +19,7 @@ is; no warranty is provided, and users accept all liability.
 
 #include "dacs.h"
 #include "indicators.h"
-#include "../syserror.h"
+#include "../osap_debug.h"
 
 // C_SCALE 
 // 1: DACs go 0->512 (of 4096, peak current is 1.6A at 4096): 0.2A
diff --git a/firmware/stepper-drop/src/homing.cpp b/firmware/stepper-drop/src/homing.cpp
index a2b782c..ffdbea3 100644
--- a/firmware/stepper-drop/src/homing.cpp
+++ b/firmware/stepper-drop/src/homing.cpp
@@ -15,7 +15,7 @@ is; no warranty is provided, and users accept all liability.
 #include "homing.h"
 #include "drivers/step_a4950.h"
 
-endpoint_t* _homeStateEP;
+Endpoint* _homeStateEP;
 uint8_t homeState = HOMESTATE_NONE;
 uint32_t homeBackoffStepsTaken = 0;
 
@@ -25,7 +25,7 @@ unsigned long lastHomeOperation = 0;
 unsigned long homeOperationPeriod = 1000; // in us 
 uint32_t homeBackoffDistance = 100;
 
-void homeSetup(endpoint_t* homeStateEP){
+void homeSetup(Endpoint* homeStateEP){
     // stash this 
     _homeStateEP = homeStateEP;
     // make an input 
@@ -52,7 +52,7 @@ uint8_t getHomeState(void){
 
 void startHomingRoutine(void){
     homeState = HOMESTATE_APPROACH;
-    endpointWrite(_homeStateEP, &homeState, 1);
+    _homeStateEP->write(&homeState, 1);
 }
 
 void runHomingRoutine(void){
@@ -67,7 +67,7 @@ void runHomingRoutine(void){
       // check for contact, 
       if(limitHit()){
         homeState = HOMESTATE_BACKOFF;
-        endpointWrite(_homeStateEP, &homeState, 1);
+        _homeStateEP->write(&homeState, 1);
         homeBackoffStepsTaken = 0;
       } else {
         stepper_hw->dir(homeDir);
@@ -80,13 +80,13 @@ void runHomingRoutine(void){
       homeBackoffStepsTaken ++;
       if(homeBackoffStepsTaken > homeBackoffDistance){
         homeState = HOMESTATE_NONE;
-        endpointWrite(_homeStateEP, &homeState, 1);
+        _homeStateEP->write(&homeState, 1);
       }
       break;
     default:
       // broken, 
       homeState = HOMESTATE_NONE;
-      endpointWrite(_homeStateEP, &homeState, 1);
+      _homeStateEP->write(&homeState, 1);
       break;
   }
 }
\ No newline at end of file
diff --git a/firmware/stepper-drop/src/homing.h b/firmware/stepper-drop/src/homing.h
index e0249bd..5dbb764 100644
--- a/firmware/stepper-drop/src/homing.h
+++ b/firmware/stepper-drop/src/homing.h
@@ -16,7 +16,7 @@ is; no warranty is provided, and users accept all liability.
 #define HOMING_H_ 
 
 #include <Arduino.h>
-#include "osape-d51/osape/osap/endpoint.h"
+#include "osape/vertices/endpoint.h"
 
 // limit 
 #define LIMIT_PORT 0 
@@ -27,7 +27,7 @@ is; no warranty is provided, and users accept all liability.
 #define HOMESTATE_APPROACH 1
 #define HOMESTATE_BACKOFF 2
 
-void homeSetup(endpoint_t* homeStateEP);
+void homeSetup(Endpoint* homeStateEP);
 boolean limitHit(void);
 uint8_t getHomeState(void);
 void writeHomeSettings(boolean dir, uint32_t stepsPerSecond, uint32_t offset);
diff --git a/firmware/stepper-drop/src/main.cpp b/firmware/stepper-drop/src/main.cpp
index 706fc85..fb1e22c 100644
--- a/firmware/stepper-drop/src/main.cpp
+++ b/firmware/stepper-drop/src/main.cpp
@@ -3,15 +3,30 @@
 #include "indicators.h"
 #include "drivers/step_a4950.h"
 #include "homing.h"
-#include "osape-d51/osape/osap/osap.h"
-#include "osape-d51/vertices/vt_usbSerial.h"
-#include "osape-d51/vertices/ucbus/vt_ucBusDrop.h"
-#include "osape-d51/vertices/ucbus/ucBusDrop.h"
+#include "osape/core/osap.h"
+#include "osape/vertices/endpoint.h"
+#include "osape_arduino/vp_arduinoSerial.h"
+#include "osape_ucbus/vb_ucBusDrop.h"
 
 // bare defaults: use vm / bus id to set on startup 
 uint8_t axisPick = 0;
 boolean invert = false;
 
+// root
+
+OSAP osap("stepper-drop");
+
+// vport, vbus, 
+
+VPort_ArduinoSerial vpUSBSer(&osap, "arduinoUSBSerial", &Serial);
+
+VBus vb_ucBusDrop(
+  &osap, "ucBusDrop", 
+  &vb_ucBusDrop_loop,
+  &vb_ucBusDrop_send,
+  &vb_ucBusDrop_cts
+);
+
 // -------------------------------------------------------- AXIS PICK EP 
 
 EP_ONDATA_RESPONSES onAxisPickData(uint8_t* data, uint16_t len){
@@ -23,7 +38,7 @@ EP_ONDATA_RESPONSES onAxisPickData(uint8_t* data, uint16_t len){
   return EP_ONDATA_ACCEPT;
 }
 
-endpoint_t* axisPickEp = osapBuildEndpoint("axisPick", onAxisPickData);
+Endpoint axisPickEp(&osap, "axisPick", onAxisPickData);
 
 // -------------------------------------------------------- AXIS INVERSION EP
 
@@ -32,7 +47,7 @@ EP_ONDATA_RESPONSES onAxisInvertData(uint8_t* data, uint16_t len){
   return EP_ONDATA_ACCEPT;
 }
 
-endpoint_t* axisInvertEp = osapBuildEndpoint("axisInvert", onAxisInvertData);
+Endpoint axisInvertEp(&osap, "axisInvert", onAxisInvertData);
 
 // -------------------------------------------------------- MICROSTEP EP 
 
@@ -41,7 +56,7 @@ EP_ONDATA_RESPONSES onMicrostepData(uint8_t* data, uint16_t len){
   return EP_ONDATA_ACCEPT;
 }
 
-endpoint_t* microstepEp = osapBuildEndpoint("microstep", onMicrostepData);
+Endpoint microstepEp(&osap, "microstep", onMicrostepData);
 
 // -------------------------------------------------------- CSCALE DATA
 
@@ -56,7 +71,7 @@ EP_ONDATA_RESPONSES onCScaleData(uint8_t* data, uint16_t len){
   return EP_ONDATA_ACCEPT;
 }
 
-endpoint_t* cScaleEp = osapBuildEndpoint("CScale", onCScaleData);
+Endpoint cScaleEp(&osap, "CScale", onCScaleData);
 
 // -------------------------------------------------------- HOME ROUTINE 
 
@@ -78,11 +93,11 @@ EP_ONDATA_RESPONSES onHomeData(uint8_t* data, uint16_t len){
   return EP_ONDATA_ACCEPT;
 }
 
-endpoint_t* homeEp = osapBuildEndpoint("Home", onHomeData);
+Endpoint homeEp(&osap, "Home", onHomeData);
 
 // -------------------------------------------------------- HOME STATE 
 
-endpoint_t* homeStateEp = osapBuildEndpoint("HomeState");
+Endpoint homeStateEp(&osap, "HomeState");
 
 // -------------------------------------------------------- SETUP 
 
@@ -90,25 +105,9 @@ void setup() {
   ERRLIGHT_SETUP;
   CLKLIGHT_SETUP;
   //DEBUG1PIN_SETUP;
-  homeSetup(homeStateEp);
-  // osap
-  osapSetup("ucbus-stepper");
-  // ports 
-  vt_usbSerial_setup();
-  osapAddVertex(vt_usbSerial);    // 0
-  vt_ucBusDrop_setup(true, 0);
-  osapAddVertex(vt_ucBusDrop);    // 1
-  // axis pick 
-  osapAddEndpoint(axisPickEp);      // 2
-  // axis invert
-  osapAddEndpoint(axisInvertEp);    // 3
-  // microstep 
-  osapAddEndpoint(microstepEp);     // 4
-  // cscale 
-  osapAddEndpoint(cScaleEp);        // 5
-  // homing 
-  osapAddEndpoint(homeEp);          // 6 
-  osapAddEndpoint(homeStateEp);     // 7 
+  homeSetup(&homeStateEp);
+  // links... 
+  vpUSBSer.begin();
   // stepper init 
   stepper_hw->init(false, 0.0F);
 }
@@ -116,7 +115,7 @@ void setup() {
 // -------------------------------------------------------- LOOP 
 
 void loop() {
-  osapLoop();
+  osap.loop();
   stepper_hw->dacRefresh();
   limitHit() ? ERRLIGHT_ON : ERRLIGHT_OFF;
 } // end loop 
diff --git a/firmware/stepper-drop/src/osap_config.h b/firmware/stepper-drop/src/osap_config.h
new file mode 100644
index 0000000..1898a3a
--- /dev/null
+++ b/firmware/stepper-drop/src/osap_config.h
@@ -0,0 +1,33 @@
+/*
+osap_config.h
+
+config options for an osap-embedded build 
+
+Jake Read at the Center for Bits and Atoms
+(c) Massachusetts Institute of Technology 2022
+
+This work may be reproduced, modified, distributed, performed, and
+displayed for any purpose, but must acknowledge the osap project.
+Copyright is retained and must be preserved. The work is provided as is;
+no warranty is provided, and users accept all liability.
+*/
+
+#ifndef OSAP_CONFIG_H_
+#define OSAP_CONFIG_H_
+
+// size of vertex stacks, lenght, then count,
+#define VT_SLOTSIZE 256
+#define VT_STACKSIZE 3  // must be >= 2 for ringbuffer operation 
+#define VT_MAXCHILDREN 16
+#define VT_MAXITEMSPERTURN 8
+
+// max # of endpoints that could be spawned here,
+#define MAX_CONTEXT_ENDPOINTS 64
+
+// count of routes each endpoint can have, 
+#define ENDPOINT_MAX_ROUTES 4
+
+// if this is defined, please also provide an osap_debug.h 
+#define OSAP_DEBUG 
+
+#endif 
\ No newline at end of file
diff --git a/firmware/stepper-drop/src/syserror.cpp b/firmware/stepper-drop/src/osap_debug.cpp
similarity index 52%
rename from firmware/stepper-drop/src/syserror.cpp
rename to firmware/stepper-drop/src/osap_debug.cpp
index bdf769f..dd93309 100644
--- a/firmware/stepper-drop/src/syserror.cpp
+++ b/firmware/stepper-drop/src/osap_debug.cpp
@@ -1,10 +1,21 @@
-#include "syserror.h"
-#include "indicators.h"
-#include "osape-d51/osape/osap/ts.h"
-#include "osape-d51/vertices/ucbus/ucBusDrop.h"
-#include "osape-d51/osape/utils/cobs.h"
-#include "config.h"
+/*
+osap_debug.cpp
+
+optional escape hatches & indicators 
+
+Jake Read at the Center for Bits and Atoms
+(c) Massachusetts Institute of Technology 2020
 
+This work may be reproduced, modified, distributed, performed, and
+displayed for any purpose, but must acknowledge the osap project.
+Copyright is retained and must be preserved. The work is provided as is;
+no warranty is provided, and users accept all liability.
+*/
+
+#include "osap_debug.h"
+#include "./osape/core/ts.h"
+#include "./osape/utils/cobs.h"
+#include "indicators.h"
 
 uint8_t errBuf[1028];
 uint8_t errEncoded[1028];
@@ -43,39 +54,25 @@ void sysErrLightCheck(void){
   }
 }
 
-#ifdef UCBUS_IS_DROP 
-
-void sysError(String msg){
-  // noop 
-}
-
-#else 
-
 // config-your-own-ll-escape-hatch
-void sysError(String msg){
+void debugPrint(String msg){
   // whatever you want,
-  //ERRLIGHT_ON;
   uint32_t len = msg.length();
-  errBuf[0] = 0; // serport looks for acks in each msg, this is not one
-  errBuf[1] = PK_PTR; 
-  errBuf[2] = PK_LLESCAPE_KEY; // the ll-errmsg-key
-  errBuf[3] = len & 255;
-  errBuf[4] = (len >> 8) & 255;
-  errBuf[5] = (len >> 16) & 255;
-  errBuf[6] = (len >> 24) & 255;
-  msg.getBytes(&(errBuf[7]), len + 1);
-  size_t ecl = cobsEncode(errBuf, len + 7, errEncoded);
-  errEncoded[ecl] = 0;
+  errBuf[0] = len + 8;  // len, key, cobs start + end, strlen (4) 
+  errBuf[1] = 172;      // serialLink debug key 
+  errBuf[2] = len & 255;
+  errBuf[3] = (len >> 8) & 255;
+  errBuf[4] = (len >> 16) & 255;
+  errBuf[5] = (len >> 24) & 255;
+  msg.getBytes(&(errBuf[6]), len + 1);
+  size_t ecl = cobsEncode(&(errBuf[2]), len + 4, errEncoded);
+  memcpy(&(errBuf[2]), errEncoded, ecl);
+  errBuf[errBuf[0] - 1] = 0;
   // direct escape 
-  //if(Serial.availableForWrite() > (int64_t)ecl){
-    Serial.write(errEncoded, ecl + 1);
-    //Serial.flush();
-  //} else {
-  //  ERRLIGHT_ON;
-  //}
+  Serial.write(errBuf, errBuf[0]);
 }
 
-#endif 
+//#endif 
 
 void logPacket(uint8_t* pck, uint16_t len){
   String errmsg;
@@ -86,6 +83,5 @@ void logPacket(uint8_t* pck, uint16_t len){
     errmsg += String(pck[i]);
     errmsg += ", ";
   }
-  sysError(errmsg);
-}
-
+  debugPrint(errmsg);
+}
\ No newline at end of file
diff --git a/firmware/stepper-drop/src/osap_debug.h b/firmware/stepper-drop/src/osap_debug.h
new file mode 100644
index 0000000..3c23680
--- /dev/null
+++ b/firmware/stepper-drop/src/osap_debug.h
@@ -0,0 +1,34 @@
+/*
+osap_debug.h
+
+optional escape hatches & indicators 
+
+Jake Read at the Center for Bits and Atoms
+(c) Massachusetts Institute of Technology 2020
+
+This work may be reproduced, modified, distributed, performed, and
+displayed for any purpose, but must acknowledge the osap project.
+Copyright is retained and must be preserved. The work is provided as is;
+no warranty is provided, and users accept all liability.
+*/
+
+#ifndef OSAP_DEBUG_H_
+#define OSAP_DEBUG_H_
+
+#include <Arduino.h>
+
+void debugPrint(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); debugPrint(msg)
+#define DEBUG(msg) debugPrint(msg)
+
+#define ERRLIGHT_ON digitalWrite(5, HIGH)
+#define ERRLIGHT_OFF digitalWrite(5, LOW)
+#define ERRLIGHT_TOGGLE digitalWrite(5, !digitalRead(5))
+
+#endif 
\ No newline at end of file
diff --git a/firmware/stepper-drop/src/osape b/firmware/stepper-drop/src/osape
new file mode 160000
index 0000000..d3aa968
--- /dev/null
+++ b/firmware/stepper-drop/src/osape
@@ -0,0 +1 @@
+Subproject commit d3aa968e42184b4b4eccc76dda05d30f4b374288
diff --git a/firmware/stepper-drop/src/osape_arduino b/firmware/stepper-drop/src/osape_arduino
new file mode 160000
index 0000000..95d5806
--- /dev/null
+++ b/firmware/stepper-drop/src/osape_arduino
@@ -0,0 +1 @@
+Subproject commit 95d580692135b43a98ee2f087a1bc51e3cfe1fcb
diff --git a/firmware/stepper-drop/src/osape_ucbus b/firmware/stepper-drop/src/osape_ucbus
new file mode 160000
index 0000000..994f762
--- /dev/null
+++ b/firmware/stepper-drop/src/osape_ucbus
@@ -0,0 +1 @@
+Subproject commit 994f7626eecf15d7db38949b309e2551411abe0d
diff --git a/firmware/stepper-drop/src/syserror.h b/firmware/stepper-drop/src/syserror.h
deleted file mode 100644
index 489173a..0000000
--- a/firmware/stepper-drop/src/syserror.h
+++ /dev/null
@@ -1,15 +0,0 @@
-#ifndef SYSERROR_H_
-#define SYSERROR_H_
-
-#include <Arduino.h>
-
-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
diff --git a/firmware/stepper-drop/src/ucbus_config.h b/firmware/stepper-drop/src/ucbus_config.h
new file mode 100644
index 0000000..5c5fd6b
--- /dev/null
+++ b/firmware/stepper-drop/src/ucbus_config.h
@@ -0,0 +1,27 @@
+/*
+ucbus_confi.h
+
+config options for an ucbus instance 
+
+Jake Read at the Center for Bits and Atoms
+(c) Massachusetts Institute of Technology 2022
+
+This work may be reproduced, modified, distributed, performed, and
+displayed for any purpose, but must acknowledge the osap project.
+Copyright is retained and must be preserved. The work is provided as is;
+no warranty is provided, and users accept all liability.
+*/
+
+#ifndef UCBUS_CONFIG_H_
+#define UCBUS_CONFIG_H_
+
+#define UCBUS_MAX_DROPS 32 
+#define UCBUS_IS_DROP 
+//#define UCBUS_IS_HEAD 
+
+#define UCBUS_BAUD 2 
+
+#define UCBUS_IS_D51
+// #define UCBUS_IS_D21
+
+#endif 
\ No newline at end of file
diff --git a/firmware/stepper-drop/src/utils_samd51 b/firmware/stepper-drop/src/utils_samd51
new file mode 160000
index 0000000..d77c3a9
--- /dev/null
+++ b/firmware/stepper-drop/src/utils_samd51
@@ -0,0 +1 @@
+Subproject commit d77c3a9993a908c52ef53efd69d407be3c288e7e
-- 
GitLab