Skip to content

Commit 4e07305

Browse files
authored
Merge pull request #260 from MacGyverNL/master
Extend SoftwareTimer with option to make it non-repeating, add reset function & ISR-safe functions.
2 parents 11614da + 2ce11a6 commit 4e07305

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

cores/nRF5/utility/SoftwareTimer.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ class SoftwareTimer
4747
SoftwareTimer() { _handle = NULL; }
4848
virtual ~SoftwareTimer() { if(_handle != NULL) xTimerDelete(_handle, 0); }
4949

50-
void begin(uint32_t ms, TimerCallbackFunction_t callback)
50+
void begin(uint32_t ms, TimerCallbackFunction_t callback, bool repeating = true)
5151
{
52-
_handle = xTimerCreate(NULL, ms2tick(ms), true, NULL, callback);
52+
_handle = xTimerCreate(NULL, ms2tick(ms), repeating, NULL, callback);
5353
}
5454

5555
TimerHandle_t getHandle(void)
@@ -59,6 +59,28 @@ class SoftwareTimer
5959

6060
void start(void) { xTimerStart(_handle, 0); }
6161
void stop (void) { xTimerStop (_handle, 0); }
62+
void reset(void) { xTimerReset(_handle, 0); }
63+
64+
bool startFromISR(void) {
65+
BaseType_t ret, xHigherPriorityTaskWoken = pdFALSE;
66+
ret = xTimerStartFromISR(_handle, &xHigherPriorityTaskWoken);
67+
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
68+
return (ret == pdPASS);
69+
}
70+
71+
bool stopFromISR(void) {
72+
BaseType_t ret, xHigherPriorityTaskWoken = pdFALSE;
73+
ret = xTimerStopFromISR(_handle, &xHigherPriorityTaskWoken);
74+
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
75+
return (ret == pdPASS);
76+
}
77+
78+
bool resetFromISR(void) {
79+
BaseType_t ret, xHigherPriorityTaskWoken = pdFALSE;
80+
ret = xTimerResetFromISR(_handle, &xHigherPriorityTaskWoken);
81+
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
82+
return (ret == pdPASS);
83+
}
6284

6385
void setPeriod(uint32_t ms)
6486
{

0 commit comments

Comments
 (0)