Backing up an unused program in case of future need

Dependencies:   mbed

Committer:
andrewboyson
Date:
Tue May 03 09:23:26 2016 +0000
Revision:
4:e076884ef8bd
Parent:
3:accba7e07a0d
Child:
6:be97d38e0b01
Added 1-wire

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 3:accba7e07a0d 6 #include "uart.h"
andrewboyson 0:09f915e6f9f6 7
andrewboyson 0:09f915e6f9f6 8 #define RECV_TIMEOUT_MS 5000
andrewboyson 0:09f915e6f9f6 9
andrewboyson 0:09f915e6f9f6 10 //State
andrewboyson 0:09f915e6f9f6 11 #define LINE_LENGTH 256
andrewboyson 0:09f915e6f9f6 12 #define IDLE 0
andrewboyson 0:09f915e6f9f6 13 #define IN_LINE 1
andrewboyson 0:09f915e6f9f6 14 #define IPD_WAIT_START 2
andrewboyson 0:09f915e6f9f6 15 #define IPD_READ 3
andrewboyson 0:09f915e6f9f6 16 #define SEND_DATA 4
andrewboyson 0:09f915e6f9f6 17 static int state; //Initialised in init
andrewboyson 0:09f915e6f9f6 18
andrewboyson 0:09f915e6f9f6 19 //Solicited responses received
andrewboyson 0:09f915e6f9f6 20 //============================
andrewboyson 0:09f915e6f9f6 21 #define LINE_LENGTH 256
andrewboyson 0:09f915e6f9f6 22 int EspLineAvailable; //Initialised in init; can be one of the values defined in esp.h
andrewboyson 0:09f915e6f9f6 23 char EspLine[LINE_LENGTH];
andrewboyson 2:06fa34661f19 24 static char* pLineNext; //Initialised in init to EspLine
andrewboyson 2:06fa34661f19 25
andrewboyson 4:e076884ef8bd 26 static char* pRespBuff;
andrewboyson 4:e076884ef8bd 27 static int lenRespBuff;
andrewboyson 4:e076884ef8bd 28 static char* pRespNext; //Initialised at each request to send a string
andrewboyson 4:e076884ef8bd 29
andrewboyson 2:06fa34661f19 30 static int addRawCharToBuffer(char* pBuff, char** ppNext, int len, char c)
andrewboyson 0:09f915e6f9f6 31 {
andrewboyson 2:06fa34661f19 32
andrewboyson 2:06fa34661f19 33 // if *ppNext is at the last position in pBuff (pBuff+len-1) then we are full and should stop
andrewboyson 2:06fa34661f19 34 if (*ppNext >= pBuff + len - 1) return -1;
andrewboyson 2:06fa34661f19 35
andrewboyson 2:06fa34661f19 36 // Put the char into *ppNext and NUL into *ppNext + 1.
andrewboyson 2:06fa34661f19 37 **ppNext = c;
andrewboyson 2:06fa34661f19 38 ++*ppNext;
andrewboyson 2:06fa34661f19 39 **ppNext = '\0';
andrewboyson 2:06fa34661f19 40
andrewboyson 2:06fa34661f19 41 return 0;
andrewboyson 2:06fa34661f19 42 }
andrewboyson 2:06fa34661f19 43 static int addCharToBuffer(char* pBuff, char** ppNext, int len, char c, int includeCrLf)
andrewboyson 2:06fa34661f19 44 {
andrewboyson 2:06fa34661f19 45 if (!pBuff) return -1;
andrewboyson 0:09f915e6f9f6 46 switch (c)
andrewboyson 0:09f915e6f9f6 47 {
andrewboyson 2:06fa34661f19 48 case '\0':
andrewboyson 2:06fa34661f19 49 if (addRawCharToBuffer(pBuff, ppNext, len, '\\')) return -1;
andrewboyson 2:06fa34661f19 50 if (addRawCharToBuffer(pBuff, ppNext, len, '0' )) return -1; //This is the character zero '0' not NUL '\0'
andrewboyson 0:09f915e6f9f6 51 break;
andrewboyson 0:09f915e6f9f6 52 case '\r':
andrewboyson 0:09f915e6f9f6 53 case '\n':
andrewboyson 2:06fa34661f19 54 if (includeCrLf && addRawCharToBuffer(pBuff, ppNext, len, c)) return -1;
andrewboyson 0:09f915e6f9f6 55 break;
andrewboyson 0:09f915e6f9f6 56 default:
andrewboyson 2:06fa34661f19 57 if (addRawCharToBuffer(pBuff, ppNext, len, c)) return -1;
andrewboyson 0:09f915e6f9f6 58 break;
andrewboyson 0:09f915e6f9f6 59 }
andrewboyson 0:09f915e6f9f6 60 return 0;
andrewboyson 0:09f915e6f9f6 61 }
andrewboyson 2:06fa34661f19 62 static int addChar(char c)
andrewboyson 2:06fa34661f19 63 {
andrewboyson 4:e076884ef8bd 64 int r = addCharToBuffer(EspLine, &pLineNext, LINE_LENGTH, c, false);
andrewboyson 4:e076884ef8bd 65 addCharToBuffer(pRespBuff, &pRespNext, lenRespBuff, c, true );
andrewboyson 2:06fa34661f19 66 return r;
andrewboyson 2:06fa34661f19 67 }
andrewboyson 0:09f915e6f9f6 68 //Unsolicited ntp or http requests
andrewboyson 0:09f915e6f9f6 69 //================================
andrewboyson 0:09f915e6f9f6 70 void *EspIpdBuffer[4];
andrewboyson 0:09f915e6f9f6 71 int EspIpdBufferLen[4];
andrewboyson 0:09f915e6f9f6 72 int EspIpdReserved[4];
andrewboyson 0:09f915e6f9f6 73 int EspIpdLength;
andrewboyson 0:09f915e6f9f6 74 int EspIpdId;
andrewboyson 0:09f915e6f9f6 75 int EspDataAvailable;
andrewboyson 0:09f915e6f9f6 76
andrewboyson 0:09f915e6f9f6 77
andrewboyson 0:09f915e6f9f6 78 //Stuff to send
andrewboyson 0:09f915e6f9f6 79 //=============
andrewboyson 0:09f915e6f9f6 80 int EspLengthToSend;
andrewboyson 0:09f915e6f9f6 81 const void * EspDataToSend;
andrewboyson 0:09f915e6f9f6 82
andrewboyson 4:e076884ef8bd 83 void EspSendStringF(char* respbuff, int resplen, char *fmt, ...)
andrewboyson 0:09f915e6f9f6 84 {
andrewboyson 0:09f915e6f9f6 85 va_list argptr;
andrewboyson 0:09f915e6f9f6 86 va_start(argptr, fmt);
andrewboyson 0:09f915e6f9f6 87 int size = vsnprintf(NULL, 0, fmt, argptr);
andrewboyson 0:09f915e6f9f6 88 char snd[size + 1];
andrewboyson 0:09f915e6f9f6 89 vsprintf(snd, fmt, argptr);
andrewboyson 0:09f915e6f9f6 90 va_end(argptr);
andrewboyson 4:e076884ef8bd 91 EspSendString(respbuff, resplen, snd);
andrewboyson 0:09f915e6f9f6 92 }
andrewboyson 4:e076884ef8bd 93 void EspSendString(char* respbuff, int resplen, char* p)
andrewboyson 0:09f915e6f9f6 94 {
andrewboyson 3:accba7e07a0d 95 //Reset the response buffer
andrewboyson 4:e076884ef8bd 96 pRespBuff = respbuff;
andrewboyson 4:e076884ef8bd 97 pRespNext = respbuff;
andrewboyson 4:e076884ef8bd 98 lenRespBuff = resplen;
andrewboyson 4:e076884ef8bd 99 *pRespNext = '\0';
andrewboyson 3:accba7e07a0d 100
andrewboyson 3:accba7e07a0d 101 //Send the string
andrewboyson 3:accba7e07a0d 102 while(*p) UartSendPush(*p++);
andrewboyson 0:09f915e6f9f6 103 }
andrewboyson 0:09f915e6f9f6 104 void EspSendData(int length, const void * snd)
andrewboyson 0:09f915e6f9f6 105 {
andrewboyson 0:09f915e6f9f6 106 const char* p = (char*)snd;
andrewboyson 0:09f915e6f9f6 107 const char* e = (char*)snd + length;
andrewboyson 3:accba7e07a0d 108 while (p < e) UartSendPush(*p++);
andrewboyson 0:09f915e6f9f6 109 }
andrewboyson 0:09f915e6f9f6 110
andrewboyson 0:09f915e6f9f6 111
andrewboyson 0:09f915e6f9f6 112 //Reset ESP8266 zone
andrewboyson 0:09f915e6f9f6 113 #define RESET_TIME_MS 250
andrewboyson 0:09f915e6f9f6 114 static DigitalOut espRunOnHighResetOnLow(p26);
andrewboyson 0:09f915e6f9f6 115 int reset; //Set to 1 by EspReset (and hence EspInit); set to 0 by EspReleasefromReset
andrewboyson 0:09f915e6f9f6 116 void EspResetAndStop()
andrewboyson 0:09f915e6f9f6 117 {
andrewboyson 0:09f915e6f9f6 118 reset = true;
andrewboyson 3:accba7e07a0d 119 UartReset();
andrewboyson 0:09f915e6f9f6 120 pLineNext = EspLine;
andrewboyson 2:06fa34661f19 121 *pLineNext = '\0';
andrewboyson 4:e076884ef8bd 122 pRespBuff = NULL;
andrewboyson 4:e076884ef8bd 123 *pRespNext = NULL;
andrewboyson 4:e076884ef8bd 124 lenRespBuff = 0;
andrewboyson 0:09f915e6f9f6 125 EspLengthToSend = 0;
andrewboyson 0:09f915e6f9f6 126 EspDataToSend = NULL;
andrewboyson 0:09f915e6f9f6 127 state = IDLE;
andrewboyson 0:09f915e6f9f6 128 }
andrewboyson 0:09f915e6f9f6 129 void EspReleaseFromReset(void)
andrewboyson 0:09f915e6f9f6 130 {
andrewboyson 0:09f915e6f9f6 131 reset = false;
andrewboyson 0:09f915e6f9f6 132 }
andrewboyson 0:09f915e6f9f6 133 void pulseResetPin()
andrewboyson 0:09f915e6f9f6 134 {
andrewboyson 0:09f915e6f9f6 135 static Timer resetTimer;
andrewboyson 0:09f915e6f9f6 136
andrewboyson 0:09f915e6f9f6 137 if (reset)
andrewboyson 0:09f915e6f9f6 138 {
andrewboyson 0:09f915e6f9f6 139 resetTimer.stop();
andrewboyson 0:09f915e6f9f6 140 resetTimer.reset();
andrewboyson 0:09f915e6f9f6 141 espRunOnHighResetOnLow = 0;
andrewboyson 0:09f915e6f9f6 142 }
andrewboyson 0:09f915e6f9f6 143 else
andrewboyson 0:09f915e6f9f6 144 {
andrewboyson 0:09f915e6f9f6 145 resetTimer.start();
andrewboyson 0:09f915e6f9f6 146 if (resetTimer.read_ms() > RESET_TIME_MS)
andrewboyson 0:09f915e6f9f6 147 {
andrewboyson 0:09f915e6f9f6 148 resetTimer.stop();
andrewboyson 0:09f915e6f9f6 149 espRunOnHighResetOnLow = 1;
andrewboyson 0:09f915e6f9f6 150 }
andrewboyson 0:09f915e6f9f6 151 }
andrewboyson 0:09f915e6f9f6 152 }
andrewboyson 0:09f915e6f9f6 153 //General commands
andrewboyson 2:06fa34661f19 154 int EspInit()
andrewboyson 0:09f915e6f9f6 155 {
andrewboyson 0:09f915e6f9f6 156 EspResetAndStop();
andrewboyson 3:accba7e07a0d 157 UartBaud(CfgBaud);
andrewboyson 0:09f915e6f9f6 158 for (int i = 0; i < 4; i++)
andrewboyson 0:09f915e6f9f6 159 {
andrewboyson 0:09f915e6f9f6 160 EspIpdBuffer[i] = NULL;
andrewboyson 0:09f915e6f9f6 161 EspIpdBufferLen[i] = 0;
andrewboyson 0:09f915e6f9f6 162 EspIpdReserved[i] = 0;
andrewboyson 0:09f915e6f9f6 163 }
andrewboyson 2:06fa34661f19 164 return 0;
andrewboyson 0:09f915e6f9f6 165 }
andrewboyson 0:09f915e6f9f6 166
andrewboyson 0:09f915e6f9f6 167 //Main loop zone
andrewboyson 0:09f915e6f9f6 168 int handleCharacter(char c)
andrewboyson 0:09f915e6f9f6 169 {
andrewboyson 0:09f915e6f9f6 170 //Static variables. Initialised on first load.
andrewboyson 0:09f915e6f9f6 171 static char ipdHeader[20];
andrewboyson 0:09f915e6f9f6 172 static char * pipdHeader; //Set to ipdHeader when a '+' is received
andrewboyson 0:09f915e6f9f6 173 static int bytesRcvd; //Set to 0 when the ':' following the +IPD is received
andrewboyson 0:09f915e6f9f6 174 static void * pData; //Set to EspIpdBuffer[EspIpdId] when the ':' following the +IPD is received
andrewboyson 0:09f915e6f9f6 175
andrewboyson 0:09f915e6f9f6 176 switch(state)
andrewboyson 0:09f915e6f9f6 177 {
andrewboyson 0:09f915e6f9f6 178 case IDLE:
andrewboyson 0:09f915e6f9f6 179 if (c == '+')
andrewboyson 0:09f915e6f9f6 180 {
andrewboyson 0:09f915e6f9f6 181 pipdHeader = ipdHeader;
andrewboyson 0:09f915e6f9f6 182 *pipdHeader = 0;
andrewboyson 0:09f915e6f9f6 183 state = IPD_WAIT_START;
andrewboyson 0:09f915e6f9f6 184 }
andrewboyson 0:09f915e6f9f6 185 else if (c == '>')
andrewboyson 0:09f915e6f9f6 186 {
andrewboyson 0:09f915e6f9f6 187 state = SEND_DATA;
andrewboyson 0:09f915e6f9f6 188 }
andrewboyson 0:09f915e6f9f6 189 else
andrewboyson 0:09f915e6f9f6 190 {
andrewboyson 0:09f915e6f9f6 191 pLineNext = EspLine;
andrewboyson 0:09f915e6f9f6 192 *pLineNext = 0;
andrewboyson 2:06fa34661f19 193 int r = addChar(c);
andrewboyson 0:09f915e6f9f6 194 if (r)
andrewboyson 0:09f915e6f9f6 195 {
andrewboyson 0:09f915e6f9f6 196 EspLineAvailable = ESP_OVERFLOW;
andrewboyson 0:09f915e6f9f6 197 state = IDLE;
andrewboyson 0:09f915e6f9f6 198 }
andrewboyson 0:09f915e6f9f6 199 else
andrewboyson 0:09f915e6f9f6 200 {
andrewboyson 0:09f915e6f9f6 201 state = IN_LINE;
andrewboyson 0:09f915e6f9f6 202 }
andrewboyson 0:09f915e6f9f6 203 }
andrewboyson 0:09f915e6f9f6 204 break;
andrewboyson 0:09f915e6f9f6 205 case IN_LINE:
andrewboyson 0:09f915e6f9f6 206 if (c == '\n')
andrewboyson 0:09f915e6f9f6 207 {
andrewboyson 0:09f915e6f9f6 208 EspLineAvailable = ESP_AVAILABLE;
andrewboyson 0:09f915e6f9f6 209 state = IDLE;
andrewboyson 0:09f915e6f9f6 210 }
andrewboyson 0:09f915e6f9f6 211 else
andrewboyson 0:09f915e6f9f6 212 {
andrewboyson 2:06fa34661f19 213 int r = addChar(c);
andrewboyson 0:09f915e6f9f6 214 if (r)
andrewboyson 0:09f915e6f9f6 215 {
andrewboyson 0:09f915e6f9f6 216 EspLineAvailable = ESP_OVERFLOW;
andrewboyson 0:09f915e6f9f6 217 state = IDLE;
andrewboyson 0:09f915e6f9f6 218 }
andrewboyson 0:09f915e6f9f6 219 }
andrewboyson 0:09f915e6f9f6 220 break;
andrewboyson 0:09f915e6f9f6 221 case IPD_WAIT_START:
andrewboyson 0:09f915e6f9f6 222 if (pipdHeader == ipdHeader && c != 'I') //If the first character after the '+' is not 'I' then start a line instead
andrewboyson 0:09f915e6f9f6 223 {
andrewboyson 0:09f915e6f9f6 224 pLineNext = EspLine;
andrewboyson 2:06fa34661f19 225 addChar('+');
andrewboyson 2:06fa34661f19 226 addChar(c);
andrewboyson 0:09f915e6f9f6 227 state = IN_LINE;
andrewboyson 0:09f915e6f9f6 228 }
andrewboyson 0:09f915e6f9f6 229 else
andrewboyson 0:09f915e6f9f6 230 {
andrewboyson 0:09f915e6f9f6 231 *pipdHeader++ = c;
andrewboyson 0:09f915e6f9f6 232 *pipdHeader = 0;
andrewboyson 0:09f915e6f9f6 233 if (c == ':')
andrewboyson 0:09f915e6f9f6 234 {
andrewboyson 0:09f915e6f9f6 235 sscanf(ipdHeader, "IPD,%d,%d:", &EspIpdId, &EspIpdLength);
andrewboyson 0:09f915e6f9f6 236 bytesRcvd = 0;
andrewboyson 0:09f915e6f9f6 237 pData = EspIpdBuffer[EspIpdId];
andrewboyson 0:09f915e6f9f6 238 state = IPD_READ;
andrewboyson 0:09f915e6f9f6 239 }
andrewboyson 0:09f915e6f9f6 240 }
andrewboyson 0:09f915e6f9f6 241 break;
andrewboyson 0:09f915e6f9f6 242 case IPD_READ:
andrewboyson 0:09f915e6f9f6 243 bytesRcvd++;
andrewboyson 0:09f915e6f9f6 244 if (bytesRcvd <= EspIpdBufferLen[EspIpdId])
andrewboyson 0:09f915e6f9f6 245 {
andrewboyson 0:09f915e6f9f6 246 *(char *)pData = c;
andrewboyson 0:09f915e6f9f6 247 pData = (char *)pData + 1;
andrewboyson 0:09f915e6f9f6 248 }
andrewboyson 0:09f915e6f9f6 249 if (bytesRcvd == EspIpdLength)
andrewboyson 0:09f915e6f9f6 250 {
andrewboyson 0:09f915e6f9f6 251 if (bytesRcvd <= EspIpdBufferLen[EspIpdId]) EspDataAvailable = ESP_AVAILABLE;
andrewboyson 0:09f915e6f9f6 252 else EspDataAvailable = ESP_OVERFLOW;
andrewboyson 0:09f915e6f9f6 253 state = IDLE;
andrewboyson 0:09f915e6f9f6 254 }
andrewboyson 0:09f915e6f9f6 255 break;
andrewboyson 0:09f915e6f9f6 256 case SEND_DATA:
andrewboyson 0:09f915e6f9f6 257 EspSendData(EspLengthToSend, EspDataToSend);
andrewboyson 0:09f915e6f9f6 258 state = IDLE;
andrewboyson 0:09f915e6f9f6 259 break;
andrewboyson 0:09f915e6f9f6 260 default:
andrewboyson 0:09f915e6f9f6 261 LogF("Unknown state %d\n\r", state);
andrewboyson 0:09f915e6f9f6 262 return -1;
andrewboyson 0:09f915e6f9f6 263 }
andrewboyson 0:09f915e6f9f6 264 return 0;
andrewboyson 0:09f915e6f9f6 265 }
andrewboyson 0:09f915e6f9f6 266 int isTimeout()
andrewboyson 0:09f915e6f9f6 267 {
andrewboyson 0:09f915e6f9f6 268 static Timer receiveTimer;
andrewboyson 0:09f915e6f9f6 269
andrewboyson 0:09f915e6f9f6 270 if (state == IDLE)
andrewboyson 0:09f915e6f9f6 271 {
andrewboyson 0:09f915e6f9f6 272 receiveTimer.stop();
andrewboyson 0:09f915e6f9f6 273 receiveTimer.reset();
andrewboyson 0:09f915e6f9f6 274 }
andrewboyson 0:09f915e6f9f6 275 else
andrewboyson 0:09f915e6f9f6 276 {
andrewboyson 0:09f915e6f9f6 277 receiveTimer.start();
andrewboyson 0:09f915e6f9f6 278 if (receiveTimer.read_ms() > RECV_TIMEOUT_MS) return true;
andrewboyson 0:09f915e6f9f6 279 }
andrewboyson 0:09f915e6f9f6 280 return false;
andrewboyson 0:09f915e6f9f6 281 }
andrewboyson 0:09f915e6f9f6 282 int EspMain()
andrewboyson 0:09f915e6f9f6 283 {
andrewboyson 0:09f915e6f9f6 284
andrewboyson 0:09f915e6f9f6 285 pulseResetPin();
andrewboyson 0:09f915e6f9f6 286
andrewboyson 0:09f915e6f9f6 287 //Reset line availability one shot
andrewboyson 0:09f915e6f9f6 288 EspLineAvailable = ESP_IDLE;
andrewboyson 0:09f915e6f9f6 289 EspDataAvailable = ESP_IDLE;
andrewboyson 0:09f915e6f9f6 290
andrewboyson 0:09f915e6f9f6 291 if (isTimeout())
andrewboyson 0:09f915e6f9f6 292 {
andrewboyson 0:09f915e6f9f6 293 if (state == IN_LINE) EspLineAvailable = ESP_TIMEOUT;
andrewboyson 0:09f915e6f9f6 294 else EspDataAvailable = ESP_TIMEOUT;
andrewboyson 0:09f915e6f9f6 295 state = IDLE;
andrewboyson 0:09f915e6f9f6 296 return 0;
andrewboyson 0:09f915e6f9f6 297 }
andrewboyson 0:09f915e6f9f6 298
andrewboyson 0:09f915e6f9f6 299 //Handle any incoming characters
andrewboyson 3:accba7e07a0d 300 int c = UartRecvPull();
andrewboyson 0:09f915e6f9f6 301 if (c != EOF)
andrewboyson 0:09f915e6f9f6 302 {
andrewboyson 0:09f915e6f9f6 303 LogPush(c);
andrewboyson 0:09f915e6f9f6 304 int r = handleCharacter(c); //This will set the EspAvailable one-shots
andrewboyson 0:09f915e6f9f6 305 if (r) return -1;
andrewboyson 0:09f915e6f9f6 306 }
andrewboyson 0:09f915e6f9f6 307
andrewboyson 0:09f915e6f9f6 308 return 0;
andrewboyson 0:09f915e6f9f6 309 }
andrewboyson 0:09f915e6f9f6 310
andrewboyson 0:09f915e6f9f6 311
andrewboyson 0:09f915e6f9f6 312