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