Skip to content

Commit 996cff9

Browse files
author
origin2007
committed
Add taskscheduler code
1 parent b284161 commit 996cff9

File tree

6 files changed

+162
-117
lines changed

6 files changed

+162
-117
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#include <MsTimer2.h>
2+
#include <taskscheduler.h>
3+
4+
#define TIME_INTERVAL 1
5+
6+
sTask taskQueue[TASK_MAX];
7+
unsigned int ticker;
8+
9+
void sch_init()
10+
{
11+
int i;
12+
for(i=0; i<TASK_MAX; i++)
13+
{
14+
sch_deleteTask(i);
15+
}
16+
MsTimer2::set(TIME_INTERVAL, sch_update);
17+
ticker = 0;
18+
}
19+
20+
void sch_addTask(void (*pFun)(), long d, long p)
21+
{
22+
int i;
23+
for(i=0; i<TASK_MAX; i++)
24+
{
25+
if(taskQueue[i].pTask == 0)
26+
{
27+
taskQueue[i].pTask = pFun;
28+
taskQueue[i].delay = d;
29+
taskQueue[i].peroid = p;
30+
taskQueue[i].runme = 0;
31+
break;
32+
}
33+
}
34+
}
35+
36+
void sch_start()
37+
{
38+
MsTimer2::start();
39+
}
40+
41+
void sch_dispatchtasks()
42+
{
43+
int index;
44+
for(index=0; index<TASK_MAX; index++)
45+
{
46+
if(taskQueue[index].runme>0)
47+
{
48+
(* taskQueue[index].pTask)();
49+
taskQueue[index].runme = 0;
50+
51+
if(taskQueue[index].peroid==0)
52+
{
53+
sch_deleteTask(index);
54+
}
55+
}
56+
} //run task
57+
}
58+
59+
void sch_deleteTask(byte i)
60+
{
61+
if(taskQueue[i].pTask != 0)
62+
{
63+
taskQueue[i].pTask = 0;
64+
taskQueue[i].delay = 0;
65+
taskQueue[i].peroid = 0;
66+
taskQueue[i].runme = 0;
67+
}
68+
}
69+
70+
void sch_update()
71+
{
72+
int i;
73+
ticker ++;
74+
for(i=0; i<TASK_MAX; i++)
75+
{
76+
if(taskQueue[i].pTask != 0)
77+
{
78+
if(taskQueue[i].delay == 0)
79+
{
80+
taskQueue[i].runme = 1;
81+
if(taskQueue[i].peroid > 0)
82+
{
83+
taskQueue[i].delay = taskQueue[i].peroid;
84+
}
85+
}
86+
else{
87+
taskQueue[i].delay -= 1;
88+
}
89+
}
90+
}
91+
}
92+
93+
unsigned int sch_getTicker()
94+
{
95+
return ticker;
96+
}
97+
98+
unsigned int sch_getQueueSize()
99+
{
100+
int i;
101+
int c = 0;
102+
for(i=0; i<TASK_MAX; i++)
103+
{
104+
if(taskQueue[i].pTask != 0) c++;
105+
}
106+
return c;
107+
}

src/TaskScheduler/taskscheduler.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef taskschedular_h
2+
#define taskschedular_h
3+
4+
#include "Arduino.h"
5+
6+
#define TASK_MAX 10
7+
8+
typedef struct {
9+
void (*pTask)();
10+
long delay;
11+
long peroid;
12+
byte runme;
13+
} sTask;
14+
15+
16+
void sch_init(void);
17+
void sch_addTask(void (*pFun)(), long delay, long peroid);
18+
void sch_deleteTask(byte i);
19+
void sch_start();
20+
void sch_dispatchtasks();
21+
void sch_update();
22+
unsigned int sch_getTicker();
23+
unsigned int sch_getQueueSize();
24+
#endif

src/TestTaskSchedular/TestTaskSchedular.ino

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/TestTaskSchedular/taskschedular.cpp

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/TestTaskSchedular/taskscheduler.h

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <taskscheduler.h>
2+
3+
void setup() {
4+
Serial.begin(9600);
5+
// put your setup code here, to run once:
6+
pinMode(13, OUTPUT);
7+
sch_init();
8+
sch_addTask(do1, 0, 100);
9+
sch_addTask(do2, 1, 100);
10+
sch_start();
11+
}
12+
13+
void loop() {
14+
// put your main code here, to run repeatedly:
15+
sch_dispatchtasks();
16+
//Serial.print("+");
17+
}
18+
19+
void do1()
20+
{
21+
//Serial.print("+");
22+
digitalWrite(13, HIGH);
23+
}
24+
25+
void do2()
26+
{
27+
//Serial.println(sch_getTicker());
28+
//Serial.println(sch_getQueueSize());
29+
digitalWrite(13, LOW);
30+
}
31+

0 commit comments

Comments
 (0)