/elec/propeller-clock

To get this branch, use:
bzr branch http://bzr.ed.am/elec/propeller-clock

« back to all changes in this revision

Viewing changes to src/time.cc

  • Committer: edam
  • Date: 2012-02-02 01:19:19 UTC
  • Revision ID: edam@waxworlds.org-20120202011919-rcu4pt8mq8pieujs
modified schematic and updated notes

Show diffs side-by-side

added added

removed removed

1
 
/*
2
 
 * time.cc
3
 
 *
4
 
 * Copyright (C) 2011 Tim Marston <tim@ed.am> and Dan Marston.
5
 
 *
6
 
 * This file is part of propeller-clock (hereafter referred to as "this
7
 
 * program"). See http://ed.am/dev/software/arduino/propeller-clock for more
8
 
 * information.
9
 
 *
10
 
 * This program is free software: you can redistribute it and/or modify
11
 
 * it under the terms of the GNU Lesser General Public License as published
12
 
 * by the Free Software Foundation, either version 3 of the License, or
13
 
 * (at your option) any later version.
14
 
 *
15
 
 * This program is distributed in the hope that it will be useful,
16
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 
 * GNU Lesser General Public License for more details.
19
 
 *
20
 
 * You should have received a copy of the GNU Lesser General Public License
21
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 
 */
23
 
#include "time.h"
24
 
#include <DS1307.h>
25
 
#include <Wire.h>
26
 
 
27
 
 
28
 
Time &Time::get_instance()
29
 
{
30
 
        static Time time;
31
 
        return time;
32
 
}
33
 
 
34
 
 
35
 
int Time::get_hours()
36
 
{
37
 
        return _hours;
38
 
}
39
 
 
40
 
 
41
 
int Time::get_minutes()
42
 
{
43
 
        return _minutes;
44
 
}
45
 
 
46
 
 
47
 
int Time::get_seconds()
48
 
{
49
 
        return _seconds;
50
 
}
51
 
 
52
 
 
53
 
void Time::update()
54
 
{
55
 
        // how many milliseonds have elapsed since we last checked?
56
 
        unsigned long millis = ::millis();
57
 
        unsigned long delta = millis - _last_millis + _carry;
58
 
 
59
 
        // update the previous time and carried-over milliseconds
60
 
        _last_millis = millis;
61
 
        _carry = delta % 1000;
62
 
 
63
 
        // add the seconds that have passed to the time
64
 
        _seconds += delta / 1000;
65
 
        while( _seconds >= 60 ) {
66
 
                _seconds -= 60;
67
 
                _minutes++;
68
 
                if( _minutes >= 60 ) {
69
 
                        _minutes -= 60;
70
 
                        _hours++;
71
 
                        if( _hours >= 24 )
72
 
                                _hours -= 24;
73
 
                }
74
 
        }
75
 
}
76
 
 
77
 
 
78
 
Time::Time()
79
 
        :
80
 
        _last_millis( millis() ),
81
 
        _carry( 0 )
82
 
{
83
 
        // get the time from the real-time clock
84
 
        int rtc_data[ 7 ];
85
 
        RTC.get( rtc_data, true );
86
 
        _hours = rtc_data[ DS1307_HR ];
87
 
        _minutes = rtc_data[ DS1307_MIN ];
88
 
        _seconds = rtc_data[ DS1307_SEC ];
89
 
}
90