Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466

Dependencies:   ssd1331

Committer:
kadonotakashi
Date:
Wed Oct 10 00:33:53 2018 +0000
Revision:
0:8fdf9a60065b
how to make mbed librry

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kadonotakashi 0:8fdf9a60065b 1 /* ESP8266Interface Example
kadonotakashi 0:8fdf9a60065b 2 * Copyright (c) 2015 ARM Limited
kadonotakashi 0:8fdf9a60065b 3 *
kadonotakashi 0:8fdf9a60065b 4 * Licensed under the Apache License, Version 2.0 (the "License");
kadonotakashi 0:8fdf9a60065b 5 * you may not use this file except in compliance with the License.
kadonotakashi 0:8fdf9a60065b 6 * You may obtain a copy of the License at
kadonotakashi 0:8fdf9a60065b 7 *
kadonotakashi 0:8fdf9a60065b 8 * http://www.apache.org/licenses/LICENSE-2.0
kadonotakashi 0:8fdf9a60065b 9 *
kadonotakashi 0:8fdf9a60065b 10 * Unless required by applicable law or agreed to in writing, software
kadonotakashi 0:8fdf9a60065b 11 * distributed under the License is distributed on an "AS IS" BASIS,
kadonotakashi 0:8fdf9a60065b 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kadonotakashi 0:8fdf9a60065b 13 * See the License for the specific language governing permissions and
kadonotakashi 0:8fdf9a60065b 14 * limitations under the License.
kadonotakashi 0:8fdf9a60065b 15 */
kadonotakashi 0:8fdf9a60065b 16
kadonotakashi 0:8fdf9a60065b 17 #ifndef ESP8266_H
kadonotakashi 0:8fdf9a60065b 18 #define ESP8266_H
kadonotakashi 0:8fdf9a60065b 19
kadonotakashi 0:8fdf9a60065b 20 #include "ATCmdParser.h"
kadonotakashi 0:8fdf9a60065b 21 #include "nsapi_types.h"
kadonotakashi 0:8fdf9a60065b 22 #include "rtos.h"
kadonotakashi 0:8fdf9a60065b 23
kadonotakashi 0:8fdf9a60065b 24 // Various timeouts for different ESP8266 operations
kadonotakashi 0:8fdf9a60065b 25 #ifndef ESP8266_CONNECT_TIMEOUT
kadonotakashi 0:8fdf9a60065b 26 #define ESP8266_CONNECT_TIMEOUT 15000
kadonotakashi 0:8fdf9a60065b 27 #endif
kadonotakashi 0:8fdf9a60065b 28 #ifndef ESP8266_SEND_TIMEOUT
kadonotakashi 0:8fdf9a60065b 29 #define ESP8266_SEND_TIMEOUT 2000
kadonotakashi 0:8fdf9a60065b 30 #endif
kadonotakashi 0:8fdf9a60065b 31 #ifndef ESP8266_RECV_TIMEOUT
kadonotakashi 0:8fdf9a60065b 32 #define ESP8266_RECV_TIMEOUT 2000
kadonotakashi 0:8fdf9a60065b 33 #endif
kadonotakashi 0:8fdf9a60065b 34 #ifndef ESP8266_MISC_TIMEOUT
kadonotakashi 0:8fdf9a60065b 35 #define ESP8266_MISC_TIMEOUT 2000
kadonotakashi 0:8fdf9a60065b 36 #endif
kadonotakashi 0:8fdf9a60065b 37
kadonotakashi 0:8fdf9a60065b 38 /** ESP8266Interface class.
kadonotakashi 0:8fdf9a60065b 39 This is an interface to a ESP8266 radio.
kadonotakashi 0:8fdf9a60065b 40 */
kadonotakashi 0:8fdf9a60065b 41 class ESP8266
kadonotakashi 0:8fdf9a60065b 42 {
kadonotakashi 0:8fdf9a60065b 43 public:
kadonotakashi 0:8fdf9a60065b 44 ESP8266(PinName tx, PinName rx, bool debug=false);
kadonotakashi 0:8fdf9a60065b 45
kadonotakashi 0:8fdf9a60065b 46 /**
kadonotakashi 0:8fdf9a60065b 47 * Check firmware version of ESP8266
kadonotakashi 0:8fdf9a60065b 48 *
kadonotakashi 0:8fdf9a60065b 49 * @return integer firmware version or -1 if firmware query command gives outdated response
kadonotakashi 0:8fdf9a60065b 50 */
kadonotakashi 0:8fdf9a60065b 51 int get_firmware_version(void);
kadonotakashi 0:8fdf9a60065b 52
kadonotakashi 0:8fdf9a60065b 53 /**
kadonotakashi 0:8fdf9a60065b 54 * Startup the ESP8266
kadonotakashi 0:8fdf9a60065b 55 *
kadonotakashi 0:8fdf9a60065b 56 * @param mode mode of WIFI 1-client, 2-host, 3-both
kadonotakashi 0:8fdf9a60065b 57 * @return true only if ESP8266 was setup correctly
kadonotakashi 0:8fdf9a60065b 58 */
kadonotakashi 0:8fdf9a60065b 59 bool startup(int mode);
kadonotakashi 0:8fdf9a60065b 60
kadonotakashi 0:8fdf9a60065b 61 /**
kadonotakashi 0:8fdf9a60065b 62 * Reset ESP8266
kadonotakashi 0:8fdf9a60065b 63 *
kadonotakashi 0:8fdf9a60065b 64 * @return true only if ESP8266 resets successfully
kadonotakashi 0:8fdf9a60065b 65 */
kadonotakashi 0:8fdf9a60065b 66 bool reset(void);
kadonotakashi 0:8fdf9a60065b 67
kadonotakashi 0:8fdf9a60065b 68 /**
kadonotakashi 0:8fdf9a60065b 69 * Enable/Disable DHCP
kadonotakashi 0:8fdf9a60065b 70 *
kadonotakashi 0:8fdf9a60065b 71 * @param enabled DHCP enabled when true
kadonotakashi 0:8fdf9a60065b 72 * @param mode mode of DHCP 0-softAP, 1-station, 2-both
kadonotakashi 0:8fdf9a60065b 73 * @return true only if ESP8266 enables/disables DHCP successfully
kadonotakashi 0:8fdf9a60065b 74 */
kadonotakashi 0:8fdf9a60065b 75 bool dhcp(bool enabled, int mode);
kadonotakashi 0:8fdf9a60065b 76
kadonotakashi 0:8fdf9a60065b 77 /**
kadonotakashi 0:8fdf9a60065b 78 * Connect ESP8266 to AP
kadonotakashi 0:8fdf9a60065b 79 *
kadonotakashi 0:8fdf9a60065b 80 * @param ap the name of the AP
kadonotakashi 0:8fdf9a60065b 81 * @param passPhrase the password of AP
kadonotakashi 0:8fdf9a60065b 82 * @return NSAPI_ERROR_OK only if ESP8266 is connected successfully
kadonotakashi 0:8fdf9a60065b 83 */
kadonotakashi 0:8fdf9a60065b 84 nsapi_error_t connect(const char *ap, const char *passPhrase);
kadonotakashi 0:8fdf9a60065b 85
kadonotakashi 0:8fdf9a60065b 86 /**
kadonotakashi 0:8fdf9a60065b 87 * Disconnect ESP8266 from AP
kadonotakashi 0:8fdf9a60065b 88 *
kadonotakashi 0:8fdf9a60065b 89 * @return true only if ESP8266 is disconnected successfully
kadonotakashi 0:8fdf9a60065b 90 */
kadonotakashi 0:8fdf9a60065b 91 bool disconnect(void);
kadonotakashi 0:8fdf9a60065b 92
kadonotakashi 0:8fdf9a60065b 93 /**
kadonotakashi 0:8fdf9a60065b 94 * Get the IP address of ESP8266
kadonotakashi 0:8fdf9a60065b 95 *
kadonotakashi 0:8fdf9a60065b 96 * @return null-teriminated IP address or null if no IP address is assigned
kadonotakashi 0:8fdf9a60065b 97 */
kadonotakashi 0:8fdf9a60065b 98 const char *getIPAddress(void);
kadonotakashi 0:8fdf9a60065b 99
kadonotakashi 0:8fdf9a60065b 100 /**
kadonotakashi 0:8fdf9a60065b 101 * Get the MAC address of ESP8266
kadonotakashi 0:8fdf9a60065b 102 *
kadonotakashi 0:8fdf9a60065b 103 * @return null-terminated MAC address or null if no MAC address is assigned
kadonotakashi 0:8fdf9a60065b 104 */
kadonotakashi 0:8fdf9a60065b 105 const char *getMACAddress(void);
kadonotakashi 0:8fdf9a60065b 106
kadonotakashi 0:8fdf9a60065b 107 /** Get the local gateway
kadonotakashi 0:8fdf9a60065b 108 *
kadonotakashi 0:8fdf9a60065b 109 * @return Null-terminated representation of the local gateway
kadonotakashi 0:8fdf9a60065b 110 * or null if no network mask has been recieved
kadonotakashi 0:8fdf9a60065b 111 */
kadonotakashi 0:8fdf9a60065b 112 const char *getGateway();
kadonotakashi 0:8fdf9a60065b 113
kadonotakashi 0:8fdf9a60065b 114 /** Get the local network mask
kadonotakashi 0:8fdf9a60065b 115 *
kadonotakashi 0:8fdf9a60065b 116 * @return Null-terminated representation of the local network mask
kadonotakashi 0:8fdf9a60065b 117 * or null if no network mask has been recieved
kadonotakashi 0:8fdf9a60065b 118 */
kadonotakashi 0:8fdf9a60065b 119 const char *getNetmask();
kadonotakashi 0:8fdf9a60065b 120
kadonotakashi 0:8fdf9a60065b 121 /* Return RSSI for active connection
kadonotakashi 0:8fdf9a60065b 122 *
kadonotakashi 0:8fdf9a60065b 123 * @return Measured RSSI
kadonotakashi 0:8fdf9a60065b 124 */
kadonotakashi 0:8fdf9a60065b 125 int8_t getRSSI();
kadonotakashi 0:8fdf9a60065b 126
kadonotakashi 0:8fdf9a60065b 127 /** Scan for available networks
kadonotakashi 0:8fdf9a60065b 128 *
kadonotakashi 0:8fdf9a60065b 129 * @param ap Pointer to allocated array to store discovered AP
kadonotakashi 0:8fdf9a60065b 130 * @param limit Size of allocated @a res array, or 0 to only count available AP
kadonotakashi 0:8fdf9a60065b 131 * @return Number of entries in @a res, or if @a count was 0 number of available networks, negative on error
kadonotakashi 0:8fdf9a60065b 132 * see @a nsapi_error
kadonotakashi 0:8fdf9a60065b 133 */
kadonotakashi 0:8fdf9a60065b 134 int scan(WiFiAccessPoint *res, unsigned limit);
kadonotakashi 0:8fdf9a60065b 135
kadonotakashi 0:8fdf9a60065b 136 /**Perform a dns query
kadonotakashi 0:8fdf9a60065b 137 *
kadonotakashi 0:8fdf9a60065b 138 * @param name Hostname to resolve
kadonotakashi 0:8fdf9a60065b 139 * @param ip Buffer to store IP address
kadonotakashi 0:8fdf9a60065b 140 * @return 0 true on success, false on failure
kadonotakashi 0:8fdf9a60065b 141 */
kadonotakashi 0:8fdf9a60065b 142 bool dns_lookup(const char *name, char *ip);
kadonotakashi 0:8fdf9a60065b 143
kadonotakashi 0:8fdf9a60065b 144 /**
kadonotakashi 0:8fdf9a60065b 145 * Open a socketed connection
kadonotakashi 0:8fdf9a60065b 146 *
kadonotakashi 0:8fdf9a60065b 147 * @param type the type of socket to open "UDP" or "TCP"
kadonotakashi 0:8fdf9a60065b 148 * @param id id to give the new socket, valid 0-4
kadonotakashi 0:8fdf9a60065b 149 * @param port port to open connection with
kadonotakashi 0:8fdf9a60065b 150 * @param addr the IP address of the destination
kadonotakashi 0:8fdf9a60065b 151 * @param port the port on the destination
kadonotakashi 0:8fdf9a60065b 152 * @param local_port UDP socket's local port, zero means any
kadonotakashi 0:8fdf9a60065b 153 * @return true only if socket opened successfully
kadonotakashi 0:8fdf9a60065b 154 */
kadonotakashi 0:8fdf9a60065b 155 nsapi_error_t open_udp(int id, const char* addr, int port, int local_port = 0);
kadonotakashi 0:8fdf9a60065b 156
kadonotakashi 0:8fdf9a60065b 157 /**
kadonotakashi 0:8fdf9a60065b 158 * Open a socketed connection
kadonotakashi 0:8fdf9a60065b 159 *
kadonotakashi 0:8fdf9a60065b 160 * @param type the type of socket to open "UDP" or "TCP"
kadonotakashi 0:8fdf9a60065b 161 * @param id id to give the new socket, valid 0-4
kadonotakashi 0:8fdf9a60065b 162 * @param port port to open connection with
kadonotakashi 0:8fdf9a60065b 163 * @param addr the IP address of the destination
kadonotakashi 0:8fdf9a60065b 164 * @param port the port on the destination
kadonotakashi 0:8fdf9a60065b 165 * @param tcp_keepalive TCP connection's keep alive time, zero means disabled
kadonotakashi 0:8fdf9a60065b 166 * @return true only if socket opened successfully
kadonotakashi 0:8fdf9a60065b 167 */
kadonotakashi 0:8fdf9a60065b 168 bool open_tcp(int id, const char* addr, int port, int keepalive = 0);
kadonotakashi 0:8fdf9a60065b 169
kadonotakashi 0:8fdf9a60065b 170 /**
kadonotakashi 0:8fdf9a60065b 171 * Sends data to an open socket
kadonotakashi 0:8fdf9a60065b 172 *
kadonotakashi 0:8fdf9a60065b 173 * @param id id of socket to send to
kadonotakashi 0:8fdf9a60065b 174 * @param data data to be sent
kadonotakashi 0:8fdf9a60065b 175 * @param amount amount of data to be sent - max 1024
kadonotakashi 0:8fdf9a60065b 176 * @return NSAPI_ERROR_OK in success, negative error code in failure
kadonotakashi 0:8fdf9a60065b 177 */
kadonotakashi 0:8fdf9a60065b 178 nsapi_error_t send(int id, const void *data, uint32_t amount);
kadonotakashi 0:8fdf9a60065b 179
kadonotakashi 0:8fdf9a60065b 180 /**
kadonotakashi 0:8fdf9a60065b 181 * Receives datagram from an open UDP socket
kadonotakashi 0:8fdf9a60065b 182 *
kadonotakashi 0:8fdf9a60065b 183 * @param id id to receive from
kadonotakashi 0:8fdf9a60065b 184 * @param data placeholder for returned information
kadonotakashi 0:8fdf9a60065b 185 * @param amount number of bytes to be received
kadonotakashi 0:8fdf9a60065b 186 * @return the number of bytes received
kadonotakashi 0:8fdf9a60065b 187 */
kadonotakashi 0:8fdf9a60065b 188 int32_t recv_udp(int id, void *data, uint32_t amount, uint32_t timeout=ESP8266_RECV_TIMEOUT);
kadonotakashi 0:8fdf9a60065b 189
kadonotakashi 0:8fdf9a60065b 190 /**
kadonotakashi 0:8fdf9a60065b 191 * Receives stream data from an open TCP socket
kadonotakashi 0:8fdf9a60065b 192 *
kadonotakashi 0:8fdf9a60065b 193 * @param id id to receive from
kadonotakashi 0:8fdf9a60065b 194 * @param data placeholder for returned information
kadonotakashi 0:8fdf9a60065b 195 * @param amount number of bytes to be received
kadonotakashi 0:8fdf9a60065b 196 * @return the number of bytes received
kadonotakashi 0:8fdf9a60065b 197 */
kadonotakashi 0:8fdf9a60065b 198 int32_t recv_tcp(int id, void *data, uint32_t amount, uint32_t timeout=ESP8266_RECV_TIMEOUT);
kadonotakashi 0:8fdf9a60065b 199
kadonotakashi 0:8fdf9a60065b 200 /**
kadonotakashi 0:8fdf9a60065b 201 * Closes a socket
kadonotakashi 0:8fdf9a60065b 202 *
kadonotakashi 0:8fdf9a60065b 203 * @param id id of socket to close, valid only 0-4
kadonotakashi 0:8fdf9a60065b 204 * @return true only if socket is closed successfully
kadonotakashi 0:8fdf9a60065b 205 */
kadonotakashi 0:8fdf9a60065b 206 bool close(int id);
kadonotakashi 0:8fdf9a60065b 207
kadonotakashi 0:8fdf9a60065b 208 /**
kadonotakashi 0:8fdf9a60065b 209 * Allows timeout to be changed between commands
kadonotakashi 0:8fdf9a60065b 210 *
kadonotakashi 0:8fdf9a60065b 211 * @param timeout_ms timeout of the connection
kadonotakashi 0:8fdf9a60065b 212 */
kadonotakashi 0:8fdf9a60065b 213 void setTimeout(uint32_t timeout_ms=ESP8266_MISC_TIMEOUT);
kadonotakashi 0:8fdf9a60065b 214
kadonotakashi 0:8fdf9a60065b 215 /**
kadonotakashi 0:8fdf9a60065b 216 * Checks if data is available
kadonotakashi 0:8fdf9a60065b 217 */
kadonotakashi 0:8fdf9a60065b 218 bool readable();
kadonotakashi 0:8fdf9a60065b 219
kadonotakashi 0:8fdf9a60065b 220 /**
kadonotakashi 0:8fdf9a60065b 221 * Checks if data can be written
kadonotakashi 0:8fdf9a60065b 222 */
kadonotakashi 0:8fdf9a60065b 223 bool writeable();
kadonotakashi 0:8fdf9a60065b 224
kadonotakashi 0:8fdf9a60065b 225 /**
kadonotakashi 0:8fdf9a60065b 226 * Attach a function to call whenever sigio happens in the serial
kadonotakashi 0:8fdf9a60065b 227 *
kadonotakashi 0:8fdf9a60065b 228 * @param func A pointer to a void function, or 0 to set as none
kadonotakashi 0:8fdf9a60065b 229 */
kadonotakashi 0:8fdf9a60065b 230 void sigio(Callback<void()> func);
kadonotakashi 0:8fdf9a60065b 231
kadonotakashi 0:8fdf9a60065b 232 /**
kadonotakashi 0:8fdf9a60065b 233 * Attach a function to call whenever sigio happens in the serial
kadonotakashi 0:8fdf9a60065b 234 *
kadonotakashi 0:8fdf9a60065b 235 * @param obj pointer to the object to call the member function on
kadonotakashi 0:8fdf9a60065b 236 * @param method pointer to the member function to call
kadonotakashi 0:8fdf9a60065b 237 */
kadonotakashi 0:8fdf9a60065b 238 template <typename T, typename M>
kadonotakashi 0:8fdf9a60065b 239 void sigio(T *obj, M method) {
kadonotakashi 0:8fdf9a60065b 240 sigio(Callback<void()>(obj, method));
kadonotakashi 0:8fdf9a60065b 241 }
kadonotakashi 0:8fdf9a60065b 242
kadonotakashi 0:8fdf9a60065b 243 /**
kadonotakashi 0:8fdf9a60065b 244 * Attach a function to call whenever network state has changed
kadonotakashi 0:8fdf9a60065b 245 *
kadonotakashi 0:8fdf9a60065b 246 * @param func A pointer to a void function, or 0 to set as none
kadonotakashi 0:8fdf9a60065b 247 */
kadonotakashi 0:8fdf9a60065b 248 void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
kadonotakashi 0:8fdf9a60065b 249
kadonotakashi 0:8fdf9a60065b 250 /**
kadonotakashi 0:8fdf9a60065b 251 * Read default Wifi mode from flash
kadonotakashi 0:8fdf9a60065b 252 *
kadonotakashi 0:8fdf9a60065b 253 * return Station, SoftAP or SoftAP+Station - 0 on failure
kadonotakashi 0:8fdf9a60065b 254 */
kadonotakashi 0:8fdf9a60065b 255 int8_t get_default_wifi_mode();
kadonotakashi 0:8fdf9a60065b 256
kadonotakashi 0:8fdf9a60065b 257 /**
kadonotakashi 0:8fdf9a60065b 258 * Write default Wifi mode to flash
kadonotakashi 0:8fdf9a60065b 259 */
kadonotakashi 0:8fdf9a60065b 260 bool set_default_wifi_mode(const int8_t mode);
kadonotakashi 0:8fdf9a60065b 261
kadonotakashi 0:8fdf9a60065b 262 /** Get the connection status
kadonotakashi 0:8fdf9a60065b 263 *
kadonotakashi 0:8fdf9a60065b 264 * @return The connection status according to ConnectionStatusType
kadonotakashi 0:8fdf9a60065b 265 */
kadonotakashi 0:8fdf9a60065b 266 nsapi_connection_status_t get_connection_status() const;
kadonotakashi 0:8fdf9a60065b 267
kadonotakashi 0:8fdf9a60065b 268 static const int8_t WIFIMODE_STATION = 1;
kadonotakashi 0:8fdf9a60065b 269 static const int8_t WIFIMODE_SOFTAP = 2;
kadonotakashi 0:8fdf9a60065b 270 static const int8_t WIFIMODE_STATION_SOFTAP = 3;
kadonotakashi 0:8fdf9a60065b 271 static const int8_t SOCKET_COUNT = 5;
kadonotakashi 0:8fdf9a60065b 272
kadonotakashi 0:8fdf9a60065b 273 private:
kadonotakashi 0:8fdf9a60065b 274 UARTSerial _serial;
kadonotakashi 0:8fdf9a60065b 275 ATCmdParser _parser;
kadonotakashi 0:8fdf9a60065b 276 Mutex _smutex; // Protect serial port access
kadonotakashi 0:8fdf9a60065b 277
kadonotakashi 0:8fdf9a60065b 278 struct packet {
kadonotakashi 0:8fdf9a60065b 279 struct packet *next;
kadonotakashi 0:8fdf9a60065b 280 int id;
kadonotakashi 0:8fdf9a60065b 281 uint32_t len;
kadonotakashi 0:8fdf9a60065b 282 // data follows
kadonotakashi 0:8fdf9a60065b 283 } *_packets, **_packets_end;
kadonotakashi 0:8fdf9a60065b 284 void _packet_handler();
kadonotakashi 0:8fdf9a60065b 285 void _connect_error_handler();
kadonotakashi 0:8fdf9a60065b 286 bool recv_ap(nsapi_wifi_ap_t *ap);
kadonotakashi 0:8fdf9a60065b 287 void _oob_socket0_closed_handler();
kadonotakashi 0:8fdf9a60065b 288 void _oob_socket1_closed_handler();
kadonotakashi 0:8fdf9a60065b 289 void _oob_socket2_closed_handler();
kadonotakashi 0:8fdf9a60065b 290 void _oob_socket3_closed_handler();
kadonotakashi 0:8fdf9a60065b 291 void _oob_socket4_closed_handler();
kadonotakashi 0:8fdf9a60065b 292 void _connection_status_handler();
kadonotakashi 0:8fdf9a60065b 293 void _oob_socket_close_error();
kadonotakashi 0:8fdf9a60065b 294 void _clear_socket_packets(int id);
kadonotakashi 0:8fdf9a60065b 295
kadonotakashi 0:8fdf9a60065b 296 char _ip_buffer[16];
kadonotakashi 0:8fdf9a60065b 297 char _gateway_buffer[16];
kadonotakashi 0:8fdf9a60065b 298 char _netmask_buffer[16];
kadonotakashi 0:8fdf9a60065b 299 char _mac_buffer[18];
kadonotakashi 0:8fdf9a60065b 300
kadonotakashi 0:8fdf9a60065b 301 int _connect_error;
kadonotakashi 0:8fdf9a60065b 302 bool _fail;
kadonotakashi 0:8fdf9a60065b 303 bool _closed;
kadonotakashi 0:8fdf9a60065b 304 int _socket_open[SOCKET_COUNT];
kadonotakashi 0:8fdf9a60065b 305 nsapi_connection_status_t _connection_status;
kadonotakashi 0:8fdf9a60065b 306 Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
kadonotakashi 0:8fdf9a60065b 307 };
kadonotakashi 0:8fdf9a60065b 308
kadonotakashi 0:8fdf9a60065b 309 #endif