Pseudo real-time clock using Ticker interruption, also implements time() and set_time() for platforms that don't (such as mBuino)

Fork of PseudoRTC by Shigenori Inoue

Files at this revision

API Documentation at this revision

Comitter:
s_inoue_mbed
Date:
Sat Sep 20 14:18:46 2014 +0000
Child:
1:fb8fd750e935
Commit message:
First version.

Changed in this revision

PseudoRTC.cpp Show annotated file Show diff for this revision Revisions of this file
PseudoRTC.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PseudoRTC.cpp	Sat Sep 20 14:18:46 2014 +0000
@@ -0,0 +1,109 @@
+/* Copyright (c) 2014 Shigenori Inoue, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include "PseudoRTC.h"
+
+PseudoRTC::PseudoRTC(void)
+{
+    year = 0;
+    month = 1;
+    day = 1;
+    hour = 1;
+    minute = 0;
+    second = 0;
+    t.attach(this, &PseudoRTC::tictoc, 1);
+}
+
+PseudoRTC::~PseudoRTC(void) {}
+
+
+void PseudoRTC::setTime(int y, int mo, int d, int h, int mi, int s)
+{
+    /* Check the validity */
+    if (month < 13 && day < 32 && hour < 24 && minute < 60 && second < 60) {
+        year = y;
+        month = mo;
+        day = d;
+        hour = h;
+        minute = mi;
+        second = s;
+    }
+}
+
+int PseudoRTC::getYear(void)
+{
+    return year;
+}
+
+int PseudoRTC::getMonth(void)
+{
+    return month;
+}
+
+int PseudoRTC::getDay(void)
+{
+    return day;
+}
+
+int PseudoRTC::getHour(void)
+{
+    return hour;
+}
+
+int PseudoRTC::getMinute(void)
+{
+    return minute;
+}
+
+int PseudoRTC::getSecond(void)
+{
+    return second;
+}
+
+void PseudoRTC::tictoc(void)
+{
+    if(second < 59) {
+        second++;
+    } else {
+        second = 0;
+        minute++;
+    }
+    if(minute > 59) {
+        minute = 0;
+        hour++;
+    }
+    if(hour > 23) {
+        hour = 0;
+        day++;
+    }
+    if(day > 30 && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)) {
+        day = 1;
+        month++;
+    }
+    if(day > 29 && (month == 4 || month == 6 || month == 9 || month == 11)) {
+        day = 1;
+        month++;
+    }
+    if(day > 27 && month == 2) {
+        day = 1;
+        month++;
+    }
+    if(month > 12) {
+        year++;
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PseudoRTC.h	Sat Sep 20 14:18:46 2014 +0000
@@ -0,0 +1,71 @@
+/* Copyright (c) 2014 Shigenori Inoue, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef __PseudoRTC__
+#define __PseudoRTC__
+
+#include "mbed.h"
+
+class PseudoRTC
+{
+public:
+    /** Create a pseudo real-time clock */
+    PseudoRTC(void);
+
+    ~PseudoRTC(void);
+
+    /** Set time in the pseudo real-time clock
+     * @param y Year
+     * @param mo Month
+     * @param d Day
+     * @param h Hour
+     * @param m Minute
+     * @param s Second
+     */
+    void setTime(int y, int mo, int d, int h, int mi, int s);
+
+    /** Get the year value */
+    int getYear(void);
+
+    /** Get the month value */
+    int getMonth(void);
+
+    /** Get the day value */
+    int getDay(void);
+
+    /** Get the hour value */
+    int getHour(void);
+
+    /** Get the minute value */
+    int getMinute(void);
+
+    /** Get the second value */
+    int getSecond(void);
+
+private:
+    int year;
+    int month;
+    int day;
+    int hour;
+    int minute;
+    int second;
+    Ticker t;
+    void tictoc(void);
+};
+
+#endif
\ No newline at end of file