|
| 1 | +/** |
| 2 | + * @file Hardware/AVR/RTC.h |
| 3 | + * @version 1.0 |
| 4 | + * |
| 5 | + * @section License |
| 6 | + * Copyright (C) 2017, Mikael Patel |
| 7 | + * |
| 8 | + * This library is free software; you can redistribute it and/or |
| 9 | + * modify it under the terms of the GNU Lesser General Public |
| 10 | + * License as published by the Free Software Foundation; either |
| 11 | + * version 2.1 of the License, or (at your option) any later version. |
| 12 | + * |
| 13 | + * This library is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | + * Lesser General Public License for more details. |
| 17 | + */ |
| 18 | + |
| 19 | +#ifndef HARDWARE_AVR_RTC_H |
| 20 | +#define HARDWARE_AVR_RTC_H |
| 21 | + |
| 22 | +/** |
| 23 | + * Software Real-Time Clock. |
| 24 | + */ |
| 25 | +class RTC { |
| 26 | +public: |
| 27 | + /** |
| 28 | + * Construct software real-time clock based on millis(). |
| 29 | + */ |
| 30 | + RTC() : |
| 31 | + m_millis(0), |
| 32 | + m_time(0) |
| 33 | + {} |
| 34 | + |
| 35 | + /** |
| 36 | + * Increment seconds counter when time has elapsed. |
| 37 | + * Return true(1) if an increment occured, otherwise false(0). |
| 38 | + */ |
| 39 | + bool tick() |
| 40 | + { |
| 41 | + uint16_t now = millis(); |
| 42 | + if (now - m_millis < 1000) return (false); |
| 43 | + uint8_t sreg = SREG; |
| 44 | + __asm__ __volatile__("cli" ::: "memory"); |
| 45 | + m_time += 1; |
| 46 | + m_millis = now; |
| 47 | + SREG = sreg; |
| 48 | + __asm__ __volatile__("" ::: "memory"); |
| 49 | + return (true); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Return the current time in seconds from epoch. |
| 54 | + */ |
| 55 | + time_t get_time() |
| 56 | + { |
| 57 | + uint8_t sreg = SREG; |
| 58 | + __asm__ __volatile__("cli" ::: "memory"); |
| 59 | + time_t res = m_time; |
| 60 | + SREG = sreg; |
| 61 | + __asm__ __volatile__("" ::: "memory"); |
| 62 | + return (res); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Set the current time (seconds) from epoch. |
| 67 | + */ |
| 68 | + void set_time(time_t time) |
| 69 | + { |
| 70 | + uint8_t sreg = SREG; |
| 71 | + __asm__ __volatile__("cli" ::: "memory"); |
| 72 | + m_time = time; |
| 73 | + SREG = sreg; |
| 74 | + __asm__ __volatile__("" ::: "memory"); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Return the current time as a time structure. |
| 79 | + */ |
| 80 | + void get_time(struct tm& now) |
| 81 | + { |
| 82 | + time_t time = get_time(); |
| 83 | + gmtime_r(&time, &now); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Set the current time based on the given time structure. |
| 88 | + */ |
| 89 | + void set_time(struct tm& now) |
| 90 | + { |
| 91 | + extern long __utc_offset; |
| 92 | + set_time(mktime(&now) + __utc_offset); |
| 93 | + } |
| 94 | + |
| 95 | +protected: |
| 96 | + /** Timestamp for previous tick call. */ |
| 97 | + volatile uint16_t m_millis; |
| 98 | + |
| 99 | + /** Current time from epoch. */ |
| 100 | + volatile time_t m_time; |
| 101 | +}; |
| 102 | + |
| 103 | +#endif |
0 commit comments