  * The software responds to the following serial commands:
  *
  * - r:  read all 4 channels, return results immediately via serial
- * - sN.: performs N scans of all 4 channels. Results are stored until p is
- *       issued. Note the full stop, this is required!
- * - p: prints the buffer, used to retrive values from sN
+ * - s:  on receiving this command, the arduino will immediately begin to
+ *       capture data into its buffer when triggered, stopping when the
+ *       buffer is filled.
+ *
+ * - p:  prints the current valid contents of the buffer. One line is emitted
+ *       for each scan of 4 channels, each line containing 4 codes separated
+ *       by space. The last line of buffer content is followed by a line
+ *       containing 'END'. All lines are terminated by '\n'.
  */
 
 #ifndef __builtin_avr_delay_cycles
-void __builtin_avr_delay_cycles(unsigned long __n) {
+inline void __builtin_avr_delay_cycles(unsigned long __n) {
   while(__n) __n--;
 }
 #endif

    Serial.print(read_analog(3));Serial.print(" ");
    Serial.print(read_analog(0));Serial.println();
  } else if (c == 's') {
    // XXX we don't use parseInt beause it takes a second to complete waiting
    // for more user input
    uint16_t nscans = 0;
    while (c != '.') {
      c = Serial.read();
      if (c >= '0') {
        c -= '0';
        nscans *= 10;
        nscans += c;
      }
    }
    scan_channels(nscans);
  } else if (c == 'p') {
    for (int idx = 0; idx < writepos; idx += 4) {
      Serial.print(idx/4);Serial.print(" ");

  // use port manipulation to read the data pins. Note that our pinmapping
  // is such that port C maps exactly onto the parallel output from the
  // ADS7825.
  uint16_t code = PINC;
  code <<=8;

  // now for the low byte
  digitalWrite(_BYTE, HIGH);
  __builtin_avr_delay_cycles(2);
  code |= PINC;

  return code;
}

// buffer for 16bit values from the ADC
volatile int16_t buf[_BUF_SIZE];
volatile uint16_t writepos = 0;
volatile uint8_t trigcnt = 0;
void trig_ISR() {
  if (++trigcnt == _DIVIDER) {
    trigcnt = 0;
    if (writepos < _BUF_SIZE) {
      buf[writepos++] = read_analog(1);
      buf[writepos++] = read_analog(2);
      buf[writepos++] = read_analog(3);
      buf[writepos++] = read_analog(0);
    }
  }
}

void loop() {
  int c = Serial.read();
