try something
[mTask.git] / int / task.c
1 #include <stdlib.h>
2 #include <string.h>
3
4 #ifndef STM
5 #include <unistd.h>
6 #include <stdio.h>
7 #endif
8
9 #include "task.h"
10 #include "interface.h"
11
12 struct task tasks[MAXTASKS];
13
14 void task_init(void)
15 {
16 memset(&tasks, 0, sizeof(struct task)*MAXTASKS);
17 }
18
19 int task_register(void)
20 {
21 uint8_t ct;
22
23 for(ct = 0; ct<MAXTASKS; ct++)
24 if(!tasks[ct].used)
25 break;
26 if(ct == MAXTASKS)
27 die("Trying to add too much tasks...");
28
29 memset(&tasks[ct], 0, sizeof(struct task));
30 //Read interval
31 tasks[ct].interval = read16();
32 //Read tasklength
33 tasks[ct].tlen = read16();
34
35 if(tasks[ct].tlen > MAXTASKSIZE)
36 die("Task is too long: %d", tasks[ct].tlen);
37 //Read task bytecode
38 for(unsigned int i = 0; i<tasks[ct].tlen; i++){
39 tasks[ct].bc[i] = read_byte();
40 // debug("t[][%i]: 0x%02x %d", i,
41 // tasks[ct].bc[i], tasks[ct].bc[i]);
42 }
43 //Return the task number for later removal
44 debug("Received a task of length %d", tasks[ct].tlen);
45 tasks[ct].used = true;
46 tasks[ct].lastrun = 0L;
47
48 return ct;
49 }
50
51 void task_delete(void)
52 {
53 tasks[read_byte()].used = false;
54 }
55
56 struct task *task_get(int num)
57 {
58 return tasks[num].used ? &tasks[num] : NULL;
59 }