+#include <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#ifdef STM32F767xx
+#include "stm32f7xx_hal.h"
+#include "gpio.h"
+#include "usart.h"
+#else
+#include <stdio.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <signal.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <unistd.h>
+#endif
+
+#include "interface.h"
+
+#define SET_LED_RED GPIOB->BSRR = GPIO_PIN_14
+#define RESET_LED_RED GPIOB->BSRR = GPIO_PIN_14 << 16
+
+#define SET_LED_BLUE GPIOB->BSRR = GPIO_PIN_7
+#define RESET_LED_BLUE GPIOB->BSRR = GPIO_PIN_7 << 16
+
+#define SET_LED_GREEN GPIOB->BSRR = GPIO_PIN_0
+#define RESET_LED_GREEN GPIOB->BSRR = GPIO_PIN_0 << 16
+
+//Globals
+#ifdef STM32F767xx
+volatile char uartf = 0;
+#else
+struct timeval tv1;
+int sock_fd = -1;
+int fd = -1;
+int gargc;
+char **gargv;
+#endif
+uint8_t bt;
+
+//Specifics
+#ifdef STM32F767xx
+void _exit(int i){
+ while(1);
+ (void)i;
+}
+
+void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
+{
+ uartf=1;
+}
+#else
+void killHandler(int i)
+{
+ printf("%i caught, Bye...\n", i);
+ exit(1);
+}
+
+void usage(FILE *o, char *arg0){
+ fprintf(o, "Usage: %s [opts]\n\nOptions\n"
+ "-p PORT Custom port number, default: 8123\n" , arg0);
+}
+#endif
+
+//Interface things
+long millis() {
+#ifdef STM32F767xx
+ return HAL_GetTick();
+#else
+ if (gettimeofday(&tv1, NULL) == -1)
+ pdie("gettimeofday");
+ return tv1.tv_sec*1000 + tv1.tv_usec/1000;
+#endif
+}
+
+bool input_available(){
+#ifdef STM32F767xx
+ return false;
+#else
+ struct timeval tv;
+ fd_set fds;
+ tv.tv_sec = 0;
+ tv.tv_usec = 0;
+ FD_ZERO(&fds);
+ FD_SET(fd, &fds);
+ if (select(fd+1, &fds, NULL, NULL, &tv) == -1)
+ pdie("select");
+ return FD_ISSET(fd, &fds);
+#endif
+}
+
+uint8_t read_byte()
+{
+#ifdef STM32F767xx
+ HAL_UART_Receive(&huart3, &bt, 1, 1000);
+ return 0;
+#else
+ read(fd, &bt, 1);
+ return bt;
+#endif
+}
+
+void write_byte(uint8_t b)
+{
+#ifdef STM32F767xx
+ HAL_UART_Transmit_DMA(&huart3, &b, 1);
+#else
+ write(fd, &b, 1);
+#endif
+}
+
+void delay(long ms)
+{
+#ifdef STM32F767xx
+ HAL_Delay(ms);
+#else
+ usleep(ms*1000);
+#endif
+}
+
+void setup()
+{
+#ifdef STM32F767xx
+#else
+ int port = 8123, opti = 1;
+ //Register signal handler
+ if(signal(SIGINT, killHandler) == SIG_ERR){
+ die("Couldn't register signal handler...\n");
+ }
+ if(signal(SIGTERM, killHandler) == SIG_ERR){
+ die("Couldn't register signal handler...\n");
+ }
+ //Command line arguments
+ while(opti < gargc){
+ if(strcmp((*gargv)+opti, "-h") == 0){
+ usage(stdout, gargv[0]);
+ exit(EXIT_SUCCESS);
+ } else if(strcmp(gargv[opti], "-p") == 0 && opti+1<gargc){
+ port = atoi(gargv[++opti]);
+ if(port < 1)
+ die("Port numbers are > 1\n");
+ } else {
+ usage(stderr, gargv[0]);
+ exit(EXIT_FAILURE);
+ }
+ opti++;
+ }
+
+ //Open file descriptors
+ struct sockaddr_in sa;
+
+ memset(&sa, 0, sizeof(sa));
+ sa.sin_family = AF_INET;
+ sa.sin_addr.s_addr = INADDR_ANY;
+ sa.sin_port = htons(port);
+
+ if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
+ pdie("socket");
+ if(bind(sock_fd, (struct sockaddr*)&sa, sizeof(sa)) == -1)
+ pdie("bind");
+ if(listen(sock_fd, 10) == -1)
+ pdie("listen");
+
+ printf("Listening on %d\n", port);
+ fflush(stdout);
+ if((fd = accept(sock_fd, (struct sockaddr*)NULL, NULL)) == -1)
+ pdie("accept");
+#endif
+}