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

Files at this revision

API Documentation at this revision

Comitter:
kalbers
Date:
Fri Dec 05 21:35:37 2014 +0000
Parent:
1:587189fb6d87
Child:
4:6dc6e8b86b7d
Commit message:
Added DataGlove and DataWifi Class, cleaned up main to use these files.

Changed in this revision

DataGlove.cpp Show annotated file Show diff for this revision Revisions of this file
DataGlove.h Show annotated file Show diff for this revision Revisions of this file
GloveWifi.cpp Show annotated file Show diff for this revision Revisions of this file
GloveWifi.h Show annotated file Show diff for this revision Revisions of this file
init.cpp Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DataGlove.cpp	Fri Dec 05 21:35:37 2014 +0000
@@ -0,0 +1,75 @@
+#include "DataGlove.h"
+
+DataGlove::DataGlove():
+notConnectedCount(0)
+{
+
+}
+
+DataGlove::~DataGlove()
+{
+
+}
+
+void DataGlove::Init()
+{
+	GloveSocket.Init();
+	GloveSocket.Connect();
+    SendBuf[0] = '$';
+    SendBuf[1] = 0x0A;
+    SendBuf[2] = 0x03;
+    SendBuf[3] = 3;
+    SendBuf[4] = (SendBuf[0]+SendBuf[1]+SendBuf[2]+SendBuf[3])%256;
+    SendBuf[5] = '#';
+    GloveSocket.SendDataToGlove(SendBuf, 6);
+}
+
+void DataGlove::Receive()
+{
+    numReceived = GloveSocket.GetDataFromBuffer(Buf, 99);
+    if(numReceived >= 42 && Buf[0] == '$') {
+        gyrox = ((int16_t *)Buf)[5];
+        gyroy = ((int16_t *)Buf)[6];
+        gyroz = ((int16_t *)Buf)[7];
+        accelx = ((int16_t *)Buf)[11];
+        accely = ((int16_t *)Buf)[12];
+        accelz = ((int16_t *)Buf)[13];
+        finger1 = ((int16_t *)Buf)[14];
+        finger2 = ((int16_t *)Buf)[15];
+        finger3 = ((int16_t *)Buf)[16];
+        finger4 = ((int16_t *)Buf)[17];
+        finger5 = ((int16_t *)Buf)[18];
+        // Switching MSB and LSB since data is the other way around
+        gyrox = ((gyrox>>8)&(0x00FF))|((gyrox<<8)&(0xFF00));
+        gyroy = ((gyroy>>8)&(0x00FF))|((gyroy<<8)&(0xFF00));
+        gyroz = ((gyroz>>8)&(0x00FF))|((gyroz<<8)&(0xFF00));
+        accelx = ((accelx>>8)&(0x00FF))|((accelx<<8)&(0xFF00));
+        accely = ((accely>>8)&(0x00FF))|((accely<<8)&(0xFF00));
+        accelz = ((accelz>>8)&(0x00FF))|((accelz<<8)&(0xFF00));
+        finger1 = ((finger1>>8)&(0x00FF))|((finger1<<8)&(0xFF00));
+        finger2 = ((finger2>>8)&(0x00FF))|((finger2<<8)&(0xFF00));
+        finger3 = ((finger3>>8)&(0x00FF))|((finger3<<8)&(0xFF00));
+        finger4 = ((finger4>>8)&(0x00FF))|((finger4<<8)&(0xFF00));
+        finger5 = ((finger5>>8)&(0x00FF))|((finger5<<8)&(0xFF00));
+        //printf("F1: %d\tF2: %d\tF3: %d\tF4: %d\tF5: %d\r\n", finger1, finger2, finger3, finger4, finger5);
+        printf("GX: %d\tGY: %d\tGZ: %d\r\n", gyrox, gyroy, gyroz);
+        //printf("AX: %d\tAY: %d\tAZ: %d\r\n", accelx, accely, accelz);
+        wait(0.05);
+        notConnectedCount = 0;
+    }
+    else {
+        notConnectedCount += 1;
+    }
+    // Re-establishing communication in case no data is received for 1s (20 frames per second*1 = 20)
+    if (notConnectedCount > 20) {
+        printf("Connection broke! Trying to re-establish...\r\n");
+		GloveSocket.Disconnect();
+        printf("Disconnected wifi\r\n");
+        notConnectedCount = 0;
+        GloveSocket.Connect();
+    }
+}
+
+void DataGlove::Parse()
+{
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DataGlove.h	Fri Dec 05 21:35:37 2014 +0000
@@ -0,0 +1,23 @@
+#include "GloveWifi.h"
+
+class DataGlove
+{
+	public:
+		DataGlove();
+		~DataGlove();
+		void Init();
+		void Receive();
+        int16_t finger1, finger2, finger3, finger4, finger5;
+        int16_t accelx, accely, accelz;
+        int16_t gyrox, gyroy, gyroz;
+        
+	private:
+		void Parse();
+        uint8_t Buf[100];
+        uint8_t SendBuf[20];
+        int16_t notConnectedCount;
+		int numReceived;
+        GloveWifi GloveSocket;
+
+
+};
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GloveWifi.cpp	Fri Dec 05 21:35:37 2014 +0000
@@ -0,0 +1,86 @@
+#include "GloveWifi.h"
+#include "mbed.h"
+#include "cc3000.h"
+#include "TCPSocketConnection.h"
+#include "TCPSocketServer.h"
+#include "NVIC_set_all_priorities.h"
+
+#define SSID "CubeNet"
+#define PASSWORD "modelbased"
+
+#define MMA8451_I2C_ADDRESS (0x1d<<1)
+
+
+using namespace mbed_cc3000;
+
+
+GloveWifi::GloveWifi() :
+ECHO_SERVER_ADDRESS("192.168.1.33"),
+ECHO_SERVER_PORT(2000)
+{
+}
+
+GloveWifi::~GloveWifi()
+{
+}
+
+void GloveWifi::Init()
+{
+
+    pc = new Serial(USBTX, USBRX);
+    //Init from the cc3000 example
+    DigitalOut PWR_EN1(PTB2);
+    DigitalOut PWR_EN2(PTB3);
+
+    // Wi-Go set current to 500mA since we're turning on the Wi-Fi
+    PWR_EN1 = 0;
+    PWR_EN2 = 1;
+
+    NVIC_set_all_irq_priorities(0x3);
+    NVIC_SetPriority(SPI0_IRQn, 0x0);     // Wi-Fi SPI interrupt must be higher priority than SysTick
+    NVIC_SetPriority(PORTA_IRQn, 0x1);
+    NVIC_SetPriority(SysTick_IRQn, 0x2);  // SysTick set to lower priority than Wi-Fi SPI bus interrupt
+    PORTA->PCR[16] |= PORT_PCR_ISF_MASK;
+    PORTA->ISFR |= (1 << 16);
+
+    pc->baud(115200);
+}
+
+void GloveWifi::Connect()
+{
+    //TODO: Make this call (or recall) the initial constructor. 
+    wifi = new cc3000(PTD4, PTC9, PTD0, SPI(PTD2, PTD3, PTD1), SSID, PASSWORD, WPA2, false); 
+    wifi->init();
+    if (wifi->connect() == -1) 
+    {
+        printf("Failed to connect. Please verify connection details and try again. \r\n");
+    } else 
+    {
+        printf("IP address: %s \r\n", wifi->getIPAddress());
+    }
+    while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) 
+    {
+        printf("Unable to connect to (%s) on port (%d) \r\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
+        wait(1);
+    }
+    socket.set_blocking(true);
+    printf("Connected!\r\n");
+}
+
+void GloveWifi::Disconnect()
+{
+    socket.close();
+    wifi->disconnect();
+}
+
+uint8_t GloveWifi::GetDataFromBuffer(uint8_t * buf, uint16_t size)
+{
+    return socket.receive((char *)buf, size);
+}
+
+uint8_t GloveWifi::SendDataToGlove(uint8_t * buf, uint16_t size)
+{
+    return socket.send((char *)buf, size);
+}
+
+        
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GloveWifi.h	Fri Dec 05 21:35:37 2014 +0000
@@ -0,0 +1,23 @@
+#include "mbed.h"
+#include "cc3000.h"
+#include "TCPSocketConnection.h"
+
+class GloveWifi
+{
+	public:
+		GloveWifi();
+		~GloveWifi();
+		void Init();
+		void Connect();
+		void Disconnect();
+		uint8_t GetDataFromBuffer(uint8_t * buf, uint16_t size);
+		uint8_t SendDataToGlove(uint8_t * buf, uint16_t size);
+
+	private:
+		const char* ECHO_SERVER_ADDRESS;
+        const int ECHO_SERVER_PORT;
+    	TCPSocketConnection socket;
+		cc3000 * wifi;
+		Serial * pc;
+	
+};
\ No newline at end of file
--- a/init.cpp	Mon Dec 01 05:43:35 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,70 +0,0 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2013 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include "main.h"
-#include "mbed.h"
-
-#if (MY_BOARD == WIGO)
-
-#include "NVIC_set_all_priorities.h"
-
-/**
- *  \brief Wi-Go initialization
- *  \param none
- *  \return none
- */
-void init() {
-    DigitalOut PWR_EN1(PTB2);
-    DigitalOut PWR_EN2(PTB3);
-
-    // Wi-Go set current to 500mA since we're turning on the Wi-Fi
-    PWR_EN1 = 0;
-    PWR_EN2 = 1;
-
-    NVIC_set_all_irq_priorities(0x3);
-    NVIC_SetPriority(SPI0_IRQn, 0x0);     // Wi-Fi SPI interrupt must be higher priority than SysTick
-    NVIC_SetPriority(PORTA_IRQn, 0x1);
-    NVIC_SetPriority(SysTick_IRQn, 0x2);  // SysTick set to lower priority than Wi-Fi SPI bus interrupt
-    PORTA->PCR[16] |= PORT_PCR_ISF_MASK;
-    PORTA->ISFR |= (1 << 16);
-}
-
-#elif (MY_BOARD == WIFI_DIPCORTEX)
-
-/**
- *  \brief Wifi DipCortex initialization
- *  \param none
- *  \return none
- */
-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);
-}
-
-#else
-
-/**
- *  \brief Place here init routine for your board
- *  \param none
- *  \return none
- */
-void init() {
-
-}
-
-#endif
--- a/main.cpp	Mon Dec 01 05:43:35 2014 +0000
+++ b/main.cpp	Fri Dec 05 21:35:37 2014 +0000
@@ -1,108 +1,18 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2013 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include "mbed.h"
-#include "cc3000.h"
-#include "main.h"
-#include "MMA8451Q.h"
-#include "TSISensor.h"
-
-#include "TCPSocketConnection.h"
-#include "TCPSocketServer.h"
-
-using namespace mbed_cc3000;
 
-//#define SSID "wifiisawesome"
-//#define PASSWORD "A915FA24"
-
-#define SSID "CubeNet"
-#define PASSWORD "modelbased"
-
-#define MMA8451_I2C_ADDRESS (0x1d<<1)
+#include "mbed.h"
+#include "main.h"
+#include "DataGlove.h"
 
-/* cc3000 module declaration specific for user's board. Check also init()
-#if (MY_BOARD == WIGO)
-cc3000 wifi(PTA16, PTA13, PTD0, SPI(PTD2, PTD3, PTC5), SSID, PASSWORD, WPA2, false);
-Serial pc(USBTX, USBRX);
-#elif (MY_BOARD == WIFI_DIPCORTEX)
-cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37), SSID, PASSWORD, WPA2, false);
-Serial pc(UART_TX, UART_RX);
-#elif (MY_BOARD == MBED_BOARD_EXAMPLE)
-cc3000 wifi(p9, p10, p8, SPI(p5, p6, p7), SSID, PASSWORD, WPA2, false);
-Serial pc(USBTX, USBRX);
-#else
 
-#endif
-*/
-
-cc3000 wifi(PTD4, PTC9, PTD0, SPI(PTD2, PTD3, PTD1), SSID, PASSWORD, WPA2, false);
-Serial pc(USBTX, USBRX);
+DataGlove MasterGlove;
 
-MMA8451Q accelerometer(PTE25, PTE24, MMA8451_I2C_ADDRESS);
-TSISensor touchSensor;
-
-/**
- *  \brief TCP client demo
- *  \param none
- *  \return int
- */
-int main() {
-    float data[4];
-    
-    init(); /* board dependent init */
-    pc.baud(115200);
-
-    printf("cc3000 tcp client demo. \r\n");
-    wifi.init();
-    if (wifi.connect() == -1) {
-        printf("Failed to connect. Please verify connection details and try again. \r\n");
-    } else {
-        printf("IP address: %s \r\n", wifi.getIPAddress());
-    }
-    
-    const char* ECHO_SERVER_ADDRESS = "192.168.1.33";
-    const int ECHO_SERVER_PORT = 2000;
-    
-     TCPSocketConnection socket;
-     while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
-         printf("Unable to connect to (%s) on port (%d) \r\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
-         wait(1);
+int main() 
+{
+    MasterGlove.Init();
+    printf("DataGlove Cube Client\r\n");
+    while(true) 
+    {
+        MasterGlove.Receive();
+        printf("Looping \r\n");
      }
-     socket.set_blocking(true);
-     printf("Connected!\r\n");
-    char sendb[50];
-    sendb[0] = '$';
-    sendb[1] = 0x0A;
-    sendb[2] = 0x03;
-    sendb[3] = 3;
-    sendb[4] = (sendb[0]+sendb[1]+sendb[2]+sendb[3])%256;
-    sendb[5] = '#';
-    socket.send(sendb, 6);
-    printf("Sent!\r\n");
-    char Buf[100];
-     while(true) {
-       int numReceived = 5;
-        //accelerometer.getAccAllAxis(data);
-        //data[3] = touchSensor.readPercentage();
-        //socket.send_all((char*)data, 4*sizeof(float));
-        //wait(0.2);
-        numReceived = socket.receive(Buf, 99);
-        if(numReceived > 0 && Buf[0] == '$') printf("Accel: %d\r\n", ((int16_t *)Buf)[33]);
-     }
-     
-     socket.close();
-     printf("Completed. \r\n");
-     wifi.disconnect();
 }