not working again.
[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 debug("interval");
33 debugi(tasks[ct].interval);
34 //Read tasklength
35 tasks[ct].tlen = read16();
36 debug("length");
37 debugi(tasks[ct].tlen);
38
39 if(tasks[ct].tlen > MAXTASKSIZE)
40 die("Task is too long: %d", tasks[ct].tlen);
41 //Read task bytecode
42 for(unsigned int i = 0; i<tasks[ct].tlen; i++){
43 tasks[ct].bc[i] = read_byte();
44 debug("bc read:");
45 debugi(i);
46 // debug("t[][%i]: 0x%02x %d", i,
47 // tasks[ct].bc[i], tasks[ct].bc[i]);
48 }
49 //Return the task number for later removal
50 debug("Received a task of length %d", tasks[ct].tlen);
51 tasks[ct].used = true;
52 tasks[ct].lastrun = 0L;
53
54 return ct;
55 }
56
57 void task_delete(void)
58 {
59 tasks[read_byte()].used = false;
60 }
61
62 struct task *task_get(int num)
63 {
64 return tasks[num].used ? &tasks[num] : NULL;
65 }