-
Notifications
You must be signed in to change notification settings - Fork 0
/
mstime.h
113 lines (94 loc) · 2.74 KB
/
mstime.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Smart Toybox milisecond time services
//
// Copyright(C) 2016. Nebojsa Sumrak and Jelena (Petra) Markovic
//
// This program is free software; you can redistribute it and / or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110 - 1301 USA.
#pragma once
#include "common.h"
#include "debug.h"
#define TIMER_MS_RESOLUTION 50
#define SYS_CLK 80000000
#define MILLISECONDS_TO_TICKS(ms) ((SYS_CLK/1000) * (ms))
#define TIMER_COUNT 8
typedef void (*timer_callback)(unsigned char id);
struct timer_desc {
BOOL active;
BOOL paused;
unsigned char loopcnt; // loop count, 255 is infinite
unsigned long time;
unsigned long rest; // ms till timer expires
timer_callback cb;
};
extern struct timer_desc timers[TIMER_COUNT];
extern unsigned long g_timercount;
void time_init();
void timer_process();
inline void timer_set_cb(unsigned char id, unsigned long time, unsigned char loopcnt, timer_callback cb)
{
ASSERT(id < TIMER_COUNT);
timers[id].time = timers[id].rest = time;
timers[id].loopcnt = loopcnt;
timers[id].cb = cb;
timers[id].paused = 0;
timers[id].active = loopcnt ? 1 : 0;
}
inline void timer_set(unsigned char id, unsigned long time, unsigned char loopcnt)
{
timer_set_cb(id,time,loopcnt,0);
}
inline void timer_reset(unsigned char id)
{
ASSERT(id < TIMER_COUNT);
timers[id].rest = timers[id].time;
timers[id].paused = 0;
}
inline void timer_start(unsigned char id)
{
ASSERT(id < TIMER_COUNT);
timers[id].active = 1;
timers[id].paused = 0;
timers[id].rest = timers[id].time;
if(!timers[id].loopcnt) timers[id].loopcnt = 1;
}
inline void timer_stop(unsigned char id)
{
ASSERT(id < TIMER_COUNT);
timers[id].active = 0;
}
inline void timer_pause(unsigned char id)
{
ASSERT(id < TIMER_COUNT);
timers[id].paused = 1;
}
inline void timer_resume(unsigned char id)
{
ASSERT(id < TIMER_COUNT);
timers[id].paused = 0;
}
inline BOOL timer_active(unsigned char id)
{
ASSERT(id < TIMER_COUNT);
return (timers[id].active && !timers[id].paused);
}
inline void timer_delay(unsigned char id, unsigned long delay)
{
ASSERT(id < TIMER_COUNT);
if (timers[id].rest < delay)
timers[id].rest = delay;
}
inline unsigned long time_get()
{
return g_timercount;
}