7 #include "stm32f7xx_hal.h"
13 #include <netinet/in.h>
15 #include <sys/socket.h>
17 #include <sys/types.h>
21 #include "interpret.h"
22 #include "mTaskSymbols.h"
27 #define MSG_GET_TASK 't'
28 #define MSG_DEL_TASK 'd'
29 #define MSG_SDS_SPEC 's'
30 #define MSG_SDS_UPD 'u'
38 volatile char uartf
= 0;
48 #define SET_LED_RED GPIOB->BSRR = GPIO_PIN_14
49 #define RESET_LED_RED GPIOB->BSRR = GPIO_PIN_14 << 16
51 #define SET_LED_BLUE GPIOB->BSRR = GPIO_PIN_7
52 #define RESET_LED_BLUE GPIOB->BSRR = GPIO_PIN_7 << 16
54 #define SET_LED_GREEN GPIOB->BSRR = GPIO_PIN_0
55 #define RESET_LED_GREEN GPIOB->BSRR = GPIO_PIN_0 << 16
61 if (gettimeofday(&tv1
, NULL
) == -1)
63 return tv1
.tv_sec
*1000 + tv1
.tv_usec
/1000;
67 bool input_available(){
77 if (select(fd
+1, &fds
, NULL
, NULL
, &tv
) == -1)
79 return FD_ISSET(fd
, &fds
);
86 HAL_UART_Receive(&huart3
, &bt
, 1, 1000);
94 void write_byte(uint8_t b
)
97 HAL_UART_Transmit_DMA(&huart3
, &b
, 1);
113 void killHandler(int i
)
115 printf("%i caught, Bye...\n", i
);
121 void HAL_UART_TxCpltCallback(UART_HandleTypeDef
*UartHandle
)
130 uint8_t c
= read_byte();
131 debug("Receiving input: %c\n", c
);
134 debug("Receiving an sds\n");
138 debug("Receiving an sds\n");
139 //TODO do something with the return value
143 debug("Receiving a delete task request\n");
147 debug("Receiving a task\n");
153 debug("Unknown message: %X\n", c
);
157 void usage(FILE *o
, char *arg0
){
158 fprintf(o
, "Usage: %s [opts]\n\nOptions\n"
159 "-p PORT Custom port number, default: 8123\n" , arg0
);
166 int port
= 8123, opti
= 1;
167 //Register signal handler
168 if(signal(SIGINT
, killHandler
) == SIG_ERR
){
169 die("Couldn't register signal handler...\n");
171 if(signal(SIGTERM
, killHandler
) == SIG_ERR
){
172 die("Couldn't register signal handler...\n");
174 //Command line arguments
176 if(strcmp((*argv
)+opti
, "-h") == 0){
177 usage(stdout
, argv
[0]);
179 } else if(strcmp(argv
[opti
], "-p") == 0 && opti
+1<*argc
){
180 port
= atoi(argv
[++opti
]);
182 die("Port numbers are > 1\n");
184 usage(stderr
, argv
[0]);
190 //Open file descriptors
191 struct sockaddr_in sa
;
193 memset(&sa
, 0, sizeof(sa
));
194 sa
.sin_family
= AF_INET
;
195 sa
.sin_addr
.s_addr
= INADDR_ANY
;
196 sa
.sin_port
= htons(port
);
198 if((sock_fd
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1)
200 if(bind(sock_fd
, (struct sockaddr
*)&sa
, sizeof(sa
)) == -1)
202 if(listen(sock_fd
, 10) == -1)
205 printf("Listening on %d\n", port
);
207 if((fd
= accept(sock_fd
, (struct sockaddr
*)NULL
, NULL
)) == -1)
220 struct task
*curtask
;
221 if(input_available())
224 cyclestart
= millis();
225 for(ct
= 0; ct
<MAXTASKS
; ct
++){
226 //See whether the task is even in use
227 if((curtask
= task_get(ct
)) == NULL
){
228 // debug("Task %d not implemented\n", ct);
231 //See whether the task interval has passed
232 if(cyclestart
-curtask
->lastrun
< curtask
->interval
){
233 // debug("Task %d not scheduled\n", ct);
237 printf("Current task to run: %d\n", ct
);
242 debug("Waiting for 500ms\n");
244 debug("done waiting\n");
251 int main(int ac
, char *av
[])
263 //Check for new tasks