 //
 // Additionally there's a pushbutton that triggers various test sequences.
 
-// set to add some debug messages to the USB serial port
+// ===========================================================================
+
+// Test Setup instructions (OSX):
+// - Assume Arduino 1.8.6 and Teensyduino are installed.
+// - In the Arduino UI:
+//      Tools -> Board -> Teensy 3.0
+//      Tools -> USB type -> Serial
+// - Verify and upload to the teensy controller.
+// - Still in the arduino UI, choose the serial port
+//      Tools -> Port -> /dev/cu.usbmodemNNN
+// - Then you should be able to run,
+//      Tools -> Serial Monitor
+
+// ===========================================================================
+
+// ASR33 terminal strip 3 & 4 are "tty send"
+// - data sent from the teletype, which is not polarity sensitive
+// - keyboard and tape reader
+// - connect these to the "Rx" (teensy input)
+//
+// ASR33 terminal strip 6 (neg) & 7 (pos) are "tty rcv"
+// - data received by teletype
+// - printer and tape punch
+// - connect these to "Tx" (teensy output)
+
+// ===========================================================================
+
+//#define CONFIG_TIMER_PRESCALE_256
+#include <AltSoftSerial.h>
+
+// pin 20 receive
+// pin 21 transmit
+
+// Uncomment this to run with "debug" messages.  It prints various "helpful" messages to the
+// USB serial port to help with debugging the code.
 // #define DEBUG_ALL
 
-// set for "local" testing (USB-only; e.g. `sudo cu -l cu.usbmodem63191`)
+// Uncomment this to run "testing mode": development without a tty attached
 // #define TESTING
 
 #ifdef TESTING
 #define HWSERIAL Serial
 #else
-// set this to the hardware serial port being used for the TTY
-#define HWSERIAL Serial1
+// Use the modified AltSoftSerial with inverted data frame
+AltSoftSerial altSerial(1,1,true);
+#define HWSERIAL altSerial
 #endif
 
 // set these to the appropriate mode selector pins (DIP switch to ground)
 #define PIN_TX_UCLC        2
 #define PIN_TX_FOLDSLASH   3
 #define PIN_TX_UTF8ARROWS  4
-#define PIN_TX_SPARE2      5
-#define PIN_RX_NLCR        7
-#define PIN_RX_EVENPARITY  8
-#define PIN_RX_DELAYS      9
-#define PIN_RX_SPARE2      10
+#define PIN_TX_8BITCLEAN   5
+#define PIN_RX_8BITCLEAN   7
+#define PIN_RX_NLCR        8
+#define PIN_RX_EVENPARITY  9
+#define PIN_RX_DELAYS      10
 
 // Pushbutton for sending test sequences (momentary switch to ground)
-#define PIN_PUSH_TEST    12
+#define PIN_PUSH_TEST      11
+#define PIN_PUSH_SPARE     12
 
 #define PIN_INTERNAL_LED 13
 
 #define CR '\r'
 #define LF '\n'
 
+// You can tweak the baud rate very slightly here, but it's better to use 110 and
+// fix whatever timing/alignment issues in hardware
+#define BAUDRATE 109
+
+elapsedMillis millisSinceActivity;
+
+
+// Pressing he "test pushbutton" runs a series of printer tests,
+// each press cycles to the next one. 
+// testId is the ID of the currently-running test.
+int testId = 0;
+
+bool isTestMode;
 bool isTxUCLC;
 bool isTxFoldSlash;
 bool isTxUtf8Arrows;
+bool isTx8BitClean;
 bool isRxNLCR;
 bool isRxEvenParity;
 bool isRxDelays;
+bool isRx8BitClean;
 
 int prevUSBByte;
 int prevTTYByte;


    case 3:
      // Space test
      printTTY(" / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /\r");
      for (int i=0; i<72; i+=2) {
        for (int j=0; j<i; j++) {
          sendToTTY(' ');
        }
        sendToTTY('\\');
        sendToTTY(CR);
      }
      sendToTTY(LF);
      break;

    case 4:


    default:
      printTTY("Tests complete.\r\n");
      testId = 0;
      break;
  }
  delay(500);
