Pseudo real-time clock using Ticker interruption

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PseudoRTC.h Source File

PseudoRTC.h

00001 /* Copyright (c) 2014 Shigenori Inoue, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 
00019 #ifndef __PseudoRTC__
00020 #define __PseudoRTC__
00021 
00022 #include "mbed.h"
00023 
00024 /**  Example:
00025  * @code
00026  * #include "mbed.h"
00027  * #include "PseudoRTC.h"
00028  *
00029  * PseudoRTC c;
00030  *
00031  * main()
00032  * {
00033  *     /* Example: September 20, 2014, 21:05:30
00034  *     c.setTime(2014, 09, 20, 21, 05, 30);
00035  * 
00036  *     while(true) {
00037  *         printf("%04d/%02d/%02d %02d:%02d:%02d\r\m", c.getYear(), c.getMonth(), c.getDay(), c.getHour(), c.getMinute(), c.getSecond());
00038  *         wait(1);
00039  *     }
00040  * }
00041  * @endcode
00042  */
00043 
00044 class PseudoRTC
00045 {
00046 public:
00047     /** Create a pseudo real-time clock */
00048     PseudoRTC(void);
00049 
00050     ~PseudoRTC(void);
00051 
00052     /** Set time in the pseudo real-time clock
00053      * @param y Year
00054      * @param mo Month
00055      * @param d Day
00056      * @param h Hour
00057      * @param m Minute
00058      * @param s Second
00059      */
00060     void setTime(int y, int mo, int d, int h, int mi, int s);
00061 
00062     /** Get the year value */
00063     int getYear(void);
00064 
00065     /** Get the month value */
00066     int getMonth(void);
00067 
00068     /** Get the day value */
00069     int getDay(void);
00070 
00071     /** Get the hour value */
00072     int getHour(void);
00073 
00074     /** Get the minute value */
00075     int getMinute(void);
00076 
00077     /** Get the second value */
00078     int getSecond(void);
00079 
00080 private:
00081     int year;
00082     int month;
00083     int day;
00084     int hour;
00085     int minute;
00086     int second;
00087     Ticker t;
00088     void tictoc(void);
00089 };
00090 
00091 #endif