|
MHVLib
20111227
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(const 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 00108 #define MHV_RTC_CREATE_TZ(_mhvObjectName,_mhvEventCount,_mhvTimezone) \ 00109 MHV_ALARM _mhvObjectName ## Alarms[_mhvEventCount]; \ 00110 MHV_RTC _mhvObjectName(_mhvObjectName ## Alarms, _mhvEventCount,_mhvTimezone); 00111 00117 #define MHV_RTC_CREATE(_mhvObjectName,_mhvEventCount) \ 00118 MHV_ALARM _mhvObjectName ## Alarms[_mhvEventCount]; \ 00119 MHV_RTC _mhvObjectName(_mhvObjectName ## Alarms, _mhvEventCount); 00120 00121 00122 class MHV_RTC { 00123 private: 00124 inline void incrementMilliseconds(); 00125 00126 protected: 00127 MHV_ALARM *_alarms; 00128 volatile uint8_t _alarmCount; 00129 uint8_t _alarmMax; 00130 uint32_t _timestamp; 00131 uint16_t _milliseconds; 00132 uint8_t _ticks; 00133 uint8_t _ticksPerMillisecond; 00134 int16_t _tzOffset; // Timezone offset in minutes +/- GMT 00135 00136 00137 public: 00138 MHV_RTC(MHV_ALARM eventBuffer[], uint8_t eventCount, int16_t timezone); 00139 MHV_RTC(MHV_ALARM eventBuffer[], uint8_t eventCount); 00140 void synchronise(MHV_Timer8 &_timer); 00141 void setTime(uint32_t timestamp, uint16_t milliseconds); 00142 void setTime(MHV_TIMESTAMP *timestamp); 00143 void tick(); 00144 void tick1ms(); 00145 void tickAndRunEvents(); 00146 void tick1msAndRunEvents(); 00147 void current(MHV_TIMESTAMP *timestamp); 00148 void elapsed(MHV_TIMESTAMP *since, MHV_TIMESTAMP *elapsed); 00149 void toTime(MHV_TIME *to, MHV_TIMESTAMP *from); 00150 void toTimestamp(MHV_TIMESTAMP *to, MHV_TIME *from); 00151 bool addAlarm(MHV_ALARM *alarm); 00152 void handleEvents(); 00153 uint8_t alarmsPending(); 00154 void removeAlarm(MHV_AlarmListener &listener); 00155 void removeAlarm(MHV_AlarmListener *listener); 00156 00157 00158 }; 00159 00160 #endif /* MHV_RTC_H_ */