MHVLib
20111011
An efficiency oriented runtime library for AVR microcontrollers
|
00001 /* 00002 * Copyright (c) 2011, Make, Hack, Void Inc 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * * Redistributions of source code must retain the above copyright 00008 * notice, this list of conditions and the following disclaimer. 00009 * * Redistributions in binary form must reproduce the above copyright 00010 * notice, this list of conditions and the following disclaimer in the 00011 * documentation and/or other materials provided with the distribution. 00012 * * Neither the name of the Make, Hack, Void nor the 00013 * names of its contributors may be used to endorse or promote products 00014 * derived from this software without specific prior written permission. 00015 * 00016 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00017 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00018 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00019 * DISCLAIMED. IN NO EVENT SHALL MAKE, HACK, VOID BE LIABLE FOR ANY 00020 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00021 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00022 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00023 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00024 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00025 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 */ 00027 #ifndef MHV_RTC_H_ 00028 #define MHV_RTC_H_ 00029 00030 #include <MHV_Timer8.h> 00031 00032 struct mhv_timestamp { 00033 uint32_t timestamp; 00034 uint16_t milliseconds; 00035 }; 00036 typedef struct mhv_timestamp MHV_TIMESTAMP; 00037 00038 00039 enum mhv_weekday { 00040 MHV_SUNDAY, 00041 MHV_MONDAY, 00042 MHV_TUESDAY, 00043 MHV_WEDNESDAY, 00044 MHV_THURSDAY, 00045 MHV_FRIDAY, 00046 MHV_SATURDAY 00047 }; 00048 typedef enum mhv_weekday MHV_WEEKDAY; 00049 00050 enum mhv_month { 00051 MHV_JANUARY = 1, 00052 MHV_FEBRUARY = 2, 00053 MHV_MARCH = 3, 00054 MHV_APRIL = 4, 00055 MHV_MAY = 5, 00056 MHV_JUNE = 6, 00057 MHV_JULY = 7, 00058 MHV_AUGUST = 8, 00059 MHV_SEPTEMBER = 9, 00060 MHV_OCTOBER = 10, 00061 MHV_NOVEMBER = 11, 00062 MHV_DECEMBER = 12 00063 }; 00064 typedef enum mhv_month MHV_MONTH; 00065 00066 00067 struct mhv_time { 00068 uint16_t milliseconds; // 0 - 999 00069 uint8_t seconds; // 0 - 59 (we don't do leap seconds) 00070 uint8_t minutes; // 0 - 59 00071 uint8_t hours; // 0 - 23 00072 uint8_t day; // 1 - 31 day of the month 00073 MHV_MONTH month; // 1 - 12 00074 uint16_t year; // 1970 - 2106 00075 // MHV_WEEKDAY weekday; // Weekday 00076 uint16_t yearday; // 0 - 365 Day of the year 00077 uint16_t timezone; // Timezone offset in minutes 00078 }; 00079 typedef struct mhv_time MHV_TIME; 00080 00081 class MHV_AlarmListener; 00082 struct mhv_alarm { 00083 MHV_TIMESTAMP when; 00084 MHV_TIMESTAMP repeat; 00085 MHV_AlarmListener *listener; 00086 }; 00087 typedef struct mhv_alarm MHV_ALARM; 00088 00089 class MHV_AlarmListener { 00090 public: 00091 virtual void alarm(MHV_ALARM *alarm) =0; 00092 }; 00093 00094 00095 void mhv_timestampIncrement(MHV_TIMESTAMP *timestamp, uint32_t seconds, uint16_t milliseconds); 00096 void mhv_timestampIncrement(MHV_TIMESTAMP *timestamp, MHV_TIMESTAMP *timestamp2); 00097 bool mhv_isLeapYear(uint16_t year); 00098 bool mhv_timestampGreaterThanOrEqual(MHV_TIMESTAMP *first, MHV_TIMESTAMP *second); 00099 bool mhv_timestampLessThan(MHV_TIMESTAMP *first, MHV_TIMESTAMP *second); 00100 uint8_t mhv_daysInMonth(MHV_MONTH month, uint16_t year); 00101 00102 class MHV_RTC { 00103 protected: 00104 MHV_Timer8 *_timer; 00105 MHV_ALARM *_alarms; 00106 volatile uint8_t _alarmCount; 00107 uint8_t _alarmMax; 00108 uint32_t _timestamp; 00109 uint16_t _milliseconds; 00110 uint8_t _ticks; 00111 uint8_t _ticksPerMillisecond; 00112 int16_t _tzOffset; // Timezone offset in minutes +/- GMT 00113 00114 00115 public: 00116 MHV_RTC(MHV_Timer8 *timer, MHV_ALARM *eventBuffer, uint8_t eventCount, int16_t timezone); 00117 void synchronise(); 00118 void setTime(uint32_t timestamp, uint16_t milliseconds); 00119 void setTime(MHV_TIMESTAMP *timestamp); 00120 void tick(); 00121 void tick1ms(); 00122 void tickAndRunEvents(); 00123 void tick1msAndRunEvents(); 00124 void current(MHV_TIMESTAMP *timestamp); 00125 void elapsed(MHV_TIMESTAMP *since, MHV_TIMESTAMP *elapsed); 00126 void toTime(MHV_TIME *to, MHV_TIMESTAMP *from); 00127 void toTimestamp(MHV_TIMESTAMP *to, MHV_TIME *from); 00128 bool addAlarm(MHV_ALARM *alarm); 00129 void handleEvents(); 00130 uint8_t alarmsPending(); 00131 void removeAlarm(MHV_AlarmListener *listener); 00132 00133 00134 }; 00135 00136 #endif /* MHV_RTC_H_ */