Robert Bui / Mbed 2 deprecated CubicHand

Dependencies:   MMA8451Q Multi_WS2811 NVIC_set_all_priorities TSI cc3000_hostdriver_mbedsocket mbed

Fork of CubicHand by Model-Based Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GloveWifi.cpp Source File

GloveWifi.cpp

00001 #include "GloveWifi.h"
00002 #include "mbed.h"
00003 #include "cc3000.h"
00004 #include "TCPSocketConnection.h"
00005 #include "TCPSocketServer.h"
00006 #include "NVIC_set_all_priorities.h"
00007 
00008 #define SSID "CubeNet"
00009 #define PASSWORD "modelbased"
00010 
00011 #define ECHO_SERVER_ADDRESS     "192.168.1.33"
00012 #define ECHO_SERVER_PORT        2000
00013 #define MMA8451_I2C_ADDRESS     (0x1d<<1)
00014 
00015 GloveWifi::GloveWifi():
00016 DropCount(0)
00017 {
00018 }
00019 
00020 GloveWifi::~GloveWifi()
00021 {
00022     delete socket;
00023     delete wifi;
00024 }
00025 
00026 void GloveWifi::Init()
00027 {
00028 
00029     //Init from the cc3000 example
00030     DigitalOut PWR_EN1(PTB2);
00031     DigitalOut PWR_EN2(PTB3);
00032 
00033     // Wi-Go set current to 500mA since we're turning on the Wi-Fi
00034     PWR_EN1 = 0;
00035     PWR_EN2 = 1;
00036 
00037     NVIC_set_all_irq_priorities(0x3);
00038     NVIC_SetPriority(SPI0_IRQn, 0x0);     // Wi-Fi SPI interrupt must be higher priority than SysTick
00039     NVIC_SetPriority(PORTA_IRQn, 0x1);
00040     NVIC_SetPriority(SysTick_IRQn, 0x2);  // SysTick set to lower priority than Wi-Fi SPI bus interrupt
00041     PORTA->PCR[16] |= PORT_PCR_ISF_MASK;
00042     PORTA->ISFR |= (1 << 16);
00043 }
00044 
00045 void GloveWifi::Connect()
00046 {
00047     
00048     //wifi = new cc3000(PTD4, PTC9, PTD0, SPI(PTD2, PTD3, PTD1), SSID, PASSWORD, WPA2, false); 
00049     wifi = new cc3000(PTD4, PTC9, PTC4, SPI(PTC6, PTC7, PTC5), SSID, PASSWORD, WPA2, false);
00050     wifi->init();
00051     if (wifi->connect() == -1) 
00052     {
00053         printf("Failed to connect. Please verify connection details and try again. \r\n");
00054     } else 
00055     {
00056         printf("IP address: %s \r\n", wifi->getIPAddress());
00057     }
00058     socket = new TCPSocketConnection;
00059     while (socket->connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) 
00060     {
00061         printf("Unable to connect to (%s) on port (%d) \r\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
00062         wait(1);
00063     }
00064     printf("Connected!\r\n");
00065     socket->set_blocking(false, 10);
00066     
00067 }
00068 
00069 void GloveWifi::Disconnect()
00070 {
00071     socket->close();
00072     wifi->disconnect();
00073     delete socket;
00074     delete wifi;
00075 }
00076 
00077 void GloveWifi::Reconnect()
00078 {
00079     delete socket;
00080     socket = new TCPSocketConnection;
00081     while (socket->connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) 
00082     {
00083         printf("Unable to connect to (%s) on port (%d) \r\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
00084         wait(1);
00085     }
00086     socket->set_blocking(false, 10);
00087     printf("Reconnected!\r\n");
00088 }
00089 
00090 int GloveWifi::GetDataFromBuffer(char * buf, int size)
00091 {
00092     //printf("Buffer Size: %d\r\n", wifi->_simple_link.get_transmit_error());
00093     int numR = 0;
00094     numR = socket->receive((char *)buf, size);
00095     if(numR == -1)
00096     {
00097         DropCount++;
00098     }else
00099     {
00100         DropCount = 0;
00101     }
00102     if(DropCount > 200)
00103     {
00104         printf("!\r\n");
00105         Reconnect();
00106         DropCount = 0;
00107     }
00108     return numR;
00109 }
00110 
00111 uint8_t GloveWifi::SendDataToGlove(uint8_t * buf, uint16_t size)
00112 {
00113     return socket->send((char *)buf, size);
00114 }
00115 
00116