update
[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 if(input_available()){
36 //debug("Receiving input: %c\n", c);
37 switch(bt){
38 case MSG_SDS_SPEC:
39 debug("Receiving an sds");
40 sds_register();
41 break;
42 case MSG_SDS_UPD:
43 debug("Receiving an sds");
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");
49 task_delete();
50 break;
51 case MSG_GET_TASK:
52 debug("Receiving a task");
53 task_register();
54 break;
55 case '\0':
56 break;
57 case '\n':
58 break;
59 default:
60 debug("Unknown message: %X", bt);
61 }
62 }
63 }
64
65 void loop()
66 {
67 int ct;
68 long cyclestart;
69 struct task *curtask;
70
71 read_message();
72
73 //Run tasks
74 cyclestart = millis();
75 for(ct = 0; ct<MAXTASKS; ct++){
76 //See whether the task is even in use
77 if((curtask = task_get(ct)) == NULL){
78 // debug("Task %d not implemented\n", ct);
79 continue;
80 }
81 //See whether the task interval has passed
82 if(cyclestart-curtask->lastrun < curtask->interval){
83 // debug("Task %d not scheduled\n", ct);
84 continue;
85 }
86 debug("Current task to run: %d", ct);
87 run_task(curtask);
88 curtask->lastrun = cyclestart;
89 write_byte('\n');
90 }
91 }
92
93 #ifdef STM32F767xx
94 char s[128] = "";
95 int main1(void){
96 uint8_t bt;
97 HAL_UART_Receive(&huart3, &bt, 1, 100000);
98 #else
99 int main(int argc, char *argv[]){
100 gargc = argc;
101 gargv = argv;
102 #endif
103 debug("booting up");
104
105 //Initialize systems
106 setup();
107 sds_init();
108 task_init();
109
110 write_byte('\n');
111
112 while(true){
113 //Check for new tasks
114 // debug("loop\r\n");
115 loop();
116 delay(100);
117 }
118 return 0;
119 }