A WiFiDipCortex based robot. Control is via sockets over WiFi. See also: https://github.com/mfurseman/robo-android

Dependencies:   Motordriver USBDevice cc3000_hostdriver_mbedsocket_hacked mbed

main.cpp

Committer:
mfurseman
Date:
2014-10-25
Revision:
2:50c151183047
Parent:
1:b66a2d756c8a
Child:
3:ba11f6207550

File content as of revision 2:50c151183047:

#include "mbed.h"
#include "cc3000.h"
#include "TCPSocketConnection.h"
#include "TCPSocketServer.h"

/* Quickly change debug flag to remove blocking serial code */
#define DEBUG
#ifdef DEBUG
#include "USBSerial.h"
USBSerial serial;
#define debug(x, ...) serial.printf(x, ##__VA_ARGS__);
#else
#define debug(x, ...)
#endif


using namespace mbed_cc3000;


/* On board LED */
DigitalOut led(P0_1);

/* Serial library for WiFi module */
cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37));

/* Struct to hold connection data */
tNetappIpconfigRetArgs ipinfo;


/* Prints CC3000 connection info */
void printConnectionInfo() {
    if (( wifi.is_enabled() ) && ( wifi.is_dhcp_configured() )) {
        wifi.get_ip_config(&ipinfo);
    }    
    if (! wifi.is_enabled() ) {
        debug("CC3000 Disabled\r\n"); 
    }
    else if ( wifi.is_dhcp_configured() ) {
        debug("SSID : %-33s|\r\n", ipinfo.uaSSID);
        debug("IP : %-35s|\r\n", wifi.getIPAddress());   
    }
    else if ( wifi.is_connected() ) {
        debug("Connecting, waiting for DHCP\r\n"); 
    }
    else {
        debug("Not Connected\r\n");   
    }
}


/* WiFi DipCortex board setup */
void init() {
    NVIC_SetPriority(SSP1_IRQn, 0x0); 
    NVIC_SetPriority(PIN_INT0_IRQn, 0x1);
    
    // SysTick set to lower priority than Wi-Fi SPI bus interrupt
    NVIC_SetPriority(SysTick_IRQn, 0x2); 
    
    // Enable RAM1
    LPC_SYSCON->SYSAHBCLKCTRL |= (0x1 << 26);
    
    // This may be neccassary for CC3000
    wait(1);
}


/* Connects WiFi assuming existing SmartConfig */
void connectWifi() {
    wifi.start(0);
    wait_ms(750);
    wifi._wlan.ioctl_set_connection_policy(0, 0, 1);
    // TODO: Timeout and switch on smart config here
    // TODO: Use static IP if possible
}

/* Opens a server on port 5678, waits for a connection, sends 'hello world'
   to the client, then closes all sockets and returns. */
void serverTest() {
    TCPSocketServer server;
    TCPSocketConnection client;

    int32_t status;
    char hello[] = "Hello World\r\n";
    
    /* Wait for a client connection on 5678 */
    server.bind(5678);
    debug("Before server.listen()\r\n");
    server.listen();
    status = server.accept(client); // This returns -1 with no waiting clients
    
    /* Send hello world message to client */
    debug("Accepted client with status %d\r\n", status);
    if(status >= 0) {
        client.set_blocking(false, 1500); // Timeout after (1.5)s
        debug("Connection from: %s \r\n", client.get_address());
        client.send_all(hello, sizeof(hello));
    }
    
    /* Disconnect all sockets */
    client.close();
    server.close();
}

int main(void) {
    init();
    debug("Completed init()\r\n");
    printConnectionInfo();
    connectWifi();
    debug("Completed connectWifi()\r\n");
    printConnectionInfo();

    while(1) {
        debug("\r\n  ::  One second test loop  ::  \r\n");
        printConnectionInfo();
        serverTest();
        led = !led;
        wait(1);
   }
}