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 "scheduler.h" |
||
18 | |||
19 | |||
20 | uint8_t DaysPerMonth(time_t t) |
||
21 | { |
||
22 | uint8_t mon = month(t); |
||
23 | switch (mon) |
||
24 | { |
||
25 | case 4: |
||
26 | case 6: |
||
27 | case 9: |
||
28 | case 11: |
||
29 | return 30; |
||
30 | case 2: |
||
31 | return (IS_LEAP_YEAR(year(t))) ? 29 : 28; |
||
32 | default: |
||
33 | return 31; |
||
34 | } |
||
35 | } |
||
36 | |||
37 | bool SchedulerTask::Set(TaskCallback pfunc, uint8_t pt, time_t time, uint8_t num) |
||
38 | { |
||
39 | timeToFire = time; |
||
40 | taskAttribs.bmTaskSet = 1; |
||
41 | taskAttribs.bmEnabled = 0; |
||
42 | taskAttribs.bmPeriodType = pt; |
||
43 | pCallback = pfunc; |
||
44 | countDown = num; |
||
45 | return true; |
||
46 | } |
||
47 | |||
48 | bool SchedulerTask::Run(time_t time) |
||
49 | { |
||
50 | if (!IsSet() || !IsEnabled()) |
||
51 | return true; |
||
52 | |||
53 | if (time >= timeToFire) |
||
54 | { |
||
55 | pCallback(); |
||
56 | |||
57 | if (countDown != DO_IT_FOREVER) |
||
58 | countDown --; |
||
59 | |||
60 | UpdateTime(); |
||
61 | } |
||
62 | return true; |
||
63 | } |
||
64 | |||
65 | bool SchedulerTask::UpdateTime() |
||
66 | { |
||
67 | if (!countDown) |
||
68 | return true; |
||
69 | switch (taskAttribs.bmPeriodType) |
||
70 | { |
||
71 | case enHourly: |
||
72 | timeToFire += SECS_PER_HOUR; |
||
73 | break; |
||
74 | case enDaily: |
||
75 | timeToFire += SECS_PER_DAY; |
||
76 | break; |
||
77 | case enWeekly: |
||
78 | timeToFire += SECS_PER_WEEK; |
||
79 | break; |
||
80 | case enMonthly: |
||
81 | timeToFire += DaysPerMonth(timeToFire) * SECS_PER_DAY; |
||
82 | break; |
||
83 | case enYearly: |
||
84 | timeToFire += DAYS_PER_YEAR(year(timeToFire)); |
||
85 | break; |
||
86 | } |
||
87 | return true; |
||
88 | } |