Skip to content

Commit 3452c2d

Browse files
author
dave
committed
#30 small fix up on enable task #33 fix ESP8266 cache attribute
1 parent c698f04 commit 3452c2d

File tree

6 files changed

+35
-26
lines changed

6 files changed

+35
-26
lines changed

examples/mbedRtos/mbedExample.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
#include <mbed.h>
1010
#include <TaskManagerIO.h>
11+
#include <TmLongSchedule.h>
12+
13+
#define LOG_TASK_MANGER_DEBUG 0
1114

1215
// Here we create a serial object to write log statements to.
1316
BufferedSerial console(USBTX, USBRX, 115200);
@@ -156,6 +159,12 @@ void setupTasks() {
156159
taskManager.scheduleFixedRate(2, [capturedValue]() {
157160
log("Execution with captured value = ", capturedValue);
158161
}, TIME_SECONDS);
162+
163+
// this shows how to create a long schedule event using the new operator, make sure the second parameter is true
164+
// as this will delete the event when it completes.
165+
taskManager.registerEvent(new TmLongSchedule(makeHourSchedule(0, 15), [] {
166+
log("Fifteen minutes passed");
167+
}), true);
159168
}
160169

161170
bool exitThreads = false;
@@ -191,12 +200,14 @@ void anotherProc() {
191200
int main() {
192201
log("starting up taskmanager example");
193202

203+
#if LOG_TASK_MANGER_DEBUG != 0
194204
// this is how we get diagnostic information from task manager
195205
// it will notify of significant events to the loggingDelegate.
196206
tm_internal::setLoggingDelegate([](tm_internal::TmErrorCode code, int task) {
197207
log("Taskmgr notification code: ", code);
198208
log(" -> Task num: ", task);
199209
});
210+
#endif //LOG_TASK_MANGER_DEBUG
200211

201212
setupTasks();
202213

src/TaskPlatformDeps.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,7 @@ namespace tm_internal {
345345
// going to run on these boards should be marked with this attribute.
346346
//
347347
#undef ISR_ATTR
348-
#if defined(ESP8266)
349-
# define ISR_ATTR ICACHE_RAM_ATTR
350-
#elif defined(ESP32)
348+
#if defined(ESP8266) || defined(ESP32)
351349
# define ISR_ATTR IRAM_ATTR
352350
#else
353351
# define ISR_ATTR

src/TaskTypes.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ void TimerTask::handleScheduling(sched_t when, TimerUnit unit, bool repeating) {
5353
this->myTimingSchedule = when;
5454
this->timingInformation = repeating ? TimerUnit(unit | TM_TIME_REPEATING) : unit;
5555
this->scheduledAt = (isMicrosSchedule()) ? micros() : millis();
56+
taskEnabled = true;
5657
}
5758

5859
void TimerTask::initialise(uint32_t when, TimerUnit unit, Executable* execCallback, bool deleteWhenDone, bool repeating) {
@@ -135,13 +136,6 @@ void TimerTask::processEvent() {
135136
scheduledAt = micros();
136137
}
137138

138-
void TimerTask::setEnabled(bool ena) {
139-
// We really don't want to do bit-masking directly on a volatile field. Copy to local first.
140-
uint8_t execTy = executeMode;
141-
bitWrite(execTy, EXECMODE_BIT_DISABLED, (!ena));
142-
executeMode = static_cast<ExecutionType>(execTy);
143-
}
144-
145139
bool TimerTask::isRepeating() const {
146140
if(ExecutionType(executeMode & EXECTYPE_MASK) == EXECTYPE_EVENT) {
147141
// if it's an event it repeats until the event is considered "complete"

src/TaskTypes.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,6 @@ enum TimerUnit : uint8_t {
138138
TM_TIME_RUNNING = 0x20,
139139
};
140140

141-
#define EXECMODE_BIT_DISABLED 4
142-
143141
/**
144142
* Internal class.
145143
* The execution types stored internally in a task, records what kind of task is in use, and if it needs deleting
@@ -152,7 +150,6 @@ enum ExecutionType : uint8_t {
152150

153151
EXECTYPE_MASK = 0x03,
154152
EXECTYPE_DELETE_ON_DONE = 0x08,
155-
EXECTYPE_TASK_DISABLED = 0x10,
156153

157154
EXECTYPE_DEL_EXECUTABLE = EXECTYPE_EXECUTABLE | EXECTYPE_DELETE_ON_DONE,
158155
EXECTYPE_DEL_EVENT = EXECTYPE_EVENT | EXECTYPE_DELETE_ON_DONE
@@ -198,6 +195,8 @@ class TimerTask {
198195
tm_internal::TmAtomicBool taskInUse;
199196
/** the mode in which the task executes, IE call a function, call an event or executable. Also if memory is owned */
200197
volatile ExecutionType executeMode;
198+
/** Stores a flag to indicate if the task is enabled */
199+
tm_internal::TmAtomicBool taskEnabled;
201200
public:
202201
TimerTask();
203202

@@ -323,13 +322,13 @@ class TimerTask {
323322
/**
324323
* @return if the task is presently enabled - IE it is being scheduled.
325324
*/
326-
bool isEnabled() { return !bitRead(executeMode, EXECMODE_BIT_DISABLED) != 0; }
325+
bool isEnabled() { return tm_internal::atomicReadBool(&taskEnabled); }
327326

328327
/**
329328
* Set the task aspi either enabled or disabled. When enabled it is scheduled, otherwise it is not scheduled.
330329
* @param ena the enablement status
331330
*/
332-
void setEnabled(bool ena);
331+
void setEnabled(bool ena) { tm_internal::atomicWriteBool(&taskEnabled, ena); }
333332
};
334333

335334
#endif //TASKMANAGER_IO_TASKTYPES_H

src/TmLongSchedule.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,24 @@ uint32_t makeDaySchedule(int days, int hours) {
1818
}
1919

2020
TmLongSchedule::TmLongSchedule(uint32_t milliScheduleNext, Executable* toExecute) : milliSchedule(milliScheduleNext),
21-
theExecutable(toExecute), lastScheduleTime(0), isTimerFn(false) { }
21+
fnCallback(nullptr), theExecutable(toExecute), lastScheduleTime(0) { }
2222

2323
TmLongSchedule::TmLongSchedule(uint32_t milliScheduleNext, TimerFn toExecute) : milliSchedule(milliScheduleNext),
24-
fnCallback(toExecute), lastScheduleTime(0), isTimerFn(true) { }
24+
fnCallback(toExecute), theExecutable(nullptr), lastScheduleTime(0) { }
2525

2626
void TmLongSchedule::exec() {
2727
lastScheduleTime = millis();
2828

2929
// never set last schedule time as 0. It is an invalid state.
3030
if(lastScheduleTime == 0) lastScheduleTime = 1;
3131

32-
if(isTimerFn && theExecutable != nullptr) {
33-
fnCallback();
34-
}
35-
else {
32+
if(theExecutable != nullptr) {
3633
theExecutable->exec();
3734
}
35+
else if(fnCallback != nullptr) {
36+
fnCallback();
37+
}
38+
3839
}
3940

4041
uint32_t TmLongSchedule::timeOfNextCheck() {

src/TmLongSchedule.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,20 @@
1515
class TmLongSchedule : public BaseEvent {
1616
private:
1717
const uint32_t milliSchedule;
18-
union {
19-
const TimerFn fnCallback;
20-
Executable *const theExecutable;
21-
};
18+
const TimerFn fnCallback;
19+
Executable *const theExecutable;
2220
uint32_t lastScheduleTime;
23-
const bool isTimerFn;
2421
public:
22+
/** Create a schedule that will call back a TimerFn functional callback.
23+
* @param milliSchedule the schedule to call back on
24+
* @param callee the functional callback
25+
*/
2526
TmLongSchedule(uint32_t milliSchedule, TimerFn callee);
27+
/**
28+
* Create schedule that will call the exec() method on an Executable
29+
* @param milliSchedule the schedule to call back on
30+
* @param callee the object extending from Executable
31+
*/
2632
TmLongSchedule(uint32_t milliSchedule, Executable* callee);
2733

2834
void exec() override;

0 commit comments

Comments
 (0)