the firmware for xadow smartstrap for pebble

Dependencies:   BufferedSerial USBDevice XadowGPS XadowNFC mbed mbedPebbleSerial IAP

Files at this revision

API Documentation at this revision

Comitter:
KillingJacky
Date:
Fri Nov 06 02:53:03 2015 +0000
Parent:
1:8d2e8f504ba3
Child:
3:40e13433d193
Commit message:
removed NFC emulation for tag

Changed in this revision

XadowNFC.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/XadowNFC.lib	Thu Nov 05 04:04:40 2015 +0000
+++ b/XadowNFC.lib	Fri Nov 06 02:53:03 2015 +0000
@@ -1,1 +1,1 @@
-https://developer.mbed.org/teams/Seeed/code/XadowNFC/#9d116099a88d
+https://developer.mbed.org/teams/Seeed/code/XadowNFC/#27242e2abd99
--- a/main.cpp	Thu Nov 05 04:04:40 2015 +0000
+++ b/main.cpp	Fri Nov 06 02:53:03 2015 +0000
@@ -31,7 +31,8 @@
 #define ATTR_NFC_READ_NDEF      0x1002
 #define ATTR_NFC_WRITE_NDEF     0x1003
 #define ATTR_NFC_ERASE_NDEF     0x1004
-#define ATTR_NFC_EMULATE_A_TAG  0x1005
+
+uint8_t nfc_online, gps_online;
 
 char    eep_block[ EEP_BLOCK_SIZE ];
 
@@ -48,7 +49,8 @@
 
 I2C i2c(P0_5, P0_4);
 
-Timer timer;
+Timer timer_notify;
+Timer timer_service;
 
 IAP   iap;
 
@@ -61,6 +63,32 @@
     return 3.3f*ain*2.25;
 }
 
+void add_services()
+{
+    int i = 0;
+    
+    /* Raw data service */
+    SUPPORTED_SERVICES[i++] = 0x0000;
+    
+    /* battery service */
+    SUPPORTED_SERVICES[i++] = SERVICE_BAT;
+    
+    /* GPS service */
+    if (gps_online)
+    {
+        SUPPORTED_SERVICES[i++] = SERVICE_GPS;
+    }
+    
+    /* NFC Service*/
+    if (nfc_online)
+    {
+        SUPPORTED_SERVICES[i++] = SERVICE_NFC;
+        nfc_adapter_init();
+    }
+    
+    mbedPebbleSerial::begin(s_pebble_buffer, sizeof(s_pebble_buffer), Baud9600, SUPPORTED_SERVICES, i);
+}
+
 int main()
 {
     /* init io */
@@ -73,43 +101,25 @@
     wait(1);
     dbg_serial.printf("Detecting connected device...\r\n");
 
-    int i = 0;
-    
-    /* Raw data service */
-    SUPPORTED_SERVICES[i++] = 0x0000;
-    
-    /* battery service */
-    SUPPORTED_SERVICES[i++] = SERVICE_BAT;
-    
     /* GPS service */
-    dbg_serial.printf("CHK_GPS\r\n");
-    uint8_t gps_online = gps_check_online();
+    gps_online = gps_check_online();
     dbg_serial.printf("gps online: %d\r\n", gps_online);
-    if (gps_online)
-    {
-        SUPPORTED_SERVICES[i++] = SERVICE_GPS;
-    }
     
     /* NFC Service*/
-    dbg_serial.printf("CHK_NFC\r\n");
-    uint8_t nfc_online = nfc_check_online();
+    nfc_online = nfc_check_online();
     dbg_serial.printf("nfc online: %d\r\n", nfc_online);
-    if (nfc_online)
-    {
-        SUPPORTED_SERVICES[i++] = SERVICE_NFC;
-        nfc_adapter_init();
-    }
-    
-    mbedPebbleSerial::begin(s_pebble_buffer, sizeof(s_pebble_buffer), Baud9600, SUPPORTED_SERVICES, i);
 
     dbg_serial.printf("Service self detecting done!\r\n");
     
+    add_services();
+    
 
     /* init i2c */
     i2c.frequency(100000); //100khz
     
-    /* start a timer */
-    timer.start();
+    /* start a timer_notify */
+    timer_notify.start();
+    timer_service.start();
     
     /* reload the last configure for charging pebble or not */
     int r;
@@ -170,7 +180,7 @@
                     mbedPebbleSerial::write(false, NULL, 0);
                 }                
             }
-            else if (service_id == SERVICE_GPS)
+            else if (service_id == SERVICE_GPS && gps_online)
             {
                 if (attribute_id == ATTR_GPS_LOCATION && type == RequestTypeRead)
                 {
@@ -203,6 +213,7 @@
                 else if (attribute_id == ATTR_GPS_FIX_QUALITY && type == RequestTypeRead)
                 {
                     uint8_t fixq = gps_get_position_fix();
+                    fixq = (fixq>8)?0:fixq;
                     dbg_serial.printf("ATTR_GPS_FIX_QUALITY: %d\r\n", fixq);
                     memcpy(s_pebble_buffer, &fixq, sizeof(fixq));
                     mbedPebbleSerial::write(true, s_pebble_buffer, sizeof(fixq));
@@ -218,7 +229,7 @@
                     mbedPebbleSerial::write(false, NULL, 0);
                 }
             }
-            else if (service_id == SERVICE_NFC)
+            else if (service_id == SERVICE_NFC && nfc_online)
             {
                 if (attribute_id == ATTR_NFC_GET_UID && type == RequestTypeRead)
                 {
@@ -274,28 +285,20 @@
                     dbg_serial.printf("ATTR_NFC_ERASE_NDEF\r\n");
                     nfc_adapter_erase();
                     mbedPebbleSerial::write(true, NULL, 0);
-                }  
-                else if((attribute_id == ATTR_NFC_EMULATE_A_TAG) && (type == RequestTypeWrite))
-                {
-                    dbg_serial.printf("ATTR_NFC_EMULATE_A_TAG: ");
-                    for(int i=0; i<length; i++)
-                    {
-                        dbg_serial.printf("%02X ", s_pebble_buffer[i]);
-                    }
-                    dbg_serial.printf("\r\n");
-                    nfc_emulate_init(s_pebble_buffer); //this function will internally take only the first 3 bytes from buffer.
-                    mbedPebbleSerial::write(true, NULL, 0);
                 } else
                 {
                     mbedPebbleSerial::write(false, NULL, 0);
                 } 
+            }else
+            {
+                mbedPebbleSerial::write(false, NULL, 0);
             }
         }
         
         //check if nfc tagid changed
-        if (timer.read_ms() > 1000 && nfc_online)
+        if (timer_notify.read_ms() > 1000 && nfc_online)
         {
-            timer.reset();
+            timer_notify.reset();
             
             uint8_t *uid = nfc_adapter_get_uid();
             uint8_t len = uid[1];
@@ -311,7 +314,6 @@
             len = min(len, 10);
             if (memcmp(last_tagid, uid+2, len) != 0 || len != last_tagid_len)
             {
-                //changed, notify
                 mbedPebbleSerial::notify(SERVICE_NFC, ATTR_NFC_GET_UID);
                 if (len == 0)
                 {
@@ -321,8 +323,38 @@
                     memcpy(last_tagid, uid+2, len);
                 }
                 last_tagid_len = len;
+                //changed, notify
             }
         }
         
+        //check services' availability
+        if (timer_service.read() > 5)
+        {
+            timer_service.reset();
+            
+            uint8_t state_gps = gps_check_online();
+            uint8_t state_nfc = nfc_check_online();
+            uint8_t mark = 0;
+            
+            if (state_gps != gps_online)
+            {
+                gps_online = state_gps;
+                mark = 1;
+                dbg_serial.printf("gps state: %d \r\n", gps_online);
+            }
+            if (state_nfc != nfc_online)
+            {
+                nfc_online = state_nfc;
+                mark = 1;
+                dbg_serial.printf("nfc state: %d \r\n", nfc_online);
+            }
+            if (mark)
+            {
+                add_services();
+            }
+            
+            dbg_serial.printf("check services --- %d, %d \r\n", gps_online, nfc_online);
+        }
+        
     }  //while 1
 }