Alarm library for use with mbed.

Committer:
charlesgt
Date:
Sat Oct 30 23:41:22 2010 +0000
Revision:
0:33dddd0644c5
Child:
1:f4c7f13e5553
First alpha, not quite working yet

Who changed what in which revision?

UserRevisionLine numberNew contents of line
charlesgt 0:33dddd0644c5 1 /* mbed AlarmTimeDate Library
charlesgt 0:33dddd0644c5 2 * Copyright (c) 2010 Charles Garcia-Tobin
charlesgt 0:33dddd0644c5 3 *
charlesgt 0:33dddd0644c5 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
charlesgt 0:33dddd0644c5 5 * of this software and associated documentation files (the "Software"), to deal
charlesgt 0:33dddd0644c5 6 * in the Software without restriction, including without limitation the rights
charlesgt 0:33dddd0644c5 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
charlesgt 0:33dddd0644c5 8 * copies of the Software, and to permit persons to whom the Software is
charlesgt 0:33dddd0644c5 9 * furnished to do so, subject to the following conditions:
charlesgt 0:33dddd0644c5 10 *
charlesgt 0:33dddd0644c5 11 * The above copyright notice and this permission notice shall be included in
charlesgt 0:33dddd0644c5 12 * all copies or substantial portions of the Software.
charlesgt 0:33dddd0644c5 13 *
charlesgt 0:33dddd0644c5 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
charlesgt 0:33dddd0644c5 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
charlesgt 0:33dddd0644c5 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
charlesgt 0:33dddd0644c5 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
charlesgt 0:33dddd0644c5 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
charlesgt 0:33dddd0644c5 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
charlesgt 0:33dddd0644c5 20 * THE SOFTWARE.
charlesgt 0:33dddd0644c5 21 */
charlesgt 0:33dddd0644c5 22
charlesgt 0:33dddd0644c5 23 #ifndef __ALARM_TIME_DATE_H__
charlesgt 0:33dddd0644c5 24 #define __ALARM_TIME_DATE_H__
charlesgt 0:33dddd0644c5 25
charlesgt 0:33dddd0644c5 26 /**
charlesgt 0:33dddd0644c5 27 * Alarm callback function pointer type
charlesgt 0:33dddd0644c5 28 */
charlesgt 0:33dddd0644c5 29 typedef void (*AlarmCbFn)(void*);
charlesgt 0:33dddd0644c5 30
charlesgt 0:33dddd0644c5 31 /**
charlesgt 0:33dddd0644c5 32 * Alarm callback function pointer type
charlesgt 0:33dddd0644c5 33 */
charlesgt 0:33dddd0644c5 34 typedef void (*SleepFn)(void*);
charlesgt 0:33dddd0644c5 35
charlesgt 0:33dddd0644c5 36 /**
charlesgt 0:33dddd0644c5 37 * AlarmTimeDate class
charlesgt 0:33dddd0644c5 38 * Alarm class, alarms can be set for a specific date and time and can be set to repeat
charlesgt 0:33dddd0644c5 39 * daily, weekly, fortnightly, monthly or yearly. It is also possible to repeat
charlesgt 0:33dddd0644c5 40 * daily skip whilst skipping weekends.
charlesgt 0:33dddd0644c5 41 * The main program loop needs to call the Tick function below. However the function
charlesgt 0:33dddd0644c5 42 * does take a SleepFn function pointer which can be used to to sleep the device
charlesgt 0:33dddd0644c5 43 * the class will generate an RTC interrupt when an alarm goes off
charlesgt 0:33dddd0644c5 44 * Example usage:
charlesgt 0:33dddd0644c5 45 * #include "mbed.h"
charlesgt 0:33dddd0644c5 46 * #include "AlarmTimeDate.h"
charlesgt 0:33dddd0644c5 47 *
charlesgt 0:33dddd0644c5 48 * DigitalOut led1(LED1);
charlesgt 0:33dddd0644c5 49 *
charlesgt 0:33dddd0644c5 50 * void __DoSleep(void *) {
charlesgt 0:33dddd0644c5 51 * __WFE();
charlesgt 0:33dddd0644c5 52 * }
charlesgt 0:33dddd0644c5 53 *
charlesgt 0:33dddd0644c5 54 * void AlarmFn(void *) {
charlesgt 0:33dddd0644c5 55 * led1 = !led1;
charlesgt 0:33dddd0644c5 56 * time_t now = time(NULL);
charlesgt 0:33dddd0644c5 57 * printf("Expiring %s! ",ctime(&now));
charlesgt 0:33dddd0644c5 58 * }
charlesgt 0:33dddd0644c5 59 *
charlesgt 0:33dddd0644c5 60 *
charlesgt 0:33dddd0644c5 61 * int main() {
charlesgt 0:33dddd0644c5 62 * led1 = 0;
charlesgt 0:33dddd0644c5 63 * time_t seconds;
charlesgt 0:33dddd0644c5 64 *
charlesgt 0:33dddd0644c5 65 * // setup time structure for 29 Oct 2010 00:00:00
charlesgt 0:33dddd0644c5 66 * struct tm t;
charlesgt 0:33dddd0644c5 67 * t.tm_sec = 00; // 0-59
charlesgt 0:33dddd0644c5 68 * t.tm_min = 00; // 0-59
charlesgt 0:33dddd0644c5 69 * t.tm_hour = 00; // 0-23
charlesgt 0:33dddd0644c5 70 * t.tm_mday = 29; // 1-31
charlesgt 0:33dddd0644c5 71 * t.tm_mon = 9; // 0-11
charlesgt 0:33dddd0644c5 72 * t.tm_year = 110; // year since 1900
charlesgt 0:33dddd0644c5 73 * seconds = mktime(&t);
charlesgt 0:33dddd0644c5 74 * set_time(seconds);
charlesgt 0:33dddd0644c5 75 * AlarmTimeDate::Init();
charlesgt 0:33dddd0644c5 76 * AlarmTimeDate a1(AlarmFn,NULL,seconds+2);
charlesgt 0:33dddd0644c5 77 * do {
charlesgt 0:33dddd0644c5 78 * AlarmTimeDate::PrintAlarms();
charlesgt 0:33dddd0644c5 79 * AlarmTimeDate::Tick(__DoSleep,NULL);
charlesgt 0:33dddd0644c5 80 * seconds+=4;
charlesgt 0:33dddd0644c5 81 * a1.Set(seconds);
charlesgt 0:33dddd0644c5 82 * } while(1);
charlesgt 0:33dddd0644c5 83 * }
charlesgt 0:33dddd0644c5 84 **/
charlesgt 0:33dddd0644c5 85
charlesgt 0:33dddd0644c5 86 class AlarmTimeDate {
charlesgt 0:33dddd0644c5 87 public:
charlesgt 0:33dddd0644c5 88 /**
charlesgt 0:33dddd0644c5 89 * repeat types
charlesgt 0:33dddd0644c5 90 */
charlesgt 0:33dddd0644c5 91 enum TRepeat {
charlesgt 0:33dddd0644c5 92 ENone,
charlesgt 0:33dddd0644c5 93 EDaily,
charlesgt 0:33dddd0644c5 94 EDailyNoWeeked,
charlesgt 0:33dddd0644c5 95 EWeekly,
charlesgt 0:33dddd0644c5 96 EFortNightly,
charlesgt 0:33dddd0644c5 97 EMonthly,
charlesgt 0:33dddd0644c5 98 EYearly
charlesgt 0:33dddd0644c5 99 };
charlesgt 0:33dddd0644c5 100
charlesgt 0:33dddd0644c5 101 /**
charlesgt 0:33dddd0644c5 102 * @return time of this alarm's expiry as a time_t (secs since 00:00:00 1/1/1970)
charlesgt 0:33dddd0644c5 103 * 0 if alarm is not set
charlesgt 0:33dddd0644c5 104 */
charlesgt 0:33dddd0644c5 105 time_t AsTimeT() { return iTime; }
charlesgt 0:33dddd0644c5 106 /**
charlesgt 0:33dddd0644c5 107 * @return time of this alarm's expiry as a tm struct
charlesgt 0:33dddd0644c5 108 * NULL if alarm is not set
charlesgt 0:33dddd0644c5 109 */
charlesgt 0:33dddd0644c5 110 struct tm* AsTM() { return (iTime) ? localtime(&iTime) : NULL; }
charlesgt 0:33dddd0644c5 111
charlesgt 0:33dddd0644c5 112 /** Set alarm using time_t value (secs since 00:00:00 1/1/1970)
charlesgt 0:33dddd0644c5 113 * @param time_t aTime alarm expiry time
charlesgt 0:33dddd0644c5 114 */
charlesgt 0:33dddd0644c5 115 void Set(time_t aTime) { iTime = aTime; Tick(NULL,NULL); }
charlesgt 0:33dddd0644c5 116
charlesgt 0:33dddd0644c5 117 /** Set alarm using tm struct
charlesgt 0:33dddd0644c5 118 * @param struct tm* aTm alarm expiry time
charlesgt 0:33dddd0644c5 119 */
charlesgt 0:33dddd0644c5 120 void Set(struct tm* aTm) {iTime = mktime(aTm); Tick(NULL,NULL); }
charlesgt 0:33dddd0644c5 121
charlesgt 0:33dddd0644c5 122 /**
charlesgt 0:33dddd0644c5 123 * @return repeat type
charlesgt 0:33dddd0644c5 124 */
charlesgt 0:33dddd0644c5 125 TRepeat Repeat() {
charlesgt 0:33dddd0644c5 126 return iRepeat;
charlesgt 0:33dddd0644c5 127 }
charlesgt 0:33dddd0644c5 128
charlesgt 0:33dddd0644c5 129 /** Set repeat type
charlesgt 0:33dddd0644c5 130 * @param TRepeat aRepeat
charlesgt 0:33dddd0644c5 131 */
charlesgt 0:33dddd0644c5 132 void SetRepeat(TRepeat aRepeat) {
charlesgt 0:33dddd0644c5 133 iRepeat = aRepeat;
charlesgt 0:33dddd0644c5 134 Tick(NULL,NULL);
charlesgt 0:33dddd0644c5 135 }
charlesgt 0:33dddd0644c5 136
charlesgt 0:33dddd0644c5 137 /** Set pointer to pass to alarm callback function
charlesgt 0:33dddd0644c5 138 * @param void* aPtr
charlesgt 0:33dddd0644c5 139 */
charlesgt 0:33dddd0644c5 140 void SetPtr(void *aPtr) { iPtr = aPtr; }
charlesgt 0:33dddd0644c5 141
charlesgt 0:33dddd0644c5 142 /**
charlesgt 0:33dddd0644c5 143 * @return pointer used in alarm callback function
charlesgt 0:33dddd0644c5 144 */
charlesgt 0:33dddd0644c5 145 void* Ptr() { return iPtr; }
charlesgt 0:33dddd0644c5 146
charlesgt 0:33dddd0644c5 147 /** Set alarm callback function
charlesgt 0:33dddd0644c5 148 * @param AlarmCbFn aFn
charlesgt 0:33dddd0644c5 149 */
charlesgt 0:33dddd0644c5 150 void SetFn(AlarmCbFn aFn) { iFn = aFn; }
charlesgt 0:33dddd0644c5 151
charlesgt 0:33dddd0644c5 152 /**
charlesgt 0:33dddd0644c5 153 * @return pointer to alarm callback function
charlesgt 0:33dddd0644c5 154 */
charlesgt 0:33dddd0644c5 155 AlarmCbFn Fn() { return iFn; }
charlesgt 0:33dddd0644c5 156
charlesgt 0:33dddd0644c5 157 /** default constructor, alarm not set
charlesgt 0:33dddd0644c5 158 * @param AlarmCbFn aFn callback function
charlesgt 0:33dddd0644c5 159 * @param void* aPtr callback function parameter
charlesgt 0:33dddd0644c5 160 */
charlesgt 0:33dddd0644c5 161 AlarmTimeDate(AlarmCbFn aFn, void* aPtr);
charlesgt 0:33dddd0644c5 162
charlesgt 0:33dddd0644c5 163 /** fixed time/date
charlesgt 0:33dddd0644c5 164 * @param AlarmCbFn aFn callback function
charlesgt 0:33dddd0644c5 165 * @param void* aPtr callback function parameter
charlesgt 0:33dddd0644c5 166 * @param int aHours hour 0 to 23
charlesgt 0:33dddd0644c5 167 * @param int aMins mins 0 to 59
charlesgt 0:33dddd0644c5 168 * @param int aSecs seconds 0 to 59
charlesgt 0:33dddd0644c5 169 * @param int aDayM day of month 1 to 31
charlesgt 0:33dddd0644c5 170 * @param int aMon month 1 to 12
charlesgt 0:33dddd0644c5 171 * @param int aYear
charlesgt 0:33dddd0644c5 172 */
charlesgt 0:33dddd0644c5 173 AlarmTimeDate(AlarmCbFn aFn, void* aPtr, int aHours, int aMins, int aSecs, int aDayM, int aMon, int aYear);
charlesgt 0:33dddd0644c5 174 /** create daily alarm
charlesgt 0:33dddd0644c5 175 * @param AlarmCbFn aFn callback function
charlesgt 0:33dddd0644c5 176 * @param void* aPtr callback function parameter
charlesgt 0:33dddd0644c5 177 * @param int aHours
charlesgt 0:33dddd0644c5 178 * @param int aMins
charlesgt 0:33dddd0644c5 179 * @param int aSecs
charlesgt 0:33dddd0644c5 180 * @param bool aSkipWeekends default to false
charlesgt 0:33dddd0644c5 181 */
charlesgt 0:33dddd0644c5 182 AlarmTimeDate(AlarmCbFn aFn, void* aPtr, int aHours, int aMins, int aSecs, bool aSkipWeekends = false);
charlesgt 0:33dddd0644c5 183
charlesgt 0:33dddd0644c5 184 /** weekly or fornightly
charlesgt 0:33dddd0644c5 185 * @param AlarmCbFn aFn callback function
charlesgt 0:33dddd0644c5 186 * @param void* aPtr callback function parameter
charlesgt 0:33dddd0644c5 187 * @param int aHours
charlesgt 0:33dddd0644c5 188 * @param int aMins
charlesgt 0:33dddd0644c5 189 * @param int aSecs
charlesgt 0:33dddd0644c5 190 * @param int aDayOfWeek day of week 0..6 (0 is Sunday)create daily alarm
charlesgt 0:33dddd0644c5 191 * @param bool aFortNightly default to false
charlesgt 0:33dddd0644c5 192 */
charlesgt 0:33dddd0644c5 193 AlarmTimeDate(AlarmCbFn aFn, void* aPtr, int aHours, int aMins, int aSecs, int aDayW, bool aFortNightly = false);
charlesgt 0:33dddd0644c5 194
charlesgt 0:33dddd0644c5 195 /** Monthly
charlesgt 0:33dddd0644c5 196 * @param AlarmCbFn aFn callback function
charlesgt 0:33dddd0644c5 197 * @param void* aPtr callback function parameter
charlesgt 0:33dddd0644c5 198 * @param int aHours
charlesgt 0:33dddd0644c5 199 * @param int aMins
charlesgt 0:33dddd0644c5 200 * @param int aSecs
charlesgt 0:33dddd0644c5 201 * @param int aDayM day of month 1 to 31
charlesgt 0:33dddd0644c5 202 */
charlesgt 0:33dddd0644c5 203 AlarmTimeDate(AlarmCbFn aFn, void* aPtr, int aHours, int aMins, int aSecs, int aDayM);
charlesgt 0:33dddd0644c5 204
charlesgt 0:33dddd0644c5 205 /** Yearly
charlesgt 0:33dddd0644c5 206 * @param AlarmCbFn aFn callback function
charlesgt 0:33dddd0644c5 207 * @param void* aPtr callback function parameter
charlesgt 0:33dddd0644c5 208 * @param int aHours
charlesgt 0:33dddd0644c5 209 * @param int aMins
charlesgt 0:33dddd0644c5 210 * @param int aSecs
charlesgt 0:33dddd0644c5 211 * @param int aDayM day of month 1 to 31
charlesgt 0:33dddd0644c5 212 * @param int aMonth 1 to 12
charlesgt 0:33dddd0644c5 213 */
charlesgt 0:33dddd0644c5 214 AlarmTimeDate(AlarmCbFn aFn, void* aPtr, int aHours, int aMins, int aSecs, int aDayM, int aMon); // Yearly
charlesgt 0:33dddd0644c5 215
charlesgt 0:33dddd0644c5 216 /** construct with tm type
charlesgt 0:33dddd0644c5 217 * note that day of year and day of week are ignored, if repeating is required use SetRepeat
charlesgt 0:33dddd0644c5 218 * @param AlarmCbFn aFn callback function
charlesgt 0:33dddd0644c5 219 * @param void* aPtr callback function parameter
charlesgt 0:33dddd0644c5 220 * @param aTm
charlesgt 0:33dddd0644c5 221 * @see SetRepeat
charlesgt 0:33dddd0644c5 222 */
charlesgt 0:33dddd0644c5 223 AlarmTimeDate(AlarmCbFn aFn, void* aPtr, struct tm* aTm );
charlesgt 0:33dddd0644c5 224
charlesgt 0:33dddd0644c5 225 /** construct with seconds since 00:00:00 1st Jan 1970
charlesgt 0:33dddd0644c5 226 * @param AlarmCbFn aFn callback function
charlesgt 0:33dddd0644c5 227 * @param void* aPtr callback function parameter
charlesgt 0:33dddd0644c5 228 * @param aSec
charlesgt 0:33dddd0644c5 229 * @see SetRepeat
charlesgt 0:33dddd0644c5 230 */
charlesgt 0:33dddd0644c5 231 AlarmTimeDate(AlarmCbFn aFn, void* aPtr, time_t aSec );
charlesgt 0:33dddd0644c5 232
charlesgt 0:33dddd0644c5 233 /** desctructor
charlesgt 0:33dddd0644c5 234 */
charlesgt 0:33dddd0644c5 235 ~AlarmTimeDate();
charlesgt 0:33dddd0644c5 236
charlesgt 0:33dddd0644c5 237 /** print when Alarm will next expire
charlesgt 0:33dddd0644c5 238 */
charlesgt 0:33dddd0644c5 239 void PrintAlarm();
charlesgt 0:33dddd0644c5 240
charlesgt 0:33dddd0644c5 241 /** print all alarms expiry times
charlesgt 0:33dddd0644c5 242 */
charlesgt 0:33dddd0644c5 243 static void PrintAlarms();
charlesgt 0:33dddd0644c5 244
charlesgt 0:33dddd0644c5 245 /** Tick the Alarm system, will expire any pending alarms, set next one to expire
charlesgt 0:33dddd0644c5 246 * and then sleep. The sleep method is provided by the SleepFn function pointer and
charlesgt 0:33dddd0644c5 247 * should be based around a WFE or WFI.The alarm expiry causes an RTC which will
charlesgt 0:33dddd0644c5 248 * cause the sleep function to wake, after that Tick will exit and should be called
charlesgt 0:33dddd0644c5 249 * as part of the main loop of the applicaiton
charlesgt 0:33dddd0644c5 250 * @param SleepFn aFn sleep function
charlesgt 0:33dddd0644c5 251 * @param void* aParam optional paramter to pass into the sleep function
charlesgt 0:33dddd0644c5 252 */
charlesgt 0:33dddd0644c5 253 static void Tick(SleepFn aFn,void* aParam);
charlesgt 0:33dddd0644c5 254
charlesgt 0:33dddd0644c5 255 /** Init function to be called once before alarms are used
charlesgt 0:33dddd0644c5 256 */
charlesgt 0:33dddd0644c5 257 static void Init();
charlesgt 0:33dddd0644c5 258 private:
charlesgt 0:33dddd0644c5 259 void Queue();
charlesgt 0:33dddd0644c5 260 void DeQueue();
charlesgt 0:33dddd0644c5 261 void Expire();
charlesgt 0:33dddd0644c5 262 void Set();
charlesgt 0:33dddd0644c5 263 static void RTCISR();
charlesgt 0:33dddd0644c5 264
charlesgt 0:33dddd0644c5 265 private:
charlesgt 0:33dddd0644c5 266 time_t iTime;
charlesgt 0:33dddd0644c5 267 AlarmCbFn iFn;
charlesgt 0:33dddd0644c5 268 void* iPtr;
charlesgt 0:33dddd0644c5 269 AlarmTimeDate* iNext;
charlesgt 0:33dddd0644c5 270 TRepeat iRepeat;
charlesgt 0:33dddd0644c5 271 };
charlesgt 0:33dddd0644c5 272
charlesgt 0:33dddd0644c5 273 #endif //__ALARM_TIME_DATE_H__