Hexiwear / Hexi_KW40Z

Dependents:   Hexi_Buttons_Example Hexi_Click_Relay-v2_Example Hexi_Click_Relay-v3_Example Hexi_Catch-the-dot_Game ... more

Files at this revision

API Documentation at this revision

Comitter:
cotigac
Date:
Mon Sep 19 03:39:36 2016 +0000
Parent:
0:c2d52562f36b
Child:
2:bb66c19c3c04
Commit message:
Updated library for simpler callbacks

Changed in this revision

Hexi_KW40Z.cpp Show annotated file Show diff for this revision Revisions of this file
Hexi_KW40Z.h Show annotated file Show diff for this revision Revisions of this file
--- a/Hexi_KW40Z.cpp	Mon Sep 19 02:46:28 2016 +0000
+++ b/Hexi_KW40Z.cpp	Mon Sep 19 03:39:36 2016 +0000
@@ -50,8 +50,16 @@
     device.format(8, Serial::None, 2);
     
     rxBuff = (uint8_t*)&hostInterface_rxPacket;
-    kw40z_Cbs = NULL;
-    //memset(&hexiwear_kw40version,0,sizeof(hexiwear_version_t));
+    
+    /* initialize callbacks */
+    buttonUpCb = NULL;
+    buttonDownCb = NULL;
+    buttonLeftCb = NULL;
+    buttonRightCb = NULL;
+    buttonSlideCb = NULL;
+    alertCb = NULL;
+    passkeyCb = NULL;
+    notificationsCb = NULL;
     
     /* intialization finalized, signal to start the threads */
     mainThread.signal_set(START_THREAD);
@@ -62,9 +70,44 @@
 {
 }
 
-void KW40Z::attach(kw40z_callbacks_t * callbacks)
+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)
 {
-    kw40z_Cbs = callbacks;
+    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) {
@@ -330,30 +373,33 @@
     {
         /* button presses */
         case packetType_pressUp:
+            if(buttonUpCb != NULL) buttonUpCb();
+            break;
+            
         case packetType_pressDown:
+            if(buttonDownCb != NULL) buttonDownCb();
+            break;
+            
         case packetType_pressLeft:
+            if(buttonLeftCb != NULL) buttonLeftCb();
+            break;
+            
         case packetType_pressRight:
+            if(buttonRightCb != NULL) buttonRightCb();
+            break;
+        
         case packetType_slide:
-            if((kw40z_Cbs != NULL) && (kw40z_Cbs->buttons != NULL))
-            {
-                kw40z_Cbs->buttons((hexi_buttons_t)(rxPacket->type));
-            }
+            if(buttonSlideCb != NULL) buttonSlideCb();
             break;
     
         /* Alert Service */
         case packetType_alertIn:
-            if((kw40z_Cbs != NULL) && (kw40z_Cbs->alert != NULL))
-            {
-                kw40z_Cbs->alert(&rxPacket->data[0], rxPacket->length);
-            }
+            if(alertCb != NULL) alertCb(&rxPacket->data[0], rxPacket->length);
             break;
         
         /* Passkey for pairing received */
         case packetType_passDisplay:
-            if((kw40z_Cbs != NULL) && (kw40z_Cbs->passkey != NULL))
-            {
-                kw40z_Cbs->passkey(&rxPacket->data[0]);
-            }
+            if(passkeyCb != NULL) passkeyCb(&rxPacket->data[0]);
             break;
             
         /* OTAP messages */
@@ -375,10 +421,7 @@
             
         /* ANCS Service Notification Received */
         case packetType_notification:
-            if((kw40z_Cbs != NULL) && (kw40z_Cbs->notifications != NULL))
-            {
-                kw40z_Cbs->notifications(rxPacket->data[0],rxPacket->data[1]);
-            }
+            if(notificationsCb != NULL) notificationsCb(rxPacket->data[0], rxPacket->data[1]);
             break;
             
         /* Build version */
--- a/Hexi_KW40Z.h	Mon Sep 19 02:46:28 2016 +0000
+++ b/Hexi_KW40Z.h	Mon Sep 19 03:39:36 2016 +0000
@@ -152,13 +152,10 @@
     slide            = 4, /**< touch slide */
 } hexi_buttons_t;
 
-typedef struct
-{
-    void (*buttons)(hexi_buttons_t button);
-    void (*alert)(uint8_t *data, uint8_t length);
-    void (*passkey)(uint8_t *data);
-    void (*notifications)(uint8_t eventId, uint8_t categoryId);
-} kw40z_callbacks_t;
+typedef void (*button_t)(void);
+typedef void (*alert_t)(uint8_t *data, uint8_t length);
+typedef void (*passkey_t)(uint8_t *data);
+typedef void (*notifications_t)(uint8_t eventId, uint8_t categoryId);
 
 typedef struct name
 {
@@ -185,7 +182,16 @@
     */   
     ~KW40Z();
     
-    void attach(kw40z_callbacks_t * callbacks);
+    void attach_buttonUp(button_t btnFct);
+    void attach_buttonDown(button_t btnFct);
+    void attach_buttonLeft(button_t btnFct);
+    void attach_buttonRight(button_t btnFct);
+    void attach_buttonSlide(button_t btnFct);
+    
+    void attach_alert(alert_t alertFct);
+    void attach_passkey(passkey_t passkeyFct);
+    void attach_notifications(notifications_t notFct);
+
     void GetVersion();
   
 private:
@@ -196,7 +202,16 @@
     hostInterface_packet_t hostInterface_rxPacket;
     hostInterface_packet_t hostInterface_txPacket;
     
-    kw40z_callbacks_t * kw40z_Cbs;
+    button_t buttonUpCb;
+    button_t buttonDownCb;
+    button_t buttonLeftCb;
+    button_t buttonRightCb;
+    button_t buttonSlideCb;
+    
+    alert_t alertCb;
+    passkey_t passkeyCb;
+    notifications_t notificationsCb;
+    
     uint8_t * rxBuff;
     bool confirmReceived;