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

Dependents:   Hexi_BLE_Example_Modified Hexi_BLE_Example_ModifiedPOTATO

Fork of Hexi_KW40Z by Hexiwear

Revision:
13:84ead63f7353
Parent:
11:a9a838035b87
Child:
14:241c491f985f
--- a/Hexi_KW40Z.cpp	Mon Sep 26 05:28:55 2016 +0000
+++ b/Hexi_KW40Z.cpp	Thu Aug 09 22:45:01 2018 +0000
@@ -36,14 +36,14 @@
 #include "Hexi_KW40Z.h"
  
 KW40Z::KW40Z(PinName txPin,PinName rxPin) : 
-#if defined (LIB_DEBUG) || defined (RAW_DEBUG)
+#if defined (LIB_DEBUG) || defined (RAW_DEBUG) || defined (RX_DEBUG)
 pc(USBTX, USBRX),
 #endif
 device(txPin, rxPin), rxThread(osPriorityNormal,1024), mainThread(osPriorityNormal,1024)
 {
-#if defined (LIB_DEBUG) 
+#if defined (LIB_DEBUG) || defined (RX_DEBUG)
     pc.baud(115200);
-    pc.printf("Initializing\r\n");
+    printf("Initializing\r\n");
 #endif
 
     device.baud(230400);
@@ -58,6 +58,7 @@
     buttonRightCb = NULL;
     buttonSlideCb = NULL;
     alertCb = NULL;
+    messageCb = NULL;
     passkeyCb = NULL;
     notificationsCb = NULL;
 
@@ -85,13 +86,14 @@
 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_message(message_t msgFct)      { messageCb = msgFct;     }
 void KW40Z::attach_passkey(passkey_t passkeyFct){ passkeyCb = passkeyFct; }
 void KW40Z::attach_notifications(notifications_t notFct) { notificationsCb = notFct; }
 
 void KW40Z::mainTask(void)
 {
 #if defined (LIB_DEBUG)     
-    pc.printf("MainTask Stared\r\n");
+    printf("MainTask Stared\r\n");
 #endif
 
     SendGetActiveTsiGroup();
@@ -113,8 +115,8 @@
 
 void KW40Z::rxTask(void)
 {
-#if defined (LIB_DEBUG)     
-    pc.printf("RxTask Stared\r\n");
+#if defined (LIB_DEBUG) || defined (RX_DEBUG)   
+    printf("RxTask Stared\r\n");
 #endif
 
     while(1)
@@ -149,7 +151,7 @@
     }
     
 #if defined (LIB_DEBUG)
-    pc.printf("SendPacket: ");
+    printf("SendPacket: ");
     DebugPrintPacket(packet);
 #endif
 
@@ -164,7 +166,7 @@
     confirmReceived = false;
     
 #if defined (LIB_DEBUG)
-    pc.printf("SendInternal: ");
+    printf("SendInternal: ");
     DebugPrintPacket(txPacket);
 #endif
     
@@ -172,7 +174,7 @@
     {
         confirmRequested = true;
 #if defined (LIB_DEBUG)
-        pc.printf("Found confirmRequested\r\n");
+        printf("Found confirmRequested\r\n");
 #endif
     }
     
@@ -188,7 +190,7 @@
         }
         
 #if defined (LIB_DEBUG)
-        pc.printf("TX: ");
+        printf("TX: ");
         DebugPrintPacket(txPacket);
 #endif
 
@@ -209,7 +211,7 @@
 void KW40Z::ProcessBuffer()
 {
 #if defined (RAW_DEBUG)
-    pc.printf("%02X ", rxBuff-1);
+    printf("%02X ", rxBuff-1);
 #endif 
     /* check if header has been received */
     if(rxBuff > ((uint8_t*)&hostInterface_rxPacket + gHostInterface_headerSize))
@@ -219,8 +221,8 @@
            (gHostInterface_startByte2 != (hostInterface_rxPacket.start2 & 0xFE))||
            (hostInterface_rxPacket.length > gHostInterface_dataSize))
         {
-#if defined (LIB_DEBUG) 
-            pc.printf("Check header failed: ");
+#if defined (LIB_DEBUG) || defined (RX_DEBUG) 
+            printf("Check header failed: ");
             DebugPrintPacket(&hostInterface_rxPacket);
 #endif
       
@@ -235,7 +237,7 @@
                 if(gHostInterface_trailerByte != hostInterface_rxPacket.data[hostInterface_rxPacket.length])
                 {
 #if defined (LIB_DEBUG) 
-                    pc.printf("Trailer byte failed: ");
+                    printf("Trailer byte failed: ");
                     DebugPrintPacket(&hostInterface_rxPacket);
 #endif
 
@@ -257,11 +259,11 @@
                     queue.put(rxPacket);
 
 #if defined (LIB_DEBUG)
-                    pc.printf("RX: ");
+                    printf("RX: ");
                     DebugPrintPacket(&hostInterface_rxPacket);
 #endif
 #if defined (RAW_DEBUG)
-                    pc.printf("\r\n");
+                    printf("\r\n");
 #endif
                     /* reset buffer position */
                     rxBuff = (uint8_t*)&hostInterface_rxPacket;
@@ -288,7 +290,7 @@
             found = true;
             
 #if defined (LIB_DEBUG) 
-            pc.printf("moving %d", len);
+            printf("moving %d", len);
 #endif
             break;
         }
@@ -303,10 +305,10 @@
     }
     
 #if defined (LIB_DEBUG) 
-    pc.printf("Search done: \r\n");
-    pc.printf("rxBuff: ");
+    printf("Search done: \r\n");
+    printf("rxBuff: ");
     DebugPrintPacket((hostInterface_packet_t*)rxBuff);
-    pc.printf("rxPack: ");
+    printf("rxPack: ");
     DebugPrintPacket(&hostInterface_rxPacket);
 #endif
 }
@@ -314,7 +316,7 @@
 void KW40Z::ProcessPacket(hostInterface_packet_t * packet)
 {
 #if defined (LIB_DEBUG)
-    pc.printf("ProcessPacket: ");
+    printf("ProcessPacket: ");
     DebugPrintPacket(packet);
 #endif
 
@@ -364,6 +366,11 @@
             case packetType_alertIn:
                 if(alertCb != NULL) alertCb(&packet->data[0], packet->length);
                 break;
+                
+            /* Message Service */
+            case packetType_messageIn:
+                if(messageCb != NULL)  messageCb(&packet->data[1], (uint8_t) packet->data[0]);
+                break;
             
             /* Passkey for pairing received */
             case packetType_passDisplay:
@@ -600,6 +607,22 @@
     SendPacket(&txPacket, true);
 }
 
+void KW40Z::SendMessage(uint8_t *pData, uint8_t length)
+{
+    hostInterface_packet_t txPacket = {0};
+    
+    txPacket.start1     = gHostInterface_startByte1;
+    txPacket.start2     = gHostInterface_startByte2;
+    txPacket.type       = packetType_messageOut;
+    txPacket.length     = 20;
+    memcpy(&txPacket.data[1],pData,length);
+    txPacket.data[0] = length;
+    txPacket.data[20] = gHostInterface_trailerByte;
+    
+    SendPacket(&txPacket, true);
+}
+
+
 void KW40Z::ToggleTsiGroup(void)
 {
     hostInterface_packet_t txPacket = {0};
@@ -733,7 +756,7 @@
     return activeTsiGroup;
 }
 
-#if defined (LIB_DEBUG) 
+#if defined (LIB_DEBUG) || defined (RX_DEBUG)
 void KW40Z::DebugPrintPacket(hostInterface_packet_t * packet)
 {
     char * idx = (char *)packet;
@@ -746,9 +769,9 @@
 
     for(uint8_t i = 0; i < length; i++)
     {
-        pc.printf("%02X ",*idx);
+        printf("%02X ",*idx);
         idx++;
     }
-    pc.printf("\r\n");
+    printf("\r\n");
 }
 #endif