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 | #ifndef __SIMPLETIMER_H__ |
||
18 | #define __SIMPLETIMER_H__ |
||
19 | |||
20 | #include <inttypes.h> |
||
21 | #include <avr/pgmspace.h> |
||
22 | #include "WProgram.h" |
||
23 | |||
24 | #define MSB_MASK 0x80000000 |
||
25 | |||
26 | typedef void (*TimerCallback)(); |
||
27 | |||
28 | class SimpleTimer |
||
29 | { |
||
30 | struct TimerAttributes |
||
31 | { |
||
32 | uint8_t bmAllocated : 1; // 1 if allocated for some routine |
||
33 | uint8_t bmTimerSet : 1; // 1 if all data for the timer is set |
||
34 | uint8_t bmEnabled : 1; // 1 if enabled and running |
||
35 | uint8_t bmOneTime : 1; // 1 if the task should be executed onece |
||
36 | uint8_t bmSign : 1; // necessary to handle millis() rollovers |
||
37 | }; |
||
38 | |||
39 | TimerAttributes timerAttribs; |
||
40 | uint32_t timeOut; |
||
41 | uint32_t timeToFire; |
||
42 | TimerCallback pCallback; |
||
43 | |||
44 | bool TimeoutEllapsed(); |
||
45 | |||
46 | public: |
||
47 | SimpleTimer() : timeOut(0), timeToFire(0), pCallback(NULL) { *((uint8_t*)&timerAttribs) = 0; }; |
||
48 | |||
49 | void SetAllocated(bool yes) { timerAttribs.bmAllocated = (yes) ? 1 : 0; }; |
||
50 | bool IsAllocated() { return timerAttribs.bmAllocated == 1; }; |
||
51 | bool IsEnabled() { return timerAttribs.bmEnabled == 1; }; |
||
52 | |||
53 | bool Set(TimerCallback task, uint32_t timeout, bool once = false); |
||
54 | void Reset(); |
||
55 | bool Enable(); |
||
56 | void Disable(); |
||
57 | void Run(); |
||
58 | |||
59 | uint32_t TimeLeft() |
||
60 | { |
||
61 | if (timerAttribs.bmEnabled != 1) |
||
62 | return 0; |
||
63 | |||
64 | int32_t time_left = timeToFire - millis(); |
||
65 | return (time_left > 0) ? time_left : 0; |
||
66 | }; |
||
67 | }; |
||
68 | |||
69 | #define INVALID_TIMER_ID 0 |
||
70 | |||
71 | template <class TIMER_TYPE, const uint8_t POOL_SIZE> |
||
72 | class TimerPool |
||
73 | { |
||
74 | TIMER_TYPE thePool[POOL_SIZE]; |
||
75 | |||
76 | public: |
||
77 | TimerPool() {}; |
||
78 | |||
79 | uint8_t SetTimer(TimerCallback pfunc, uint32_t msec, bool once); |
||
80 | bool EnableTimer(uint8_t timer_id) { if (!timer_id) return false; thePool[timer_id-1].Enable(); return true; }; |
||
81 | bool DisableTimer(uint8_t timer_id) { if (!timer_id) return false; thePool[timer_id-1].Disable(); return true; }; |
||
82 | bool ReleaseTimer(uint8_t &timer_id){ if (!timer_id) return false; thePool[timer_id-1].SetAllocated(false); thePool[timer_id-1].Reset(); timer_id = 0; return true; }; |
||
83 | void Run(); |
||
84 | }; |
||
85 | |||
86 | template <class TIMER_TYPE, const uint8_t POOL_SIZE> |
||
87 | uint8_t TimerPool<TIMER_TYPE, POOL_SIZE>::SetTimer(TimerCallback pfunc, uint32_t msec, bool once) |
||
88 | { |
||
89 | for (uint8_t i=0; i<POOL_SIZE; i++) |
||
90 | { |
||
91 | if (!thePool[i].IsAllocated()) |
||
92 | { |
||
93 | thePool[i].SetAllocated(true); |
||
94 | thePool[i].SetTimer(pfunc, msec, once); |
||
95 | return i + 1; |
||
96 | } |
||
97 | } |
||
98 | return INVALID_TIMER_ID; |
||
99 | } |
||
100 | |||
101 | template <class TIMER_TYPE, const uint8_t POOL_SIZE> |
||
102 | void TimerPool<TIMER_TYPE, POOL_SIZE>::Run() |
||
103 | { |
||
104 | for (uint8_t i=0; i<POOL_SIZE; i++) |
||
105 | thePool[i].Run(); |
||
106 | } |
||
107 | |||
108 | #endif // __SIMPLETIMER_H__ |