Auto updating alarm watch - accepts alarm settings from a BLE device like a Raspberry Pi and buzzes at the appropriate time - also displays binary time

Dependencies:   BLE_API mbed-src nRF51822 nrf51_rtc

Files at this revision

API Documentation at this revision

Comitter:
Bobty
Date:
Mon Jul 27 15:25:59 2015 +0000
Parent:
2:9090120e2656
Child:
4:f0b030a3223f
Commit message:
Basic functionality of setting the time is working but no RTC is enabled

Changed in this revision

ButtonService.h Show annotated file Show diff for this revision Revisions of this file
WatchTimeService.h 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/ButtonService.h	Sun Jul 26 15:38:59 2015 +0000
+++ b/ButtonService.h	Mon Jul 27 15:25:59 2015 +0000
@@ -34,6 +34,11 @@
         ble.gattServer().write(buttonState.getValueHandle(), (uint8_t *)&newState, sizeof(bool));
     }
 
+    GattAttribute::Handle_t getValueHandle()
+    {
+        return buttonState.getValueHandle();
+    }
+    
 private:
     BLE                              &ble;
     ReadOnlyGattCharacteristic<bool>  buttonState;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WatchTimeService.h	Mon Jul 27 15:25:59 2015 +0000
@@ -0,0 +1,36 @@
+// BLE Service for Watch Time
+// To allow a watch device to be told the current time
+
+#ifndef __BLE_WATCHTIME_SERVICE_H__
+#define __BLE_WATCHTIME_SERVICE_H__
+
+class WatchTimeService {
+public:
+
+    static const int WatchTime_BlockSize = 7;
+    const static uint16_t WATCHTIME_SERVICE_UUID              = 0xFE32;
+    const static uint16_t WATCHTIME_STATE_CHARACTERISTIC_UUID = 0xFE33;
+
+    WatchTimeService(BLE &_ble, uint8_t* pInitialWatchTime) :
+        ble(_ble), watchTimeVal(WATCHTIME_STATE_CHARACTERISTIC_UUID, pInitialWatchTime, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
+    {
+        GattCharacteristic *charTable[] = {&watchTimeVal};
+        GattService watchTimeService(WatchTimeService::WATCHTIME_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
+        ble.gattServer().addService(watchTimeService);
+    }
+
+    void sendWatchTime(uint8_t* pWatchTime) {
+        ble.gattServer().write(watchTimeVal.getValueHandle(), pWatchTime, WatchTime_BlockSize);
+    }
+
+    GattAttribute::Handle_t getValueHandle()
+    {
+        return watchTimeVal.getValueHandle();
+    }
+
+private:
+    BLE &ble;
+    ReadWriteArrayGattCharacteristic<uint8_t, WatchTime_BlockSize> watchTimeVal;
+};
+
+#endif /* #ifndef __BLE_WATCHTIME_SERVICE_H__ */
--- a/main.cpp	Sun Jul 26 15:38:59 2015 +0000
+++ b/main.cpp	Mon Jul 27 15:25:59 2015 +0000
@@ -5,6 +5,7 @@
 #include "mbed.h"
 #include "BLE.h"
 #include "ButtonService.h"
+#include "WatchTimeService.h"
 
 // BLE platform
 BLE ble;
@@ -16,12 +17,23 @@
 InterruptIn button(D8);
 
 // Device name - this is the visible name of device on BLE
-const static char     DEVICE_NAME[] = "Button";
+const static char     DEVICE_NAME[] = "JoesAlarm";
+
+// UUIDs of services offered
+static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID, WatchTimeService::WATCHTIME_SERVICE_UUID};
+
+// Service offering to read and set the time on the watch
+WatchTimeService *pWatchTimeService;
 
 // TEST CODE
-static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID};
-
-// TEST CODE
+int callbackCount = 0;
+uint8_t testbuf[WatchTimeService::WatchTime_BlockSize];
+time_t retval = 0;
+int servcode = 0;
+int buflen = 0;
+int mycode = 0;
+int offs = 0;
+int butcode = 0;
 ButtonService *buttonServicePtr;
 
 // Handle button press to read watch
@@ -50,9 +62,51 @@
     led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */
 }
 
+time_t watchTimeToUnix(const uint8_t* pWatchTime)
+{
+    struct tm tminfo;
+    tminfo.tm_year = int(pWatchTime[0])*256 + pWatchTime[1] - 1900;
+    tminfo.tm_mon = pWatchTime[2] - 1;
+    tminfo.tm_mday = pWatchTime[3];
+    tminfo.tm_hour = pWatchTime[4];
+    tminfo.tm_min = pWatchTime[5];
+    tminfo.tm_sec = pWatchTime[6];
+    tminfo.tm_isdst = -1;
+    time_t timest = mktime(&tminfo);
+    return timest;
+}
+
+void onDataWrittenCallback(const GattWriteCallbackParams *params) 
+{
+    // TEST code
+    callbackCount++;
+    memcpy(testbuf, params->data, WatchTimeService::WatchTime_BlockSize);
+    servcode = params->handle;
+    buflen = params->len;
+    mycode = pWatchTimeService->getValueHandle();
+    butcode = buttonServicePtr->getValueHandle();
+    offs = params->offset;
+
+    // Check if this is time setting
+    if (pWatchTimeService->getValueHandle() == params->handle) 
+    {
+        if (params->len == WatchTimeService::WatchTime_BlockSize)
+        {
+            time_t timest = watchTimeToUnix(params->data);
+            retval = timest;
+            if (timest != -1)
+                set_time(timest);
+        }
+    }
+}
+    
 int main(void)
 {
+    printf("AlarmWatch\r\n");
+    
     // TEST CODE
+    memset(testbuf, 0, WatchTimeService::WatchTime_BlockSize);
+    int loopCount = 0;
     led1 = 1;
     Ticker ticker;
     ticker.attach(periodicCallback, 1);
@@ -62,10 +116,16 @@
     // BLE init
     ble.init();
     ble.gap().onDisconnection(disconnectionCallback);
-
+    ble.onDataWritten(onDataWrittenCallback);
+    
     // TEST CODE
     ButtonService buttonService(ble, false /* initial value for button pressed */);
     buttonServicePtr = &buttonService;
+    
+    // Watch Time Service
+    uint8_t initialTime[] = { uint8_t(2015/256), uint8_t(2015%256), 7, 26, 12, 8, 0 };    
+    WatchTimeService watchTimeService(ble, initialTime);
+    pWatchTimeService = &watchTimeService;
 
     // Setup advertising
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
@@ -77,6 +137,21 @@
 
     while (true) {
         ble.waitForEvent();
+        
+        loopCount++;
+        if (loopCount < 5)
+            continue;
+        loopCount = 0;
+        
+        time_t rawtime;
+        struct tm * timeinfo;        
+        time (&rawtime);
+        timeinfo = localtime (&rawtime);
+        printf ("Current local time and date: %s callbacks %d retval %d\r\n", asctime(timeinfo), callbackCount, retval);
+        printf ("Timest %02x %02x %02x %02x %02x %02x %02x %ld\r\n", testbuf[0],
+                testbuf[1], testbuf[2], testbuf[3], testbuf[4], testbuf[5], testbuf[6], retval); 
+        printf ("serv %d buflen %d mycode %d offs %d butcode %d\r\n", servcode, buflen, mycode, offs, butcode);
+        printf ("val %ld\r\n", watchTimeService.getValueHandle());
     }
 }