-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathScheduledAction.cpp
63 lines (48 loc) · 1.42 KB
/
ScheduledAction.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
/*
Copyright (c) 2012-2013 Dominic Clifton. All right reserved.
*/
#ifndef LINUX
#include <Arduino.h>
#endif
#include <stdio.h>
#include "ScheduledAction.h"
void ScheduledAction::reset() {
nextActionAt = micros() + delayMicros;
missedActions = 0;
}
void ScheduledAction::setDelayMicros(unsigned long delayMicros) {
this->delayMicros = delayMicros;
}
void ScheduledAction::setDelayMillis(unsigned long delayMillis) {
this->delayMicros = delayMillis * 1000L;
}
unsigned long ScheduledAction::getLateBy(void) {
return lateBy;
}
int ScheduledAction::getMissedActions(void) {
return missedActions;
}
bool ScheduledAction::isActionDue(void) {
unsigned long now = micros();
//printf("now: %lu\n", now);
long signedDiff = now - nextActionAt;
//printf("signedDiff: %ld\n", signedDiff);
if (signedDiff < 0L) {
missedActions = 0;
lateBy = 0;
return false;
}
missedActions = signedDiff / delayMicros;
//printf("missedActions: %u\n", missedActions);
lateBy = signedDiff % delayMicros;
//printf("lateBy: %lu\n", lateBy);
nextActionAt = nextActionAt + (delayMicros - lateBy) + (delayMicros * missedActions);
//printf("nextActionAt: %lu\n", nextActionAt);
return true;
}
#ifdef LINUX
unsigned long ScheduledAction::micros(void) {
gettimeofday(&tempTime, NULL);
return (tempTime.tv_sec * (1000L * 1000L)) + tempTime.tv_usec;
}
#endif