Dependencies:   mbed-dsp mbed

Fork of DSP_200kHz by Mazzeo Research Group

main.cpp

Committer:
timmey9
Date:
2014-12-03
Revision:
21:1fb5023b72af
Parent:
20:f533b3c9296f
Child:
22:523e316cbe70

File content as of revision 21:1fb5023b72af:

// Server code
#include "mbed.h"
#include "EthernetInterface.h"

#include "NetworkAPI/buffer.hpp"
#include "NetworkAPI/select.hpp"
#include "NetworkAPI/ip/address.hpp"
#include "NetworkAPI/tcp/socket.hpp"

// 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



#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);

#define SAMPLES 5000
uint32_t sample_array[SAMPLES];

using namespace network;
 
int main() {
    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;
    pc.baud(230400);
    pc.printf("Starting Server\r\n");
    
    EthernetInterface interface;

    #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;
    tcp::Socket client[MAX_CLIENTS];
    tcp::Socket *socket = NULL;
     
    int result = 0;
    int index = 0;
     
    network::Buffer buffer(4*SAMPLES);
    std::string message(NAME);
     
    // Configure the server socket (assume every thing works)
    server.open();
    server.bind(PORT);
    server.listen(MAX_CLIENTS);
   
    // Add sockets to the select api
    select.set(&server, Select::Read);
    for (index = 0; index < MAX_CLIENTS; index++) {
        select.set(&client[index], Select::Read);
    }
     
    do {
        // Wait for activity
        result = select.wait();
        if (result < -1) {
            pc.printf("Failed to select\n\r");
            break;
        }
         
        // Get the first socket
        socket = (tcp::Socket *)select.getReadable();
         
        for (; socket != NULL; socket = (tcp::Socket *)select.getReadable()) {
            // Check if there was a connection request.
            if (socket->getHandle() == server.getHandle()) {                
                // Find an unused client
                for (index = 0; index < MAX_CLIENTS; index++) {
                    if (client[index].getStatus() == network::Socket::Closed) {
                        break;
                    }
                }
                 
                // Maximum connections reached
                if (index == MAX_CLIENTS) {
                    pc.printf("Maximum connections reached\n\r");
                    wait(1);
                    continue;
                }
                
                // Accept the client
                socket->accept(client[index]);
                pc.printf("Client connected %s:%d\n\r",
                    client[index].getRemoteEndpoint().getAddress().toString().c_str(),
                    client[index].getRemoteEndpoint().getPort());
                     
                // 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.
                //uint32_t* rcr = (uint32_t*) 0x400C0084;
                //uint32_t* ecr = (uint32_t*) 0x400C0024;
                //pc.printf("RCR register: %x\r\n", *rcr);
                //pc.printf("ECR register: %x\r\n", *ecr);
                
                continue;
            }
            
            // It was not the server socket, so it must be a client talking to us.
            switch (socket->read(buffer)) {
                case 0:
                    // Remote end disconnected
                    pc.printf("Client disconnected %s:%d\n\r",
                        socket->getRemoteEndpoint().getAddress().toString().c_str(),
                        socket->getRemoteEndpoint().getPort());
                     
                    // Close socket
                    socket->close();
                    break;
                
                case -1:
                    pc.printf("Error while reading data from socket\n\r");
                    socket->close();
                    break;
//************* this is where data is printed to the screen
                default:
                    pc.printf("Message from %s:%d\n\r",
                        socket->getRemoteEndpoint().getAddress().toString().c_str(),
                        socket->getRemoteEndpoint().getPort());
                         
                    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
                    
                    //client[index].write((void *)&sample_array,SAMPLES);
                    
                    
            
                    
                    /*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;
            }
        }
             
    } while (server.getStatus() == network::Socket::Listening);
}