Backing up an unused program in case of future need

Dependencies:   mbed

Committer:
andrewboyson
Date:
Fri Apr 22 09:23:57 2016 +0000
Revision:
2:06fa34661f19
Parent:
0:09f915e6f9f6
Child:
3:accba7e07a0d
Added configuration file.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 0:09f915e6f9f6 1 #include "mbed.h"
andrewboyson 2:06fa34661f19 2 #include "log.h"
andrewboyson 2:06fa34661f19 3 #include "esp.h"
andrewboyson 2:06fa34661f19 4 #include "io.h"
andrewboyson 2:06fa34661f19 5 #include "cfg.h"
andrewboyson 0:09f915e6f9f6 6 #include <stdarg.h>
andrewboyson 0:09f915e6f9f6 7
andrewboyson 0:09f915e6f9f6 8
andrewboyson 0:09f915e6f9f6 9 #define RECV_TIMEOUT_MS 5000
andrewboyson 0:09f915e6f9f6 10 static RawSerial esp(p9, p10); // tx, rx
andrewboyson 0:09f915e6f9f6 11
andrewboyson 0:09f915e6f9f6 12 //State
andrewboyson 0:09f915e6f9f6 13 #define LINE_LENGTH 256
andrewboyson 0:09f915e6f9f6 14 #define IDLE 0
andrewboyson 0:09f915e6f9f6 15 #define IN_LINE 1
andrewboyson 0:09f915e6f9f6 16 #define IPD_WAIT_START 2
andrewboyson 0:09f915e6f9f6 17 #define IPD_READ 3
andrewboyson 0:09f915e6f9f6 18 #define SEND_DATA 4
andrewboyson 0:09f915e6f9f6 19 static int state; //Initialised in init
andrewboyson 0:09f915e6f9f6 20
andrewboyson 0:09f915e6f9f6 21 //Solicited responses received
andrewboyson 0:09f915e6f9f6 22 //============================
andrewboyson 0:09f915e6f9f6 23 #define LINE_LENGTH 256
andrewboyson 0:09f915e6f9f6 24 int EspLineAvailable; //Initialised in init; can be one of the values defined in esp.h
andrewboyson 0:09f915e6f9f6 25 char EspLine[LINE_LENGTH];
andrewboyson 2:06fa34661f19 26 static char* pLineNext; //Initialised in init to EspLine
andrewboyson 2:06fa34661f19 27
andrewboyson 2:06fa34661f19 28 #define RESP_LENGTH 256
andrewboyson 2:06fa34661f19 29 char EspResp[RESP_LENGTH];
andrewboyson 2:06fa34661f19 30 static char* pRespNext; //Initialised at each request to EspResp
andrewboyson 2:06fa34661f19 31 static int addRawCharToBuffer(char* pBuff, char** ppNext, int len, char c)
andrewboyson 0:09f915e6f9f6 32 {
andrewboyson 2:06fa34661f19 33
andrewboyson 2:06fa34661f19 34 // if *ppNext is at the last position in pBuff (pBuff+len-1) then we are full and should stop
andrewboyson 2:06fa34661f19 35 if (*ppNext >= pBuff + len - 1) return -1;
andrewboyson 2:06fa34661f19 36
andrewboyson 2:06fa34661f19 37 // Put the char into *ppNext and NUL into *ppNext + 1.
andrewboyson 2:06fa34661f19 38 **ppNext = c;
andrewboyson 2:06fa34661f19 39 ++*ppNext;
andrewboyson 2:06fa34661f19 40 **ppNext = '\0';
andrewboyson 2:06fa34661f19 41
andrewboyson 2:06fa34661f19 42 return 0;
andrewboyson 2:06fa34661f19 43 }
andrewboyson 2:06fa34661f19 44 static int addCharToBuffer(char* pBuff, char** ppNext, int len, char c, int includeCrLf)
andrewboyson 2:06fa34661f19 45 {
andrewboyson 2:06fa34661f19 46 if (!pBuff) return -1;
andrewboyson 0:09f915e6f9f6 47 switch (c)
andrewboyson 0:09f915e6f9f6 48 {
andrewboyson 2:06fa34661f19 49 case '\0':
andrewboyson 2:06fa34661f19 50 if (addRawCharToBuffer(pBuff, ppNext, len, '\\')) return -1;
andrewboyson 2:06fa34661f19 51 if (addRawCharToBuffer(pBuff, ppNext, len, '0' )) return -1; //This is the character zero '0' not NUL '\0'
andrewboyson 0:09f915e6f9f6 52 break;
andrewboyson 0:09f915e6f9f6 53 case '\r':
andrewboyson 0:09f915e6f9f6 54 case '\n':
andrewboyson 2:06fa34661f19 55 if (includeCrLf && addRawCharToBuffer(pBuff, ppNext, len, c)) return -1;
andrewboyson 0:09f915e6f9f6 56 break;
andrewboyson 0:09f915e6f9f6 57 default:
andrewboyson 2:06fa34661f19 58 if (addRawCharToBuffer(pBuff, ppNext, len, c)) return -1;
andrewboyson 0:09f915e6f9f6 59 break;
andrewboyson 0:09f915e6f9f6 60 }
andrewboyson 0:09f915e6f9f6 61 return 0;
andrewboyson 0:09f915e6f9f6 62 }
andrewboyson 2:06fa34661f19 63 static int addChar(char c)
andrewboyson 2:06fa34661f19 64 {
andrewboyson 2:06fa34661f19 65 int r = addCharToBuffer(EspLine, &pLineNext, LINE_LENGTH, c, false);
andrewboyson 2:06fa34661f19 66 addCharToBuffer(EspResp, &pRespNext, RESP_LENGTH, c, true );
andrewboyson 2:06fa34661f19 67 return r;
andrewboyson 2:06fa34661f19 68 }
andrewboyson 0:09f915e6f9f6 69 //Unsolicited ntp or http requests
andrewboyson 0:09f915e6f9f6 70 //================================
andrewboyson 0:09f915e6f9f6 71 void *EspIpdBuffer[4];
andrewboyson 0:09f915e6f9f6 72 int EspIpdBufferLen[4];
andrewboyson 0:09f915e6f9f6 73 int EspIpdReserved[4];
andrewboyson 0:09f915e6f9f6 74 int EspIpdLength;
andrewboyson 0:09f915e6f9f6 75 int EspIpdId;
andrewboyson 0:09f915e6f9f6 76 int EspDataAvailable;
andrewboyson 0:09f915e6f9f6 77
andrewboyson 0:09f915e6f9f6 78 //Wrap around buffers
andrewboyson 0:09f915e6f9f6 79 //===================
andrewboyson 0:09f915e6f9f6 80 #define RECV_BUFFER_LENGTH 1024
andrewboyson 0:09f915e6f9f6 81 #define SEND_BUFFER_LENGTH 1024
andrewboyson 0:09f915e6f9f6 82 static char recvbuffer[RECV_BUFFER_LENGTH];
andrewboyson 0:09f915e6f9f6 83 static char sendbuffer[SEND_BUFFER_LENGTH];
andrewboyson 0:09f915e6f9f6 84 static char* pRecvPush; //Initialised in init
andrewboyson 0:09f915e6f9f6 85 static char* pRecvPull; //Initialised in init
andrewboyson 0:09f915e6f9f6 86 static char* pSendPush; //Initialised in init
andrewboyson 0:09f915e6f9f6 87 static char* pSendPull; //Initialised in init
andrewboyson 0:09f915e6f9f6 88
andrewboyson 2:06fa34661f19 89 static void incrementPushPullPointer(char** pp, char* pbuffer, int bufferlength)
andrewboyson 0:09f915e6f9f6 90 {
andrewboyson 2:06fa34661f19 91 ++*pp; //increment the pointer by one
andrewboyson 2:06fa34661f19 92 if (*pp == pbuffer + bufferlength) *pp = pbuffer; //if the pointer is now beyond the end then point it back to the start
andrewboyson 0:09f915e6f9f6 93 }
andrewboyson 0:09f915e6f9f6 94 static void recvpush(void) //Called by the esp data received interrupt
andrewboyson 0:09f915e6f9f6 95 {
andrewboyson 0:09f915e6f9f6 96 while (esp.readable())
andrewboyson 0:09f915e6f9f6 97 {
andrewboyson 0:09f915e6f9f6 98 int c = esp.getc();
andrewboyson 0:09f915e6f9f6 99 *pRecvPush = c;
andrewboyson 0:09f915e6f9f6 100 incrementPushPullPointer(&pRecvPush, recvbuffer, RECV_BUFFER_LENGTH);
andrewboyson 0:09f915e6f9f6 101 }
andrewboyson 0:09f915e6f9f6 102 }
andrewboyson 0:09f915e6f9f6 103 static int recvpull(void) //Called every scan. Returns the next byte or EOF if no more are available
andrewboyson 0:09f915e6f9f6 104 {
andrewboyson 0:09f915e6f9f6 105 if (pRecvPull == pRecvPush) return EOF;
andrewboyson 0:09f915e6f9f6 106 char c = *pRecvPull;
andrewboyson 0:09f915e6f9f6 107 incrementPushPullPointer(&pRecvPull, recvbuffer, RECV_BUFFER_LENGTH);
andrewboyson 0:09f915e6f9f6 108 return c;
andrewboyson 0:09f915e6f9f6 109 }
andrewboyson 0:09f915e6f9f6 110 static void sendpush(char c) //Called whenever something needs to be sent
andrewboyson 0:09f915e6f9f6 111 {
andrewboyson 0:09f915e6f9f6 112 *pSendPush = c;
andrewboyson 0:09f915e6f9f6 113 incrementPushPullPointer(&pSendPush, sendbuffer, SEND_BUFFER_LENGTH);
andrewboyson 0:09f915e6f9f6 114 }
andrewboyson 0:09f915e6f9f6 115 static int sendpull(void) //Called every scan. Returns the next byte or EOF if no more are available
andrewboyson 0:09f915e6f9f6 116 {
andrewboyson 0:09f915e6f9f6 117 if (pSendPull == pSendPush) return EOF;
andrewboyson 0:09f915e6f9f6 118 char c = *pSendPull;
andrewboyson 0:09f915e6f9f6 119 incrementPushPullPointer(&pSendPull, sendbuffer, SEND_BUFFER_LENGTH);
andrewboyson 0:09f915e6f9f6 120 return c;
andrewboyson 0:09f915e6f9f6 121 }
andrewboyson 0:09f915e6f9f6 122
andrewboyson 0:09f915e6f9f6 123 //Stuff to send
andrewboyson 0:09f915e6f9f6 124 //=============
andrewboyson 0:09f915e6f9f6 125 int EspLengthToSend;
andrewboyson 0:09f915e6f9f6 126 const void * EspDataToSend;
andrewboyson 0:09f915e6f9f6 127
andrewboyson 0:09f915e6f9f6 128 void EspSendStringF(char *fmt, ...)
andrewboyson 0:09f915e6f9f6 129 {
andrewboyson 0:09f915e6f9f6 130 va_list argptr;
andrewboyson 0:09f915e6f9f6 131 va_start(argptr, fmt);
andrewboyson 0:09f915e6f9f6 132 int size = vsnprintf(NULL, 0, fmt, argptr);
andrewboyson 0:09f915e6f9f6 133 char snd[size + 1];
andrewboyson 0:09f915e6f9f6 134 vsprintf(snd, fmt, argptr);
andrewboyson 0:09f915e6f9f6 135 va_end(argptr);
andrewboyson 0:09f915e6f9f6 136 EspSendString(snd);
andrewboyson 0:09f915e6f9f6 137 }
andrewboyson 0:09f915e6f9f6 138 void EspSendString(char* p)
andrewboyson 0:09f915e6f9f6 139 {
andrewboyson 2:06fa34661f19 140 pRespNext = EspResp;
andrewboyson 2:06fa34661f19 141 *pRespNext = '\0';
andrewboyson 0:09f915e6f9f6 142 while(*p) sendpush(*p++);
andrewboyson 0:09f915e6f9f6 143 }
andrewboyson 0:09f915e6f9f6 144 void EspSendData(int length, const void * snd)
andrewboyson 0:09f915e6f9f6 145 {
andrewboyson 0:09f915e6f9f6 146 const char* p = (char*)snd;
andrewboyson 0:09f915e6f9f6 147 const char* e = (char*)snd + length;
andrewboyson 0:09f915e6f9f6 148 while (p < e) sendpush(*p++);
andrewboyson 0:09f915e6f9f6 149 }
andrewboyson 0:09f915e6f9f6 150
andrewboyson 0:09f915e6f9f6 151
andrewboyson 0:09f915e6f9f6 152 //Reset ESP8266 zone
andrewboyson 0:09f915e6f9f6 153 #define RESET_TIME_MS 250
andrewboyson 0:09f915e6f9f6 154 static DigitalOut espRunOnHighResetOnLow(p26);
andrewboyson 0:09f915e6f9f6 155 int reset; //Set to 1 by EspReset (and hence EspInit); set to 0 by EspReleasefromReset
andrewboyson 0:09f915e6f9f6 156 void EspResetAndStop()
andrewboyson 0:09f915e6f9f6 157 {
andrewboyson 0:09f915e6f9f6 158 reset = true;
andrewboyson 0:09f915e6f9f6 159 pRecvPush = recvbuffer;
andrewboyson 0:09f915e6f9f6 160 pRecvPull = recvbuffer;
andrewboyson 0:09f915e6f9f6 161 pSendPush = sendbuffer;
andrewboyson 0:09f915e6f9f6 162 pSendPull = sendbuffer;
andrewboyson 0:09f915e6f9f6 163 pLineNext = EspLine;
andrewboyson 2:06fa34661f19 164 *pLineNext = '\0';
andrewboyson 2:06fa34661f19 165 pRespNext = EspResp;
andrewboyson 2:06fa34661f19 166 *pRespNext = '\0';
andrewboyson 0:09f915e6f9f6 167 EspLengthToSend = 0;
andrewboyson 0:09f915e6f9f6 168 EspDataToSend = NULL;
andrewboyson 0:09f915e6f9f6 169 state = IDLE;
andrewboyson 0:09f915e6f9f6 170 }
andrewboyson 0:09f915e6f9f6 171 void EspReleaseFromReset(void)
andrewboyson 0:09f915e6f9f6 172 {
andrewboyson 0:09f915e6f9f6 173 reset = false;
andrewboyson 0:09f915e6f9f6 174 }
andrewboyson 0:09f915e6f9f6 175 void pulseResetPin()
andrewboyson 0:09f915e6f9f6 176 {
andrewboyson 0:09f915e6f9f6 177 static Timer resetTimer;
andrewboyson 0:09f915e6f9f6 178
andrewboyson 0:09f915e6f9f6 179 if (reset)
andrewboyson 0:09f915e6f9f6 180 {
andrewboyson 0:09f915e6f9f6 181 resetTimer.stop();
andrewboyson 0:09f915e6f9f6 182 resetTimer.reset();
andrewboyson 0:09f915e6f9f6 183 espRunOnHighResetOnLow = 0;
andrewboyson 0:09f915e6f9f6 184 }
andrewboyson 0:09f915e6f9f6 185 else
andrewboyson 0:09f915e6f9f6 186 {
andrewboyson 0:09f915e6f9f6 187 resetTimer.start();
andrewboyson 0:09f915e6f9f6 188 if (resetTimer.read_ms() > RESET_TIME_MS)
andrewboyson 0:09f915e6f9f6 189 {
andrewboyson 0:09f915e6f9f6 190 resetTimer.stop();
andrewboyson 0:09f915e6f9f6 191 espRunOnHighResetOnLow = 1;
andrewboyson 0:09f915e6f9f6 192 }
andrewboyson 0:09f915e6f9f6 193 }
andrewboyson 0:09f915e6f9f6 194 }
andrewboyson 0:09f915e6f9f6 195 //General commands
andrewboyson 2:06fa34661f19 196 int EspInit()
andrewboyson 0:09f915e6f9f6 197 {
andrewboyson 0:09f915e6f9f6 198 EspResetAndStop();
andrewboyson 0:09f915e6f9f6 199 esp.attach(&recvpush, Serial::RxIrq);
andrewboyson 2:06fa34661f19 200 esp.baud(CfgBaud);
andrewboyson 0:09f915e6f9f6 201 for (int i = 0; i < 4; i++)
andrewboyson 0:09f915e6f9f6 202 {
andrewboyson 0:09f915e6f9f6 203 EspIpdBuffer[i] = NULL;
andrewboyson 0:09f915e6f9f6 204 EspIpdBufferLen[i] = 0;
andrewboyson 0:09f915e6f9f6 205 EspIpdReserved[i] = 0;
andrewboyson 0:09f915e6f9f6 206 }
andrewboyson 2:06fa34661f19 207 return 0;
andrewboyson 0:09f915e6f9f6 208 }
andrewboyson 0:09f915e6f9f6 209 void EspBaud(int baud)
andrewboyson 0:09f915e6f9f6 210 {
andrewboyson 0:09f915e6f9f6 211 esp.baud(baud);
andrewboyson 0:09f915e6f9f6 212 }
andrewboyson 0:09f915e6f9f6 213
andrewboyson 0:09f915e6f9f6 214 //Main loop zone
andrewboyson 0:09f915e6f9f6 215 int handleCharacter(char c)
andrewboyson 0:09f915e6f9f6 216 {
andrewboyson 0:09f915e6f9f6 217 //Static variables. Initialised on first load.
andrewboyson 0:09f915e6f9f6 218 static char ipdHeader[20];
andrewboyson 0:09f915e6f9f6 219 static char * pipdHeader; //Set to ipdHeader when a '+' is received
andrewboyson 0:09f915e6f9f6 220 static int bytesRcvd; //Set to 0 when the ':' following the +IPD is received
andrewboyson 0:09f915e6f9f6 221 static void * pData; //Set to EspIpdBuffer[EspIpdId] when the ':' following the +IPD is received
andrewboyson 0:09f915e6f9f6 222
andrewboyson 0:09f915e6f9f6 223 switch(state)
andrewboyson 0:09f915e6f9f6 224 {
andrewboyson 0:09f915e6f9f6 225 case IDLE:
andrewboyson 0:09f915e6f9f6 226 if (c == '+')
andrewboyson 0:09f915e6f9f6 227 {
andrewboyson 0:09f915e6f9f6 228 pipdHeader = ipdHeader;
andrewboyson 0:09f915e6f9f6 229 *pipdHeader = 0;
andrewboyson 0:09f915e6f9f6 230 state = IPD_WAIT_START;
andrewboyson 0:09f915e6f9f6 231 }
andrewboyson 0:09f915e6f9f6 232 else if (c == '>')
andrewboyson 0:09f915e6f9f6 233 {
andrewboyson 0:09f915e6f9f6 234 state = SEND_DATA;
andrewboyson 0:09f915e6f9f6 235 }
andrewboyson 0:09f915e6f9f6 236 else
andrewboyson 0:09f915e6f9f6 237 {
andrewboyson 0:09f915e6f9f6 238 pLineNext = EspLine;
andrewboyson 0:09f915e6f9f6 239 *pLineNext = 0;
andrewboyson 2:06fa34661f19 240 int r = addChar(c);
andrewboyson 0:09f915e6f9f6 241 if (r)
andrewboyson 0:09f915e6f9f6 242 {
andrewboyson 0:09f915e6f9f6 243 EspLineAvailable = ESP_OVERFLOW;
andrewboyson 0:09f915e6f9f6 244 state = IDLE;
andrewboyson 0:09f915e6f9f6 245 }
andrewboyson 0:09f915e6f9f6 246 else
andrewboyson 0:09f915e6f9f6 247 {
andrewboyson 0:09f915e6f9f6 248 state = IN_LINE;
andrewboyson 0:09f915e6f9f6 249 }
andrewboyson 0:09f915e6f9f6 250 }
andrewboyson 0:09f915e6f9f6 251 break;
andrewboyson 0:09f915e6f9f6 252 case IN_LINE:
andrewboyson 0:09f915e6f9f6 253 if (c == '\n')
andrewboyson 0:09f915e6f9f6 254 {
andrewboyson 0:09f915e6f9f6 255 EspLineAvailable = ESP_AVAILABLE;
andrewboyson 0:09f915e6f9f6 256 state = IDLE;
andrewboyson 0:09f915e6f9f6 257 }
andrewboyson 0:09f915e6f9f6 258 else
andrewboyson 0:09f915e6f9f6 259 {
andrewboyson 2:06fa34661f19 260 int r = addChar(c);
andrewboyson 0:09f915e6f9f6 261 if (r)
andrewboyson 0:09f915e6f9f6 262 {
andrewboyson 0:09f915e6f9f6 263 EspLineAvailable = ESP_OVERFLOW;
andrewboyson 0:09f915e6f9f6 264 state = IDLE;
andrewboyson 0:09f915e6f9f6 265 }
andrewboyson 0:09f915e6f9f6 266 }
andrewboyson 0:09f915e6f9f6 267 break;
andrewboyson 0:09f915e6f9f6 268 case IPD_WAIT_START:
andrewboyson 0:09f915e6f9f6 269 if (pipdHeader == ipdHeader && c != 'I') //If the first character after the '+' is not 'I' then start a line instead
andrewboyson 0:09f915e6f9f6 270 {
andrewboyson 0:09f915e6f9f6 271 pLineNext = EspLine;
andrewboyson 2:06fa34661f19 272 addChar('+');
andrewboyson 2:06fa34661f19 273 addChar(c);
andrewboyson 0:09f915e6f9f6 274 state = IN_LINE;
andrewboyson 0:09f915e6f9f6 275 }
andrewboyson 0:09f915e6f9f6 276 else
andrewboyson 0:09f915e6f9f6 277 {
andrewboyson 0:09f915e6f9f6 278 *pipdHeader++ = c;
andrewboyson 0:09f915e6f9f6 279 *pipdHeader = 0;
andrewboyson 0:09f915e6f9f6 280 if (c == ':')
andrewboyson 0:09f915e6f9f6 281 {
andrewboyson 0:09f915e6f9f6 282 sscanf(ipdHeader, "IPD,%d,%d:", &EspIpdId, &EspIpdLength);
andrewboyson 0:09f915e6f9f6 283 bytesRcvd = 0;
andrewboyson 0:09f915e6f9f6 284 pData = EspIpdBuffer[EspIpdId];
andrewboyson 0:09f915e6f9f6 285 state = IPD_READ;
andrewboyson 0:09f915e6f9f6 286 }
andrewboyson 0:09f915e6f9f6 287 }
andrewboyson 0:09f915e6f9f6 288 break;
andrewboyson 0:09f915e6f9f6 289 case IPD_READ:
andrewboyson 0:09f915e6f9f6 290 bytesRcvd++;
andrewboyson 0:09f915e6f9f6 291 if (bytesRcvd <= EspIpdBufferLen[EspIpdId])
andrewboyson 0:09f915e6f9f6 292 {
andrewboyson 0:09f915e6f9f6 293 *(char *)pData = c;
andrewboyson 0:09f915e6f9f6 294 pData = (char *)pData + 1;
andrewboyson 0:09f915e6f9f6 295 }
andrewboyson 0:09f915e6f9f6 296 if (bytesRcvd == EspIpdLength)
andrewboyson 0:09f915e6f9f6 297 {
andrewboyson 0:09f915e6f9f6 298 if (bytesRcvd <= EspIpdBufferLen[EspIpdId]) EspDataAvailable = ESP_AVAILABLE;
andrewboyson 0:09f915e6f9f6 299 else EspDataAvailable = ESP_OVERFLOW;
andrewboyson 0:09f915e6f9f6 300 state = IDLE;
andrewboyson 0:09f915e6f9f6 301 }
andrewboyson 0:09f915e6f9f6 302 break;
andrewboyson 0:09f915e6f9f6 303 case SEND_DATA:
andrewboyson 0:09f915e6f9f6 304 EspSendData(EspLengthToSend, EspDataToSend);
andrewboyson 0:09f915e6f9f6 305 state = IDLE;
andrewboyson 0:09f915e6f9f6 306 break;
andrewboyson 0:09f915e6f9f6 307 default:
andrewboyson 0:09f915e6f9f6 308 LogF("Unknown state %d\n\r", state);
andrewboyson 0:09f915e6f9f6 309 return -1;
andrewboyson 0:09f915e6f9f6 310 }
andrewboyson 0:09f915e6f9f6 311 return 0;
andrewboyson 0:09f915e6f9f6 312 }
andrewboyson 0:09f915e6f9f6 313 void sendAnyData()
andrewboyson 0:09f915e6f9f6 314 {
andrewboyson 0:09f915e6f9f6 315 while(esp.writeable())
andrewboyson 0:09f915e6f9f6 316 {
andrewboyson 0:09f915e6f9f6 317 int c = sendpull();
andrewboyson 0:09f915e6f9f6 318 if (c == EOF) break;
andrewboyson 0:09f915e6f9f6 319 esp.putc(c);
andrewboyson 0:09f915e6f9f6 320 }
andrewboyson 0:09f915e6f9f6 321 }
andrewboyson 0:09f915e6f9f6 322 int isTimeout()
andrewboyson 0:09f915e6f9f6 323 {
andrewboyson 0:09f915e6f9f6 324 static Timer receiveTimer;
andrewboyson 0:09f915e6f9f6 325
andrewboyson 0:09f915e6f9f6 326 if (state == IDLE)
andrewboyson 0:09f915e6f9f6 327 {
andrewboyson 0:09f915e6f9f6 328 receiveTimer.stop();
andrewboyson 0:09f915e6f9f6 329 receiveTimer.reset();
andrewboyson 0:09f915e6f9f6 330 }
andrewboyson 0:09f915e6f9f6 331 else
andrewboyson 0:09f915e6f9f6 332 {
andrewboyson 0:09f915e6f9f6 333 receiveTimer.start();
andrewboyson 0:09f915e6f9f6 334 if (receiveTimer.read_ms() > RECV_TIMEOUT_MS) return true;
andrewboyson 0:09f915e6f9f6 335 }
andrewboyson 0:09f915e6f9f6 336 return false;
andrewboyson 0:09f915e6f9f6 337 }
andrewboyson 0:09f915e6f9f6 338 int EspMain()
andrewboyson 0:09f915e6f9f6 339 {
andrewboyson 0:09f915e6f9f6 340 sendAnyData();
andrewboyson 0:09f915e6f9f6 341
andrewboyson 0:09f915e6f9f6 342 pulseResetPin();
andrewboyson 0:09f915e6f9f6 343
andrewboyson 0:09f915e6f9f6 344 //Reset line availability one shot
andrewboyson 0:09f915e6f9f6 345 EspLineAvailable = ESP_IDLE;
andrewboyson 0:09f915e6f9f6 346 EspDataAvailable = ESP_IDLE;
andrewboyson 0:09f915e6f9f6 347
andrewboyson 0:09f915e6f9f6 348 if (isTimeout())
andrewboyson 0:09f915e6f9f6 349 {
andrewboyson 0:09f915e6f9f6 350 if (state == IN_LINE) EspLineAvailable = ESP_TIMEOUT;
andrewboyson 0:09f915e6f9f6 351 else EspDataAvailable = ESP_TIMEOUT;
andrewboyson 0:09f915e6f9f6 352 state = IDLE;
andrewboyson 0:09f915e6f9f6 353 return 0;
andrewboyson 0:09f915e6f9f6 354 }
andrewboyson 0:09f915e6f9f6 355
andrewboyson 0:09f915e6f9f6 356 //Handle any incoming characters
andrewboyson 0:09f915e6f9f6 357 int c = recvpull();
andrewboyson 0:09f915e6f9f6 358 if (c != EOF)
andrewboyson 0:09f915e6f9f6 359 {
andrewboyson 0:09f915e6f9f6 360 LogPush(c);
andrewboyson 0:09f915e6f9f6 361 int r = handleCharacter(c); //This will set the EspAvailable one-shots
andrewboyson 0:09f915e6f9f6 362 if (r) return -1;
andrewboyson 0:09f915e6f9f6 363 }
andrewboyson 0:09f915e6f9f6 364
andrewboyson 0:09f915e6f9f6 365 return 0;
andrewboyson 0:09f915e6f9f6 366 }
andrewboyson 0:09f915e6f9f6 367
andrewboyson 0:09f915e6f9f6 368
andrewboyson 0:09f915e6f9f6 369