forked from arduino-libraries/ArduinoIoTCloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimedAttempt.cpp
79 lines (62 loc) · 1.99 KB
/
TimedAttempt.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
/*
This file is part of the Arduino_SecureElement library.
Copyright (c) 2024 Arduino SA
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/******************************************************************************
* INCLUDE
******************************************************************************/
#include <Arduino.h>
#include "TimedAttempt.h"
/******************************************************************************
* CTOR/DTOR
******************************************************************************/
TimedAttempt::TimedAttempt(unsigned long minDelay, unsigned long maxDelay)
: _minDelay(minDelay)
, _maxDelay(maxDelay) {
}
/******************************************************************************
* PUBLIC MEMBER FUNCTIONS
******************************************************************************/
void TimedAttempt::begin(unsigned long delay) {
_retryCount = 0;
_minDelay = delay;
_maxDelay = delay;
}
void TimedAttempt::begin(unsigned long minDelay, unsigned long maxDelay) {
_retryCount = 0;
_minDelay = minDelay;
_maxDelay = maxDelay;
}
unsigned long TimedAttempt::reconfigure(unsigned long minDelay, unsigned long maxDelay) {
_minDelay = minDelay;
_maxDelay = maxDelay;
return reload();
}
unsigned long TimedAttempt::retry() {
_retryCount++;
return reload();
}
unsigned long TimedAttempt::reload() {
unsigned long retryDelay = (1 << _retryCount) * _minDelay;
_retryDelay = min(retryDelay, _maxDelay);
_nextRetryTick = millis() + _retryDelay;
return _retryDelay;
}
void TimedAttempt::reset() {
_retryCount = 0;
}
bool TimedAttempt::isRetry() {
return _retryCount > 0;
}
bool TimedAttempt::isExpired() {
return millis() > _nextRetryTick;
}
unsigned int TimedAttempt::getRetryCount() {
return _retryCount;
}
unsigned int TimedAttempt::getWaitTime() {
return _retryDelay;
}