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
WatchTimeService.h@6:4a12e0f03381, 2016-03-01 (annotated)
- Committer:
- Bobty
- Date:
- Tue Mar 01 13:03:32 2016 +0000
- Revision:
- 6:4a12e0f03381
- Parent:
- 5:2682353a8c32
Completed work on display and alarm
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Bobty | 3:a4b8d67de1b1 | 1 | // BLE Service for Watch Time |
Bobty | 3:a4b8d67de1b1 | 2 | // To allow a watch device to be told the current time |
Bobty | 3:a4b8d67de1b1 | 3 | |
Bobty | 3:a4b8d67de1b1 | 4 | #ifndef __BLE_WATCHTIME_SERVICE_H__ |
Bobty | 3:a4b8d67de1b1 | 5 | #define __BLE_WATCHTIME_SERVICE_H__ |
Bobty | 3:a4b8d67de1b1 | 6 | |
Bobty | 3:a4b8d67de1b1 | 7 | class WatchTimeService { |
Bobty | 3:a4b8d67de1b1 | 8 | public: |
Bobty | 3:a4b8d67de1b1 | 9 | |
Bobty | 3:a4b8d67de1b1 | 10 | static const int WatchTime_BlockSize = 7; |
Bobty | 3:a4b8d67de1b1 | 11 | const static uint16_t WATCHTIME_SERVICE_UUID = 0xFE32; |
Bobty | 3:a4b8d67de1b1 | 12 | const static uint16_t WATCHTIME_STATE_CHARACTERISTIC_UUID = 0xFE33; |
Bobty | 6:4a12e0f03381 | 13 | const static uint16_t ALARMTIME_STATE_CHARACTERISTIC_UUID = 0xFE34; |
Bobty | 3:a4b8d67de1b1 | 14 | |
Bobty | 6:4a12e0f03381 | 15 | WatchTimeService(BLE &ble, uint8_t* pInitialWatchTime, uint8_t* pInitialAlarmTime) : |
Bobty | 5:2682353a8c32 | 16 | _ble(ble), |
Bobty | 6:4a12e0f03381 | 17 | _watchTimeVal(WATCHTIME_STATE_CHARACTERISTIC_UUID, pInitialWatchTime, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
Bobty | 6:4a12e0f03381 | 18 | _alarmTimeVal(ALARMTIME_STATE_CHARACTERISTIC_UUID, pInitialAlarmTime, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) |
Bobty | 3:a4b8d67de1b1 | 19 | { |
Bobty | 6:4a12e0f03381 | 20 | GattCharacteristic *charTable[] = {&_watchTimeVal, &_alarmTimeVal}; |
Bobty | 3:a4b8d67de1b1 | 21 | GattService watchTimeService(WatchTimeService::WATCHTIME_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
Bobty | 5:2682353a8c32 | 22 | _ble.gattServer().addService(watchTimeService); |
Bobty | 3:a4b8d67de1b1 | 23 | } |
Bobty | 3:a4b8d67de1b1 | 24 | |
Bobty | 6:4a12e0f03381 | 25 | void writeWatchTime(time_t timeToWrite) |
Bobty | 5:2682353a8c32 | 26 | { |
Bobty | 6:4a12e0f03381 | 27 | uint8_t watchTime[WatchTimeService::WatchTime_BlockSize]; |
Bobty | 6:4a12e0f03381 | 28 | unixTimeToWatchTime(timeToWrite, watchTime); |
Bobty | 6:4a12e0f03381 | 29 | _ble.gattServer().write(_watchTimeVal.getValueHandle(), watchTime, WatchTime_BlockSize); |
Bobty | 3:a4b8d67de1b1 | 30 | } |
Bobty | 3:a4b8d67de1b1 | 31 | |
Bobty | 6:4a12e0f03381 | 32 | GattAttribute::Handle_t getWatchTimeValueHandle() |
Bobty | 3:a4b8d67de1b1 | 33 | { |
Bobty | 5:2682353a8c32 | 34 | return _watchTimeVal.getValueHandle(); |
Bobty | 3:a4b8d67de1b1 | 35 | } |
Bobty | 6:4a12e0f03381 | 36 | |
Bobty | 6:4a12e0f03381 | 37 | GattAttribute::Handle_t getAlarmTimeValueHandle() |
Bobty | 6:4a12e0f03381 | 38 | { |
Bobty | 6:4a12e0f03381 | 39 | return _alarmTimeVal.getValueHandle(); |
Bobty | 6:4a12e0f03381 | 40 | } |
Bobty | 6:4a12e0f03381 | 41 | |
Bobty | 6:4a12e0f03381 | 42 | static time_t watchTimeToUnixTime(const uint8_t* pWatchTime) |
Bobty | 6:4a12e0f03381 | 43 | { |
Bobty | 6:4a12e0f03381 | 44 | struct tm tminfo; |
Bobty | 6:4a12e0f03381 | 45 | tminfo.tm_year = int(pWatchTime[0])*256 + pWatchTime[1] - 1900; |
Bobty | 6:4a12e0f03381 | 46 | tminfo.tm_mon = pWatchTime[2] - 1; |
Bobty | 6:4a12e0f03381 | 47 | tminfo.tm_mday = pWatchTime[3]; |
Bobty | 6:4a12e0f03381 | 48 | tminfo.tm_hour = pWatchTime[4]; |
Bobty | 6:4a12e0f03381 | 49 | tminfo.tm_min = pWatchTime[5]; |
Bobty | 6:4a12e0f03381 | 50 | tminfo.tm_sec = pWatchTime[6]; |
Bobty | 6:4a12e0f03381 | 51 | tminfo.tm_isdst = -1; |
Bobty | 6:4a12e0f03381 | 52 | time_t timest = mktime(&tminfo); |
Bobty | 6:4a12e0f03381 | 53 | return timest; |
Bobty | 6:4a12e0f03381 | 54 | } |
Bobty | 6:4a12e0f03381 | 55 | |
Bobty | 6:4a12e0f03381 | 56 | static void unixTimeToWatchTime(time_t unixTime, uint8_t* pWatchTime) |
Bobty | 6:4a12e0f03381 | 57 | { |
Bobty | 6:4a12e0f03381 | 58 | // Convert to localtime |
Bobty | 6:4a12e0f03381 | 59 | struct tm * timeinfo; |
Bobty | 6:4a12e0f03381 | 60 | timeinfo = localtime (&unixTime); |
Bobty | 6:4a12e0f03381 | 61 | pWatchTime[0] = (timeinfo->tm_year + 1900) / 256; |
Bobty | 6:4a12e0f03381 | 62 | pWatchTime[1] = (timeinfo->tm_year + 1900) % 256; |
Bobty | 6:4a12e0f03381 | 63 | pWatchTime[2] = timeinfo->tm_mon + 1; |
Bobty | 6:4a12e0f03381 | 64 | pWatchTime[3] = timeinfo->tm_mday; |
Bobty | 6:4a12e0f03381 | 65 | pWatchTime[4] = timeinfo->tm_hour; |
Bobty | 6:4a12e0f03381 | 66 | pWatchTime[5] = timeinfo->tm_min; |
Bobty | 6:4a12e0f03381 | 67 | pWatchTime[6] = timeinfo->tm_sec; |
Bobty | 6:4a12e0f03381 | 68 | } |
Bobty | 3:a4b8d67de1b1 | 69 | |
Bobty | 3:a4b8d67de1b1 | 70 | private: |
Bobty | 5:2682353a8c32 | 71 | BLE &_ble; |
Bobty | 5:2682353a8c32 | 72 | ReadWriteArrayGattCharacteristic<uint8_t, WatchTime_BlockSize> _watchTimeVal; |
Bobty | 6:4a12e0f03381 | 73 | ReadWriteArrayGattCharacteristic<uint8_t, WatchTime_BlockSize> _alarmTimeVal; |
Bobty | 3:a4b8d67de1b1 | 74 | }; |
Bobty | 3:a4b8d67de1b1 | 75 | |
Bobty | 3:a4b8d67de1b1 | 76 | #endif /* #ifndef __BLE_WATCHTIME_SERVICE_H__ */ |