Hexiwear library for communicating with the on-board KW40Z BLE device. KW40Z handles also the touch buttons.

Fork of Hexi_KW40Z by Hexiwear

Revision:
2:bb66c19c3c04
Parent:
1:f6f9b24aea57
Child:
3:9e92f113c671
--- a/Hexi_KW40Z.cpp	Mon Sep 19 03:39:36 2016 +0000
+++ b/Hexi_KW40Z.cpp	Mon Sep 19 05:45:31 2016 +0000
@@ -60,6 +60,14 @@
     alertCb = NULL;
     passkeyCb = NULL;
     notificationsCb = NULL;
+
+    kw40_version.ver_patchNumber = 0;
+    kw40_version.ver_minorNumber = 0;
+    kw40_version.ver_majorNumber = 0;
+    
+    activeTsiGroup = 0;
+    advertisementMode = 0;
+    linkState = 0;
     
     /* intialization finalized, signal to start the threads */
     mainThread.signal_set(START_THREAD);
@@ -70,45 +78,14 @@
 {
 }
 
-void KW40Z::attach_buttonUp(button_t btnFct)
-{
-    buttonUpCb = btnFct;
-}
-
-void KW40Z::attach_buttonDown(button_t btnFct)
-{
-    buttonDownCb = btnFct;
-}
-
-void KW40Z::attach_buttonLeft(button_t btnFct)
-{
-    buttonLeftCb = btnFct;
-}
-
-void KW40Z::attach_buttonRight(button_t btnFct)
-{
-    buttonRightCb = btnFct;
-}
-
-void KW40Z::attach_buttonSlide(button_t btnFct)
-{
-    buttonSlideCb = btnFct;
-}
-
-void KW40Z::attach_alert(alert_t alertFct)
-{
-    alertCb = alertFct;
-}
-
-void KW40Z::attach_passkey(passkey_t passkeyFct)
-{
-    passkeyCb = passkeyFct;
-}
-
-void KW40Z::attach_notifications(notifications_t notFct)
-{
-    notificationsCb = notFct;
-}
+void KW40Z::attach_buttonUp(button_t btnFct)    { buttonUpCb = btnFct;    }
+void KW40Z::attach_buttonDown(button_t btnFct)  { buttonDownCb = btnFct;  }
+void KW40Z::attach_buttonLeft(button_t btnFct)  { buttonLeftCb = btnFct;  }
+void KW40Z::attach_buttonRight(button_t btnFct) { buttonRightCb = btnFct; }
+void KW40Z::attach_buttonSlide(button_t btnFct) { buttonSlideCb = btnFct; }
+void KW40Z::attach_alert(alert_t alertFct)      { alertCb = alertFct;     }
+void KW40Z::attach_passkey(passkey_t passkeyFct){ passkeyCb = passkeyFct; }
+void KW40Z::attach_notifications(notifications_t notFct) { notificationsCb = notFct; }
 
 void KW40Z::rxStarter(void const *p) {
   KW40Z *instance = (KW40Z*)p;
@@ -120,61 +97,6 @@
   instance->mainTask();
 }
 
-void KW40Z::GetVersion()
-{
-    hostInterface_packet_t txPacket = {0};
-    
-    txPacket.start1     = gHostInterface_startByte1,
-    txPacket.start2     = gHostInterface_startByte2,
-    txPacket.type       = packetType_buildVersion,
-    txPacket.length     = 3,
-    txPacket.data[0]    = HEXIWEAR_VERSION_MAJOR;
-    txPacket.data[1]    = HEXIWEAR_VERSION_MINOR;
-    txPacket.data[2]    = HEXIWEAR_VERSION_PATCH;
-    txPacket.data[3]    = gHostInterface_trailerByte;
-    
-    SendPacket(&txPacket, false);
-}
-
-void KW40Z::SendPacket(hostInterface_packet_t * txPacket, bool confirmRequested)
-{
-    uint8_t retries = 0;
-    confirmReceived = false;
-    
-    do
-    {
-        char * txBuff = (char *)txPacket;
-        uint8_t length = txPacket->length + gHostInterface_headerSize + 1;
-        
-        if(confirmRequested == true)
-        {
-            txPacket->start2 |= 0x01;
-        }
-        
-        for(uint8_t i = 0; i < length; i++)
-        {
-            device.putc(*txBuff);
-            txBuff++;
-        }
-        
-#if defined (LIB_DEBUG)   
-        DebugPrintTxPacket(txPacket);
-#endif
-
-        retries++;
-        
-        #if defined (gHostInterface_RxConfirmationEnable)
-        if((confirmRequested == true) && (confirmReceived == false))
-        {
-            Thread::wait(gHostInterface_retransmitTimeout);
-        }
-        #endif
-    }
-    while((confirmRequested == true) && 
-          (confirmReceived == false) && 
-          (retries < gHostInterface_retransmitCount));
-}
-
 void KW40Z::mainTask(void)
 {
     mainThread.signal_wait(START_THREAD);
@@ -183,6 +105,13 @@
     pc.printf("MainTask Stared\r\n");
 #endif
 
+#if 0
+    SendGetActiveTsiGroup();
+    SendGetAdvertisementMode();
+    SendGetLinkState();
+    SendGetVersion();
+#endif
+
     while(1)
     {
         osEvent evt = queue.get();
@@ -245,6 +174,46 @@
 }
 #endif
 
+
+void KW40Z::SendPacket(hostInterface_packet_t * txPacket, bool confirmRequested)
+{
+    uint8_t retries = 0;
+    confirmReceived = false;
+    
+    do
+    {
+        char * txBuff = (char *)txPacket;
+        uint8_t length = txPacket->length + gHostInterface_headerSize + 1;
+        
+        if(confirmRequested == true)
+        {
+            txPacket->start2 |= 0x01;
+        }
+        
+        for(uint8_t i = 0; i < length; i++)
+        {
+            device.putc(*txBuff);
+            txBuff++;
+        }
+        
+#if defined (LIB_DEBUG)   
+        DebugPrintTxPacket(txPacket);
+#endif
+
+        retries++;
+        
+        #if defined (gHostInterface_RxConfirmationEnable)
+        if((confirmRequested == true) && (confirmReceived == false))
+        {
+            Thread::wait(gHostInterface_retransmitTimeout);
+        }
+        #endif
+    }
+    while((confirmRequested == true) && 
+          (confirmReceived == false) && 
+          (retries < gHostInterface_retransmitCount));
+}
+
 void KW40Z::ProcessBuffer()
 {    
     /* check if header has been received */
@@ -338,23 +307,6 @@
 #endif
 }
 
-void KW40Z::SendPacketOK()
-{
-    hostInterface_packet_t txPacket = {0};
-    
-    txPacket.start1     = gHostInterface_startByte1,
-    txPacket.start2     = gHostInterface_startByte2,
-    txPacket.type       = packetType_OK,
-    txPacket.length     = 0,
-    txPacket.data[0]    = gHostInterface_trailerByte;
-    
-    SendPacket(&txPacket, false);    
-}
-
-void KW40Z::ConfirmPacketOK()
-{
-}
-
 void KW40Z::ProcessReceivedPacket(hostInterface_packet_t * rxPacket)
 {
 #if defined (LIB_DEBUG) 
@@ -409,14 +361,17 @@
             
         /* TSI Status */
         case packetType_buttonsGroupSendActive:
+            activeTsiGroup = rxPacket->data[0];
             break;
             
         /* Advertisement Mode Info */
         case packetType_advModeSend:
+            advertisementMode = rxPacket->data[0];
             break;
         
         /* Link State */
         case packetType_linkStateSend:
+            linkState = rxPacket->data[0];
             break;
             
         /* ANCS Service Notification Received */
@@ -426,6 +381,9 @@
             
         /* Build version */
         case packetType_buildVersion:
+            kw40_version.ver_patchNumber = rxPacket->data[2];
+            kw40_version.ver_minorNumber = rxPacket->data[1];
+            kw40_version.ver_majorNumber = rxPacket->data[0];
             break;
       
         case packetType_OK:
@@ -436,3 +394,285 @@
             break;
     }
 }
+
+void KW40Z::SendBatteryLevel(uint8_t percentage)
+{
+    hostInterface_packet_t txPacket = {0};
+    
+    txPacket.start1     = gHostInterface_startByte1;
+    txPacket.start2     = gHostInterface_startByte2;
+    txPacket.type       = packetType_batteryLevel;
+    txPacket.length     = 1;
+    txPacket.data[0]    = percentage;
+    txPacket.data[1]    = gHostInterface_trailerByte;
+    
+    SendPacket(&txPacket, true);
+}
+
+void KW40Z::SendAccel(uint8_t x, uint8_t y, uint8_t z)
+{
+    hostInterface_packet_t txPacket = {0};
+    
+    txPacket.start1     = gHostInterface_startByte1;
+    txPacket.start2     = gHostInterface_startByte2;
+    txPacket.type       = packetType_accel;
+    txPacket.length     = 3;
+    txPacket.data[0]    = x;
+    txPacket.data[1]    = y;
+    txPacket.data[2]    = z;    
+    txPacket.data[3]    = gHostInterface_trailerByte;
+    
+    SendPacket(&txPacket, true);
+}
+
+void KW40Z::SendGyro(uint8_t x, uint8_t y, uint8_t z)
+{
+    hostInterface_packet_t txPacket = {0};
+    
+    txPacket.start1     = gHostInterface_startByte1;
+    txPacket.start2     = gHostInterface_startByte2;
+    txPacket.type       = packetType_gyro;
+    txPacket.length     = 3;
+    txPacket.data[0]    = x;
+    txPacket.data[1]    = y;
+    txPacket.data[2]    = z;    
+    txPacket.data[3]    = gHostInterface_trailerByte;
+    
+    SendPacket(&txPacket, true);
+}
+
+void KW40Z::SendMag(uint8_t x, uint8_t y, uint8_t z)
+{
+    hostInterface_packet_t txPacket = {0};
+    
+    txPacket.start1     = gHostInterface_startByte1;
+    txPacket.start2     = gHostInterface_startByte2;
+    txPacket.type       = packetType_magnet;
+    txPacket.length     = 3;
+    txPacket.data[0]    = x;
+    txPacket.data[1]    = y;
+    txPacket.data[2]    = z;    
+    txPacket.data[3]    = gHostInterface_trailerByte;
+    
+    SendPacket(&txPacket, true);
+}
+
+void KW40Z::SendAmbientLight(uint8_t percentage)
+{
+    hostInterface_packet_t txPacket = {0};
+    
+    txPacket.start1     = gHostInterface_startByte1;
+    txPacket.start2     = gHostInterface_startByte2;
+    txPacket.type       = packetType_ambiLight;
+    txPacket.length     = 1;
+    txPacket.data[0]    = percentage;
+    txPacket.data[1]    = gHostInterface_trailerByte;
+    
+    SendPacket(&txPacket, true);
+}
+
+void KW40Z::SendTemperature(uint16_t celsius)
+{
+    hostInterface_packet_t txPacket = {0};
+    
+    txPacket.start1     = gHostInterface_startByte1;
+    txPacket.start2     = gHostInterface_startByte2;
+    txPacket.type       = packetType_temperature;
+    txPacket.length     = 2;
+    memcpy(&txPacket.data[0],(uint8_t*)&celsius,txPacket.length);
+    txPacket.data[2]    = gHostInterface_trailerByte;
+    
+    SendPacket(&txPacket, true);
+}
+
+void KW40Z::SendHumidity(uint16_t percentage)
+{
+    hostInterface_packet_t txPacket = {0};
+    
+    txPacket.start1     = gHostInterface_startByte1;
+    txPacket.start2     = gHostInterface_startByte2;
+    txPacket.type       = packetType_humidity;
+    txPacket.length     = 2;
+    memcpy(&txPacket.data[0],(uint8_t*)&percentage,txPacket.length);
+    txPacket.data[2]    = gHostInterface_trailerByte;
+    
+    SendPacket(&txPacket, true);
+}
+
+void KW40Z::SendPressure(uint16_t pascal)
+{
+    hostInterface_packet_t txPacket = {0};
+    
+    txPacket.start1     = gHostInterface_startByte1;
+    txPacket.start2     = gHostInterface_startByte2;
+    txPacket.type       = packetType_pressure;
+    txPacket.length     = 2;
+    memcpy(&txPacket.data[0],(uint8_t*)&pascal,txPacket.length);
+    txPacket.data[2]    = gHostInterface_trailerByte;
+    
+    SendPacket(&txPacket, true);
+}
+
+void KW40Z::SendHearRate(uint8_t rate)
+{
+    hostInterface_packet_t txPacket = {0};
+    
+    txPacket.start1     = gHostInterface_startByte1;
+    txPacket.start2     = gHostInterface_startByte2;
+    txPacket.type       = packetType_steps;
+    txPacket.length     = 1;
+    txPacket.data[0]    = rate;
+    txPacket.data[1]    = gHostInterface_trailerByte;
+    
+    SendPacket(&txPacket, true);
+}
+
+void KW40Z::SendSteps(uint16_t steps)
+{
+    hostInterface_packet_t txPacket = {0};
+    
+    txPacket.start1     = gHostInterface_startByte1;
+    txPacket.start2     = gHostInterface_startByte2;
+    txPacket.type       = packetType_steps;
+    txPacket.length     = 2;
+    memcpy(&txPacket.data[0],(uint8_t*)&steps,txPacket.length);
+    txPacket.data[2]    = gHostInterface_trailerByte;
+    
+    SendPacket(&txPacket, true);
+}
+
+void KW40Z::SendCalories(uint16_t calories)
+{
+    hostInterface_packet_t txPacket = {0};
+    
+    txPacket.start1     = gHostInterface_startByte1;
+    txPacket.start2     = gHostInterface_startByte2;
+    txPacket.type       = packetType_calories;
+    txPacket.length     = 2;
+    memcpy(&txPacket.data[0],(uint8_t*)&calories,txPacket.length);
+    txPacket.data[2]    = gHostInterface_trailerByte;
+    
+    SendPacket(&txPacket, true);
+}
+
+void KW40Z::SendAlert(uint8_t *pData, uint8_t length)
+{
+    hostInterface_packet_t txPacket = {0};
+    
+    txPacket.start1     = gHostInterface_startByte1;
+    txPacket.start2     = gHostInterface_startByte2;
+    txPacket.type       = packetType_alertOut;
+    txPacket.length     = length;
+    memcpy(&txPacket.data[0],pData,length);
+    txPacket.data[length] = gHostInterface_trailerByte;
+    
+    SendPacket(&txPacket, true);
+}
+
+void KW40Z::ToggleTsiGroup(void)
+{
+    hostInterface_packet_t txPacket = {0};
+    
+    txPacket.start1     = gHostInterface_startByte1;
+    txPacket.start2     = gHostInterface_startByte2;
+    txPacket.type       = packetType_buttonsGroupToggleActive;
+    txPacket.length     = 0;
+    txPacket.data[0]    = gHostInterface_trailerByte;
+    
+    SendPacket(&txPacket, true);
+}
+
+void KW40Z::ToggleAdvertisementMode(void)
+{
+    hostInterface_packet_t txPacket = {0};
+    
+    txPacket.start1     = gHostInterface_startByte1;
+    txPacket.start2     = gHostInterface_startByte2;
+    txPacket.type       = packetType_advModeToggle;
+    txPacket.length     = 0;
+    txPacket.data[0]    = gHostInterface_trailerByte;
+    
+    SendPacket(&txPacket, true);
+}
+
+void KW40Z::SendSetApplicationMode(gui_current_app_t mode)
+{
+    hostInterface_packet_t txPacket = {0};
+    
+    txPacket.start1     = gHostInterface_startByte1;
+    txPacket.start2     = gHostInterface_startByte2;
+    txPacket.type       = packetType_appMode;
+    txPacket.length     = 1;
+    txPacket.data[0]    = (uint8_t)mode;
+    txPacket.data[1]    = gHostInterface_trailerByte;
+    
+    SendPacket(&txPacket, true);
+}
+
+void KW40Z::SendGetActiveTsiGroup(void)
+{
+    hostInterface_packet_t txPacket = {0};
+    
+    txPacket.start1     = gHostInterface_startByte1;
+    txPacket.start2     = gHostInterface_startByte2;
+    txPacket.type       = packetType_buttonsGroupGetActive;
+    txPacket.length     = 0;
+    txPacket.data[0]    = gHostInterface_trailerByte;
+    
+    SendPacket(&txPacket, false);
+}
+
+void KW40Z::SendGetAdvertisementMode(void)
+{
+    hostInterface_packet_t txPacket = {0};
+    
+    txPacket.start1     = gHostInterface_startByte1;
+    txPacket.start2     = gHostInterface_startByte2;
+    txPacket.type       = packetType_advModeGet;
+    txPacket.length     = 0;
+    txPacket.data[0]    = gHostInterface_trailerByte;
+    
+    SendPacket(&txPacket, false);
+}
+
+void KW40Z::SendGetLinkState(void)
+{
+    hostInterface_packet_t txPacket = {0};
+    
+    txPacket.start1     = gHostInterface_startByte1;
+    txPacket.start2     = gHostInterface_startByte2;
+    txPacket.type       = packetType_linkStateGet;
+    txPacket.length     = 0;
+    txPacket.data[0]    = gHostInterface_trailerByte;
+    
+    SendPacket(&txPacket, false);
+}
+
+void KW40Z::SendGetVersion(void)
+{
+    hostInterface_packet_t txPacket = {0};
+    
+    txPacket.start1     = gHostInterface_startByte1;
+    txPacket.start2     = gHostInterface_startByte2;
+    txPacket.type       = packetType_buildVersion;
+    txPacket.length     = 3;
+    txPacket.data[0]    = HEXIWEAR_VERSION_MAJOR;
+    txPacket.data[1]    = HEXIWEAR_VERSION_MINOR;
+    txPacket.data[2]    = HEXIWEAR_VERSION_PATCH;
+    txPacket.data[3]    = gHostInterface_trailerByte;
+    
+    SendPacket(&txPacket, false);
+}
+
+void KW40Z::SendPacketOK(void)
+{
+    hostInterface_packet_t txPacket = {0};
+    
+    txPacket.start1     = gHostInterface_startByte1;
+    txPacket.start2     = gHostInterface_startByte2;
+    txPacket.type       = packetType_OK;
+    txPacket.length     = 0;
+    txPacket.data[0]    = gHostInterface_trailerByte;
+    
+    SendPacket(&txPacket, false);    
+}