Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1702 - 1
/* Copyright (C) 2010-2011 Circuits At Home, LTD. All rights reserved.
2
 
3
This software may be distributed and modified under the terms of the GNU
4
General Public License version 2 (GPL2) as published by the Free Software
5
Foundation and appearing in the file GPL2.TXT included in the packaging of
6
this file. Please note that GPL2 Section 2[b] requires that all works based
7
on this software must also be made publicly available under the terms of
8
the GPL2 ("Copyleft").
9
 
10
Contact information
11
-------------------
12
 
13
Circuits At Home, LTD
14
Web      :  http://www.circuitsathome.com
15
e-mail   :  support@circuitsathome.com
16
*/
17
#include "SimpleTimer.h"
18
 
19
bool SimpleTimer::Set(TimerCallback task, uint32_t timeout, bool once)
20
{
21
    if (!task)
22
        return false;
23
 
24
    timeOut     = timeout;
25
    pCallback   = task;
26
    timerAttribs.bmOneTime   = (once) ? 1 : 0;
27
    timerAttribs.bmTimerSet  = 1;
28
    timerAttribs.bmEnabled   = 0;
29
    return true;
30
}
31
 
32
void SimpleTimer::Reset()
33
{
34
    timerAttribs.bmTimerSet  = 0;
35
    timerAttribs.bmEnabled   = 0;
36
    timerAttribs.bmOneTime   = 0;
37
    timerAttribs.bmSign      = 0;
38
 
39
    pCallback = NULL;
40
}
41
 
42
bool SimpleTimer::Enable()
43
{
44
    if (!pCallback || !timeOut)
45
        return false;
46
 
47
    uint32_t time = millis();
48
 
49
    timeToFire = time + timeOut;
50
 
51
    timerAttribs.bmEnabled   = 1;
52
    timerAttribs.bmSign = time & MSB_MASK;
53
    return true;
54
}
55
 
56
void SimpleTimer::Disable()
57
{
58
    timerAttribs.bmEnabled   = 0;
59
}
60
 
61
void SimpleTimer::Run()
62
{
63
    if (timerAttribs.bmEnabled == 0)
64
        return;
65
 
66
    if (TimeoutEllapsed())
67
    {
68
        if (pCallback)
69
            pCallback();
70
 
71
        if (timerAttribs.bmOneTime == 1)
72
            timerAttribs.bmEnabled = 0;
73
    }
74
}
75
 
76
bool SimpleTimer::TimeoutEllapsed()
77
{
78
    bool      ret   = false;
79
    uint32_t  time  = millis();
80
 
81
    if (time >= timeToFire || (time < timeToFire && timerAttribs.bmSign == 1) )
82
    {
83
        timeToFire = time + timeOut;
84
        ret = true;
85
    }
86
    timerAttribs.bmSign = (time & MSB_MASK) ? 1 : 0;
87
    return ret;
88
}