-
Notifications
You must be signed in to change notification settings - Fork 0
/
StepMultiplier.cpp
65 lines (58 loc) · 1.79 KB
/
StepMultiplier.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
/*
* StepMultiplier.cpp
*
* Created on: Sep 4, 2014
* Author: bastl
*/
#include "StepMultiplier.h"
//#define DEBUG
#ifdef DEBUG
#include <iostream>
#endif
StepMultiplier::StepMultiplier(void (*stepCallback)(), StepSwinger * _swinger, unsigned int timeUnitsPerSecond) : BastlStepper(stepCallback, _swinger) {
timeUnitsPerStep_ = (timeUnitsPerSecond * 60) / 120;
anyStep_ = false;
gotStep_ = false;
gotSyncStep_ = false;
stepsReceived_ = 0;
stepsLeftToTrigger_ = 0;
#ifdef DEBUG
printf("StepMultiplier::init - Setting timeUnitsPerStep %d\n", timeUnitsPerStep_);
#endif
}
void StepMultiplier::doStep(unsigned int elapsedTimeUnits) {
// We just make sure main loop thread knows something has happened
// and can process forced step
gotStep_ = true;
}
void StepMultiplier::update(unsigned int elapsedTimeUnits) {
if (gotStep_) {
gotStep_ = false;
if (anyStep_) {
timeUnitsPerStep_ = (elapsedTimeUnits - lastStepTimeUnits_) / multiplication_;
}
anyStep_ = true;
lastStepTimeUnits_ = elapsedTimeUnits;
bool sync = (!swinger_->swingUsed()) || ((stepsReceived_ * multiplication_) % 8 == 0);
stepsReceived_++;
if (sync) {
for (unsigned char stepNumber = 0; stepNumber <= stepsLeftToTrigger_; stepNumber++) {
stepCallback_();
}
stepsLeftToTrigger_ = 0;
lastSubStepTimeUnits_ = lastStepTimeUnits_;
}
stepsLeftToTrigger_ += multiplication_;
if (sync)
stepsLeftToTrigger_--;
}
unsigned int nextStepTime = swinger_->getNextStepLength(timeUnitsPerStep_ * 4);
if ((stepsLeftToTrigger_ != 0) &&
(elapsedTimeUnits - lastSubStepTimeUnits_ > nextStepTime)) {
stepCallback_();
stepsLeftToTrigger_--;
lastSubStepTimeUnits_ += nextStepTime;
}
//Need to do something with this one
closerToNextStep_ = (elapsedTimeUnits - lastSubStepTimeUnits_) > (nextStepTime / 2);
}