Skip to content

Commit 847a4ab

Browse files
committed
customize main loop priority level
1 parent a2cecac commit 847a4ab

3 files changed

Lines changed: 12 additions & 12 deletions

File tree

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Taskfun
2-
version=0.1.91
2+
version=0.1.92
33
author=Eugene Pistrak
44
maintainer=Eugene Pistrak
55
sentence=Preemptive multitasking for Arduino AVR and SAMD21

src/BTaskSwitcher.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ int BTaskSwitcher::get_next_task() {
6262
unsigned weights[] = { 50, 33, 17 };
6363
const unsigned priCount = sizeof(weights) / sizeof(weights[0]);
6464
auto total = 0;
65-
for(auto i = 0; i < priCount; ++i) {
65+
for(unsigned i = 0; i < priCount; ++i) {
6666
if (!_pri[i].count || (_pri[i].count == 1 && _pri[i].current == _yielded_task)) {
6767
weights[i] = 0;
6868
}
@@ -99,7 +99,7 @@ int BTaskSwitcher::get_next_task() {
9999

100100
void BTaskSwitcher::pause_task(int id) {
101101
BDisableInterrupts cli;
102-
if (id >=0 && id < _tasks.Length() && _tasks[id] && !_tasks[id]->paused()) {
102+
if (id >= 0 && id < (int)_tasks.Length() && _tasks[id] && !_tasks[id]->paused()) {
103103
--_pri[_tasks[id]->priority()].count;
104104
_tasks[id]->pause();
105105
if (id == _current_task) {
@@ -110,7 +110,7 @@ void BTaskSwitcher::pause_task(int id) {
110110

111111
void BTaskSwitcher::resume_task(int id) {
112112
BDisableInterrupts cli;
113-
if (id >=0 && id < _tasks.Length() && _tasks[id] && _tasks[id]->paused()) {
113+
if (id >= 0 && id < (int)_tasks.Length() && _tasks[id] && _tasks[id]->paused()) {
114114
++_pri[_tasks[id]->priority()].count;
115115
_tasks[id]->resume();
116116
}
@@ -159,16 +159,16 @@ void BTaskSwitcher::yield_task() {
159159
}
160160
}
161161

162-
void BTaskSwitcher::initialize(int tasks, int slice) {
162+
void BTaskSwitcher::initialize(int tasks, int slice, uint8_t loop_pri) {
163163
BDisableInterrupts cli;
164-
if (!_initialized && tasks > 0 && slice > 0) {
164+
if (!_initialized && tasks > 0 && slice > 0 && loop_pri <= TaskPriority::Low) {
165165
_slice = slice;
166166
_tasks.Resize(tasks + 1); // 1 for main loop()
167167

168168
// add the initial loop() task
169169
_tasks.Add(new BTaskInfoBase()); // loop() already has a stack
170170
_tasks[0]->id = 0;
171-
_tasks[0]->priority(TaskPriority::Medium);
171+
_tasks[0]->priority(loop_pri);
172172
_pri[_tasks[0]->priority()].count = 1;
173173

174174
init_arch();
@@ -197,8 +197,8 @@ int currentTask() {
197197
return BTaskSwitcher::current_task_id();
198198
}
199199

200-
void setupTasks(int numTasks, int msSlice) {
201-
BTaskSwitcher::initialize(numTasks, msSlice);
200+
void setupTasks(int numTasks, int msSlice, uint8_t loopPriority) {
201+
BTaskSwitcher::initialize(numTasks, msSlice, loopPriority);
202202
}
203203

204204
// used by arduino's delay()

src/BTaskSwitcher.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void stopTask(int id);
2626
int currentTask();
2727
void pauseTask(int id);
2828
void resumeTask(int id);
29-
void setupTasks(int numTasks = 3, int msSlice = 1);
29+
void setupTasks(int numTasks = 3, int msSlice = 1, uint8_t loopPriority = 1);
3030

3131
extern "C" void yield();
3232

@@ -114,7 +114,7 @@ class BTaskSwitcher {
114114
static unsigned context_size();
115115
static bool disable();
116116
static void restore(bool enable);
117-
static void initialize(int tasks, int slice);
117+
static void initialize(int tasks, int slice, uint8_t loop_pri);
118118
static void yield_task();
119119
static void pause_task(int id);
120120
static void resume_task(int id);
@@ -205,7 +205,7 @@ class BTaskSwitcher {
205205
friend int ::currentTask();
206206
friend void ::pauseTask(int);
207207
friend void ::resumeTask(int);
208-
friend void ::setupTasks(int, int);
208+
friend void ::setupTasks(int, int, uint8_t);
209209
friend void ::yield();
210210
template<typename T>
211211
friend class SyncVar;

0 commit comments

Comments
 (0)