Same as mallet... but distance
Dependencies: EthernetInterface NetworkAPI mbed-rtos mbed
Fork of MalletFirmware by
Revision 21:1fb5023b72af, committed 2014-12-03
- Comitter:
- timmey9
- Date:
- Wed Dec 03 03:28:54 2014 +0000
- Parent:
- 20:f533b3c9296f
- Child:
- 22:523e316cbe70
- Commit message:
- API interfaces with MATLAB
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Tue Nov 18 01:31:55 2014 +0000 +++ b/main.cpp Wed Dec 03 03:28:54 2014 +0000 @@ -1,5 +1,4 @@ // Server code - #include "mbed.h" #include "EthernetInterface.h" @@ -8,29 +7,75 @@ #include "NetworkAPI/ip/address.hpp" #include "NetworkAPI/tcp/socket.hpp" -#define STATIC 1 -#ifdef STATIC -#define IP "127.0.0.5" // "192.168.1.5" -#define MASK "255.255.255.0" -#define GATEWAY "127.0.0.1"//"192.168.1.1" -#define MAC "00:02:f7:f0:00:00" +// some macro variables to set before compiling +#define MALLET 6 // set mallet to a value between 1-7 +#define STATIC 1 // set STATIC to 1 for static ip, set STATIC to 0 for dynamic +#define PORT 22 // set to a random port number. All the mallets can use the same port number. +#define MAX_CLIENTS 2 // set the max number of clients to at least 2 (first client is MATLAB, second is the distance unit) + + + +#if MALLET == 1 +extern "C" void mbed_mac_address(char *mac) { mac[0]=0x00; mac[1]=0x02; mac[2]=0xf7; mac[3]=0xf0; mac[4]=0x00; mac[5]=0x01; } // overwrites the MAC address because mbed gave them duplicate address (this is fine for a closed network) +char mac[16]; +#define NAME "Mallet1\n\r" + +#elif MALLET == 2 +extern "C" void mbed_mac_address(char *mac) { mac[0]=0x00; mac[1]=0x02; mac[2]=0xf7; mac[3]=0xf0; mac[4]=0x00; mac[5]=0x02; } // overwrites the MAC address because mbed gave them duplicate address (this is fine for a closed network) +char mac[16]; +#define NAME "Mallet2\n\r" + +#elif MALLET == 3 +extern "C" void mbed_mac_address(char *mac) { mac[0]=0x00; mac[1]=0x02; mac[2]=0xf7; mac[3]=0xf0; mac[4]=0x00; mac[5]=0x03; } // overwrites the MAC address because mbed gave them duplicate address (this is fine for a closed network) +char mac[16]; +#define NAME "Mallet3\n\r" + +#elif MALLET == 4 +extern "C" void mbed_mac_address(char *mac) { mac[0]=0x00; mac[1]=0x02; mac[2]=0xf7; mac[3]=0xf0; mac[4]=0x00; mac[5]=0x04; } // overwrites the MAC address because mbed gave them duplicate address (this is fine for a closed network) +char mac[16]; +#define NAME "Mallet4\n\r" + +#elif MALLET == 5 +extern "C" void mbed_mac_address(char *mac) { mac[0]=0x00; mac[1]=0x02; mac[2]=0xf7; mac[3]=0xf0; mac[4]=0x00; mac[5]=0x05; } // overwrites the MAC address because mbed gave them duplicate address (this is fine for a closed network) +char mac[16]; +#define NAME "Mallet5\n\r" + +#elif MALLET == 6 +extern "C" void mbed_mac_address(char *mac) { mac[0]=0x00; mac[1]=0x02; mac[2]=0xf7; mac[3]=0xf0; mac[4]=0x00; mac[5]=0x06; } // overwrites the MAC address because mbed gave them duplicate address (this is fine for a closed network) +char mac[16]; +#define NAME "Mallet6\n\r" + +#elif MALLET == 7 +extern "C" void mbed_mac_address(char *mac) { mac[0]=0x00; mac[1]=0x02; mac[2]=0xf7; mac[3]=0xf0; mac[4]=0x00; mac[5]=0x07; } // overwrites the MAC address because mbed gave them duplicate address (this is fine for a closed network) +char mac[16]; +#define NAME "Mallet7\n\r" + #endif -#define PORT 23 + + +#if STATIC == 1 +#define IP "169.254.225.206"//"127.0.0.5" // "192.168.1.5" +#define GATEWAY "169.254.225.1"//"127.0.0.1"//"192.168.1.1" +#define MASK "255.255.0.0" +#endif + + Serial pc(USBTX, USBRX); DigitalOut led_red(LED_RED); DigitalOut led_green(LED_GREEN); DigitalOut led_blue(LED_BLUE); -uint32_t sample_array[30000]; +#define SAMPLES 5000 +uint32_t sample_array[SAMPLES]; using namespace network; -#define MAX_CLIENTS 5 - int main() { - for(int i = 0; i < 30000; i++) sample_array[i] = 0x3132; + for(int i = 0; i < SAMPLES; i++) sample_array[i] = i; + sample_array[0] = 0x12345678; + sample_array[SAMPLES-1] = 0xDEADBEEF; led_red = 1; led_green = 1; led_blue = 1; @@ -38,18 +83,20 @@ pc.printf("Starting Server\r\n"); EthernetInterface interface; - #ifdef STATIC + + #if STATIC == 1 interface.init(IP, MASK, GATEWAY); #else interface.init(); #endif + interface.connect(); + pc.printf(NAME); pc.printf("IP Address is: %s\n\r", interface.getIPAddress()); pc.printf("Network Mask is: %s\n\r", interface.getNetworkMask()); pc.printf("MAC address is: %s\n\r", interface.getMACAddress()); pc.printf("Gateway is: %s\n\r", interface.getGateway()); - - + pc.printf("Port is: %i\n\r", PORT); Select select; tcp::Socket server; @@ -59,10 +106,10 @@ int result = 0; int index = 0; - network::Buffer buffer(256); - std::string message("Hello world!"); + network::Buffer buffer(4*SAMPLES); + std::string message(NAME); - // Configure the server socket (assume everty thing works) + // Configure the server socket (assume every thing works) server.open(); server.bind(PORT); server.listen(MAX_CLIENTS); @@ -97,6 +144,7 @@ // Maximum connections reached if (index == MAX_CLIENTS) { pc.printf("Maximum connections reached\n\r"); + wait(1); continue; } @@ -106,7 +154,7 @@ client[index].getRemoteEndpoint().getAddress().toString().c_str(), client[index].getRemoteEndpoint().getPort()); - // Send a nice message to the client + // Send a nice message to the client (tell MATLAB your name client[index].write((void *)message.data(), message.size()); // read some registers for some info. @@ -140,24 +188,69 @@ socket->getRemoteEndpoint().getAddress().toString().c_str(), socket->getRemoteEndpoint().getPort()); - //pc.printf("%s\n\r", (char *)buffer.data()); + pc.printf("%s\n\r", (char *)buffer.data()); + + // read first character for command + char command[2]; + buffer.read(command,2,0); + if(command[1] == ':') { + switch(command[0]) + { + case 'b': + led_blue = !led_blue; + client[index].write((void *)"Blue LED\n",9); + break; + case 'r': + led_red = !led_red; + client[index].write((void *)"Red LED\n",8); + break; + case 'p': + led_green = !led_green; + client[index].write((void *)"Data\n",5); + client[index].write((void *)&sample_array,4*SAMPLES); + } + } + else { + + } + + //***************** print a message back to the client - Timer timeStamp; - timeStamp.stop(); - timeStamp.reset(); - timeStamp.start(); - client[index].write((void *)&sample_array,30000); - int timeStampVar = timeStamp.read_us(); - timeStamp.stop(); - wait(1); - pc.printf("*******\r\nTime taken to send data: %i\r\n", timeStampVar); + + //client[index].write((void *)&sample_array,SAMPLES); + + + - char premessage[40]; - sprintf(premessage, "Time taken to send data: %i\r\n", timeStampVar); - std::string response1 = premessage; - client[index].write((void *)response1.data(), response1.size()); - + /*for(int i = 1; i <= SAMPLES+1;) + { + for(int j = 0; j < 20; j++) + { + Timer timeStamp; + timeStamp.stop(); + timeStamp.reset(); + timeStamp.start(); + + client[index].write((void *)&sample_array,i); + int timeStampVar = timeStamp.read_us(); + timeStamp.stop(); + + pc.printf("*******\r\n%i\r\nTime taken to send data: %i\r\n", i,timeStampVar); + + char premessage[40]; + sprintf(premessage, "******\r\n%i\r\nTime taken to send data: %i\r\n", i, timeStampVar); + std::string response1 = premessage; + client[index].write((void *)response1.data(), response1.size()); + wait_us(5000); + } + if(i == 10000) i = SAMPLES; + else if(i == SAMPLES) i = i*10; + else i = i*10; + } + std::string endMessage("end"); + client[index].write((void *)endMessage.data(), endMessage.size()); + */ break; } }