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
LedDisplay.h
00001 #ifndef __LED_DISPLAY_H__ 00002 #define __LED_DISPLAY_H__ 00003 00004 #include "nrf51_rtc.h" 00005 00006 // Hour LEDs 00007 const int numHourPins = 5; 00008 DigitalOut hourPins[numHourPins] = { p0, p1, p2, p3, p4 }; 00009 00010 // Minute LEDs 00011 const int numMinPins = 6; 00012 DigitalOut minPins[numMinPins] = { p23, p24, p25, p28, p29, p30 }; 00013 00014 // Status LEDs 00015 const int numStatusPins = 3; 00016 DigitalOut statusPins[numStatusPins] = { p6, p10, p11 }; 00017 00018 // Status bit numbers 00019 const int STATUS_BITS_EXTRA_BIT = 0; 00020 const int STATUS_BITS_SECS_BIT = 1; 00021 const int STATUS_BITS_ALARM_BIT = 2; 00022 00023 void callbackForLEDMuxing(); 00024 00025 class LedDisplay 00026 { 00027 public: 00028 enum DispType { 00029 DispType_None = 0, 00030 DispType_CurTime = 1, 00031 DispType_AlarmTime = 2, 00032 }; 00033 00034 LedDisplay() 00035 { 00036 _dispType = DispType_None; 00037 _timeDisplayStartTime = 0; 00038 _secsToDisplayFor = 0; 00039 _timeToShow_Hour = 0; 00040 _timeToShow_Min = 0; 00041 _statusBits = 0; 00042 _curTimeLEDBitPos = 0; 00043 clear(); 00044 } 00045 00046 void clear() 00047 { 00048 // Clear all LEDs 00049 for (int i = 0; i < numHourPins; i++) 00050 hourPins[i] = 0; 00051 for (int i = 0; i < numMinPins; i++) 00052 minPins[i] = 0; 00053 for (int i = 0; i < numStatusPins; i++) 00054 statusPins[i] = 0; 00055 } 00056 00057 void start(time_t timeToShow, DispType dispType, int secsToDisplayFor, bool extraInfoBit) 00058 { 00059 // Save time to show 00060 _timeToShow_time_t = timeToShow; 00061 _dispType = dispType; 00062 _secsToDisplayFor = secsToDisplayFor; 00063 if (dispType == DispType_None) 00064 return; 00065 00066 // Convert time to binary displayable info (localtime) 00067 if (((int)_timeToShow_time_t != -1) && ((int)_timeToShow_time_t != 0)) 00068 { 00069 struct tm * timeinfo; 00070 timeinfo = localtime (&_timeToShow_time_t); 00071 _timeToShow_Hour = timeinfo->tm_hour; 00072 _timeToShow_Min = timeinfo->tm_min; 00073 } 00074 else 00075 { 00076 _timeToShow_Hour = 0; 00077 _timeToShow_Min = 0; 00078 } 00079 00080 // Status - extra bit 00081 _statusBits = 0; 00082 if (extraInfoBit) 00083 _statusBits |= (1 << STATUS_BITS_EXTRA_BIT); 00084 00085 // Status - alarm bit 00086 if (dispType == DispType_AlarmTime) 00087 _statusBits |= (1 << STATUS_BITS_ALARM_BIT); 00088 else 00089 _statusBits &= (~(1 << STATUS_BITS_ALARM_BIT)); 00090 00091 // Record time we started displaying 00092 time_t curTime = rtc.time(); 00093 _timeDisplayStartTime = curTime; 00094 00095 // Ready to display on next callback 00096 _curTimeLEDBitPos = 0; 00097 _timerForLEDMuxing.attach(callbackForLEDMuxing, 0.001); 00098 00099 } 00100 00101 void stop() 00102 { 00103 _dispType = DispType_None; 00104 } 00105 00106 void showNext() 00107 { 00108 // Clear LEDs 00109 clear(); 00110 00111 // Check display type 00112 if (_dispType == DispType_None) 00113 return; 00114 00115 // Get current time 00116 time_t curTime = rtc.time(); 00117 00118 // Stop displaying time after a certain number of seconds 00119 if (curTime - _timeDisplayStartTime >= _secsToDisplayFor) 00120 { 00121 stop(); 00122 return; 00123 } 00124 00125 // Flash status seconds 00126 if (_dispType == DispType_CurTime) 00127 { 00128 if ((curTime % 2) == 0) 00129 _statusBits |= (1 << STATUS_BITS_SECS_BIT); 00130 else 00131 _statusBits &= (~(1 << STATUS_BITS_SECS_BIT)); 00132 } 00133 00134 // Display binary time 00135 int mask = 1 << _curTimeLEDBitPos; 00136 if ((_curTimeLEDBitPos < numHourPins) && ((_timeToShow_Hour & mask) != 0)) 00137 hourPins[_curTimeLEDBitPos] = 1; 00138 if ((_curTimeLEDBitPos < numMinPins) && ((_timeToShow_Min & mask) != 0)) 00139 minPins[_curTimeLEDBitPos] = 1; 00140 if ((_curTimeLEDBitPos < numStatusPins) && ((_statusBits & mask) != 0)) 00141 statusPins[_curTimeLEDBitPos] = 1; 00142 00143 // Next bit pos 00144 _curTimeLEDBitPos++; 00145 if (_curTimeLEDBitPos > numMinPins) 00146 _curTimeLEDBitPos = 0; 00147 00148 // Set for another callback 00149 _timerForLEDMuxing.attach(callbackForLEDMuxing, 0.001); 00150 } 00151 00152 private: 00153 00154 // Display type 00155 DispType _dispType; 00156 00157 // Time to show 00158 time_t _timeToShow_time_t; 00159 int _timeToShow_Hour; 00160 int _timeToShow_Min; 00161 00162 // Time displaying started and time to show for 00163 time_t _timeDisplayStartTime; 00164 int _secsToDisplayFor; 00165 00166 // Status bit 00167 int _statusBits; 00168 00169 // Timeout used to display LEDs 00170 Timeout _timerForLEDMuxing; 00171 00172 // Cur time disp info 00173 int _curTimeLEDBitPos; 00174 }; 00175 00176 static LedDisplay ledDisplay; 00177 00178 void callbackForLEDMuxing() 00179 { 00180 ledDisplay.showNext(); 00181 } 00182 00183 00184 #endif /* #ifndef __BLE_BUTTON_SERVICE_H__ */
Generated on Tue Jul 12 2022 19:13:45 by 1.7.2