-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisionState.cpp
89 lines (80 loc) · 1.96 KB
/
VisionState.cpp
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
#include "VisionState.h"
void VisionState::wait(unsigned long timeInMs, int nextState)
{
originalState = *this;
if (nextState == STATE_NEXT)
stateToSetAfterWait = *this + 1;
else if (nextState == STATE_LAST)
stateToSetAfterWait = *this - 1;
else
stateToSetAfterWait = nextState;
*this = STATE_WAIT;
time = 0;
timeToWait = timeInMs;
}
void VisionState::waitMicros(unsigned long timeInMicros, int nextState)
{
originalState = *this;
if (nextState == STATE_NEXT)
stateToSetAfterWait = *this + 1;
else if (nextState == STATE_LAST)
stateToSetAfterWait = *this - 1;
else
stateToSetAfterWait = nextState;
*this = STATE_WAIT_MICROS;
timeMicros = 0;
timeToWaitInMicros = timeInMicros;
}
void VisionState::waitFor(boolean (*functionToTestFor)(), int nextState)
{
originalState = *this;
if (nextState == STATE_NEXT)
stateToSetAfterWait = *this + 1;
else if (nextState == STATE_LAST)
stateToSetAfterWait = *this - 1;
else stateToSetAfterWait = nextState;
*this = STATE_WAIT_FOR;
testFunction = functionToTestFor;
}
void VisionState::doLoop()
{
switch (state)
{
case STATE_STOP:
break;
case STATE_WAIT:
if (time > timeToWait)
*this = stateToSetAfterWait;
break;
case STATE_WAIT_MICROS:
if (timeMicros > timeToWaitInMicros)
*this = stateToSetAfterWait;
break;
case STATE_WAIT_FOR:
if (testFunction())
*this = stateToSetAfterWait;
break;
}
}
VisionState::operator int()
{
return state;
}
int VisionState::operator =(const int state)
{
if (state == STATE_NEXT)
return ++(this->state);
return (this->state = state);
}
VisionState& VisionState::operator+=(const int val) {
*this = *this + val;
}
VisionState& VisionState::operator++() {
*this += 1;
return *this;
}
VisionState VisionState::operator++(int) {
VisionState tmp(*this); // copy
operator++(); // pre-incrementoriginalState = *this;
return tmp; // return old value
}