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

Committer:
Bobty
Date:
Tue Mar 01 13:03:32 2016 +0000
Revision:
6:4a12e0f03381
Completed work on display and alarm

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Bobty 6:4a12e0f03381 1 #ifndef __LED_DISPLAY_H__
Bobty 6:4a12e0f03381 2 #define __LED_DISPLAY_H__
Bobty 6:4a12e0f03381 3
Bobty 6:4a12e0f03381 4 #include "nrf51_rtc.h"
Bobty 6:4a12e0f03381 5
Bobty 6:4a12e0f03381 6 // Hour LEDs
Bobty 6:4a12e0f03381 7 const int numHourPins = 5;
Bobty 6:4a12e0f03381 8 DigitalOut hourPins[numHourPins] = { p0, p1, p2, p3, p4 };
Bobty 6:4a12e0f03381 9
Bobty 6:4a12e0f03381 10 // Minute LEDs
Bobty 6:4a12e0f03381 11 const int numMinPins = 6;
Bobty 6:4a12e0f03381 12 DigitalOut minPins[numMinPins] = { p23, p24, p25, p28, p29, p30 };
Bobty 6:4a12e0f03381 13
Bobty 6:4a12e0f03381 14 // Status LEDs
Bobty 6:4a12e0f03381 15 const int numStatusPins = 3;
Bobty 6:4a12e0f03381 16 DigitalOut statusPins[numStatusPins] = { p6, p10, p11 };
Bobty 6:4a12e0f03381 17
Bobty 6:4a12e0f03381 18 // Status bit numbers
Bobty 6:4a12e0f03381 19 const int STATUS_BITS_EXTRA_BIT = 0;
Bobty 6:4a12e0f03381 20 const int STATUS_BITS_SECS_BIT = 1;
Bobty 6:4a12e0f03381 21 const int STATUS_BITS_ALARM_BIT = 2;
Bobty 6:4a12e0f03381 22
Bobty 6:4a12e0f03381 23 void callbackForLEDMuxing();
Bobty 6:4a12e0f03381 24
Bobty 6:4a12e0f03381 25 class LedDisplay
Bobty 6:4a12e0f03381 26 {
Bobty 6:4a12e0f03381 27 public:
Bobty 6:4a12e0f03381 28 enum DispType {
Bobty 6:4a12e0f03381 29 DispType_None = 0,
Bobty 6:4a12e0f03381 30 DispType_CurTime = 1,
Bobty 6:4a12e0f03381 31 DispType_AlarmTime = 2,
Bobty 6:4a12e0f03381 32 };
Bobty 6:4a12e0f03381 33
Bobty 6:4a12e0f03381 34 LedDisplay()
Bobty 6:4a12e0f03381 35 {
Bobty 6:4a12e0f03381 36 _dispType = DispType_None;
Bobty 6:4a12e0f03381 37 _timeDisplayStartTime = 0;
Bobty 6:4a12e0f03381 38 _secsToDisplayFor = 0;
Bobty 6:4a12e0f03381 39 _timeToShow_Hour = 0;
Bobty 6:4a12e0f03381 40 _timeToShow_Min = 0;
Bobty 6:4a12e0f03381 41 _statusBits = 0;
Bobty 6:4a12e0f03381 42 _curTimeLEDBitPos = 0;
Bobty 6:4a12e0f03381 43 clear();
Bobty 6:4a12e0f03381 44 }
Bobty 6:4a12e0f03381 45
Bobty 6:4a12e0f03381 46 void clear()
Bobty 6:4a12e0f03381 47 {
Bobty 6:4a12e0f03381 48 // Clear all LEDs
Bobty 6:4a12e0f03381 49 for (int i = 0; i < numHourPins; i++)
Bobty 6:4a12e0f03381 50 hourPins[i] = 0;
Bobty 6:4a12e0f03381 51 for (int i = 0; i < numMinPins; i++)
Bobty 6:4a12e0f03381 52 minPins[i] = 0;
Bobty 6:4a12e0f03381 53 for (int i = 0; i < numStatusPins; i++)
Bobty 6:4a12e0f03381 54 statusPins[i] = 0;
Bobty 6:4a12e0f03381 55 }
Bobty 6:4a12e0f03381 56
Bobty 6:4a12e0f03381 57 void start(time_t timeToShow, DispType dispType, int secsToDisplayFor, bool extraInfoBit)
Bobty 6:4a12e0f03381 58 {
Bobty 6:4a12e0f03381 59 // Save time to show
Bobty 6:4a12e0f03381 60 _timeToShow_time_t = timeToShow;
Bobty 6:4a12e0f03381 61 _dispType = dispType;
Bobty 6:4a12e0f03381 62 _secsToDisplayFor = secsToDisplayFor;
Bobty 6:4a12e0f03381 63 if (dispType == DispType_None)
Bobty 6:4a12e0f03381 64 return;
Bobty 6:4a12e0f03381 65
Bobty 6:4a12e0f03381 66 // Convert time to binary displayable info (localtime)
Bobty 6:4a12e0f03381 67 if (((int)_timeToShow_time_t != -1) && ((int)_timeToShow_time_t != 0))
Bobty 6:4a12e0f03381 68 {
Bobty 6:4a12e0f03381 69 struct tm * timeinfo;
Bobty 6:4a12e0f03381 70 timeinfo = localtime (&_timeToShow_time_t);
Bobty 6:4a12e0f03381 71 _timeToShow_Hour = timeinfo->tm_hour;
Bobty 6:4a12e0f03381 72 _timeToShow_Min = timeinfo->tm_min;
Bobty 6:4a12e0f03381 73 }
Bobty 6:4a12e0f03381 74 else
Bobty 6:4a12e0f03381 75 {
Bobty 6:4a12e0f03381 76 _timeToShow_Hour = 0;
Bobty 6:4a12e0f03381 77 _timeToShow_Min = 0;
Bobty 6:4a12e0f03381 78 }
Bobty 6:4a12e0f03381 79
Bobty 6:4a12e0f03381 80 // Status - extra bit
Bobty 6:4a12e0f03381 81 _statusBits = 0;
Bobty 6:4a12e0f03381 82 if (extraInfoBit)
Bobty 6:4a12e0f03381 83 _statusBits |= (1 << STATUS_BITS_EXTRA_BIT);
Bobty 6:4a12e0f03381 84
Bobty 6:4a12e0f03381 85 // Status - alarm bit
Bobty 6:4a12e0f03381 86 if (dispType == DispType_AlarmTime)
Bobty 6:4a12e0f03381 87 _statusBits |= (1 << STATUS_BITS_ALARM_BIT);
Bobty 6:4a12e0f03381 88 else
Bobty 6:4a12e0f03381 89 _statusBits &= (~(1 << STATUS_BITS_ALARM_BIT));
Bobty 6:4a12e0f03381 90
Bobty 6:4a12e0f03381 91 // Record time we started displaying
Bobty 6:4a12e0f03381 92 time_t curTime = rtc.time();
Bobty 6:4a12e0f03381 93 _timeDisplayStartTime = curTime;
Bobty 6:4a12e0f03381 94
Bobty 6:4a12e0f03381 95 // Ready to display on next callback
Bobty 6:4a12e0f03381 96 _curTimeLEDBitPos = 0;
Bobty 6:4a12e0f03381 97 _timerForLEDMuxing.attach(callbackForLEDMuxing, 0.001);
Bobty 6:4a12e0f03381 98
Bobty 6:4a12e0f03381 99 }
Bobty 6:4a12e0f03381 100
Bobty 6:4a12e0f03381 101 void stop()
Bobty 6:4a12e0f03381 102 {
Bobty 6:4a12e0f03381 103 _dispType = DispType_None;
Bobty 6:4a12e0f03381 104 }
Bobty 6:4a12e0f03381 105
Bobty 6:4a12e0f03381 106 void showNext()
Bobty 6:4a12e0f03381 107 {
Bobty 6:4a12e0f03381 108 // Clear LEDs
Bobty 6:4a12e0f03381 109 clear();
Bobty 6:4a12e0f03381 110
Bobty 6:4a12e0f03381 111 // Check display type
Bobty 6:4a12e0f03381 112 if (_dispType == DispType_None)
Bobty 6:4a12e0f03381 113 return;
Bobty 6:4a12e0f03381 114
Bobty 6:4a12e0f03381 115 // Get current time
Bobty 6:4a12e0f03381 116 time_t curTime = rtc.time();
Bobty 6:4a12e0f03381 117
Bobty 6:4a12e0f03381 118 // Stop displaying time after a certain number of seconds
Bobty 6:4a12e0f03381 119 if (curTime - _timeDisplayStartTime >= _secsToDisplayFor)
Bobty 6:4a12e0f03381 120 {
Bobty 6:4a12e0f03381 121 stop();
Bobty 6:4a12e0f03381 122 return;
Bobty 6:4a12e0f03381 123 }
Bobty 6:4a12e0f03381 124
Bobty 6:4a12e0f03381 125 // Flash status seconds
Bobty 6:4a12e0f03381 126 if (_dispType == DispType_CurTime)
Bobty 6:4a12e0f03381 127 {
Bobty 6:4a12e0f03381 128 if ((curTime % 2) == 0)
Bobty 6:4a12e0f03381 129 _statusBits |= (1 << STATUS_BITS_SECS_BIT);
Bobty 6:4a12e0f03381 130 else
Bobty 6:4a12e0f03381 131 _statusBits &= (~(1 << STATUS_BITS_SECS_BIT));
Bobty 6:4a12e0f03381 132 }
Bobty 6:4a12e0f03381 133
Bobty 6:4a12e0f03381 134 // Display binary time
Bobty 6:4a12e0f03381 135 int mask = 1 << _curTimeLEDBitPos;
Bobty 6:4a12e0f03381 136 if ((_curTimeLEDBitPos < numHourPins) && ((_timeToShow_Hour & mask) != 0))
Bobty 6:4a12e0f03381 137 hourPins[_curTimeLEDBitPos] = 1;
Bobty 6:4a12e0f03381 138 if ((_curTimeLEDBitPos < numMinPins) && ((_timeToShow_Min & mask) != 0))
Bobty 6:4a12e0f03381 139 minPins[_curTimeLEDBitPos] = 1;
Bobty 6:4a12e0f03381 140 if ((_curTimeLEDBitPos < numStatusPins) && ((_statusBits & mask) != 0))
Bobty 6:4a12e0f03381 141 statusPins[_curTimeLEDBitPos] = 1;
Bobty 6:4a12e0f03381 142
Bobty 6:4a12e0f03381 143 // Next bit pos
Bobty 6:4a12e0f03381 144 _curTimeLEDBitPos++;
Bobty 6:4a12e0f03381 145 if (_curTimeLEDBitPos > numMinPins)
Bobty 6:4a12e0f03381 146 _curTimeLEDBitPos = 0;
Bobty 6:4a12e0f03381 147
Bobty 6:4a12e0f03381 148 // Set for another callback
Bobty 6:4a12e0f03381 149 _timerForLEDMuxing.attach(callbackForLEDMuxing, 0.001);
Bobty 6:4a12e0f03381 150 }
Bobty 6:4a12e0f03381 151
Bobty 6:4a12e0f03381 152 private:
Bobty 6:4a12e0f03381 153
Bobty 6:4a12e0f03381 154 // Display type
Bobty 6:4a12e0f03381 155 DispType _dispType;
Bobty 6:4a12e0f03381 156
Bobty 6:4a12e0f03381 157 // Time to show
Bobty 6:4a12e0f03381 158 time_t _timeToShow_time_t;
Bobty 6:4a12e0f03381 159 int _timeToShow_Hour;
Bobty 6:4a12e0f03381 160 int _timeToShow_Min;
Bobty 6:4a12e0f03381 161
Bobty 6:4a12e0f03381 162 // Time displaying started and time to show for
Bobty 6:4a12e0f03381 163 time_t _timeDisplayStartTime;
Bobty 6:4a12e0f03381 164 int _secsToDisplayFor;
Bobty 6:4a12e0f03381 165
Bobty 6:4a12e0f03381 166 // Status bit
Bobty 6:4a12e0f03381 167 int _statusBits;
Bobty 6:4a12e0f03381 168
Bobty 6:4a12e0f03381 169 // Timeout used to display LEDs
Bobty 6:4a12e0f03381 170 Timeout _timerForLEDMuxing;
Bobty 6:4a12e0f03381 171
Bobty 6:4a12e0f03381 172 // Cur time disp info
Bobty 6:4a12e0f03381 173 int _curTimeLEDBitPos;
Bobty 6:4a12e0f03381 174 };
Bobty 6:4a12e0f03381 175
Bobty 6:4a12e0f03381 176 static LedDisplay ledDisplay;
Bobty 6:4a12e0f03381 177
Bobty 6:4a12e0f03381 178 void callbackForLEDMuxing()
Bobty 6:4a12e0f03381 179 {
Bobty 6:4a12e0f03381 180 ledDisplay.showNext();
Bobty 6:4a12e0f03381 181 }
Bobty 6:4a12e0f03381 182
Bobty 6:4a12e0f03381 183
Bobty 6:4a12e0f03381 184 #endif /* #ifndef __BLE_BUTTON_SERVICE_H__ */