diff options
-rw-r--r-- | robot/makefile | 11 | ||||
-rw-r--r-- | robot/sim.c | 35 | ||||
-rw-r--r-- | robot/sim.h | 13 | ||||
-rw-r--r-- | shared/makefile | 9 | ||||
-rw-r--r-- | shared/os.mk | 12 |
5 files changed, 63 insertions, 17 deletions
diff --git a/robot/makefile b/robot/makefile index 85ed2e3..a913bd7 100644 --- a/robot/makefile +++ b/robot/makefile @@ -6,18 +6,19 @@ MCU ?= atmega168 AVRDUDE_DEVICE ?= m168 PORT ?= /dev/ttyACM0 +SIM = true CFLAGS=-g -Wall $(DEVICE_SPECIFIC_CFLAGS) -Os LDFLAGS=-Wl,-gc-sections -Wl,-relax -all: $(if $(SIM), a.out, out.hex) +include ../shared/os.mk +all: $(if $(SIM), $(TARGET), out.hex) SOURCES := $(filter-out sim.c, $(wildcard *.c)) HEADERS := $(filter-out sim.h, $(wildcard *.h)) include ../shared/makefile # simulation -# SIM = true CFLAGS += $(if $(SIM), -DW2_SIM, -mcall-prologues -mmcu=$(MCU)) LDFLAGS += $(if $(SIM), , -lpololu_$(DEVICE)) PREFIX := $(if $(SIM), , avr-) @@ -31,15 +32,15 @@ CC=$(PREFIX)gcc OBJ2HEX=$(PREFIX)objcopy clean:: - rm -f *.o out.hex a.out + rm -f *.o out.hex $(TARGET) -a.out: $(OBJECTS) +$(TARGET): $(OBJECTS) $(CC) $(OBJECTS) $(CFLAGS) $(LDFLAGS) .o: $(CC) -c $(CFLAGS) $< -out.hex: a.out +out.hex: $(TARGET) $(OBJ2HEX) -R .eeprom -O ihex $< $@ $(info build $(BUILD_STR) complete) diff --git a/robot/sim.c b/robot/sim.c index 0f1c7e6..ba9aa4a 100644 --- a/robot/sim.c +++ b/robot/sim.c @@ -2,16 +2,31 @@ #include <time.h> #include <string.h> #include <stdint.h> -#include <termios.h> #include <unistd.h> +#ifdef W2_HOST_LINUX +#include <termios.h> +#endif + +#ifdef W2_HOST_WIN32 +// garbage os compatibility +#include <windows.h> +#include <timeapi.h> +#define STDIN_FILENO 0 +#endif + #include "sim.h" #include "../shared/consts.h" #include "../shared/protocol.h" #include "sercomm.h" #include "errcatch.h" +#ifdef W2_HOST_LINUX struct timespec reference_time; // NOLINT +#endif +#ifdef W2_HOST_WIN32 +DWORD reference_time; // NOLINT +#endif bool g_w2_sim_headless = false; static const char* const W2_CMD_NAMES[] = { @@ -38,15 +53,20 @@ static const char* const W2_CMD_DIRECTIONS[] = { void time_reset() { simprintfunc("time_reset", ""); +#ifdef W2_HOST_LINUX clock_gettime(CLOCK_MONOTONIC, &reference_time); +#endif } unsigned long get_ms() { simprintfunc("get_ms", ""); +#ifdef W2_HOST_LINUX struct timespec elapsed; clock_gettime(CLOCK_MONOTONIC, &elapsed); return ((elapsed.tv_sec * 1000) + (elapsed.tv_nsec / 1000000)) - ((reference_time.tv_sec * 1000) + (reference_time.tv_nsec / 1000000)); +#endif + return 0; } void red_led(unsigned char on) { @@ -85,7 +105,7 @@ void serial_send(char* message, unsigned int length) { } void serial_receive_ring(char* buffer, unsigned char size) { - simprintfunc("serial_receive_ring", "0x%016lx, %u", (unsigned long) buffer, size); + simprintfunc("serial_receive_ring", "0x%016llx, %u", (uint64_t) buffer, size); } unsigned char serial_get_received_bytes() { @@ -98,12 +118,21 @@ void w2_sim_setup(int argc, char **argv) { g_w2_sim_headless = true; // disable echo and enable raw mode +#ifdef W2_HOST_LINUX struct termios term; tcgetattr(STDIN_FILENO, &term); term.c_lflag &= ~(ECHO | ICANON); term.c_cc[VTIME] = 0; term.c_cc[VMIN] = 0; tcsetattr(STDIN_FILENO, 0, &term); +#endif +#ifdef W2_HOST_WIN32 + DWORD mode; + HANDLE console = GetStdHandle(STD_INPUT_HANDLE); + + GetConsoleMode(console, &mode); + SetConsoleMode(console, mode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT)); +#endif // debug error // w2_errcatch_throw(W2_E_WARN_BATTERY_LOW); @@ -141,7 +170,7 @@ unsigned char get_single_debounced_button_press(unsigned char buttons) { } void qtr_read(unsigned int* sensor_values, unsigned char read_mode) { - simprintfunc("qtr_read", "0x%016lx, %s", (uint64_t) sensor_values, read_mode == QTR_EMITTERS_ON ? "QTR_EMITTERS_ON" : "???"); + simprintfunc("qtr_read", "0x%016llx, %s", (uint64_t) sensor_values, read_mode == QTR_EMITTERS_ON ? "QTR_EMITTERS_ON" : "???"); sensor_values[0] = 0; sensor_values[1] = 0; sensor_values[2] = 0; diff --git a/robot/sim.h b/robot/sim.h index ab1cd77..08c1d8a 100644 --- a/robot/sim.h +++ b/robot/sim.h @@ -22,6 +22,7 @@ extern bool g_w2_sim_headless; #define DBG_BYTES_PER_LINE 16 // debug colors +#ifdef W2_HOST_LINUX #define COL_BLK "\e[0;30m" #define COL_RED "\e[0;31m" #define COL_GRN "\e[0;32m" @@ -31,6 +32,18 @@ extern bool g_w2_sim_headless; #define COL_CYN "\e[0;36m" #define COL_WHT "\e[0;37m" #define COL_RST "\e[0m" +#endif +#ifdef W2_HOST_WIN32 +#define COL_BLK "" +#define COL_RED "" +#define COL_GRN "" +#define COL_YEL "" +#define COL_BLU "" +#define COL_MAG "" +#define COL_CYN "" +#define COL_WHT "" +#define COL_RST "" +#endif // debug stdout print macros #define simprintf(message, ...) if (!g_w2_sim_headless) printf(COL_RED "[SIM] " COL_RST message, ##__VA_ARGS__) diff --git a/shared/makefile b/shared/makefile index 7f0ceb9..e5a6ff6 100644 --- a/shared/makefile +++ b/shared/makefile @@ -5,14 +5,5 @@ HEADERS += $(wildcard ../shared/*.h) BUILD_STR=$(shell git update-index -q --refresh; git describe --tags --dirty='*' --broken='x' | cut -c1-20) CFLAGS += -DW2_BUILD_STR=\"$(BUILD_STR)\" -# os info -OS=$(strip $(shell uname -o)) -ifeq ($(OS),GNU/Linux) -CFLAGS += -DW2_HOST_LINUX -endif -ifeq ($(OS),Msys) -CFLAGS += -DW2_HOST_WIN32 -endif - clean:: rm -f ../shared/*.o diff --git a/shared/os.mk b/shared/os.mk new file mode 100644 index 0000000..e4ede42 --- /dev/null +++ b/shared/os.mk @@ -0,0 +1,12 @@ +# os info
+OS=$(strip $(shell uname -o))
+ifeq ($(OS),GNU/Linux)
+CFLAGS += -DW2_HOST_LINUX
+LINUX := true
+TARGET := a.out
+endif
+ifeq ($(OS),Msys)
+CFLAGS += -DW2_HOST_WIN32
+WIN32 := true
+TARGET := a.exe
+endif
\ No newline at end of file |