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

Dependencies:   mbed

Committer:
jksoft
Date:
Fri Jul 22 05:36:02 2016 +0000
Revision:
0:3c24a40c2343
??

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:3c24a40c2343 1 /* Copyright (C) 2012 mbed.org, MIT License
jksoft 0:3c24a40c2343 2 *
jksoft 0:3c24a40c2343 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
jksoft 0:3c24a40c2343 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
jksoft 0:3c24a40c2343 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
jksoft 0:3c24a40c2343 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
jksoft 0:3c24a40c2343 7 * furnished to do so, subject to the following conditions:
jksoft 0:3c24a40c2343 8 *
jksoft 0:3c24a40c2343 9 * The above copyright notice and this permission notice shall be included in all copies or
jksoft 0:3c24a40c2343 10 * substantial portions of the Software.
jksoft 0:3c24a40c2343 11 *
jksoft 0:3c24a40c2343 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
jksoft 0:3c24a40c2343 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
jksoft 0:3c24a40c2343 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
jksoft 0:3c24a40c2343 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jksoft 0:3c24a40c2343 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
jksoft 0:3c24a40c2343 17 */
jksoft 0:3c24a40c2343 18
jksoft 0:3c24a40c2343 19 #include "UDPSocket.h"
jksoft 0:3c24a40c2343 20
jksoft 0:3c24a40c2343 21 #include <string>
jksoft 0:3c24a40c2343 22 #include <algorithm>
jksoft 0:3c24a40c2343 23
jksoft 0:3c24a40c2343 24 UDPSocket::UDPSocket()
jksoft 0:3c24a40c2343 25 {
jksoft 0:3c24a40c2343 26 endpoint_configured = false;
jksoft 0:3c24a40c2343 27 endpoint_read = false;
jksoft 0:3c24a40c2343 28 Endpoint currentEndpoint;
jksoft 0:3c24a40c2343 29 }
jksoft 0:3c24a40c2343 30
jksoft 0:3c24a40c2343 31 int UDPSocket::init(void)
jksoft 0:3c24a40c2343 32 {
jksoft 0:3c24a40c2343 33 return 0;
jksoft 0:3c24a40c2343 34 }
jksoft 0:3c24a40c2343 35
jksoft 0:3c24a40c2343 36 // Server initialization
jksoft 0:3c24a40c2343 37 int UDPSocket::bind(int port)
jksoft 0:3c24a40c2343 38 {
jksoft 0:3c24a40c2343 39 return 0;
jksoft 0:3c24a40c2343 40 }
jksoft 0:3c24a40c2343 41
jksoft 0:3c24a40c2343 42 // -1 if unsuccessful, else number of bytes written
jksoft 0:3c24a40c2343 43 int UDPSocket::sendTo(Endpoint &remote, char *packet, int length)
jksoft 0:3c24a40c2343 44 {
jksoft 0:3c24a40c2343 45 Timer tmr;
jksoft 0:3c24a40c2343 46 int idx = 0;
jksoft 0:3c24a40c2343 47
jksoft 0:3c24a40c2343 48
jksoft 0:3c24a40c2343 49 confEndpoint(remote);
jksoft 0:3c24a40c2343 50
jksoft 0:3c24a40c2343 51 // initialize transparent mode if not already done
jksoft 0:3c24a40c2343 52 if(!endpoint_configured) {
jksoft 0:3c24a40c2343 53 // initialize UDP (default id of -1 means transparent mode)
jksoft 0:3c24a40c2343 54 //!wifi->start(ESP_UDP_TYPE, remote._ipAddress, remote._port, remote._id
jksoft 0:3c24a40c2343 55 if(!wifi->startUDP(remote._ipAddress, remote._port, 0,length)) {
jksoft 0:3c24a40c2343 56 return(-1);
jksoft 0:3c24a40c2343 57 }
jksoft 0:3c24a40c2343 58 endpoint_configured = true;
jksoft 0:3c24a40c2343 59 }
jksoft 0:3c24a40c2343 60
jksoft 0:3c24a40c2343 61 tmr.start();
jksoft 0:3c24a40c2343 62
jksoft 0:3c24a40c2343 63 while ((tmr.read_ms() < _timeout) || _blocking) {
jksoft 0:3c24a40c2343 64
jksoft 0:3c24a40c2343 65 idx += wifi->send(packet, length);
jksoft 0:3c24a40c2343 66
jksoft 0:3c24a40c2343 67 if (idx == length)
jksoft 0:3c24a40c2343 68 return idx;
jksoft 0:3c24a40c2343 69 }
jksoft 0:3c24a40c2343 70 return (idx == 0) ? -1 : idx;
jksoft 0:3c24a40c2343 71 }
jksoft 0:3c24a40c2343 72
jksoft 0:3c24a40c2343 73 // -1 if unsuccessful, else number of bytes received
jksoft 0:3c24a40c2343 74 int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length)
jksoft 0:3c24a40c2343 75 {
jksoft 0:3c24a40c2343 76 Timer tmr;
jksoft 0:3c24a40c2343 77 int idx = 0;
jksoft 0:3c24a40c2343 78 int nb_available = 0;
jksoft 0:3c24a40c2343 79 int time = -1;
jksoft 0:3c24a40c2343 80
jksoft 0:3c24a40c2343 81 //make this the non-blocking case and return if <= 0
jksoft 0:3c24a40c2343 82 // remember to change the config to blocking
jksoft 0:3c24a40c2343 83 // if ( ! _blocking) {
jksoft 0:3c24a40c2343 84 // if ( wifi.readable <= 0 ) {
jksoft 0:3c24a40c2343 85 // return (wifi.readable);
jksoft 0:3c24a40c2343 86 // }
jksoft 0:3c24a40c2343 87 // }
jksoft 0:3c24a40c2343 88 //---
jksoft 0:3c24a40c2343 89 tmr.start();
jksoft 0:3c24a40c2343 90 if (_blocking) {
jksoft 0:3c24a40c2343 91 while (1) {
jksoft 0:3c24a40c2343 92 nb_available = wifi->readable();
jksoft 0:3c24a40c2343 93 if (nb_available != 0) {
jksoft 0:3c24a40c2343 94 break;
jksoft 0:3c24a40c2343 95 }
jksoft 0:3c24a40c2343 96 }
jksoft 0:3c24a40c2343 97 }
jksoft 0:3c24a40c2343 98 //---
jksoft 0:3c24a40c2343 99 // blocking case
jksoft 0:3c24a40c2343 100 else {
jksoft 0:3c24a40c2343 101 tmr.reset();
jksoft 0:3c24a40c2343 102
jksoft 0:3c24a40c2343 103 while (time < _timeout) {
jksoft 0:3c24a40c2343 104 nb_available = wifi->readable();
jksoft 0:3c24a40c2343 105 if (nb_available < 0) return nb_available;
jksoft 0:3c24a40c2343 106 if (nb_available > 0) break ;
jksoft 0:3c24a40c2343 107 time = tmr.read_ms();
jksoft 0:3c24a40c2343 108 }
jksoft 0:3c24a40c2343 109
jksoft 0:3c24a40c2343 110 if (nb_available == 0) return nb_available;
jksoft 0:3c24a40c2343 111 }
jksoft 0:3c24a40c2343 112
jksoft 0:3c24a40c2343 113 // change this to < 20 mS timeout per byte to detect end of packet gap
jksoft 0:3c24a40c2343 114 // this may not work due to buffering at the UART interface
jksoft 0:3c24a40c2343 115 tmr.reset();
jksoft 0:3c24a40c2343 116 // while ( tmr.read_ms() < 20 ) {
jksoft 0:3c24a40c2343 117 // if ( wifi.readable() && (idx < length) ) {
jksoft 0:3c24a40c2343 118 // buffer[idx++] = wifi->getc();
jksoft 0:3c24a40c2343 119 // tmr.reset();
jksoft 0:3c24a40c2343 120 // }
jksoft 0:3c24a40c2343 121 // if ( idx == length ) {
jksoft 0:3c24a40c2343 122 // break;
jksoft 0:3c24a40c2343 123 // }
jksoft 0:3c24a40c2343 124 // }
jksoft 0:3c24a40c2343 125 //---
jksoft 0:3c24a40c2343 126 while (time < _timeout) {
jksoft 0:3c24a40c2343 127
jksoft 0:3c24a40c2343 128 nb_available = wifi->readable();
jksoft 0:3c24a40c2343 129 //for (int i = 0; i < min(nb_available, length); i++) {
jksoft 0:3c24a40c2343 130 for (int i = 0; i < min(nb_available, (length-idx)); i++) {
jksoft 0:3c24a40c2343 131 buffer[idx] = wifi->getc();
jksoft 0:3c24a40c2343 132 idx++;
jksoft 0:3c24a40c2343 133 }
jksoft 0:3c24a40c2343 134 if (idx == length) {
jksoft 0:3c24a40c2343 135 break;
jksoft 0:3c24a40c2343 136 }
jksoft 0:3c24a40c2343 137 time = tmr.read_ms();
jksoft 0:3c24a40c2343 138 }
jksoft 0:3c24a40c2343 139 //---
jksoft 0:3c24a40c2343 140 readEndpoint(remote);
jksoft 0:3c24a40c2343 141 return (idx == 0) ? -1 : idx;
jksoft 0:3c24a40c2343 142 }
jksoft 0:3c24a40c2343 143
jksoft 0:3c24a40c2343 144 bool UDPSocket::confEndpoint(Endpoint & ep)
jksoft 0:3c24a40c2343 145 {
jksoft 0:3c24a40c2343 146 currentEndpoint = ep;
jksoft 0:3c24a40c2343 147 return true;
jksoft 0:3c24a40c2343 148 }
jksoft 0:3c24a40c2343 149
jksoft 0:3c24a40c2343 150 bool UDPSocket::readEndpoint(Endpoint & ep)
jksoft 0:3c24a40c2343 151 {
jksoft 0:3c24a40c2343 152 ep = currentEndpoint;
jksoft 0:3c24a40c2343 153 return true;
jksoft 0:3c24a40c2343 154 }