1 /*
2 * Author: Jon Trulson <jtrulson@ics.com>
3 * Copyright (c) 2014 Intel Corporation.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include <unistd.h>
26 #include <iostream>
27 #include "ds1307.h"
28
29 using namespace std;
30
printTime(upm::DS1307 * rtc)31 void printTime(upm::DS1307 *rtc)
32 {
33 cout << "The time is: " <<
34 rtc->month << "/" << rtc->dayOfMonth << "/" << rtc->year << " "
35 << rtc->hours << ":" << rtc->minutes << ":" << rtc->seconds;
36
37 if (rtc->amPmMode)
38 cout << (rtc->pm) ? " PM " : " AM ";
39
40 cout << endl;
41
42 cout << "Clock is in " << ((rtc->amPmMode) ? "AM/PM mode" : "24hr mode")
43 << endl;
44 }
45
46 int
main(int argc,char ** argv)47 main(int argc, char **argv)
48 {
49 //! [Interesting]
50 // Instantiate a DS1037 on I2C bus 0
51 upm::DS1307 *rtc = new upm::DS1307(0);
52
53 // always do this first
54 cout << "Loading the current time... " << endl;
55 if (!rtc->loadTime())
56 {
57 cerr << "rtc->loadTime() failed." << endl;
58 return 0;
59 }
60
61 printTime(rtc);
62
63 // set the year as an example
64 cout << "setting the year to 50" << endl;
65 rtc->year = 50;
66
67 rtc->setTime();
68
69 // reload the time and print it
70 rtc->loadTime();
71 printTime(rtc);
72
73 //! [Interesting]
74
75 delete rtc;
76 return 0;
77 }
78