A metronome using the FRDM K64F board

Committer:
ram54288
Date:
Sun May 14 18:40:18 2017 +0000
Revision:
0:a7a43371b306
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ram54288 0:a7a43371b306 1 /* ESP8266Interface Example
ram54288 0:a7a43371b306 2 * Copyright (c) 2015 ARM Limited
ram54288 0:a7a43371b306 3 *
ram54288 0:a7a43371b306 4 * Licensed under the Apache License, Version 2.0 (the "License");
ram54288 0:a7a43371b306 5 * you may not use this file except in compliance with the License.
ram54288 0:a7a43371b306 6 * You may obtain a copy of the License at
ram54288 0:a7a43371b306 7 *
ram54288 0:a7a43371b306 8 * http://www.apache.org/licenses/LICENSE-2.0
ram54288 0:a7a43371b306 9 *
ram54288 0:a7a43371b306 10 * Unless required by applicable law or agreed to in writing, software
ram54288 0:a7a43371b306 11 * distributed under the License is distributed on an "AS IS" BASIS,
ram54288 0:a7a43371b306 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ram54288 0:a7a43371b306 13 * See the License for the specific language governing permissions and
ram54288 0:a7a43371b306 14 * limitations under the License.
ram54288 0:a7a43371b306 15 */
ram54288 0:a7a43371b306 16
ram54288 0:a7a43371b306 17 #ifndef ESP8266_H
ram54288 0:a7a43371b306 18 #define ESP8266_H
ram54288 0:a7a43371b306 19
ram54288 0:a7a43371b306 20 #include "ATParser.h"
ram54288 0:a7a43371b306 21
ram54288 0:a7a43371b306 22 /** ESP8266Interface class.
ram54288 0:a7a43371b306 23 This is an interface to a ESP8266 radio.
ram54288 0:a7a43371b306 24 */
ram54288 0:a7a43371b306 25 class ESP8266
ram54288 0:a7a43371b306 26 {
ram54288 0:a7a43371b306 27 public:
ram54288 0:a7a43371b306 28 ESP8266(PinName tx, PinName rx, bool debug=false);
ram54288 0:a7a43371b306 29
ram54288 0:a7a43371b306 30 /**
ram54288 0:a7a43371b306 31 * Startup the ESP8266
ram54288 0:a7a43371b306 32 *
ram54288 0:a7a43371b306 33 * @param mode mode of WIFI 1-client, 2-host, 3-both
ram54288 0:a7a43371b306 34 * @return true only if ESP8266 was setup correctly
ram54288 0:a7a43371b306 35 */
ram54288 0:a7a43371b306 36 bool startup(int mode);
ram54288 0:a7a43371b306 37
ram54288 0:a7a43371b306 38 /**
ram54288 0:a7a43371b306 39 * Reset ESP8266
ram54288 0:a7a43371b306 40 *
ram54288 0:a7a43371b306 41 * @return true only if ESP8266 resets successfully
ram54288 0:a7a43371b306 42 */
ram54288 0:a7a43371b306 43 bool reset(void);
ram54288 0:a7a43371b306 44
ram54288 0:a7a43371b306 45 /**
ram54288 0:a7a43371b306 46 * Enable/Disable DHCP
ram54288 0:a7a43371b306 47 *
ram54288 0:a7a43371b306 48 * @param enabled DHCP enabled when true
ram54288 0:a7a43371b306 49 * @param mode mode of DHCP 0-softAP, 1-station, 2-both
ram54288 0:a7a43371b306 50 * @return true only if ESP8266 enables/disables DHCP successfully
ram54288 0:a7a43371b306 51 */
ram54288 0:a7a43371b306 52 bool dhcp(bool enabled, int mode);
ram54288 0:a7a43371b306 53
ram54288 0:a7a43371b306 54 /**
ram54288 0:a7a43371b306 55 * Connect ESP8266 to AP
ram54288 0:a7a43371b306 56 *
ram54288 0:a7a43371b306 57 * @param ap the name of the AP
ram54288 0:a7a43371b306 58 * @param passPhrase the password of AP
ram54288 0:a7a43371b306 59 * @return true only if ESP8266 is connected successfully
ram54288 0:a7a43371b306 60 */
ram54288 0:a7a43371b306 61 bool connect(const char *ap, const char *passPhrase);
ram54288 0:a7a43371b306 62
ram54288 0:a7a43371b306 63 /**
ram54288 0:a7a43371b306 64 * Disconnect ESP8266 from AP
ram54288 0:a7a43371b306 65 *
ram54288 0:a7a43371b306 66 * @return true only if ESP8266 is disconnected successfully
ram54288 0:a7a43371b306 67 */
ram54288 0:a7a43371b306 68 bool disconnect(void);
ram54288 0:a7a43371b306 69
ram54288 0:a7a43371b306 70 /**
ram54288 0:a7a43371b306 71 * Get the IP address of ESP8266
ram54288 0:a7a43371b306 72 *
ram54288 0:a7a43371b306 73 * @return null-teriminated IP address or null if no IP address is assigned
ram54288 0:a7a43371b306 74 */
ram54288 0:a7a43371b306 75 const char *getIPAddress(void);
ram54288 0:a7a43371b306 76
ram54288 0:a7a43371b306 77 /**
ram54288 0:a7a43371b306 78 * Get the MAC address of ESP8266
ram54288 0:a7a43371b306 79 *
ram54288 0:a7a43371b306 80 * @return null-terminated MAC address or null if no MAC address is assigned
ram54288 0:a7a43371b306 81 */
ram54288 0:a7a43371b306 82 const char *getMACAddress(void);
ram54288 0:a7a43371b306 83
ram54288 0:a7a43371b306 84 /** Get the local gateway
ram54288 0:a7a43371b306 85 *
ram54288 0:a7a43371b306 86 * @return Null-terminated representation of the local gateway
ram54288 0:a7a43371b306 87 * or null if no network mask has been recieved
ram54288 0:a7a43371b306 88 */
ram54288 0:a7a43371b306 89 const char *getGateway();
ram54288 0:a7a43371b306 90
ram54288 0:a7a43371b306 91 /** Get the local network mask
ram54288 0:a7a43371b306 92 *
ram54288 0:a7a43371b306 93 * @return Null-terminated representation of the local network mask
ram54288 0:a7a43371b306 94 * or null if no network mask has been recieved
ram54288 0:a7a43371b306 95 */
ram54288 0:a7a43371b306 96 const char *getNetmask();
ram54288 0:a7a43371b306 97
ram54288 0:a7a43371b306 98 /* Return RSSI for active connection
ram54288 0:a7a43371b306 99 *
ram54288 0:a7a43371b306 100 * @return Measured RSSI
ram54288 0:a7a43371b306 101 */
ram54288 0:a7a43371b306 102 int8_t getRSSI();
ram54288 0:a7a43371b306 103
ram54288 0:a7a43371b306 104 /**
ram54288 0:a7a43371b306 105 * Check if ESP8266 is conenected
ram54288 0:a7a43371b306 106 *
ram54288 0:a7a43371b306 107 * @return true only if the chip has an IP address
ram54288 0:a7a43371b306 108 */
ram54288 0:a7a43371b306 109 bool isConnected(void);
ram54288 0:a7a43371b306 110
ram54288 0:a7a43371b306 111 /** Scan for available networks
ram54288 0:a7a43371b306 112 *
ram54288 0:a7a43371b306 113 * @param ap Pointer to allocated array to store discovered AP
ram54288 0:a7a43371b306 114 * @param limit Size of allocated @a res array, or 0 to only count available AP
ram54288 0:a7a43371b306 115 * @return Number of entries in @a res, or if @a count was 0 number of available networks, negative on error
ram54288 0:a7a43371b306 116 * see @a nsapi_error
ram54288 0:a7a43371b306 117 */
ram54288 0:a7a43371b306 118 int scan(WiFiAccessPoint *res, unsigned limit);
ram54288 0:a7a43371b306 119
ram54288 0:a7a43371b306 120 /**
ram54288 0:a7a43371b306 121 * Open a socketed connection
ram54288 0:a7a43371b306 122 *
ram54288 0:a7a43371b306 123 * @param type the type of socket to open "UDP" or "TCP"
ram54288 0:a7a43371b306 124 * @param id id to give the new socket, valid 0-4
ram54288 0:a7a43371b306 125 * @param port port to open connection with
ram54288 0:a7a43371b306 126 * @param addr the IP address of the destination
ram54288 0:a7a43371b306 127 * @return true only if socket opened successfully
ram54288 0:a7a43371b306 128 */
ram54288 0:a7a43371b306 129 bool open(const char *type, int id, const char* addr, int port);
ram54288 0:a7a43371b306 130
ram54288 0:a7a43371b306 131 /**
ram54288 0:a7a43371b306 132 * Sends data to an open socket
ram54288 0:a7a43371b306 133 *
ram54288 0:a7a43371b306 134 * @param id id of socket to send to
ram54288 0:a7a43371b306 135 * @param data data to be sent
ram54288 0:a7a43371b306 136 * @param amount amount of data to be sent - max 1024
ram54288 0:a7a43371b306 137 * @return true only if data sent successfully
ram54288 0:a7a43371b306 138 */
ram54288 0:a7a43371b306 139 bool send(int id, const void *data, uint32_t amount);
ram54288 0:a7a43371b306 140
ram54288 0:a7a43371b306 141 /**
ram54288 0:a7a43371b306 142 * Receives data from an open socket
ram54288 0:a7a43371b306 143 *
ram54288 0:a7a43371b306 144 * @param id id to receive from
ram54288 0:a7a43371b306 145 * @param data placeholder for returned information
ram54288 0:a7a43371b306 146 * @param amount number of bytes to be received
ram54288 0:a7a43371b306 147 * @return the number of bytes received
ram54288 0:a7a43371b306 148 */
ram54288 0:a7a43371b306 149 int32_t recv(int id, void *data, uint32_t amount);
ram54288 0:a7a43371b306 150
ram54288 0:a7a43371b306 151 /**
ram54288 0:a7a43371b306 152 * Closes a socket
ram54288 0:a7a43371b306 153 *
ram54288 0:a7a43371b306 154 * @param id id of socket to close, valid only 0-4
ram54288 0:a7a43371b306 155 * @return true only if socket is closed successfully
ram54288 0:a7a43371b306 156 */
ram54288 0:a7a43371b306 157 bool close(int id);
ram54288 0:a7a43371b306 158
ram54288 0:a7a43371b306 159 /**
ram54288 0:a7a43371b306 160 * Allows timeout to be changed between commands
ram54288 0:a7a43371b306 161 *
ram54288 0:a7a43371b306 162 * @param timeout_ms timeout of the connection
ram54288 0:a7a43371b306 163 */
ram54288 0:a7a43371b306 164 void setTimeout(uint32_t timeout_ms);
ram54288 0:a7a43371b306 165
ram54288 0:a7a43371b306 166 /**
ram54288 0:a7a43371b306 167 * Checks if data is available
ram54288 0:a7a43371b306 168 */
ram54288 0:a7a43371b306 169 bool readable();
ram54288 0:a7a43371b306 170
ram54288 0:a7a43371b306 171 /**
ram54288 0:a7a43371b306 172 * Checks if data can be written
ram54288 0:a7a43371b306 173 */
ram54288 0:a7a43371b306 174 bool writeable();
ram54288 0:a7a43371b306 175
ram54288 0:a7a43371b306 176 /**
ram54288 0:a7a43371b306 177 * Attach a function to call whenever network state has changed
ram54288 0:a7a43371b306 178 *
ram54288 0:a7a43371b306 179 * @param func A pointer to a void function, or 0 to set as none
ram54288 0:a7a43371b306 180 */
ram54288 0:a7a43371b306 181 void attach(Callback<void()> func);
ram54288 0:a7a43371b306 182
ram54288 0:a7a43371b306 183 /**
ram54288 0:a7a43371b306 184 * Attach a function to call whenever network state has changed
ram54288 0:a7a43371b306 185 *
ram54288 0:a7a43371b306 186 * @param obj pointer to the object to call the member function on
ram54288 0:a7a43371b306 187 * @param method pointer to the member function to call
ram54288 0:a7a43371b306 188 */
ram54288 0:a7a43371b306 189 template <typename T, typename M>
ram54288 0:a7a43371b306 190 void attach(T *obj, M method) {
ram54288 0:a7a43371b306 191 attach(Callback<void()>(obj, method));
ram54288 0:a7a43371b306 192 }
ram54288 0:a7a43371b306 193
ram54288 0:a7a43371b306 194 private:
ram54288 0:a7a43371b306 195 BufferedSerial _serial;
ram54288 0:a7a43371b306 196 ATParser _parser;
ram54288 0:a7a43371b306 197
ram54288 0:a7a43371b306 198 struct packet {
ram54288 0:a7a43371b306 199 struct packet *next;
ram54288 0:a7a43371b306 200 int id;
ram54288 0:a7a43371b306 201 uint32_t len;
ram54288 0:a7a43371b306 202 // data follows
ram54288 0:a7a43371b306 203 } *_packets, **_packets_end;
ram54288 0:a7a43371b306 204 void _packet_handler();
ram54288 0:a7a43371b306 205 bool recv_ap(nsapi_wifi_ap_t *ap);
ram54288 0:a7a43371b306 206
ram54288 0:a7a43371b306 207 char _ip_buffer[16];
ram54288 0:a7a43371b306 208 char _gateway_buffer[16];
ram54288 0:a7a43371b306 209 char _netmask_buffer[16];
ram54288 0:a7a43371b306 210 char _mac_buffer[18];
ram54288 0:a7a43371b306 211 };
ram54288 0:a7a43371b306 212
ram54288 0:a7a43371b306 213 #endif