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