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