Prueba ESP y SMT32f103c8t6

Dependents:   ESP8266_Prueba

Fork of esp8266-driver by ESP8266

Committer:
josuechopite
Date:
Sun Aug 06 16:09:25 2017 +0000
Revision:
1:9e953a34e172
Parent:
0:6946b0b9e323
Prueba

Who changed what in which revision?

UserRevisionLine numberNew contents of line
group-ESP8266 0:6946b0b9e323 1 /* Copyright (c) 2015 ARM Limited
group-ESP8266 0:6946b0b9e323 2 *
group-ESP8266 0:6946b0b9e323 3 * Licensed under the Apache License, Version 2.0 (the "License");
group-ESP8266 0:6946b0b9e323 4 * you may not use this file except in compliance with the License.
group-ESP8266 0:6946b0b9e323 5 * You may obtain a copy of the License at
group-ESP8266 0:6946b0b9e323 6 *
group-ESP8266 0:6946b0b9e323 7 * http://www.apache.org/licenses/LICENSE-2.0
group-ESP8266 0:6946b0b9e323 8 *
group-ESP8266 0:6946b0b9e323 9 * Unless required by applicable law or agreed to in writing, software
group-ESP8266 0:6946b0b9e323 10 * distributed under the License is distributed on an "AS IS" BASIS,
group-ESP8266 0:6946b0b9e323 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
group-ESP8266 0:6946b0b9e323 12 * See the License for the specific language governing permissions and
group-ESP8266 0:6946b0b9e323 13 * limitations under the License.
group-ESP8266 0:6946b0b9e323 14 *
group-ESP8266 0:6946b0b9e323 15 * @section DESCRIPTION
group-ESP8266 0:6946b0b9e323 16 *
group-ESP8266 0:6946b0b9e323 17 * Parser for the AT command syntax
group-ESP8266 0:6946b0b9e323 18 *
group-ESP8266 0:6946b0b9e323 19 */
group-ESP8266 0:6946b0b9e323 20 #ifndef AT_PARSER_H
group-ESP8266 0:6946b0b9e323 21 #define AT_PARSER_H
group-ESP8266 0:6946b0b9e323 22
group-ESP8266 0:6946b0b9e323 23 #include "mbed.h"
group-ESP8266 0:6946b0b9e323 24 #include <cstdarg>
group-ESP8266 0:6946b0b9e323 25 #include <vector>
group-ESP8266 0:6946b0b9e323 26 #include "BufferedSerial.h"
group-ESP8266 0:6946b0b9e323 27 #include "Callback.h"
group-ESP8266 0:6946b0b9e323 28
group-ESP8266 0:6946b0b9e323 29
group-ESP8266 0:6946b0b9e323 30 /**
group-ESP8266 0:6946b0b9e323 31 * Parser class for parsing AT commands
group-ESP8266 0:6946b0b9e323 32 *
group-ESP8266 0:6946b0b9e323 33 * Here are some examples:
group-ESP8266 0:6946b0b9e323 34 * @code
group-ESP8266 0:6946b0b9e323 35 * ATParser at = ATParser(serial, "\r\n");
group-ESP8266 0:6946b0b9e323 36 * int value;
group-ESP8266 0:6946b0b9e323 37 * char buffer[100];
group-ESP8266 0:6946b0b9e323 38 *
group-ESP8266 0:6946b0b9e323 39 * at.send("AT") && at.recv("OK");
group-ESP8266 0:6946b0b9e323 40 * at.send("AT+CWMODE=%d", 3) && at.recv("OK");
group-ESP8266 0:6946b0b9e323 41 * at.send("AT+CWMODE?") && at.recv("+CWMODE:%d\r\nOK", &value);
group-ESP8266 0:6946b0b9e323 42 * at.recv("+IPD,%d:", &value);
group-ESP8266 0:6946b0b9e323 43 * at.read(buffer, value);
group-ESP8266 0:6946b0b9e323 44 * at.recv("OK");
group-ESP8266 0:6946b0b9e323 45 * @endcode
group-ESP8266 0:6946b0b9e323 46 */
group-ESP8266 0:6946b0b9e323 47 class ATParser
group-ESP8266 0:6946b0b9e323 48 {
group-ESP8266 0:6946b0b9e323 49 private:
group-ESP8266 0:6946b0b9e323 50 // Serial information
group-ESP8266 0:6946b0b9e323 51 BufferedSerial *_serial;
group-ESP8266 0:6946b0b9e323 52 int _buffer_size;
group-ESP8266 0:6946b0b9e323 53 char *_buffer;
group-ESP8266 0:6946b0b9e323 54 int _timeout;
group-ESP8266 0:6946b0b9e323 55
group-ESP8266 0:6946b0b9e323 56 // Parsing information
group-ESP8266 0:6946b0b9e323 57 const char *_delimiter;
group-ESP8266 0:6946b0b9e323 58 int _delim_size;
group-ESP8266 0:6946b0b9e323 59 bool dbg_on;
group-ESP8266 0:6946b0b9e323 60
group-ESP8266 0:6946b0b9e323 61 struct oob {
group-ESP8266 0:6946b0b9e323 62 unsigned len;
group-ESP8266 0:6946b0b9e323 63 const char *prefix;
group-ESP8266 0:6946b0b9e323 64 mbed::Callback<void()> cb;
group-ESP8266 0:6946b0b9e323 65 };
group-ESP8266 0:6946b0b9e323 66 std::vector<oob> _oobs;
group-ESP8266 0:6946b0b9e323 67
group-ESP8266 0:6946b0b9e323 68 public:
group-ESP8266 0:6946b0b9e323 69 /**
group-ESP8266 0:6946b0b9e323 70 * Constructor
group-ESP8266 0:6946b0b9e323 71 *
group-ESP8266 0:6946b0b9e323 72 * @param serial serial interface to use for AT commands
group-ESP8266 0:6946b0b9e323 73 * @param buffer_size size of internal buffer for transaction
group-ESP8266 0:6946b0b9e323 74 * @param timeout timeout of the connection
group-ESP8266 0:6946b0b9e323 75 * @param delimiter string of characters to use as line delimiters
group-ESP8266 0:6946b0b9e323 76 */
group-ESP8266 0:6946b0b9e323 77 ATParser(BufferedSerial &serial, const char *delimiter = "\r\n", int buffer_size = 256, int timeout = 8000, bool debug = false) :
group-ESP8266 0:6946b0b9e323 78 _serial(&serial),
group-ESP8266 0:6946b0b9e323 79 _buffer_size(buffer_size) {
group-ESP8266 0:6946b0b9e323 80 _buffer = new char[buffer_size];
group-ESP8266 0:6946b0b9e323 81 setTimeout(timeout);
group-ESP8266 0:6946b0b9e323 82 setDelimiter(delimiter);
group-ESP8266 0:6946b0b9e323 83 debugOn(debug);
group-ESP8266 0:6946b0b9e323 84 }
group-ESP8266 0:6946b0b9e323 85
group-ESP8266 0:6946b0b9e323 86 /**
group-ESP8266 0:6946b0b9e323 87 * Destructor
group-ESP8266 0:6946b0b9e323 88 */
group-ESP8266 0:6946b0b9e323 89 ~ATParser() {
group-ESP8266 0:6946b0b9e323 90 delete [] _buffer;
group-ESP8266 0:6946b0b9e323 91 }
group-ESP8266 0:6946b0b9e323 92
group-ESP8266 0:6946b0b9e323 93 /**
group-ESP8266 0:6946b0b9e323 94 * Allows timeout to be changed between commands
group-ESP8266 0:6946b0b9e323 95 *
group-ESP8266 0:6946b0b9e323 96 * @param timeout timeout of the connection
group-ESP8266 0:6946b0b9e323 97 */
group-ESP8266 0:6946b0b9e323 98 void setTimeout(int timeout) {
group-ESP8266 0:6946b0b9e323 99 _timeout = timeout;
group-ESP8266 0:6946b0b9e323 100 }
group-ESP8266 0:6946b0b9e323 101
group-ESP8266 0:6946b0b9e323 102 /**
group-ESP8266 0:6946b0b9e323 103 * Sets string of characters to use as line delimiters
group-ESP8266 0:6946b0b9e323 104 *
group-ESP8266 0:6946b0b9e323 105 * @param delimiter string of characters to use as line delimiters
group-ESP8266 0:6946b0b9e323 106 */
group-ESP8266 0:6946b0b9e323 107 void setDelimiter(const char *delimiter) {
group-ESP8266 0:6946b0b9e323 108 _delimiter = delimiter;
group-ESP8266 0:6946b0b9e323 109 _delim_size = strlen(delimiter);
group-ESP8266 0:6946b0b9e323 110 }
group-ESP8266 0:6946b0b9e323 111
group-ESP8266 0:6946b0b9e323 112 /**
group-ESP8266 0:6946b0b9e323 113 * Allows echo to be on or off
group-ESP8266 0:6946b0b9e323 114 *
group-ESP8266 0:6946b0b9e323 115 * @param echo 1 for echo and 0 turns it off
group-ESP8266 0:6946b0b9e323 116 */
group-ESP8266 0:6946b0b9e323 117 void debugOn(uint8_t on) {
group-ESP8266 0:6946b0b9e323 118 dbg_on = (on) ? 1 : 0;
group-ESP8266 0:6946b0b9e323 119 }
group-ESP8266 0:6946b0b9e323 120
group-ESP8266 0:6946b0b9e323 121 /**
group-ESP8266 0:6946b0b9e323 122 * Sends an AT command
group-ESP8266 0:6946b0b9e323 123 *
group-ESP8266 0:6946b0b9e323 124 * Sends a formatted command using printf style formatting
group-ESP8266 0:6946b0b9e323 125 * @see ::printf
group-ESP8266 0:6946b0b9e323 126 *
group-ESP8266 0:6946b0b9e323 127 * @param command printf-like format string of command to send which
group-ESP8266 0:6946b0b9e323 128 * is appended with the specified delimiter
group-ESP8266 0:6946b0b9e323 129 * @param ... all printf-like arguments to insert into command
group-ESP8266 0:6946b0b9e323 130 * @return true only if command is successfully sent
group-ESP8266 0:6946b0b9e323 131 */
group-ESP8266 0:6946b0b9e323 132 bool send(const char *command, ...);
group-ESP8266 0:6946b0b9e323 133 bool vsend(const char *command, va_list args);
group-ESP8266 0:6946b0b9e323 134
group-ESP8266 0:6946b0b9e323 135 /**
group-ESP8266 0:6946b0b9e323 136 * Recieve an AT response
group-ESP8266 0:6946b0b9e323 137 *
group-ESP8266 0:6946b0b9e323 138 * Recieves a formatted response using scanf style formatting
group-ESP8266 0:6946b0b9e323 139 * @see ::scanf
group-ESP8266 0:6946b0b9e323 140 *
group-ESP8266 0:6946b0b9e323 141 * Responses are parsed line at a time using the specified delimiter.
group-ESP8266 0:6946b0b9e323 142 * Any recieved data that does not match the response is ignored until
group-ESP8266 0:6946b0b9e323 143 * a timeout occurs.
group-ESP8266 0:6946b0b9e323 144 *
group-ESP8266 0:6946b0b9e323 145 * @param response scanf-like format string of response to expect
group-ESP8266 0:6946b0b9e323 146 * @param ... all scanf-like arguments to extract from response
group-ESP8266 0:6946b0b9e323 147 * @return true only if response is successfully matched
group-ESP8266 0:6946b0b9e323 148 */
group-ESP8266 0:6946b0b9e323 149 bool recv(const char *response, ...);
group-ESP8266 0:6946b0b9e323 150 bool vrecv(const char *response, va_list args);
group-ESP8266 0:6946b0b9e323 151
group-ESP8266 0:6946b0b9e323 152 /**
group-ESP8266 0:6946b0b9e323 153 * Write a single byte to the underlying stream
group-ESP8266 0:6946b0b9e323 154 *
group-ESP8266 0:6946b0b9e323 155 * @param c The byte to write
group-ESP8266 0:6946b0b9e323 156 * @return The byte that was written or -1 during a timeout
group-ESP8266 0:6946b0b9e323 157 */
group-ESP8266 0:6946b0b9e323 158 int putc(char c);
group-ESP8266 0:6946b0b9e323 159
group-ESP8266 0:6946b0b9e323 160 /**
group-ESP8266 0:6946b0b9e323 161 * Get a single byte from the underlying stream
group-ESP8266 0:6946b0b9e323 162 *
group-ESP8266 0:6946b0b9e323 163 * @return The byte that was read or -1 during a timeout
group-ESP8266 0:6946b0b9e323 164 */
group-ESP8266 0:6946b0b9e323 165 int getc();
group-ESP8266 0:6946b0b9e323 166
group-ESP8266 0:6946b0b9e323 167 /**
group-ESP8266 0:6946b0b9e323 168 * Write an array of bytes to the underlying stream
group-ESP8266 0:6946b0b9e323 169 *
group-ESP8266 0:6946b0b9e323 170 * @param data the array of bytes to write
group-ESP8266 0:6946b0b9e323 171 * @param size number of bytes to write
group-ESP8266 0:6946b0b9e323 172 * @return number of bytes written or -1 on failure
group-ESP8266 0:6946b0b9e323 173 */
group-ESP8266 0:6946b0b9e323 174 int write(const char *data, int size);
group-ESP8266 0:6946b0b9e323 175
group-ESP8266 0:6946b0b9e323 176 /**
group-ESP8266 0:6946b0b9e323 177 * Read an array of bytes from the underlying stream
group-ESP8266 0:6946b0b9e323 178 *
group-ESP8266 0:6946b0b9e323 179 * @param data the destination for the read bytes
group-ESP8266 0:6946b0b9e323 180 * @param size number of bytes to read
group-ESP8266 0:6946b0b9e323 181 * @return number of bytes read or -1 on failure
group-ESP8266 0:6946b0b9e323 182 */
group-ESP8266 0:6946b0b9e323 183 int read(char *data, int size);
group-ESP8266 0:6946b0b9e323 184
group-ESP8266 0:6946b0b9e323 185 /**
group-ESP8266 0:6946b0b9e323 186 * Direct printf to underlying stream
group-ESP8266 0:6946b0b9e323 187 * @see ::printf
group-ESP8266 0:6946b0b9e323 188 *
group-ESP8266 0:6946b0b9e323 189 * @param format format string to pass to printf
group-ESP8266 0:6946b0b9e323 190 * @param ... arguments to printf
group-ESP8266 0:6946b0b9e323 191 * @return number of bytes written or -1 on failure
group-ESP8266 0:6946b0b9e323 192 */
group-ESP8266 0:6946b0b9e323 193 int printf(const char *format, ...);
group-ESP8266 0:6946b0b9e323 194 int vprintf(const char *format, va_list args);
group-ESP8266 0:6946b0b9e323 195
group-ESP8266 0:6946b0b9e323 196 /**
group-ESP8266 0:6946b0b9e323 197 * Direct scanf on underlying stream
group-ESP8266 0:6946b0b9e323 198 * @see ::scanf
group-ESP8266 0:6946b0b9e323 199 *
group-ESP8266 0:6946b0b9e323 200 * @param format format string to pass to scanf
group-ESP8266 0:6946b0b9e323 201 * @param ... arguments to scanf
group-ESP8266 0:6946b0b9e323 202 * @return number of bytes read or -1 on failure
group-ESP8266 0:6946b0b9e323 203 */
group-ESP8266 0:6946b0b9e323 204 int scanf(const char *format, ...);
group-ESP8266 0:6946b0b9e323 205 int vscanf(const char *format, va_list args);
group-ESP8266 0:6946b0b9e323 206
group-ESP8266 0:6946b0b9e323 207 /**
group-ESP8266 0:6946b0b9e323 208 * Attach a callback for out-of-band data
group-ESP8266 0:6946b0b9e323 209 *
group-ESP8266 0:6946b0b9e323 210 * @param prefix string on when to initiate callback
group-ESP8266 0:6946b0b9e323 211 * @param func callback to call when string is read
group-ESP8266 0:6946b0b9e323 212 * @note out-of-band data is only processed during a scanf call
group-ESP8266 0:6946b0b9e323 213 */
group-ESP8266 0:6946b0b9e323 214 void oob(const char *prefix, mbed::Callback<void()> func);
group-ESP8266 0:6946b0b9e323 215
group-ESP8266 0:6946b0b9e323 216 /**
group-ESP8266 0:6946b0b9e323 217 * Attach a callback for out-of-band data
group-ESP8266 0:6946b0b9e323 218 *
group-ESP8266 0:6946b0b9e323 219 * @param prefix string on when to initiate callback
group-ESP8266 0:6946b0b9e323 220 * @param obj pointer to object to call member function on
group-ESP8266 0:6946b0b9e323 221 * @param method callback to call when string is read
group-ESP8266 0:6946b0b9e323 222 * @note out-of-band data is only processed during a scanf call
group-ESP8266 0:6946b0b9e323 223 */
group-ESP8266 0:6946b0b9e323 224 template <typename T, typename M>
group-ESP8266 0:6946b0b9e323 225 void oob(const char *prefix, T *obj, M method) {
group-ESP8266 0:6946b0b9e323 226 return oob(prefix, mbed::Callback<void()>(obj, method));
group-ESP8266 0:6946b0b9e323 227 }
group-ESP8266 0:6946b0b9e323 228
group-ESP8266 0:6946b0b9e323 229 /**
group-ESP8266 0:6946b0b9e323 230 * Flushes the underlying stream
group-ESP8266 0:6946b0b9e323 231 */
group-ESP8266 0:6946b0b9e323 232 void flush();
group-ESP8266 0:6946b0b9e323 233 };
group-ESP8266 0:6946b0b9e323 234 #endif