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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WatchTimeService.h Source File

WatchTimeService.h

00001 // BLE Service for Watch Time
00002 // To allow a watch device to be told the current time
00003 
00004 #ifndef __BLE_WATCHTIME_SERVICE_H__
00005 #define __BLE_WATCHTIME_SERVICE_H__
00006 
00007 class WatchTimeService {
00008 public:
00009 
00010     static const int WatchTime_BlockSize = 7;
00011     const static uint16_t WATCHTIME_SERVICE_UUID              = 0xFE32;
00012     const static uint16_t WATCHTIME_STATE_CHARACTERISTIC_UUID = 0xFE33;
00013     const static uint16_t ALARMTIME_STATE_CHARACTERISTIC_UUID = 0xFE34;
00014 
00015     WatchTimeService(BLE &ble, uint8_t* pInitialWatchTime, uint8_t* pInitialAlarmTime) :
00016         _ble(ble), 
00017         _watchTimeVal(WATCHTIME_STATE_CHARACTERISTIC_UUID, pInitialWatchTime, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
00018         _alarmTimeVal(ALARMTIME_STATE_CHARACTERISTIC_UUID, pInitialAlarmTime, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)        
00019     {
00020         GattCharacteristic *charTable[] = {&_watchTimeVal, &_alarmTimeVal};
00021         GattService watchTimeService(WatchTimeService::WATCHTIME_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
00022         _ble.gattServer().addService(watchTimeService);
00023     }
00024 
00025     void writeWatchTime(time_t timeToWrite)
00026     {
00027         uint8_t watchTime[WatchTimeService::WatchTime_BlockSize];
00028         unixTimeToWatchTime(timeToWrite, watchTime);
00029         _ble.gattServer().write(_watchTimeVal.getValueHandle(), watchTime, WatchTime_BlockSize);
00030     }
00031 
00032     GattAttribute::Handle_t getWatchTimeValueHandle()
00033     {
00034         return _watchTimeVal.getValueHandle();
00035     }
00036     
00037     GattAttribute::Handle_t getAlarmTimeValueHandle()
00038     {
00039         return _alarmTimeVal.getValueHandle();
00040     }
00041     
00042     static time_t watchTimeToUnixTime(const uint8_t* pWatchTime)
00043     {
00044         struct tm tminfo;
00045         tminfo.tm_year = int(pWatchTime[0])*256 + pWatchTime[1] - 1900;
00046         tminfo.tm_mon = pWatchTime[2] - 1;
00047         tminfo.tm_mday = pWatchTime[3];
00048         tminfo.tm_hour = pWatchTime[4];
00049         tminfo.tm_min = pWatchTime[5];
00050         tminfo.tm_sec = pWatchTime[6];
00051         tminfo.tm_isdst = -1;
00052         time_t timest = mktime(&tminfo);
00053         return timest;
00054     }
00055     
00056     static void unixTimeToWatchTime(time_t unixTime, uint8_t* pWatchTime)
00057     {
00058         // Convert to localtime
00059         struct tm * timeinfo;     
00060         timeinfo = localtime (&unixTime);
00061         pWatchTime[0] = (timeinfo->tm_year + 1900) / 256;
00062         pWatchTime[1] = (timeinfo->tm_year + 1900) % 256;
00063         pWatchTime[2] = timeinfo->tm_mon + 1;
00064         pWatchTime[3] = timeinfo->tm_mday;
00065         pWatchTime[4] = timeinfo->tm_hour;
00066         pWatchTime[5] = timeinfo->tm_min;
00067         pWatchTime[6] = timeinfo->tm_sec;
00068     }
00069 
00070 private:
00071     BLE &_ble;
00072     ReadWriteArrayGattCharacteristic<uint8_t, WatchTime_BlockSize> _watchTimeVal;
00073     ReadWriteArrayGattCharacteristic<uint8_t, WatchTime_BlockSize> _alarmTimeVal;
00074 };
00075 
00076 #endif /* #ifndef __BLE_WATCHTIME_SERVICE_H__ */