モータードライバとWi-FiモジュールESP-WROOM-02をmbed LPC1114FN28に繋げて、RCWControllerからコントロールするプログラム

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UDPSocket.cpp Source File

UDPSocket.cpp

00001 /* Copyright (C) 2012 mbed.org, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 
00019 #include "UDPSocket.h"
00020 
00021 #include <string>
00022 #include <algorithm>
00023 
00024 UDPSocket::UDPSocket()
00025 {
00026     endpoint_configured = false;
00027     endpoint_read = false;
00028     Endpoint currentEndpoint;
00029 }
00030 
00031 int UDPSocket::init(void)
00032 {
00033     return 0;
00034 }
00035 
00036 // Server initialization
00037 int UDPSocket::bind(int port)
00038 {
00039     return 0;
00040 }
00041 
00042 // -1 if unsuccessful, else number of bytes written
00043 int UDPSocket::sendTo(Endpoint &remote, char *packet, int length)
00044 {
00045     Timer tmr;
00046     int idx = 0;
00047 
00048 
00049     confEndpoint(remote);
00050     
00051     // initialize transparent mode if not already done
00052     if(!endpoint_configured) {
00053         // initialize UDP (default id of -1 means transparent mode)
00054         //!wifi->start(ESP_UDP_TYPE, remote._ipAddress, remote._port, remote._id
00055         if(!wifi->startUDP(remote._ipAddress, remote._port, 0,length)) {
00056             return(-1);
00057         }
00058         endpoint_configured = true;
00059     }
00060     
00061     tmr.start();
00062 
00063     while ((tmr.read_ms() < _timeout) || _blocking) {
00064 
00065         idx += wifi->send(packet, length);
00066 
00067         if (idx == length)
00068             return idx;
00069     }
00070     return (idx == 0) ? -1 : idx;
00071 }
00072 
00073 // -1 if unsuccessful, else number of bytes received
00074 int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length)
00075 {
00076     Timer tmr;
00077     int idx = 0;
00078     int nb_available = 0;
00079     int time = -1;
00080 
00081     //make this the non-blocking case and return if <= 0
00082     // remember to change the config to blocking
00083     // if ( ! _blocking) {
00084     // if ( wifi.readable <= 0 ) {
00085     // return (wifi.readable);
00086     // }
00087     // }
00088     //---
00089     tmr.start();
00090     if (_blocking) {
00091         while (1) {
00092             nb_available = wifi->readable();
00093             if (nb_available != 0) {
00094                 break;
00095             }
00096         }
00097     }
00098     //---
00099     // blocking case
00100     else {
00101         tmr.reset();
00102 
00103         while (time < _timeout) {
00104             nb_available = wifi->readable();
00105             if (nb_available < 0) return nb_available;
00106             if (nb_available > 0) break ;
00107             time = tmr.read_ms();
00108         }
00109 
00110         if (nb_available == 0) return nb_available;
00111     }
00112 
00113     // change this to < 20 mS timeout per byte to detect end of packet gap
00114     // this may not work due to buffering at the UART interface
00115     tmr.reset();
00116     // while ( tmr.read_ms() < 20 ) {
00117     // if ( wifi.readable() && (idx < length) ) {
00118     // buffer[idx++] = wifi->getc();
00119     // tmr.reset();
00120     // }
00121     // if ( idx == length ) {
00122     // break;
00123     // }
00124     // }
00125     //---
00126     while (time < _timeout) {
00127 
00128         nb_available = wifi->readable();
00129         //for (int i = 0; i < min(nb_available, length); i++) {
00130         for (int i = 0; i < min(nb_available, (length-idx)); i++) {
00131             buffer[idx] = wifi->getc();
00132             idx++;
00133         }
00134         if (idx == length) {
00135             break;
00136         }
00137         time = tmr.read_ms();
00138     }
00139     //---
00140     readEndpoint(remote);
00141     return (idx == 0) ? -1 : idx;
00142 }
00143 
00144 bool UDPSocket::confEndpoint(Endpoint & ep)
00145 {
00146     currentEndpoint = ep;
00147     return true;
00148 }
00149 
00150 bool UDPSocket::readEndpoint(Endpoint & ep)
00151 {
00152     ep = currentEndpoint;
00153     return true;
00154 }