-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcounter.cpp
More file actions
47 lines (36 loc) · 817 Bytes
/
counter.cpp
File metadata and controls
47 lines (36 loc) · 817 Bytes
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
#include "counter.h"
Counter::Counter(
uint32_t limit, Counter::Mode mode, uint32_t index ) :
goingForward(true), index(index), limit(limit), mode(mode) { }
uint32_t Counter::getIndex() {
return index;
}
void Counter::increment() {
switch( mode ) {
case RESETTING : incrementResetting();
break;
case RETURNING : incrementReturning();
break;
}
}
void Counter::incrementResetting() {
index = (index + 1) % limit;
}
void Counter::incrementReturning() {
if( goingForward ) {
index++;
if( index >= limit ) {
index = limit - 1;
goingForward = false;
}
} else {
index--;
if( index <= 0 ) {
index = 1;
goingForward = true;
}
}
}
float Counter::percent() {
return (float) index / (float) limit;
}