From bbe4080e94308149b74dd9ccefddf95878eec5d0 Mon Sep 17 00:00:00 2001 From: Maximilien Cuony Date: Thu, 2 Jun 2016 22:00:22 +0200 Subject: Simulator: UART, Leds, begining of GPS --- src/simulator/Makefile | 2 + src/simulator/src/Core/main.c | 19 +- src/simulator/src/Core/usart.c | 190 ------------------- src/simulator/src/GPIO/leds.c | 44 +++++ src/simulator/src/GPIO/usart.c | 97 ++++++++++ src/simulator/src/GPS/gps.c | 11 ++ src/simulator/src/GPS/minema.c | 1 + src/simulator/src/Gui/gui.c | 402 +++++++++++++++++++++++++++++++++++++++++ src/simulator/src/Gui/test.c | 285 ----------------------------- src/simulator/vc.h | 2 +- 10 files changed, 573 insertions(+), 480 deletions(-) delete mode 100644 src/simulator/src/Core/usart.c create mode 100644 src/simulator/src/GPIO/leds.c create mode 100644 src/simulator/src/GPIO/usart.c create mode 100644 src/simulator/src/GPS/gps.c create mode 100644 src/simulator/src/GPS/minema.c create mode 100644 src/simulator/src/Gui/gui.c delete mode 100644 src/simulator/src/Gui/test.c (limited to 'src/simulator') diff --git a/src/simulator/Makefile b/src/simulator/Makefile index 4e74498..511b6e6 100644 --- a/src/simulator/Makefile +++ b/src/simulator/Makefile @@ -16,6 +16,8 @@ VPATH += $(SRCROOT)/Source VPATH += $(SRCROOT)/Source/portable/MemMang VPATH += $(SRCROOT)/Source/portable/GCC/POSIX VPATH += $(SRCROOT)/src/Core +VPATH += $(SRCROOT)/src/GPIO +VPATH += $(SRCROOT)/src/GPS # FreeRTOS Objects C_FILES += croutine.c diff --git a/src/simulator/src/Core/main.c b/src/simulator/src/Core/main.c index 7e93176..a326dbf 100644 --- a/src/simulator/src/Core/main.c +++ b/src/simulator/src/Core/main.c @@ -27,11 +27,22 @@ #include -void *threadFunc(void *arg) { - main2(); +static void thread_gui(void *arg) { + main_gui(); } void init() { - pthread_t pth; - pthread_create(&pth, NULL, threadFunc, "processing..."); + + /* pthread_t pth; */ + /* pthread_create(&pth, NULL, thread_gui, "processing..."); */ + + TaskHandle_t task_handle; + xTaskCreate( + thread_gui, + "Thread GUI", + configMINIMAL_STACK_SIZE, + (void*) NULL, + tskIDLE_PRIORITY + 2UL, + &task_handle); + } diff --git a/src/simulator/src/Core/usart.c b/src/simulator/src/Core/usart.c deleted file mode 100644 index bd91428..0000000 --- a/src/simulator/src/Core/usart.c +++ /dev/null @@ -1,190 +0,0 @@ - -/* - * The MIT License (MIT) - * - * Copyright (c) 2016 Matthias P. Braendli - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. -*/ - -#include -#include -#include -#include -#include "Core/common.h" -#include "Core/usart.h" -#include "FreeRTOS.h" -#include "task.h" -#include "queue.h" - -// The ISR writes into this buffer -static char nmea_sentence[MAX_NMEA_SENTENCE_LEN]; -static int nmea_sentence_last_written = 0; - -// Once a completed NMEA sentence is received in the ISR, -// it is appended to this queue -static QueueHandle_t usart_nmea_queue; - -#define USART_TypeDef void - -int _USART2 = 2; -int _USART3 = 3; - -#define USART2 &_USART2 -#define USART3 &_USART3 - -void usart_init() { -} - -void usart_gps_init() { - usart_nmea_queue = xQueueCreate(15, MAX_NMEA_SENTENCE_LEN); - if (usart_nmea_queue == 0) { - while(1); /* fatal error */ - } - -} - -// Make sure Tasks are suspended when this is called! -static void usart_puts(USART_TypeDef* USART, const char* str) -{ - while(*str) { - // TODO - putchar(*str); - str++; - } -} - -void usart_gps_puts(const char* str) -{ - vTaskSuspendAll(); - return usart_puts(USART3, str); - xTaskResumeAll(); -} - -#define MAX_MSG_LEN 80 -static char usart_debug_message[MAX_MSG_LEN]; - -void usart_debug_timestamp() { - // Don't call printf here, to reduce stack usage - uint64_t now = timestamp_now(); - if (now == 0) { - usart_puts(USART2, "[0] "); - } - else { - char ts_str[64]; - int i = 63; - - ts_str[i--] = '\0'; - ts_str[i--] = ' '; - ts_str[i--] = ']'; - - while (now > 0 && i >= 0) { - ts_str[i--] = '0' + (now % 10); - now /= 10; - } - ts_str[i] = '['; - - usart_puts(USART2, &ts_str[i]); - } -} - -void usart_debug(const char *format, ...) -{ - va_list list; - va_start(list, format); - vsnprintf(usart_debug_message, MAX_MSG_LEN-1, format, list); - - vTaskSuspendAll(); - usart_debug_timestamp(); - usart_puts(USART2, usart_debug_message); - xTaskResumeAll(); - - va_end(list); -} - -void usart_debug_puts(const char* str) -{ - vTaskSuspendAll(); - usart_debug_timestamp(); - usart_puts(USART2, str); - xTaskResumeAll(); -} - -int usart_get_nmea_sentence(char* nmea) -{ - return xQueueReceive(usart_nmea_queue, nmea, portMAX_DELAY); -} - - -static void usart_clear_nmea_buffer(void) -{ - for (int i = 0; i < MAX_NMEA_SENTENCE_LEN; i++) { - nmea_sentence[i] = '\0'; - } - nmea_sentence_last_written = 0; -} - -/* void USART3_IRQHandler(void) */ -/* { */ -/* if (USART_GetITStatus(USART3, USART_IT_RXNE)) { */ -/* char t = USART3->DR; */ -/* */ -/* if (nmea_sentence_last_written == 0) { */ -/* if (t == '$') { */ -/* // Likely new start of sentence */ -/* nmea_sentence[nmea_sentence_last_written] = t; */ -/* nmea_sentence_last_written++; */ -/* } */ -/* } */ -/* else if (nmea_sentence_last_written < MAX_NMEA_SENTENCE_LEN) { */ -/* nmea_sentence[nmea_sentence_last_written] = t; */ -/* nmea_sentence_last_written++; */ -/* */ -/* if (t == '\n') { */ -/* int success = xQueueSendToBackFromISR( */ -/* usart_nmea_queue, */ -/* nmea_sentence, */ -/* NULL); */ -/* */ -/* if (success == pdFALSE) { */ -/* trigger_fault(FAULT_SOURCE_USART); */ -/* } */ -/* */ -/* usart_clear_nmea_buffer(); */ -/* } */ -/* } */ -/* else { */ -/* // Buffer overrun without a meaningful NMEA message. */ -/* usart_clear_nmea_buffer(); */ -/* } */ -/* } */ -/* } */ -/* */ -/* void USART2_IRQHandler(void) */ -/* { */ -/* if (USART_GetITStatus(USART2, USART_IT_RXNE)) { */ -/* char t = USART2->DR; */ -/* if (t == 'h') { */ -/* usart_debug_puts("help: no commands supported yet!\r\n"); */ -/* } */ -/* else { */ -/* usart_debug("Unknown command %c\r\n", t); */ -/* } */ -/* } */ -/* } */ diff --git a/src/simulator/src/GPIO/leds.c b/src/simulator/src/GPIO/leds.c new file mode 100644 index 0000000..874cc19 --- /dev/null +++ b/src/simulator/src/GPIO/leds.c @@ -0,0 +1,44 @@ +#include "../../../common/src/GPIO/leds.c" + + +extern char led_blue; +extern char led_green; +extern char led_orange; +extern char led_red; + +void leds_turn_on(int l) { + + switch (l) { + case LED_GREEN: + led_green = 1; + break; + case LED_ORANGE: + led_orange = 1; + break; + case LED_RED: + led_red = 1; + break; + case LED_BLUE: + led_blue = 1; + break; + + } +} + +void leds_turn_off(int l) { + + switch (l) { + case LED_GREEN: + led_green = 0; + break; + case LED_ORANGE: + led_orange = 0; + break; + case LED_RED: + led_red = 0; + break; + case LED_BLUE: + led_blue = 0; + break; + } +} diff --git a/src/simulator/src/GPIO/usart.c b/src/simulator/src/GPIO/usart.c new file mode 100644 index 0000000..650602a --- /dev/null +++ b/src/simulator/src/GPIO/usart.c @@ -0,0 +1,97 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016 Maximilien Cuony + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. +*/ + + +#define USART_TypeDef int + +int _USART2 = 2; +int _USART3 = 3; + +#define USART2 &_USART2 +#define USART3 &_USART3 + +static void usart_puts(USART_TypeDef*, const char*); + +#include "../../../common/includes/GPIO/usart.h" +#include "../../../common/src/GPIO/usart.c" + + +extern char uart_recv_txt[4096]; +int uart_recv_pointer = 0; + + +void usart_move_buffer_up(); + + +void usart_init() { + + // Zero buffer + for (int i = 0; i < 4096; i++) { + uart_recv_txt[i] = '\0'; + } + +} + +void usart_gps_specific_init() { +} + +void usart_move_buffer_up() { + + for (int i = 0; i <= 4010; i++) { + uart_recv_txt[i] = uart_recv_txt[i + 10]; + uart_recv_txt[i + 1] = '\0'; + } + + uart_recv_pointer -= 10; + +} + +// Make sure Tasks are suspended when this is called! +static void usart_puts(USART_TypeDef* USART, const char* str) { + + if (*USART == _USART2) { + while(*str) { + if (*str != '\r') { + uart_recv_txt[uart_recv_pointer+1] = '\0'; + uart_recv_txt[uart_recv_pointer] = *str; + uart_recv_pointer++; + + if (uart_recv_pointer >= 4000) { + usart_move_buffer_up(); + } + } + str++; + } + } +} + +void gui_usart_send(char * string) { + + while(*string) { + usart_process_char(*string); + + string++; + } + +} diff --git a/src/simulator/src/GPS/gps.c b/src/simulator/src/GPS/gps.c new file mode 100644 index 0000000..0ca20f3 --- /dev/null +++ b/src/simulator/src/GPS/gps.c @@ -0,0 +1,11 @@ +#include "../../../common/src/GPS/gps.c" + +void gps_usart_send(char * string) { + + while(*string) { + usart_gps_process_char(*string); + + string++; + } + +} diff --git a/src/simulator/src/GPS/minema.c b/src/simulator/src/GPS/minema.c new file mode 100644 index 0000000..10df198 --- /dev/null +++ b/src/simulator/src/GPS/minema.c @@ -0,0 +1 @@ +#include "../../../common/src/GPS/minmea.c" diff --git a/src/simulator/src/Gui/gui.c b/src/simulator/src/Gui/gui.c new file mode 100644 index 0000000..81c80b6 --- /dev/null +++ b/src/simulator/src/Gui/gui.c @@ -0,0 +1,402 @@ +#include "FreeRTOS.h" +#include "FreeRTOSConfig.h" +#include "task.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define NK_INCLUDE_FIXED_TYPES +#define NK_INCLUDE_STANDARD_IO +#define NK_INCLUDE_DEFAULT_ALLOCATOR +#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT +#define NK_INCLUDE_FONT_BAKING +#define NK_INCLUDE_DEFAULT_FONT +#define NK_IMPLEMENTATION +#define NK_XLIB_GL3_IMPLEMENTATION +#define NK_XLIB_LOAD_OPENGL_EXTENSIONS +#include "nuklear.h" +#include "nuklear_xlib_gl3.h" + +#define WINDOW_WIDTH 1200 +#define WINDOW_HEIGHT 800 + +#define MAX_VERTEX_BUFFER 512 * 1024 +#define MAX_ELEMENT_BUFFER 128 * 1024 + +#define UNUSED(a) (void)a +#define MIN(a,b) ((a) < (b) ? (a) : (b)) +#define MAX(a,b) ((a) < (b) ? (b) : (a)) +#define LEN(a) (sizeof(a)/sizeof(a)[0]) + + +/** + * USART + **/ +char uart_recv_txt[4096]; +static char uart_send_txt[512]; +static int uart_send_txt_len; +void gui_usart_send(char*); + +/** + * Leds + **/ +char led_blue = 0; +char led_green = 0; +char led_orange = 0; +char led_red = 0; + +struct XWindow { + Display *dpy; + Window win; + XVisualInfo *vis; + Colormap cmap; + XSetWindowAttributes swa; + XWindowAttributes attr; + GLXFBConfig fbc; + int width, height; +}; + +static int gl_err = FALSE; +static int gl_error_handler(Display *dpy, XErrorEvent *ev) { + UNUSED((dpy, ev)); + gl_err = TRUE; + return 0; +} + +static void die(const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + fputs("\n", stderr); + exit(EXIT_FAILURE); +} + +static int has_extension(const char *string, const char *ext) { + + const char *start, *where, *term; + where = strchr(ext, ' '); + + if (where || *ext == '\0') + return FALSE; + + for (start = string;;) { + where = strstr((const char*)start, ext); + if (!where) break; + term = where + strlen(ext); + if (where == start || *(where - 1) == ' ') { + if (*term == ' ' || *term == '\0') + return TRUE; + } + start = term; + } + return FALSE; +} + + +int main_gui() { + /* Platform */ + int running = 1; + struct XWindow win; + GLXContext glContext; + struct nk_context *ctx; + struct nk_color background; + + memset(&win, 0, sizeof(win)); + win.dpy = XOpenDisplay(NULL); + + if (!win.dpy) { + die("Failed to open X display\n"); + } + + { + /* check glx version */ + int glx_major, glx_minor; + if (!glXQueryVersion(win.dpy, &glx_major, &glx_minor)) + die("[X11]: Error: Failed to query OpenGL version\n"); + if ((glx_major == 1 && glx_minor < 3) || (glx_major < 1)) + die("[X11]: Error: Invalid GLX version!\n"); + } + + { + /* find and pick matching framebuffer visual */ + int fb_count; + static GLint attr[] = { + GLX_X_RENDERABLE, True, + GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT, + GLX_RENDER_TYPE, GLX_RGBA_BIT, + GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR, + GLX_RED_SIZE, 8, + GLX_GREEN_SIZE, 8, + GLX_BLUE_SIZE, 8, + GLX_ALPHA_SIZE, 8, + GLX_DEPTH_SIZE, 24, + GLX_STENCIL_SIZE, 8, + GLX_DOUBLEBUFFER, True, + None + }; + GLXFBConfig *fbc; + fbc = glXChooseFBConfig(win.dpy, DefaultScreen(win.dpy), attr, &fb_count); + + if (!fbc) { + die("[X11]: Error: failed to retrieve framebuffer configuration\n"); + } + + { + /* pick framebuffer with most samples per pixel */ + int i; + int fb_best = -1; + int best_num_samples = -1; + + for (i = 0; i < fb_count; ++i) { + XVisualInfo *vi = glXGetVisualFromFBConfig(win.dpy, fbc[i]); + if (vi) { + int sample_buffer, samples; + glXGetFBConfigAttrib(win.dpy, fbc[i], GLX_SAMPLE_BUFFERS, &sample_buffer); + glXGetFBConfigAttrib(win.dpy, fbc[i], GLX_SAMPLES, &samples); + if ((fb_best < 0) || (sample_buffer && samples > best_num_samples)) + fb_best = i; best_num_samples = samples; + } + } + win.fbc = fbc[fb_best]; + XFree(fbc); + win.vis = glXGetVisualFromFBConfig(win.dpy, win.fbc); + } + } + { + /* create window */ + win.cmap = XCreateColormap(win.dpy, RootWindow(win.dpy, win.vis->screen), win.vis->visual, AllocNone); + win.swa.colormap = win.cmap; + win.swa.background_pixmap = None; + win.swa.border_pixel = 0; + win.swa.event_mask = + ExposureMask | KeyPressMask | KeyReleaseMask | + ButtonPress | ButtonReleaseMask| ButtonMotionMask | + Button1MotionMask | Button3MotionMask | Button4MotionMask | Button5MotionMask| + PointerMotionMask| StructureNotifyMask; + win.win = XCreateWindow(win.dpy, RootWindow(win.dpy, win.vis->screen), 0, 0, + WINDOW_WIDTH, WINDOW_HEIGHT, 0, win.vis->depth, InputOutput, + win.vis->visual, CWBorderPixel|CWColormap|CWEventMask, &win.swa); + if (!win.win) die("[X11]: Failed to create window\n"); + XFree(win.vis); + XStoreName(win.dpy, win.win, "glutt-o-matique simulator 3000"); + XMapWindow(win.dpy, win.win); + } + { + /* create opengl context */ + typedef GLXContext(*glxCreateContext)(Display*, GLXFBConfig, GLXContext, Bool, const int*); + int(*old_handler)(Display*, XErrorEvent*) = XSetErrorHandler(gl_error_handler); + const char *extensions_str = glXQueryExtensionsString(win.dpy, DefaultScreen(win.dpy)); + glxCreateContext create_context = (glxCreateContext) + glXGetProcAddressARB((const GLubyte*)"glXCreateContextAttribsARB"); + + gl_err = FALSE; + if (!has_extension(extensions_str, "GLX_ARB_create_context") || !create_context) { + fprintf(stdout, "[X11]: glXCreateContextAttribARB() not found...\n"); + fprintf(stdout, "[X11]: ... using old-style GLX context\n"); + glContext = glXCreateNewContext(win.dpy, win.fbc, GLX_RGBA_TYPE, 0, True); + } else { + GLint attr[] = { + GLX_CONTEXT_MAJOR_VERSION_ARB, 3, + GLX_CONTEXT_MINOR_VERSION_ARB, 0, + None + }; + glContext = create_context(win.dpy, win.fbc, 0, True, attr); + XSync(win.dpy, False); + if (gl_err || !glContext) { + /* Could not create GL 3.0 context. Fallback to old 2.x context. + * If a version below 3.0 is requested, implementations will + * return the newest context version compatible with OpenGL + * version less than version 3.0.*/ + attr[1] = 1; attr[3] = 0; + gl_err = FALSE; + fprintf(stdout, "[X11] Failed to create OpenGL 3.0 context\n"); + fprintf(stdout, "[X11] ... using old-style GLX context!\n"); + glContext = create_context(win.dpy, win.fbc, 0, True, attr); + } + } + XSync(win.dpy, False); + XSetErrorHandler(old_handler); + if (gl_err || !glContext) + die("[X11]: Failed to create an OpenGL context\n"); + glXMakeCurrent(win.dpy, win.win, glContext); + } + + ctx = nk_x11_init(win.dpy, win.win); + /* Load Fonts: if none of these are loaded a default font will be used */ + {struct nk_font_atlas *atlas; + nk_x11_font_stash_begin(&atlas); + nk_x11_font_stash_end(); + } + + + background = nk_rgb(28,48,62); + + while (running) + { + taskYIELD(); + + vTaskSuspendAll(); + + /* Input */ + XEvent evt; + nk_input_begin(ctx); + while (XCheckWindowEvent(win.dpy, win.win, win.swa.event_mask, &evt)){ + if (XFilterEvent(&evt, win.win)) continue; + nk_x11_handle_event(&evt); + } + nk_input_end(ctx); + + /* GUI */ + { + + struct nk_panel layout; + + if (nk_begin(ctx, &layout, "UART", nk_rect(50, 50, 400, 300), NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE)) { + + + nk_menubar_begin(ctx); + nk_layout_row_begin(ctx, NK_STATIC, 25, 2); + nk_layout_row_push(ctx, 280); + + int active = nk_edit_string(ctx, NK_EDIT_FIELD|NK_EDIT_SIG_ENTER, uart_send_txt, &uart_send_txt_len, 512, nk_filter_default); + + nk_layout_row_push(ctx, 70); + if (nk_button_label(ctx, "Send", NK_BUTTON_DEFAULT) || (active & NK_EDIT_COMMITED)) { + + uart_send_txt[uart_send_txt_len] = '\0'; + + gui_usart_send(uart_send_txt); + + uart_send_txt[0] = '\0'; + uart_send_txt_len = 0; + } + + nk_menubar_end(ctx); + + nk_layout_row_dynamic(ctx, 25, 1); + + nk_label(ctx, "UART Output:", NK_TEXT_LEFT); + + nk_layout_row_dynamic(ctx, 16, 1); + + char * current_pointer = uart_recv_txt; + int l = 0; + + while (*current_pointer != 0) { + + if (*current_pointer == '\n') { + if (l > 1) { + nk_text(ctx, current_pointer - l, l, NK_TEXT_LEFT); + } + current_pointer++; + l = 0; + } + current_pointer++; + l++; + } + + if (l > 1) { + nk_text(ctx, current_pointer - l, l, NK_TEXT_LEFT); + } + + /* nk_layout_row_end(ctx); */ + + + nk_end(ctx); + } + + + if (nk_begin(ctx, &layout, "LEDs", nk_rect(460, 50, 100, 155), NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE)) { + + nk_layout_row_static(ctx, 20, 20, 3); + + struct nk_color color; + + + nk_text(ctx, "", 0, NK_TEXT_LEFT); + + color.r = 255; color.g = 0; color.b = 0; + + if (led_red == 1) { + color.a = 255; + } else { + color.a = 30; + } + nk_button_color(ctx, color, NK_BUTTON_DEFAULT); + + nk_text(ctx, "", 0, NK_TEXT_LEFT); + + color.r = 0; color.g = 255; color.b = 0; + + if (led_green == 1) { + color.a = 255; + } else { + color.a = 30; + } + nk_button_color(ctx, color, NK_BUTTON_DEFAULT); + + nk_text(ctx, "", 0, NK_TEXT_LEFT); + + color.r = 255; color.g = 165; color.b = 0; + + if (led_orange == 1) { + color.a = 255; + } else { + color.a = 30; + } + nk_button_color(ctx, color, NK_BUTTON_DEFAULT); + + nk_text(ctx, "", 0, NK_TEXT_LEFT); + + color.r = 0; color.g = 0; color.b = 255; + + if (led_blue == 1) { + color.a = 255; + } else { + color.a = 30; + } + nk_button_color(ctx, color, NK_BUTTON_DEFAULT); + + nk_text(ctx, "", 0, NK_TEXT_LEFT); + + nk_end(ctx); + + } + + + } + /* if (nk_window_is_closed(ctx, "Demo")) break; */ + + { + float bg[4]; + nk_color_fv(bg, background); + XGetWindowAttributes(win.dpy, win.win, &win.attr); + glViewport(0, 0, win.width, win.height); + glClear(GL_COLOR_BUFFER_BIT); + glClearColor(bg[0], bg[1], bg[2], bg[3]); + nk_x11_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER); + glXSwapBuffers(win.dpy, win.win); + } + + xTaskResumeAll(); + } + + nk_x11_shutdown(); + glXMakeCurrent(win.dpy, 0, 0); + glXDestroyContext(win.dpy, glContext); + XUnmapWindow(win.dpy, win.win); + XFreeColormap(win.dpy, win.cmap); + XDestroyWindow(win.dpy, win.win); + XCloseDisplay(win.dpy); + return 0; + +} diff --git a/src/simulator/src/Gui/test.c b/src/simulator/src/Gui/test.c deleted file mode 100644 index f742fe8..0000000 --- a/src/simulator/src/Gui/test.c +++ /dev/null @@ -1,285 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define NK_INCLUDE_FIXED_TYPES -#define NK_INCLUDE_STANDARD_IO -#define NK_INCLUDE_DEFAULT_ALLOCATOR -#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT -#define NK_INCLUDE_FONT_BAKING -#define NK_INCLUDE_DEFAULT_FONT -#define NK_IMPLEMENTATION -#define NK_XLIB_GL3_IMPLEMENTATION -#define NK_XLIB_LOAD_OPENGL_EXTENSIONS -#include "nuklear.h" -#include "nuklear_xlib_gl3.h" - -#define WINDOW_WIDTH 1200 -#define WINDOW_HEIGHT 800 - -#define MAX_VERTEX_BUFFER 512 * 1024 -#define MAX_ELEMENT_BUFFER 128 * 1024 - -#define UNUSED(a) (void)a -#define MIN(a,b) ((a) < (b) ? (a) : (b)) -#define MAX(a,b) ((a) < (b) ? (b) : (a)) -#define LEN(a) (sizeof(a)/sizeof(a)[0]) - - -struct XWindow { - Display *dpy; - Window win; - XVisualInfo *vis; - Colormap cmap; - XSetWindowAttributes swa; - XWindowAttributes attr; - GLXFBConfig fbc; - int width, height; -}; - -static int gl_err = FALSE; -static int gl_error_handler(Display *dpy, XErrorEvent *ev) { - UNUSED((dpy, ev)); - gl_err = TRUE; - return 0; -} - -static void die(const char *fmt, ...) { - va_list ap; - va_start(ap, fmt); - vfprintf(stderr, fmt, ap); - va_end(ap); - fputs("\n", stderr); - exit(EXIT_FAILURE); -} - -static int has_extension(const char *string, const char *ext) { - - const char *start, *where, *term; - where = strchr(ext, ' '); - - if (where || *ext == '\0') - return FALSE; - - for (start = string;;) { - where = strstr((const char*)start, ext); - if (!where) break; - term = where + strlen(ext); - if (where == start || *(where - 1) == ' ') { - if (*term == ' ' || *term == '\0') - return TRUE; - } - start = term; - } - return FALSE; -} - -int main2() { - /* Platform */ - int running = 1; - struct XWindow win; - GLXContext glContext; - struct nk_context *ctx; - struct nk_color background; - - memset(&win, 0, sizeof(win)); - win.dpy = XOpenDisplay(NULL); - - if (!win.dpy) { - die("Failed to open X display\n"); - } - - { - /* check glx version */ - int glx_major, glx_minor; - if (!glXQueryVersion(win.dpy, &glx_major, &glx_minor)) - die("[X11]: Error: Failed to query OpenGL version\n"); - if ((glx_major == 1 && glx_minor < 3) || (glx_major < 1)) - die("[X11]: Error: Invalid GLX version!\n"); - } - - { - /* find and pick matching framebuffer visual */ - int fb_count; - static GLint attr[] = { - GLX_X_RENDERABLE, True, - GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT, - GLX_RENDER_TYPE, GLX_RGBA_BIT, - GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR, - GLX_RED_SIZE, 8, - GLX_GREEN_SIZE, 8, - GLX_BLUE_SIZE, 8, - GLX_ALPHA_SIZE, 8, - GLX_DEPTH_SIZE, 24, - GLX_STENCIL_SIZE, 8, - GLX_DOUBLEBUFFER, True, - None - }; - GLXFBConfig *fbc; - fbc = glXChooseFBConfig(win.dpy, DefaultScreen(win.dpy), attr, &fb_count); - - if (!fbc) { - die("[X11]: Error: failed to retrieve framebuffer configuration\n"); - } - - { - /* pick framebuffer with most samples per pixel */ - int i; - int fb_best = -1, best_num_samples = -1; - for (i = 0; i < fb_count; ++i) { - XVisualInfo *vi = glXGetVisualFromFBConfig(win.dpy, fbc[i]); - if (vi) { - int sample_buffer, samples; - glXGetFBConfigAttrib(win.dpy, fbc[i], GLX_SAMPLE_BUFFERS, &sample_buffer); - glXGetFBConfigAttrib(win.dpy, fbc[i], GLX_SAMPLES, &samples); - if ((fb_best < 0) || (sample_buffer && samples > best_num_samples)) - fb_best = i; best_num_samples = samples; - } - } - win.fbc = fbc[fb_best]; - XFree(fbc); - win.vis = glXGetVisualFromFBConfig(win.dpy, win.fbc); - } - } - { - /* create window */ - win.cmap = XCreateColormap(win.dpy, RootWindow(win.dpy, win.vis->screen), win.vis->visual, AllocNone); - win.swa.colormap = win.cmap; - win.swa.background_pixmap = None; - win.swa.border_pixel = 0; - win.swa.event_mask = - ExposureMask | KeyPressMask | KeyReleaseMask | - ButtonPress | ButtonReleaseMask| ButtonMotionMask | - Button1MotionMask | Button3MotionMask | Button4MotionMask | Button5MotionMask| - PointerMotionMask| StructureNotifyMask; - win.win = XCreateWindow(win.dpy, RootWindow(win.dpy, win.vis->screen), 0, 0, - WINDOW_WIDTH, WINDOW_HEIGHT, 0, win.vis->depth, InputOutput, - win.vis->visual, CWBorderPixel|CWColormap|CWEventMask, &win.swa); - if (!win.win) die("[X11]: Failed to create window\n"); - XFree(win.vis); - XStoreName(win.dpy, win.win, "glutt-o-matique simulator 3000"); - XMapWindow(win.dpy, win.win); - } - { - /* create opengl context */ - typedef GLXContext(*glxCreateContext)(Display*, GLXFBConfig, GLXContext, Bool, const int*); - int(*old_handler)(Display*, XErrorEvent*) = XSetErrorHandler(gl_error_handler); - const char *extensions_str = glXQueryExtensionsString(win.dpy, DefaultScreen(win.dpy)); - glxCreateContext create_context = (glxCreateContext) - glXGetProcAddressARB((const GLubyte*)"glXCreateContextAttribsARB"); - - gl_err = FALSE; - if (!has_extension(extensions_str, "GLX_ARB_create_context") || !create_context) { - fprintf(stdout, "[X11]: glXCreateContextAttribARB() not found...\n"); - fprintf(stdout, "[X11]: ... using old-style GLX context\n"); - glContext = glXCreateNewContext(win.dpy, win.fbc, GLX_RGBA_TYPE, 0, True); - } else { - GLint attr[] = { - GLX_CONTEXT_MAJOR_VERSION_ARB, 3, - GLX_CONTEXT_MINOR_VERSION_ARB, 0, - None - }; - glContext = create_context(win.dpy, win.fbc, 0, True, attr); - XSync(win.dpy, False); - if (gl_err || !glContext) { - /* Could not create GL 3.0 context. Fallback to old 2.x context. - * If a version below 3.0 is requested, implementations will - * return the newest context version compatible with OpenGL - * version less than version 3.0.*/ - attr[1] = 1; attr[3] = 0; - gl_err = FALSE; - fprintf(stdout, "[X11] Failed to create OpenGL 3.0 context\n"); - fprintf(stdout, "[X11] ... using old-style GLX context!\n"); - glContext = create_context(win.dpy, win.fbc, 0, True, attr); - } - } - XSync(win.dpy, False); - XSetErrorHandler(old_handler); - if (gl_err || !glContext) - die("[X11]: Failed to create an OpenGL context\n"); - glXMakeCurrent(win.dpy, win.win, glContext); - } - - ctx = nk_x11_init(win.dpy, win.win); - /* Load Fonts: if none of these are loaded a default font will be used */ - {struct nk_font_atlas *atlas; - nk_x11_font_stash_begin(&atlas); - nk_x11_font_stash_end(); - } - - - background = nk_rgb(28,48,62); - - static char box_buffer[512]; - static int box_len; - - while (running) - { - /* Input */ - XEvent evt; - nk_input_begin(ctx); - while (XCheckWindowEvent(win.dpy, win.win, win.swa.event_mask, &evt)){ - if (XFilterEvent(&evt, win.win)) continue; - nk_x11_handle_event(&evt); - } - nk_input_end(ctx); - - /* GUI */ - {struct nk_panel layout; - if (nk_begin(ctx, &layout, "UART", nk_rect(50, 50, 400, 200), NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE)) { - - nk_layout_row_dynamic(ctx, 25, 1); - - nk_label(ctx, "UART Output:", NK_TEXT_LEFT); - - nk_layout_row_dynamic(ctx, 75, 1); - nk_edit_string(ctx, NK_EDIT_BOX, box_buffer, &box_len, 512, nk_filter_default); - - /* #<{(| nk_layout_row(ctx, NK_STATIC, 25, 2, ratio); |)}># */ - /* active = nk_edit_string(ctx, NK_EDIT_FIELD|NK_EDIT_SIG_ENTER, text[7], &text_len[7], 64, nk_filter_ascii); */ - /* if (nk_button_label(ctx, "Submit", NK_BUTTON_DEFAULT) || */ - /* (active & NK_EDIT_COMMITED)) */ - /* { */ - /* text[7][text_len[7]] = '\n'; */ - /* text_len[7]++; */ - /* memcpy(&box_buffer[box_len], &text[7], (nk_size)text_len[7]); */ - /* box_len += text_len[7]; */ - /* text_len[7] = 0; */ - /* } */ - nk_layout_row_end(ctx); - - /* nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1); */ - - } - nk_end(ctx);} - /* if (nk_window_is_closed(ctx, "Demo")) break; */ - - { - float bg[4]; - nk_color_fv(bg, background); - XGetWindowAttributes(win.dpy, win.win, &win.attr); - glViewport(0, 0, win.width, win.height); - glClear(GL_COLOR_BUFFER_BIT); - glClearColor(bg[0], bg[1], bg[2], bg[3]); - nk_x11_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER); - glXSwapBuffers(win.dpy, win.win); - } - } - - nk_x11_shutdown(); - glXMakeCurrent(win.dpy, 0, 0); - glXDestroyContext(win.dpy, glContext); - XUnmapWindow(win.dpy, win.win); - XFreeColormap(win.dpy, win.cmap); - XDestroyWindow(win.dpy, win.win); - XCloseDisplay(win.dpy); - return 0; - -} diff --git a/src/simulator/vc.h b/src/simulator/vc.h index d8d7797..fa3f184 100644 --- a/src/simulator/vc.h +++ b/src/simulator/vc.h @@ -1,4 +1,4 @@ // This file is generated by Makefile. // Do not edit this file! -#define GIT_VERSION "4803231" +#define GIT_VERSION "9069fc1" -- cgit v1.2.3