The MGC3130 is the world’s first electrical-field (E-field) based three-dimensional (3D) tracking and gesture controller

Dependencies:   BufferedArray

Dependents:   NucleoMGC3130 i2c_master

Files at this revision

API Documentation at this revision

Comitter:
yangcq88517
Date:
Tue Oct 13 19:59:27 2015 +0000
Parent:
5:4666b36a738d
Child:
7:eacd776fce29
Commit message:
bug fix

Changed in this revision

GestILibrarMessage/BufferedArray.cpp Show annotated file Show diff for this revision Revisions of this file
GestILibrarMessage/BufferedArray.h Show annotated file Show diff for this revision Revisions of this file
GestILibrarMessage/GestICMsg.cpp Show annotated file Show diff for this revision Revisions of this file
GestILibrarMessage/GestICMsg.h Show annotated file Show diff for this revision Revisions of this file
GestILibrarMessage/SensorData.h Show annotated file Show diff for this revision Revisions of this file
MGC3130.cpp Show annotated file Show diff for this revision Revisions of this file
MGC3130.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GestILibrarMessage/BufferedArray.cpp	Tue Oct 13 19:59:27 2015 +0000
@@ -0,0 +1,122 @@
+#include "BufferedArray.h"
+
+BufferedArray::BufferedArray()
+{
+    max = EXPANDSIZE;
+    data = new char[EXPANDSIZE];
+    index = 0;
+}
+
+BufferedArray::BufferedArray(BufferedArray * bufferedArray)
+{
+    this->data = bufferedArray->data;
+    this->index = bufferedArray->index;
+}
+
+BufferedArray::~BufferedArray()
+{
+    if (data == NULL)
+        return;
+
+    delete[] data;
+}
+
+char * BufferedArray::gets()
+{
+    return data;
+}
+
+char * BufferedArray::gets(int position)
+{
+    return data + position;
+}
+
+char BufferedArray::get(int position)
+{
+    return *(data + position);
+}
+
+int BufferedArray::getPosition()
+{
+    return index;
+}
+
+void BufferedArray::setPosition(int position)
+{
+    if (this->index > max)
+        this->index = max;
+    else this->index = position;
+}
+
+void BufferedArray::allocate(int length)
+{
+    if (length <= 0)
+        return;
+
+    if (length > max) {
+        delete[] data;
+        data = new char[length];
+    }
+
+    rewind();
+}
+
+void BufferedArray::rewind()
+{
+    index = 0;
+}
+
+void BufferedArray::expandSpace(int length)
+{
+    max += EXPANDSIZE * (1 + length / EXPANDSIZE);
+    char * temp = new char[max];
+    memcpy(temp, data, index);
+    delete[] data;
+    data = temp;
+}
+
+void BufferedArray::set(int position, char value)
+{
+    if (position < 0)
+        return;
+
+    if (position >= max)
+        expandSpace(1);
+
+    data[position] = value;
+}
+
+void BufferedArray::set(char value)
+{
+    set(index, value);
+    index++;
+}
+
+void BufferedArray::sets(char * value, int offset, int length)
+{
+    if (length <= 0)
+        return;
+        
+    if (offset < 0)
+        return;
+
+    sets(index, value, offset, length);
+    index += length;
+}
+
+void BufferedArray::sets(int position, char * value, int offset, int length)
+{
+    if (position < 0)
+        return;
+
+    if (length <= 0)
+        return;
+
+    if (offset < 0)
+        return;
+
+    if (position + length - offset > max)
+        expandSpace(position + length - offset - max);
+
+    memcpy(data + position, value + offset, length);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GestILibrarMessage/BufferedArray.h	Tue Oct 13 19:59:27 2015 +0000
@@ -0,0 +1,98 @@
+#ifndef UK_AC_HERTS_SMARTLAB_BufferedArray
+#define UK_AC_HERTS_SMARTLAB_BufferedArray
+
+#include "mbed.h"
+
+class BufferedArray
+{
+protected :
+    /// initial size and automatically increase length.
+    static const int EXPANDSIZE = 20;
+
+    /// Raw data
+    char * data;
+
+    /// Current index of the data, could also used as data lendth.
+    int index;
+
+    /// Max data size that the raw data can hold.
+    int max;
+    
+    void expandSpace(int length);
+
+public:
+    BufferedArray();
+
+    BufferedArray(BufferedArray * bufferedArray);
+    
+    ~BufferedArray();
+
+    /** Get the raw data.
+    * @returns char array.
+    */
+    char * gets();
+        
+    /** Get the raw data from a specific location.
+    *
+    * @param position where to retrieve
+    *
+    * @returns char array.
+    */
+    char * gets(int position);
+           
+    /** Get 1 byte data from a specific location.
+    *
+    * @param position where to retrieve
+    *
+    * @returns char.
+    */
+    char get(int position);
+
+    /** Get the current index.
+    * @returns char array.
+    */
+    int getPosition();
+
+    /** Set the index within the max length of raw data.
+    * @param position where to begin read and write
+    */
+    void setPosition(int position);
+
+    /** Reset the raw data.
+    * @param length current max size for the raw data
+    */
+    void allocate(int length);
+
+    /** Reset the position and does not affect the content of the data.
+    * @param length current max size for the raw data
+    */
+    void rewind();
+    
+    /** Write 8-bit data into current posiston and increase by 1.
+    * @param value sigle byte
+    */
+    void set(char value);
+
+    /** Write array of data into current posiston, and increase by the lenght.
+    * @param value array of byte
+    * @param offset start point of the data
+    * @param length length to write
+    */
+    void sets(char * value, int offset, int length);
+
+    /** Write 8-bit data into specific posiston and deos not affect the current position.
+    * @param position where to write
+    * @param value sigle byte
+    */
+    void set(int position, char value);
+
+    /** Write array of data into specific posiston and deos not affect the current position.
+    * @param position where to write
+    * @param value array of byte
+    * @param offset start point of the data
+    * @param length length to write
+    */
+    void sets(int position, char * value, int offset, int length);
+};
+
+#endif
--- a/GestILibrarMessage/GestICMsg.cpp	Fri Oct 09 11:26:22 2015 +0000
+++ b/GestILibrarMessage/GestICMsg.cpp	Tue Oct 13 19:59:27 2015 +0000
@@ -1,45 +1,7 @@
 #include "GestICMsg.h"
 
-GestICMsg::GestICMsg() {}
-
-GestICMsg::GestICMsg(int size)
-{
-    data = new char[size];
-}
-
-GestICMsg::~GestICMsg()
-{
-    if (data != NULL)
-        delete[] data;
-}
-
-void GestICMsg::reset()
-{
-    position = 0;
-}
-
-void GestICMsg::set(char value)
-{
-    if (position >= 255)
-        return;
-
-    data[position++] = value;
-}
-
-char GestICMsg::get(int index)
-{
-    return data[index];
-}
-
-char * GestICMsg::gets(int index)
-{
-    return &data[index];
-}
-
-int GestICMsg::getPosition()
-{
-    return position;
-}
+GestICMsg::GestICMsg(): BufferedArray()
+{}
 
 int GestICMsg::getMsgSize()
 {
--- a/GestILibrarMessage/GestICMsg.h	Fri Oct 09 11:26:22 2015 +0000
+++ b/GestILibrarMessage/GestICMsg.h	Tue Oct 13 19:59:27 2015 +0000
@@ -1,7 +1,7 @@
 #ifndef UK_AC_HERTS_SMARTLAB_MGC3130_GestICMsg
 #define UK_AC_HERTS_SMARTLAB_MGC3130_GestICMsg
 
-#include "mbed.h"
+#include "BufferedArray.h"
 
 /**
 *A message is the container to exchange data between GestIC Library and the
@@ -9,15 +9,8 @@
 *255 bytes, and fits into the data packets of the communication interface (e.g., I2C).
 *Each frame transports a single message (see Figure 3-1).
 */
-class GestICMsg
+class GestICMsg : public BufferedArray
 {
-protected:
-    /// max 255 bytes char array.
-    char * data;
-    
-    /// indicate the current length of the message.
-    int position;
-
 public:
     static const int  System_Status = 0x15;
     static const int  Request_Message = 0x06;
@@ -33,48 +26,6 @@
     /// Construct a empty message.
     GestICMsg();
     
-    /** Construct a message with max lenght of payload indicated by size.
-    *
-    * @param size the max size of the payload
-    *
-    */
-    GestICMsg(int size);
-    
-    ~GestICMsg();
-    
-    /// Reset the current position to 0, does not affect the content.
-    void reset();
-    
-    /** Update the payload content at the current position, and increase the position by 1.
-    *
-    * @param value 1 byte data
-    *
-    */
-    void set(char value);
-    
-    /** Retrieve data from the payload.
-    *
-    * @param index the position
-    * @returns 1 byte data
-    *
-    */
-    char get(int index);
-    
-    /** Retrieve data from the payload.
-    *
-    * @param index the position
-    * @returns char array
-    *
-    */
-    char * gets(int index);
-    
-    /** Get the current position, it is the same as payload lenth. 
-    *
-    * @returns current position
-    *
-    */
-    int getPosition();
-
     /** Complete size of the message in bytes including the header
     *
     * @returns payload length, include head.
--- a/GestILibrarMessage/SensorData.h	Fri Oct 09 11:26:22 2015 +0000
+++ b/GestILibrarMessage/SensorData.h	Tue Oct 13 19:59:27 2015 +0000
@@ -226,7 +226,7 @@
 
     /**
     *First: Value represents the current angular position with a resolution of 32 counts for a full revolution.
-    *Second: Counts of full rotations.
+    *Second: [0 - 7] Counts of full rotations.
     *Each time the angular position crosses ‘0’, a full revolution is counted.
     *If the users hand is moving in clockwise direction the counter is
     *increased. For counterclockwise movements, the counter is decreased.
--- a/MGC3130.cpp	Fri Oct 09 11:26:22 2015 +0000
+++ b/MGC3130.cpp	Tue Oct 13 19:59:27 2015 +0000
@@ -1,7 +1,7 @@
 #include "MGC3130.h"
 
 MGC3130::MGC3130(PinName sda, PinName scl, PinName EI0, bool IS2)
-    :TS_Line(EI0), _i2c_bus(sda,scl), msg(255), sensor()
+    :TS_Line(EI0), _i2c_bus(sda,scl), msg(), sensor()
 {
     TS_Line.output();
     TS_Line.write(1);
@@ -19,7 +19,7 @@
     if (TS_Line.read() == 0)
         return NULL;
 
-    msg.reset();
+    msg.rewind();
 
     TS_Line.output();
     TS_Line.write(0);
--- a/MGC3130.h	Fri Oct 09 11:26:22 2015 +0000
+++ b/MGC3130.h	Tue Oct 13 19:59:27 2015 +0000
@@ -57,7 +57,7 @@
     */
     MGC3130(PinName sda, PinName scl, PinName EI0, bool IS2);
 
-    /** Get a sensor data payload from the device, will block if the device is busy.
+    /** Get a sensor data payload from the device.
     *
     * @returns @SensorData NULL means data not avaliable
     *