Project

General

Profile

Statistics
| Branch: | Revision:

pse-2015-16-grupo-3 / tpf / tempo.c @ 441bdf28

History | View | Annotate | Download (4.71 KB)

1
#include "tempo.h"
2
#include "power.h"
3
#include "display.h"
4
#include "pse_timer1.h"
5

    
6
pse_time_t relogio;
7

    
8
uint8_t working;
9

    
10

    
11
//sets the initial time
12
uint8_t setTime(pse_time_t tempoInicial)
13
{
14
        //sets the seconds
15
        relogio.segundos = tempoInicial.segundos;
16
        //sets the minutes
17
        relogio.minutos = tempoInicial.minutos;
18
        //sets the hours
19
        relogio.horas = tempoInicial.horas;
20
        
21
        //sets the time in the display
22
        setDisplayTime(relogio);
23
        
24
        return 0;
25
}
26

    
27
//gets the current time
28
uint8_t getTime(pse_time_t *tempoActual)
29
{
30
        //gets the seconds
31
        tempoActual->segundos = relogio.segundos;
32
        //gets the minutes
33
        tempoActual->minutos = relogio.minutos;
34
        //gets the hours
35
        tempoActual->horas = relogio.horas;
36
        return 0;
37
}
38

    
39
//increases the time
40
uint8_t increaseTime(uint8_t nSeconds)
41
{
42
        int i;
43

    
44
        for(i = 0 ; i < nSeconds ; i++)
45
        {
46
                //increases one second each loop
47
                relogio.segundos++;
48

    
49
                //if the seconds == 60, sets seconds to 0 and increases minutes by 1
50
                if(relogio.segundos == 60)
51
                {
52
                        //set seconds to 0
53
                        relogio.segundos = 0;
54
                        //increases one minute
55
                        relogio.minutos++;
56
                }
57
                //if minutes == 60, sets minutes to 0 and increases hours by 1
58
                if(relogio.minutos == 60)
59
                {
60
                        //sets minutes to 0
61
                        relogio.minutos = 0;
62
                        //increases one hour
63
                        relogio.horas++;
64
                }
65
                //if hours == 24, sets hours to 0
66
                if(relogio.horas == 24)
67
                {        
68
                        //reset the hours
69
                        relogio.horas = 0;
70
                }
71
        }
72
        
73
        
74
        //increase successful
75
        return 0;
76
                
77
}
78

    
79
//decreases the time by certain number of seconds
80
uint8_t decreaseTime(uint8_t nSeconds)
81
{
82
        int i;
83
        for(i = 0 ; i < nSeconds ; i++)
84
        {
85
                //decreases 1 second
86
                relogio.segundos--;
87
                //if seconds == 255, sets segundos to 59 and decreases 1 minute
88
                if(relogio.segundos == 255)
89
                {
90
                        //sets seconds to 59
91
                        relogio.segundos = 59;
92
                        //decreases one minute
93
                        relogio.minutos--;
94
                }
95
                //if seconds == 255, sets minutes to 59 and decreases 1 hour
96
                if(relogio.minutos == 255)
97
                {
98
                        //sets minutes to 59
99
                        relogio.minutos = 59;
100
                        //decreases one hour
101
                        relogio.horas--;
102
                }
103
                //if hours == 255, sets hours to 23
104
                if(relogio.horas == 255)
105
                {
106
                        relogio.horas = 23;
107
                }
108
        }
109
        //sets the time in the display
110
        setDisplayTime(relogio);
111
        
112
        //successful decrease of time
113
        return 0;
114
}
115

    
116
//checks the onoff cycle
117
uint8_t checkONOFFCycle()
118
{
119
        //result variable, used for sucess verification
120
    uint8_t result = 0;
121
    
122

    
123
    //if the time is equal to 1 or more hours or to 1 or more minutes, sets the onoff cycle to 9000, else sets it to 4500
124
    if(relogio.horas > 0 || relogio.minutos > 0)
125
    {
126
        setONOFFCycle(9000);
127
    }
128
    else
129
    {
130
        setONOFFCycle(4500);
131
    }
132
    
133
    //returns result variable
134
    return result;
135
}
136

    
137

    
138
uint8_t increase()
139
{
140
        // "Time values under 1 min are set to the second"
141
        if(relogio.minutos < 1 && relogio.horas == 0)
142
        {
143
                increaseTime(1);
144
        }
145
        // "Time values from 1 to 3 min are set in multiples of 10 s"
146
        else if(relogio.minutos >= 1 && relogio.minutos <= 3)
147
        {
148
                increaseTime(10);
149
        }
150
        
151
        // "Time values larger than 3 min are set in multiples of 30 s"
152
        else if(relogio.minutos > 3)
153
        {
154
                increaseTime(30);
155
        }
156
        
157
        (uint8_t) checkONOFFCycle();
158
        
159
        setDisplayTime(relogio);
160
        
161
        return 0;
162
}
163

    
164
uint8_t decrease()
165
{
166
        // "Time values under 1 min are set to the second"
167
        if(relogio.minutos < 1 && relogio.horas == 0)
168
        {
169
                decreaseTime(1);        
170
        }
171
        // "Time values from 1 to 3 min are set in multiples of 10 s"
172
        else if(relogio.minutos >= 1 && relogio.minutos <= 3)
173
        {
174
                decreaseTime(10);
175
        }
176
        // "Time values larger than 3 min are set in multiples of 30 s"
177
        else if(relogio.minutos > 3 )
178
        {
179
                decreaseTime(30);
180
        }
181
        
182
        (uint8_t) checkONOFFCycle();
183
        
184
        setDisplayTime(relogio);
185
        
186
        return 0;        
187
}
188

    
189
//runs the time. 
190
uint8_t runTempo()
191
{
192
    uint8_t result=0;
193
    
194
    //if the time is not running, working state is set to 1 and the timer is configured
195
    if(working==0)
196
    {
197
        working = 1;
198
        setTimer(1, 1);
199
    }
200
    else
201
    {
202
        if (eocTimer(1, (double) 1) == 1) {
203
            decrease();
204
            resetTimer(1);
205
        }
206
    }
207
    
208
    return result;
209
    
210
    
211
}
212

    
213
//stops the time, by setting working state to 0
214
uint8_t stopTempo()
215
{
216
        //returned variable
217
    uint8_t result=0;
218
    
219
    working=0;
220
    
221

    
222
    return result;
223
}
224

    
225
//checks if time is equal to 0
226
uint8_t isTimeZero()
227
{
228
        //returned variable
229
    uint8_t result=0;
230
    
231
    //if hours, minutes and seconds equal to 0, sets the variable to return to 1 (true), 
232
    //indicating the time is indeed equal to 0. else 0 (false) is returned
233
    if(relogio.horas==0 && relogio.minutos==0 && relogio.segundos==0)
234
    {
235
        result=1;
236
    }
237
    
238
    return result;
239
}