Imported Upstream version 2.9.0
[debian/cc1111] / device / examples / ds390 / rtc390 / rtc390.c
1 /*-------------------------------------------------------------------------
2   rtc390.c - time demo using the DS1315 (tested on TINI)
3   
4    Written By - Johan Knol, johan.knol@iduna.nl
5     
6    This program is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by the
8    Free Software Foundation; either version 2, or (at your option) any
9    later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19    
20    In other words, you are welcome to use, share and improve this program.
21    You are forbidden to forbid anyone else to use, share and improve
22    what you give them.   Help stamp out software-hoarding!  
23 -------------------------------------------------------------------------*/
24
25 #include <stdio.h>
26 #include <ctype.h>
27
28 // until we have a decent scanf:
29 int ScanInt(int current) {
30   char reply[32], *r;
31
32   gets(reply);
33   if (isdigit(*(r=reply))) {
34     current=0;
35     do {
36       current*=10;
37       current+=(*r++)-'0';
38     } while (isdigit(*r));
39   }
40   return current;
41 }
42
43 char GetTime(struct tm *rtcTime) {
44   printf ("Enter year [%d]: ", rtcTime->tm_year+1900);
45   rtcTime->tm_year=ScanInt(rtcTime->tm_year+1900);
46   printf ("Enter month [%d]: ", rtcTime->tm_mon+1);
47   rtcTime->tm_mon=ScanInt(rtcTime->tm_mon)-1;
48   printf ("Enter day [%d]: ", rtcTime->tm_mday);
49   rtcTime->tm_mday=ScanInt(rtcTime->tm_mday);
50   printf ("Enter hour [%d]: ", rtcTime->tm_hour);
51   rtcTime->tm_hour=ScanInt(rtcTime->tm_hour);
52   printf ("Enter minute [%d]: ", rtcTime->tm_min);
53   rtcTime->tm_min=ScanInt(rtcTime->tm_min);
54   printf ("Enter second [%d]: ", rtcTime->tm_sec);
55   rtcTime->tm_sec=ScanInt(rtcTime->tm_sec);
56   return 1;
57 }
58
59 void PrintTime(struct tm *rtcTime, char verbose) {
60
61   printf ("%s%04d-%02d-%02d %02d:%02d:%02d.%02d\n", 
62           verbose ? "RTC time: " : "",
63           rtcTime->tm_year+1900, rtcTime->tm_mon+1, rtcTime->tm_mday,
64           rtcTime->tm_hour, rtcTime->tm_min, rtcTime->tm_sec,
65           rtcTime->tm_hundredth);
66
67   if (verbose) {
68     time_t calendarTime=mktime(rtcTime);
69     printf ("Seconds since 00:00:00 Jan 01 1970: %ld\n", calendarTime);
70   }
71 }
72
73 void main (void) {
74   struct tm rtcTime, *now;
75   time_t calendarTime;
76   char seconds=-1;
77
78   printf ("\nStarting RTC demo.\n");
79
80   RtcRead(&rtcTime);
81   PrintTime(&rtcTime,1);
82
83   while(1) {
84     calendarTime=time(0);
85     now=localtime(&calendarTime);
86
87     if (now->tm_sec!=seconds) {
88       printf(ctime(&calendarTime));
89       seconds=now->tm_sec;
90     }
91
92     if (Serial0CharArrived()) {
93       switch (getchar()) 
94         {
95         case 0:
96           break;
97         case 'q': 
98           printf ("Ok.\n");
99           return;
100         default:
101           RtcRead(&rtcTime);
102           if (GetTime(&rtcTime)) {
103             RtcWrite(&rtcTime);
104             printf ("Time written.\n");
105           }
106         }
107     }
108   }
109 }
110