 #include "ov2640_regs.h"
 
 //#define HOSTCONN
-//#if defined HOSTCONN
-//#include <AltSoftSerial.h>
+//#include <AltSoftSerial.h>  // Uncomment all three lines to enable software serial debugger (bug in IDE)
 //AltSoftSerial SWserial;
-//#endif
 
 void handle_host ();
 void xlog (char * origin, char * text);
 void xlognum (char * origin, char * text, int number);
 void w_handle_line ();
 void rsend (char * msg);
+void send_image_packet();
 
 void state_0_1 ();
 void state_1_2 ();

      }

      if (capture == 2) {
        xlognum ("H","packet",imglen);
        uint8_t temp, temp_last;
        uint32_t len = imglen;
        int found_end = 0;
        if (len > 1024) len = 1024; // Cap this at 1k at a time.
        Serial.print ("AT+CIPSEND=0,");
        Serial.print (len+7);
        Serial.print ("\r\n");
        delay(10);
        Serial.print ("+img:");

        while (len) {
          imglen--;
          temp_last = temp;
          temp = SPI.transfer(0x00);
          Serial.write(temp);
          if ((temp == 0xD9) && (temp_last == 0xFF)) break;
          delayMicroseconds(12);
          len--;
        }
        if (len) {
          xlognum ("H","imgend", len);
          found_end = 1;
          len--;
          while (len--) { Serial.write(0x00); }
        }
        Serial.print ("\r\n");
        xlog("H","done sending");
        state = 8;
        if (found_end || !imglen) {
          xlog ("H","image done");
          cam.CS_HIGH();
          capture = 3;
        }
      }
    }
}

void handle_host () {
//#ifdef HOSTCONN
//      if (SWserial.available() ) {
//      int char_in = SWserial.read();
//      switch (char_in) {
//        case '\n': break; // ignore linefeed
//        case '\r':
//           SWserial.print ("\n\r");
//           h_handle_line();
//           break;
//        default:
//           SWserial.write (char_in);  // Mirror to host for human consumption
//           hlinebuf[hlineoff++] = char_in;
//           if (hlineoff > 254) {
//              xlog("H", "line too long, ignoring");
//              hlineoff = 0;
 //          }
 //       }
 //     }
//#endif
}

void xlog (char * origin, char * text) {
//#ifdef HOSTCONN
//    SWserial.print (origin);
//    SWserial.print (":");
//    SWserial.println (text);
//#endif
}
void xlognum (char * origin, char * text, int number) {
//#ifdef HOSTCONN
//    SWserial.print (origin);
//    SWserial.print (":");
//    SWserial.print (text);
//    SWserial.print (" ");
//    SWserial.println (number);
//#endif
}

void wsend (char * text) {

void setup() 
{
    Serial.begin(115200);  // ESP8266
#if defined HOSTCONN
    SWserial.begin(9600);  // Host connection/log
#endif
    xlog ("H", "starting");
    pinMode(2, OUTPUT);
}
