1a704b4cb0803fc2e7ad2c79d6c808948ce86847
[mTask.git] / int / nucleo-f767-blinky / src / main.c
1 #include <stdbool.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #ifdef STM32F767xx
7 #include "stm32f7xx_hal.h"
8 #include "gpio.h"
9 #include "usart.h"
10 #else
11 #include <stdio.h>
12 #include <netdb.h>
13 #include <netinet/in.h>
14 #include <signal.h>
15 #include <sys/socket.h>
16 #include <sys/time.h>
17 #include <sys/types.h>
18 #include <unistd.h>
19 #endif
20
21 #include "interpret.h"
22 #include "mTaskSymbols.h"
23 #include "sds.h"
24 #include "task.h"
25 #include "interface.h"
26
27 #define MSG_GET_TASK 't'
28 #define MSG_DEL_TASK 'd'
29 #define MSG_SDS_SPEC 's'
30 #define MSG_SDS_UPD 'u'
31
32 void read_message()
33 {
34 //Find next task
35 uint8_t c = read_byte();
36 debug("Receiving input: %c\n", c);
37 switch(c){
38 case MSG_SDS_SPEC:
39 debug("Receiving an sds\n");
40 sds_register();
41 break;
42 case MSG_SDS_UPD:
43 debug("Receiving an sds\n");
44 //TODO do something with the return value
45 sds_update();
46 break;
47 case MSG_DEL_TASK:
48 debug("Receiving a delete task request\n");
49 task_delete();
50 break;
51 case MSG_GET_TASK:
52 debug("Receiving a task\n");
53 c = task_register();
54 break;
55 case '\n':
56 break;
57 default:
58 debug("Unknown message: %X\n", c);
59 }
60 }
61
62 void loop()
63 {
64 int ct;
65 long cyclestart;
66 struct task *curtask;
67
68 if(input_available())
69 read_message();
70
71 //Run tasks
72 cyclestart = millis();
73 for(ct = 0; ct<MAXTASKS; ct++){
74 //See whether the task is even in use
75 if((curtask = task_get(ct)) == NULL){
76 // debug("Task %d not implemented\n", ct);
77 continue;
78 }
79 //See whether the task interval has passed
80 if(cyclestart-curtask->lastrun < curtask->interval){
81 // debug("Task %d not scheduled\n", ct);
82 continue;
83 }
84 printf("Current task to run: %d\n", ct);
85 run_task(curtask);
86 curtask->lastrun = cyclestart;
87 }
88 write_byte('\n');
89 }
90
91 #ifdef STM32F767xx
92 int main1(void){
93 #else
94 int main(int argc, char *argv[]){
95 gargc = argc;
96 gargv = argv;
97 #endif
98 //Initialize systems
99 setup();
100 sds_init();
101 task_init();
102
103 write_byte('\n');
104
105 while(true){
106 //Check for new tasks
107 loop();
108 delay(10);
109 }
110 return 0;
111 }