Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Wifly.cpp@0:27e403b8981e, 2012-04-28 (annotated)
- Committer:
- stretch
- Date:
- Sat Apr 28 21:09:44 2012 +0000
- Revision:
- 0:27e403b8981e
Initial Public Release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stretch | 0:27e403b8981e | 1 | #include "mbed.h" |
stretch | 0:27e403b8981e | 2 | #include "Wifly.h" |
stretch | 0:27e403b8981e | 3 | #include <string> |
stretch | 0:27e403b8981e | 4 | |
stretch | 0:27e403b8981e | 5 | // #define DEBUG |
stretch | 0:27e403b8981e | 6 | |
stretch | 0:27e403b8981e | 7 | |
stretch | 0:27e403b8981e | 8 | Wifly::Wifly( PinName tx, PinName rx, PinName _reset, char * ssid, char * ip, char * netmask, int channel, int baudrate): |
stretch | 0:27e403b8981e | 9 | wifi(tx, rx), reset_pin(_reset) { |
stretch | 0:27e403b8981e | 10 | wifi.baud(baudrate); |
stretch | 0:27e403b8981e | 11 | wifi.format(8, Serial::None, 1); |
stretch | 0:27e403b8981e | 12 | adhoc = true; |
stretch | 0:27e403b8981e | 13 | strcpy(this->ssid, ssid); |
stretch | 0:27e403b8981e | 14 | strcpy(this->ip, ip); |
stretch | 0:27e403b8981e | 15 | strcpy(this->netmask, netmask); |
stretch | 0:27e403b8981e | 16 | this->channel = channel; |
stretch | 0:27e403b8981e | 17 | last_char = '\0'; |
stretch | 0:27e403b8981e | 18 | reset(); |
stretch | 0:27e403b8981e | 19 | } |
stretch | 0:27e403b8981e | 20 | |
stretch | 0:27e403b8981e | 21 | |
stretch | 0:27e403b8981e | 22 | void Wifly::handler_rx(void) { |
stretch | 0:27e403b8981e | 23 | char read = wifi.getc(); |
stretch | 0:27e403b8981e | 24 | if(read == 'q') { |
stretch | 0:27e403b8981e | 25 | attach_img(true); |
stretch | 0:27e403b8981e | 26 | gettingImg = true; |
stretch | 0:27e403b8981e | 27 | last_char = read; |
stretch | 0:27e403b8981e | 28 | } else if(read == 'g'){ |
stretch | 0:27e403b8981e | 29 | attach_cmd(true); |
stretch | 0:27e403b8981e | 30 | } else { |
stretch | 0:27e403b8981e | 31 | buf_wifly.queue(read); |
stretch | 0:27e403b8981e | 32 | } |
stretch | 0:27e403b8981e | 33 | //call user callback |
stretch | 0:27e403b8981e | 34 | rx.call(); |
stretch | 0:27e403b8981e | 35 | } |
stretch | 0:27e403b8981e | 36 | |
stretch | 0:27e403b8981e | 37 | void Wifly::handler_img(void) { |
stretch | 0:27e403b8981e | 38 | char read = wifi.getc(); |
stretch | 0:27e403b8981e | 39 | // printf("%c", read); |
stretch | 0:27e403b8981e | 40 | if (read == '*') { |
stretch | 0:27e403b8981e | 41 | gettingImg = false; |
stretch | 0:27e403b8981e | 42 | gotImg = true; |
stretch | 0:27e403b8981e | 43 | attach_img(false); |
stretch | 0:27e403b8981e | 44 | attach_rx(true); |
stretch | 0:27e403b8981e | 45 | } |
stretch | 0:27e403b8981e | 46 | img_buffer.queue(read); |
stretch | 0:27e403b8981e | 47 | // printf("%d",img_buffer.available()); |
stretch | 0:27e403b8981e | 48 | if (img_buffer.isFull()) { |
stretch | 0:27e403b8981e | 49 | write_img(); |
stretch | 0:27e403b8981e | 50 | } |
stretch | 0:27e403b8981e | 51 | } |
stretch | 0:27e403b8981e | 52 | |
stretch | 0:27e403b8981e | 53 | void Wifly::handler_cmd(void) { |
stretch | 0:27e403b8981e | 54 | char read = wifi.getc(); |
stretch | 0:27e403b8981e | 55 | if (cmd_buffer.queue(read)) { |
stretch | 0:27e403b8981e | 56 | attach_rx(true); |
stretch | 0:27e403b8981e | 57 | } |
stretch | 0:27e403b8981e | 58 | } |
stretch | 0:27e403b8981e | 59 | |
stretch | 0:27e403b8981e | 60 | void Wifly::attach_rx(bool null) { |
stretch | 0:27e403b8981e | 61 | if (!null) { |
stretch | 0:27e403b8981e | 62 | wifi.attach(NULL); |
stretch | 0:27e403b8981e | 63 | } else { |
stretch | 0:27e403b8981e | 64 | wifi.attach(this, &Wifly::handler_rx); |
stretch | 0:27e403b8981e | 65 | } |
stretch | 0:27e403b8981e | 66 | } |
stretch | 0:27e403b8981e | 67 | |
stretch | 0:27e403b8981e | 68 | void Wifly::write_img(void) { |
stretch | 0:27e403b8981e | 69 | printf("\n\rWriting Image\n\r"); |
stretch | 0:27e403b8981e | 70 | fp = fopen("/local/pic.bmp", "a"); |
stretch | 0:27e403b8981e | 71 | char a, c; |
stretch | 0:27e403b8981e | 72 | while (!img_buffer.isEmpty()) { |
stretch | 0:27e403b8981e | 73 | img_buffer.dequeue((uint8_t *) &c); |
stretch | 0:27e403b8981e | 74 | a = (char_to_num(c)<<4) & 0xf0; |
stretch | 0:27e403b8981e | 75 | img_buffer.dequeue((uint8_t *) &c); |
stretch | 0:27e403b8981e | 76 | a |= char_to_num(c) & 0x0f; |
stretch | 0:27e403b8981e | 77 | printf("%x",a); |
stretch | 0:27e403b8981e | 78 | fwrite(&a, 1, sizeof(char), fp); |
stretch | 0:27e403b8981e | 79 | } |
stretch | 0:27e403b8981e | 80 | fpos_t pos; |
stretch | 0:27e403b8981e | 81 | fgetpos(fp,&pos); |
stretch | 0:27e403b8981e | 82 | fclose(fp); |
stretch | 0:27e403b8981e | 83 | wifi.printf("Did it!"); |
stretch | 0:27e403b8981e | 84 | } |
stretch | 0:27e403b8981e | 85 | |
stretch | 0:27e403b8981e | 86 | void Wifly::attach_img(bool null) { |
stretch | 0:27e403b8981e | 87 | printf("img "); |
stretch | 0:27e403b8981e | 88 | if (!null) { |
stretch | 0:27e403b8981e | 89 | wifi.attach(NULL); |
stretch | 0:27e403b8981e | 90 | if (!img_buffer.isEmpty()) { |
stretch | 0:27e403b8981e | 91 | write_img(); |
stretch | 0:27e403b8981e | 92 | } |
stretch | 0:27e403b8981e | 93 | printf("end\n\r"); |
stretch | 0:27e403b8981e | 94 | } else { |
stretch | 0:27e403b8981e | 95 | printf("start\n\r"); |
stretch | 0:27e403b8981e | 96 | remove("/local/pic.bmp"); |
stretch | 0:27e403b8981e | 97 | img_buffer.clear(); |
stretch | 0:27e403b8981e | 98 | wifi.attach(this, &Wifly::handler_img); |
stretch | 0:27e403b8981e | 99 | } |
stretch | 0:27e403b8981e | 100 | } |
stretch | 0:27e403b8981e | 101 | |
stretch | 0:27e403b8981e | 102 | void Wifly::attach_cmd(bool null) { |
stretch | 0:27e403b8981e | 103 | if (!null) |
stretch | 0:27e403b8981e | 104 | wifi.attach(NULL); |
stretch | 0:27e403b8981e | 105 | else |
stretch | 0:27e403b8981e | 106 | wifi.attach(this, &Wifly::handler_cmd); |
stretch | 0:27e403b8981e | 107 | } |
stretch | 0:27e403b8981e | 108 | |
stretch | 0:27e403b8981e | 109 | bool Wifly::send(char * str, char * ACK, char * res) { |
stretch | 0:27e403b8981e | 110 | char read; |
stretch | 0:27e403b8981e | 111 | size_t found = string::npos; |
stretch | 0:27e403b8981e | 112 | string checking; |
stretch | 0:27e403b8981e | 113 | Timer tmr; |
stretch | 0:27e403b8981e | 114 | |
stretch | 0:27e403b8981e | 115 | #ifdef DEBUG |
stretch | 0:27e403b8981e | 116 | printf("will send: %s\r\n",str); |
stretch | 0:27e403b8981e | 117 | #endif |
stretch | 0:27e403b8981e | 118 | |
stretch | 0:27e403b8981e | 119 | |
stretch | 0:27e403b8981e | 120 | attach_rx(false); |
stretch | 0:27e403b8981e | 121 | if (!strcmp(ACK, "NO")) { |
stretch | 0:27e403b8981e | 122 | wifi.printf("%s", str); |
stretch | 0:27e403b8981e | 123 | } else { |
stretch | 0:27e403b8981e | 124 | //We flush the buffer |
stretch | 0:27e403b8981e | 125 | while (wifi.readable()){ |
stretch | 0:27e403b8981e | 126 | wifi.getc(); |
stretch | 0:27e403b8981e | 127 | } |
stretch | 0:27e403b8981e | 128 | |
stretch | 0:27e403b8981e | 129 | tmr.start(); |
stretch | 0:27e403b8981e | 130 | wifi.printf("%s", str); |
stretch | 0:27e403b8981e | 131 | |
stretch | 0:27e403b8981e | 132 | while (1) { |
stretch | 0:27e403b8981e | 133 | if (tmr.read() > 3) { |
stretch | 0:27e403b8981e | 134 | //We flush the buffer |
stretch | 0:27e403b8981e | 135 | while (wifi.readable()) |
stretch | 0:27e403b8981e | 136 | wifi.getc(); |
stretch | 0:27e403b8981e | 137 | #ifdef DEBUG |
stretch | 0:27e403b8981e | 138 | printf("check: %s\r\n", checking.c_str()); |
stretch | 0:27e403b8981e | 139 | #endif |
stretch | 0:27e403b8981e | 140 | attach_rx(true); |
stretch | 0:27e403b8981e | 141 | return false; |
stretch | 0:27e403b8981e | 142 | } else if (wifi.readable()) { |
stretch | 0:27e403b8981e | 143 | read = wifi.getc(); |
stretch | 0:27e403b8981e | 144 | if ( read != '\r' && read != '\n') { |
stretch | 0:27e403b8981e | 145 | checking += read; |
stretch | 0:27e403b8981e | 146 | found = checking.find(ACK); |
stretch | 0:27e403b8981e | 147 | if (found != string::npos) { |
stretch | 0:27e403b8981e | 148 | wait(0.01); |
stretch | 0:27e403b8981e | 149 | |
stretch | 0:27e403b8981e | 150 | //We flush the buffer |
stretch | 0:27e403b8981e | 151 | while (wifi.readable()) |
stretch | 0:27e403b8981e | 152 | wifi.getc(); |
stretch | 0:27e403b8981e | 153 | |
stretch | 0:27e403b8981e | 154 | break; |
stretch | 0:27e403b8981e | 155 | } |
stretch | 0:27e403b8981e | 156 | } |
stretch | 0:27e403b8981e | 157 | } |
stretch | 0:27e403b8981e | 158 | } |
stretch | 0:27e403b8981e | 159 | #ifdef DEBUG |
stretch | 0:27e403b8981e | 160 | printf("check: %s\r\n", checking.c_str()); |
stretch | 0:27e403b8981e | 161 | #endif |
stretch | 0:27e403b8981e | 162 | attach_rx(true); |
stretch | 0:27e403b8981e | 163 | return true; |
stretch | 0:27e403b8981e | 164 | } |
stretch | 0:27e403b8981e | 165 | |
stretch | 0:27e403b8981e | 166 | //the user wants the result from the command (ACK == "NO", res != NULL) |
stretch | 0:27e403b8981e | 167 | if ( res != NULL) { |
stretch | 0:27e403b8981e | 168 | int i = 0; |
stretch | 0:27e403b8981e | 169 | Timer timeout; |
stretch | 0:27e403b8981e | 170 | timeout.start(); |
stretch | 0:27e403b8981e | 171 | while (1) { |
stretch | 0:27e403b8981e | 172 | if (timeout.read() > 3) { |
stretch | 0:27e403b8981e | 173 | read = NULL; |
stretch | 0:27e403b8981e | 174 | return false; |
stretch | 0:27e403b8981e | 175 | } else { |
stretch | 0:27e403b8981e | 176 | if (tmr.read_ms() > 500) { |
stretch | 0:27e403b8981e | 177 | res[i] = 0; |
stretch | 0:27e403b8981e | 178 | #ifdef DEBUG |
stretch | 0:27e403b8981e | 179 | printf("user str: %s\r\n", res); |
stretch | 0:27e403b8981e | 180 | #endif |
stretch | 0:27e403b8981e | 181 | attach_rx(true); |
stretch | 0:27e403b8981e | 182 | return true; |
stretch | 0:27e403b8981e | 183 | } |
stretch | 0:27e403b8981e | 184 | if (wifi.readable()) { |
stretch | 0:27e403b8981e | 185 | tmr.start(); |
stretch | 0:27e403b8981e | 186 | read = wifi.getc(); |
stretch | 0:27e403b8981e | 187 | if ( read != '\r' && read != '\n') { |
stretch | 0:27e403b8981e | 188 | res[i++] = read; |
stretch | 0:27e403b8981e | 189 | } |
stretch | 0:27e403b8981e | 190 | } |
stretch | 0:27e403b8981e | 191 | } |
stretch | 0:27e403b8981e | 192 | } |
stretch | 0:27e403b8981e | 193 | } |
stretch | 0:27e403b8981e | 194 | attach_rx(true); |
stretch | 0:27e403b8981e | 195 | return true; |
stretch | 0:27e403b8981e | 196 | } |
stretch | 0:27e403b8981e | 197 | |
stretch | 0:27e403b8981e | 198 | bool Wifly::attach_interrupt(char cmd, void (*func)(Command *), int num) { |
stretch | 0:27e403b8981e | 199 | return cmd_buffer.attach_interrupt(cmd, func, num); |
stretch | 0:27e403b8981e | 200 | } |
stretch | 0:27e403b8981e | 201 | |
stretch | 0:27e403b8981e | 202 | bool Wifly::hasCmd() { |
stretch | 0:27e403b8981e | 203 | return cmd_buffer.available(); |
stretch | 0:27e403b8981e | 204 | } |
stretch | 0:27e403b8981e | 205 | |
stretch | 0:27e403b8981e | 206 | Command * Wifly::getCmd() { |
stretch | 0:27e403b8981e | 207 | Command * c = NULL; |
stretch | 0:27e403b8981e | 208 | if (cmd_buffer.available()){ |
stretch | 0:27e403b8981e | 209 | c = cmd_buffer.dequeue(); |
stretch | 0:27e403b8981e | 210 | } |
stretch | 0:27e403b8981e | 211 | return c; |
stretch | 0:27e403b8981e | 212 | } |
stretch | 0:27e403b8981e | 213 | |
stretch | 0:27e403b8981e | 214 | bool Wifly::sendCmd(Command * c){ |
stretch | 0:27e403b8981e | 215 | char str[4 + 5*9]; |
stretch | 0:27e403b8981e | 216 | str[0] = 'g'; |
stretch | 0:27e403b8981e | 217 | str[1] = '0'; |
stretch | 0:27e403b8981e | 218 | str[2] = num_to_char(c->cmd); |
stretch | 0:27e403b8981e | 219 | str[3] = ' '; |
stretch | 0:27e403b8981e | 220 | switch(c->cmd){ |
stretch | 0:27e403b8981e | 221 | case 7: |
stretch | 0:27e403b8981e | 222 | for (int i=0; i < 5; i++) { |
stretch | 0:27e403b8981e | 223 | long_to_str(str + 4 + 9*i, c->l[i]); |
stretch | 0:27e403b8981e | 224 | } |
stretch | 0:27e403b8981e | 225 | str[4 + 5*9 - 1] = '\0'; |
stretch | 0:27e403b8981e | 226 | return send(str); |
stretch | 0:27e403b8981e | 227 | case 9: |
stretch | 0:27e403b8981e | 228 | for (int i=0; i < 2; i++) { |
stretch | 0:27e403b8981e | 229 | long_to_str(str + 4 + 9*i, c->l[i]); |
stretch | 0:27e403b8981e | 230 | } |
stretch | 0:27e403b8981e | 231 | str[4 + 2*9 - 1] = '\0'; |
stretch | 0:27e403b8981e | 232 | return send(str); |
stretch | 0:27e403b8981e | 233 | case 8: |
stretch | 0:27e403b8981e | 234 | default: |
stretch | 0:27e403b8981e | 235 | for (int i=0; i < 5; i++) { |
stretch | 0:27e403b8981e | 236 | long_to_str(str + 4 + 9*i, c->l[i]); |
stretch | 0:27e403b8981e | 237 | str[12 + 9 * i] = ' '; |
stretch | 0:27e403b8981e | 238 | } |
stretch | 0:27e403b8981e | 239 | str[4 + 5*9 - 1] = '\0'; |
stretch | 0:27e403b8981e | 240 | return send(str); |
stretch | 0:27e403b8981e | 241 | } |
stretch | 0:27e403b8981e | 242 | } |
stretch | 0:27e403b8981e | 243 | |
stretch | 0:27e403b8981e | 244 | bool Wifly::createAdhocNetwork() { |
stretch | 0:27e403b8981e | 245 | if (adhoc) { |
stretch | 0:27e403b8981e | 246 | char cmd[50]; |
stretch | 0:27e403b8981e | 247 | |
stretch | 0:27e403b8981e | 248 | if (!cmdMode()) { |
stretch | 0:27e403b8981e | 249 | #ifdef DEBUG |
stretch | 0:27e403b8981e | 250 | printf("Wifly::CreateAdhocNetwork: Failed to enter cmd mode. Retrying...\r\n"); |
stretch | 0:27e403b8981e | 251 | #endif |
stretch | 0:27e403b8981e | 252 | if (!cmdMode()) { |
stretch | 0:27e403b8981e | 253 | #ifdef DEBUG |
stretch | 0:27e403b8981e | 254 | printf("Wifly::CreateAdhocNetwork: Cannot enter cmd mode.\r\n"); |
stretch | 0:27e403b8981e | 255 | #endif |
stretch | 0:27e403b8981e | 256 | exit(); |
stretch | 0:27e403b8981e | 257 | return false; |
stretch | 0:27e403b8981e | 258 | } |
stretch | 0:27e403b8981e | 259 | } |
stretch | 0:27e403b8981e | 260 | |
stretch | 0:27e403b8981e | 261 | if (!send("set w j 4\r", "AOK")) { |
stretch | 0:27e403b8981e | 262 | #ifdef DEBUG |
stretch | 0:27e403b8981e | 263 | printf("Wifly::CreateAdhocNetwork: cannot set join 4\r\n"); |
stretch | 0:27e403b8981e | 264 | #endif |
stretch | 0:27e403b8981e | 265 | exit(); |
stretch | 0:27e403b8981e | 266 | return false; |
stretch | 0:27e403b8981e | 267 | } |
stretch | 0:27e403b8981e | 268 | |
stretch | 0:27e403b8981e | 269 | //no echo |
stretch | 0:27e403b8981e | 270 | if (!send("set u m 1\r", "AOK")) { |
stretch | 0:27e403b8981e | 271 | #ifdef DEBUG |
stretch | 0:27e403b8981e | 272 | printf("Wifly::CreateAdhocNetwork: cannot set no echo\r\n"); |
stretch | 0:27e403b8981e | 273 | #endif |
stretch | 0:27e403b8981e | 274 | exit(); |
stretch | 0:27e403b8981e | 275 | return false; |
stretch | 0:27e403b8981e | 276 | } |
stretch | 0:27e403b8981e | 277 | |
stretch | 0:27e403b8981e | 278 | //ssid |
stretch | 0:27e403b8981e | 279 | sprintf(cmd, "set w s %s\r", ssid); |
stretch | 0:27e403b8981e | 280 | if (!send(cmd, "AOK")) { |
stretch | 0:27e403b8981e | 281 | #ifdef DEBUG |
stretch | 0:27e403b8981e | 282 | printf("Wifly::CreateAdhocNetwork: cannot set ssid\r\n"); |
stretch | 0:27e403b8981e | 283 | #endif |
stretch | 0:27e403b8981e | 284 | exit(); |
stretch | 0:27e403b8981e | 285 | return false; |
stretch | 0:27e403b8981e | 286 | } |
stretch | 0:27e403b8981e | 287 | |
stretch | 0:27e403b8981e | 288 | sprintf(cmd, "set w c %d\r", channel); |
stretch | 0:27e403b8981e | 289 | if (!send(cmd, "AOK")) { |
stretch | 0:27e403b8981e | 290 | #ifdef DEBUG |
stretch | 0:27e403b8981e | 291 | printf("Wifly::CreateAdhocNetwork: cannot set channel\r\n"); |
stretch | 0:27e403b8981e | 292 | #endif |
stretch | 0:27e403b8981e | 293 | exit(); |
stretch | 0:27e403b8981e | 294 | return false; |
stretch | 0:27e403b8981e | 295 | } |
stretch | 0:27e403b8981e | 296 | |
stretch | 0:27e403b8981e | 297 | sprintf(cmd, "set i a %s\r", ip); |
stretch | 0:27e403b8981e | 298 | if (!send(cmd, "AOK")) { |
stretch | 0:27e403b8981e | 299 | #ifdef DEBUG |
stretch | 0:27e403b8981e | 300 | printf("Wifly::CreateAdhocNetwork: cannot set ip address\r\n"); |
stretch | 0:27e403b8981e | 301 | #endif |
stretch | 0:27e403b8981e | 302 | exit(); |
stretch | 0:27e403b8981e | 303 | return false; |
stretch | 0:27e403b8981e | 304 | } |
stretch | 0:27e403b8981e | 305 | |
stretch | 0:27e403b8981e | 306 | sprintf(cmd, "set i n %s\r", netmask); |
stretch | 0:27e403b8981e | 307 | if (!send(cmd, "AOK")) { |
stretch | 0:27e403b8981e | 308 | #ifdef DEBUG |
stretch | 0:27e403b8981e | 309 | printf("Wifly::CreateAdhocNetwork: cannot set netmask\r\n"); |
stretch | 0:27e403b8981e | 310 | #endif |
stretch | 0:27e403b8981e | 311 | exit(); |
stretch | 0:27e403b8981e | 312 | return false; |
stretch | 0:27e403b8981e | 313 | } |
stretch | 0:27e403b8981e | 314 | |
stretch | 0:27e403b8981e | 315 | if (!send("set i d 0\r", "AOK")) { |
stretch | 0:27e403b8981e | 316 | #ifdef DEBUG |
stretch | 0:27e403b8981e | 317 | printf("Wifly::CreateAdhocNetwork: cannot set dhcp off\r\n"); |
stretch | 0:27e403b8981e | 318 | #endif |
stretch | 0:27e403b8981e | 319 | exit(); |
stretch | 0:27e403b8981e | 320 | return false; |
stretch | 0:27e403b8981e | 321 | } |
stretch | 0:27e403b8981e | 322 | |
stretch | 0:27e403b8981e | 323 | if (!send("save\r", "Stor")) { |
stretch | 0:27e403b8981e | 324 | #ifdef DEBUG |
stretch | 0:27e403b8981e | 325 | printf("Wifly::CreateAdhocNetwork: cannot save\r\n"); |
stretch | 0:27e403b8981e | 326 | #endif |
stretch | 0:27e403b8981e | 327 | exit(); |
stretch | 0:27e403b8981e | 328 | return false; |
stretch | 0:27e403b8981e | 329 | } |
stretch | 0:27e403b8981e | 330 | |
stretch | 0:27e403b8981e | 331 | flush(); |
stretch | 0:27e403b8981e | 332 | |
stretch | 0:27e403b8981e | 333 | send("reboot\r", "NO"); |
stretch | 0:27e403b8981e | 334 | #ifdef DEBUG |
stretch | 0:27e403b8981e | 335 | printf("\r\ncreating an adhoc\r\nnetwork: %s\r\nip: %s\r\nnetmask: %s\r\nchannel: %d\r\n\r\n", ssid, ip, netmask, channel); |
stretch | 0:27e403b8981e | 336 | #endif |
stretch | 0:27e403b8981e | 337 | |
stretch | 0:27e403b8981e | 338 | wait(0.1); |
stretch | 0:27e403b8981e | 339 | flush(); |
stretch | 0:27e403b8981e | 340 | return true; |
stretch | 0:27e403b8981e | 341 | } else { |
stretch | 0:27e403b8981e | 342 | #ifdef DEBUG |
stretch | 0:27e403b8981e | 343 | printf("Wifly::CreateAdhocNetwork: You didn't choose the right constructor for creating an adhoc mode!\r\n"); |
stretch | 0:27e403b8981e | 344 | #endif |
stretch | 0:27e403b8981e | 345 | return false; |
stretch | 0:27e403b8981e | 346 | } |
stretch | 0:27e403b8981e | 347 | } |
stretch | 0:27e403b8981e | 348 | |
stretch | 0:27e403b8981e | 349 | void Wifly::flush() { |
stretch | 0:27e403b8981e | 350 | while (readable()) |
stretch | 0:27e403b8981e | 351 | getc(); |
stretch | 0:27e403b8981e | 352 | } |
stretch | 0:27e403b8981e | 353 | |
stretch | 0:27e403b8981e | 354 | bool Wifly::cmdMode() { |
stretch | 0:27e403b8981e | 355 | if (!send("$$$", "CMD")) { |
stretch | 0:27e403b8981e | 356 | #ifdef DEBUG |
stretch | 0:27e403b8981e | 357 | printf("Wifly::cmdMode: cannot enter in cmd mode\r\n"); |
stretch | 0:27e403b8981e | 358 | #endif |
stretch | 0:27e403b8981e | 359 | return false; |
stretch | 0:27e403b8981e | 360 | } |
stretch | 0:27e403b8981e | 361 | return true; |
stretch | 0:27e403b8981e | 362 | } |
stretch | 0:27e403b8981e | 363 | |
stretch | 0:27e403b8981e | 364 | void Wifly::reset() { |
stretch | 0:27e403b8981e | 365 | reset_pin = 0; |
stretch | 0:27e403b8981e | 366 | wait(0.2); |
stretch | 0:27e403b8981e | 367 | reset_pin = 1; |
stretch | 0:27e403b8981e | 368 | wait(0.2); |
stretch | 0:27e403b8981e | 369 | } |
stretch | 0:27e403b8981e | 370 | |
stretch | 0:27e403b8981e | 371 | void Wifly::putc(char c) { |
stretch | 0:27e403b8981e | 372 | while (!wifi.writeable()); |
stretch | 0:27e403b8981e | 373 | wifi.putc(c); |
stretch | 0:27e403b8981e | 374 | } |
stretch | 0:27e403b8981e | 375 | |
stretch | 0:27e403b8981e | 376 | bool Wifly::read(char * str) { |
stretch | 0:27e403b8981e | 377 | int length = buf_wifly.available(); |
stretch | 0:27e403b8981e | 378 | if (length == 0) |
stretch | 0:27e403b8981e | 379 | return false; |
stretch | 0:27e403b8981e | 380 | for (int i = 0; i < length; i++) |
stretch | 0:27e403b8981e | 381 | buf_wifly.dequeue((uint8_t *)&str[i]); |
stretch | 0:27e403b8981e | 382 | str[length] = 0; |
stretch | 0:27e403b8981e | 383 | return true; |
stretch | 0:27e403b8981e | 384 | } |
stretch | 0:27e403b8981e | 385 | |
stretch | 0:27e403b8981e | 386 | bool Wifly::exit() { |
stretch | 0:27e403b8981e | 387 | return send("exit\r", "EXIT"); |
stretch | 0:27e403b8981e | 388 | } |
stretch | 0:27e403b8981e | 389 | |
stretch | 0:27e403b8981e | 390 | |
stretch | 0:27e403b8981e | 391 | int Wifly::readable() { |
stretch | 0:27e403b8981e | 392 | return buf_wifly.available(); |
stretch | 0:27e403b8981e | 393 | } |
stretch | 0:27e403b8981e | 394 | |
stretch | 0:27e403b8981e | 395 | char Wifly::getc() { |
stretch | 0:27e403b8981e | 396 | char c; |
stretch | 0:27e403b8981e | 397 | while (!buf_wifly.available()); |
stretch | 0:27e403b8981e | 398 | buf_wifly.dequeue((uint8_t *)&c); |
stretch | 0:27e403b8981e | 399 | return c; |
stretch | 0:27e403b8981e | 400 | } |
stretch | 0:27e403b8981e | 401 | |
stretch | 0:27e403b8981e | 402 | char Wifly::num_to_char(char n) { |
stretch | 0:27e403b8981e | 403 | if(n <= 9) { |
stretch | 0:27e403b8981e | 404 | return n + '0'; |
stretch | 0:27e403b8981e | 405 | } else if (n >= 0xa && n <= 0xf) { |
stretch | 0:27e403b8981e | 406 | return n + 'a' - 0xa; |
stretch | 0:27e403b8981e | 407 | } else { |
stretch | 0:27e403b8981e | 408 | return 0; |
stretch | 0:27e403b8981e | 409 | } |
stretch | 0:27e403b8981e | 410 | } |
stretch | 0:27e403b8981e | 411 | |
stretch | 0:27e403b8981e | 412 | void Wifly::long_to_str(char * str, long n) { |
stretch | 0:27e403b8981e | 413 | for (int i = 0; i < 8; i += 2) { |
stretch | 0:27e403b8981e | 414 | str[i+1] = num_to_char(n & 0x0f); |
stretch | 0:27e403b8981e | 415 | n = n >> 4; |
stretch | 0:27e403b8981e | 416 | str[i] = num_to_char(n & 0x0f); |
stretch | 0:27e403b8981e | 417 | n = n >> 4; |
stretch | 0:27e403b8981e | 418 | } |
stretch | 0:27e403b8981e | 419 | } |
stretch | 0:27e403b8981e | 420 | |
stretch | 0:27e403b8981e | 421 | void Wifly::float_to_str(char * str, float f) { |
stretch | 0:27e403b8981e | 422 | long_to_str(str, (long) f); |
stretch | 0:27e403b8981e | 423 | } |
stretch | 0:27e403b8981e | 424 | |
stretch | 0:27e403b8981e | 425 | // Return 0xf if char is invalid |
stretch | 0:27e403b8981e | 426 | char Wifly::char_to_num(char c) { |
stretch | 0:27e403b8981e | 427 | if(c >= '0' && c <= '9') { |
stretch | 0:27e403b8981e | 428 | return c - '0'; |
stretch | 0:27e403b8981e | 429 | } else if(c >= 'a' && c <= 'f') { |
stretch | 0:27e403b8981e | 430 | return c - 'a' + 0xa; |
stretch | 0:27e403b8981e | 431 | } else if(c >= 'A' && c <= 'F') { |
stretch | 0:27e403b8981e | 432 | return c - 'A' + 0xa; |
stretch | 0:27e403b8981e | 433 | } else { |
stretch | 0:27e403b8981e | 434 | return 0xf; |
stretch | 0:27e403b8981e | 435 | } |
stretch | 0:27e403b8981e | 436 | } |