From 53c2afbf719d91f660e815c160e73066ef1ec9e0 Mon Sep 17 00:00:00 2001
From: Maximilien Cuony <maximilien@theglu.org>
Date: Fri, 3 Jun 2016 21:44:56 +0200
Subject: Working GPS simulator

---
 src/common/src/Core/common.c  |   2 +
 src/common/src/Core/main.c    |  13 ++---
 src/simulator/src/Core/main.c | 130 ++++++++++++++++++++++++++++++++++++++++--
 src/simulator/src/Gui/gui.c   |  74 +++++++++++++++++++++++-
 4 files changed, 206 insertions(+), 13 deletions(-)

(limited to 'src')

diff --git a/src/common/src/Core/common.c b/src/common/src/Core/common.c
index 4adca26..c31bbf8 100644
--- a/src/common/src/Core/common.c
+++ b/src/common/src/Core/common.c
@@ -128,7 +128,9 @@ int local_time(struct tm *time)
             time->tm_isdst = 1;
         }
 
+#ifdef SIMULATOR
         time->tm_year -= 1900;  //TODO Same on hardware ?
+#endif
 
         // Let mktime fix the struct tm *time
         if (mktime(time) == (time_t)-1) {
diff --git a/src/common/src/Core/main.c b/src/common/src/Core/main.c
index 4eebfa2..1a72e32 100644
--- a/src/common/src/Core/main.c
+++ b/src/common/src/Core/main.c
@@ -126,12 +126,6 @@ static void test_task(void *pvParameters) {
             leds_turn_off(LED_RED);
         }
 
-        char * poney = "$GNRMC,202140.00,A,4719.59789,N,00830.92084,E,0.012,,310516,,,A*65\n";
-        gps_usart_send(poney);
-        /* char * poney2 = "$GNRMC,202141.00,A,4719.59788,N,00830.92085,E,0.012,,310516,,,A*64"; */
-        /* gps_usart_send(poney2); */
-        /* char * poney3 = "$GNRMC,202142.00,A,4719.59785,N,00830.92087,E,0.010,,310516,,,A*6A"; */
-        /* gps_usart_send(poney3); */
     }
 
 }
@@ -339,7 +333,12 @@ static void gps_monit_task(void *pvParameters) {
                     gps_time.tm_hour, gps_time.tm_min, gps_time.tm_sec);
 
             usart_debug("TIME  %04d-%02d-%02d %02d:%02d:%02d\r\n",
-                    time.tm_year, time.tm_mon, time.tm_mday,
+#ifdef SIMULATOR
+                    time.tm_year + 1900,
+#else
+                    time.tm_year,
+#endif
+                    time.tm_mon, time.tm_mday,
                     time.tm_hour, time.tm_min, time.tm_sec);
 
             t_gps_print_latch = 1;
diff --git a/src/simulator/src/Core/main.c b/src/simulator/src/Core/main.c
index 85e6a81..853ee83 100644
--- a/src/simulator/src/Core/main.c
+++ b/src/simulator/src/Core/main.c
@@ -35,11 +35,32 @@ static void thread_gui(void *arg) {
 extern int gui_gps_send_frame;
 extern int gui_gps_frames_valid;
 extern char gui_gps_lat[64];
+extern int gui_gps_lat_len;
 extern int gui_gps_lat_hem;
 extern char gui_gps_lon[64];
+extern int gui_gps_lon_len;
 extern int gui_gps_lon_hem;
 extern int gui_gps_send_current_time;
 
+int gui_gps_custom_hour_on;
+int gui_gps_custom_min_on;
+int gui_gps_custom_sec_on;
+int gui_gps_custom_day_on;
+int gui_gps_custom_month_on;
+int gui_gps_custom_year_on;
+char gui_gps_custom_hour[4];
+int gui_gps_custom_hour_len;
+char gui_gps_custom_min[4];
+int gui_gps_custom_min_len;
+char gui_gps_custom_sec[4];
+int gui_gps_custom_sec_len;
+char gui_gps_custom_day[4];
+int gui_gps_custom_day_len;
+char gui_gps_custom_month[4];
+int gui_gps_custom_month_len;
+char gui_gps_custom_year[6];
+int gui_gps_custom_year_len;
+
 static void thread_gui_gps(void *arg) {
 
     while(1) {
@@ -47,14 +68,41 @@ static void thread_gui_gps(void *arg) {
 
         if (gui_gps_send_frame) {
 
+            time_t now = time(NULL);
+            struct tm *t = gmtime(&now);
+
             char gps_frame_buffer[128];
             int gps_buffer_pointer = 0;
 
-            strcpy(gps_frame_buffer + gps_buffer_pointer, "$GPRMC,");
+            strcpy(gps_frame_buffer + gps_buffer_pointer, "$GNRMC,");
             gps_buffer_pointer += 7;
 
-            strcpy(gps_frame_buffer + gps_buffer_pointer, "225446,");
-            gps_buffer_pointer += 7;
+            if (gui_gps_send_current_time) {
+
+                sprintf(gps_frame_buffer + gps_buffer_pointer, "%02d%02d%02d", t->tm_hour, t->tm_min, t->tm_sec);
+
+            } else {
+
+                int hour = atoi(gui_gps_custom_hour);
+                int min = atoi(gui_gps_custom_min);
+                int sec = atoi(gui_gps_custom_sec);
+
+                if (!gui_gps_custom_hour_on) {
+                    hour = t->tm_hour;
+                }
+                if (!gui_gps_custom_min_on) {
+                    min = t->tm_min;
+                }
+                if (!gui_gps_custom_sec_on) {
+                    sec = t->tm_sec;
+                }
+
+                sprintf(gps_frame_buffer + gps_buffer_pointer, "%02d%02d%02d", hour, min, sec);
+            }
+            gps_buffer_pointer += 6;
+
+            gps_frame_buffer[gps_buffer_pointer] = ',';
+            gps_buffer_pointer++;
 
             if (gui_gps_frames_valid) {
                 gps_frame_buffer[gps_buffer_pointer] = 'A';
@@ -62,12 +110,86 @@ static void thread_gui_gps(void *arg) {
                 gps_frame_buffer[gps_buffer_pointer] = 'V';
             }
             gps_buffer_pointer++;
+
+            gps_frame_buffer[gps_buffer_pointer] = ',';
+            gps_buffer_pointer++;
+
+            strcpy(gps_frame_buffer + gps_buffer_pointer, gui_gps_lat);
+            gps_buffer_pointer += gui_gps_lat_len;
+
             gps_frame_buffer[gps_buffer_pointer] = ',';
             gps_buffer_pointer++;
 
+            if (gui_gps_lat_hem == 0) {
+                gps_frame_buffer[gps_buffer_pointer] = 'N';
+            } else {
+                gps_frame_buffer[gps_buffer_pointer] = 'S';
+            }
+            gps_buffer_pointer++;
+
+            gps_frame_buffer[gps_buffer_pointer] = ',';
+            gps_buffer_pointer++;
+
+            strcpy(gps_frame_buffer + gps_buffer_pointer, gui_gps_lon);
+            gps_buffer_pointer += gui_gps_lon_len;
+
+            gps_frame_buffer[gps_buffer_pointer] = ',';
+            gps_buffer_pointer++;
+
+            if (gui_gps_lon_hem == 0) {
+                gps_frame_buffer[gps_buffer_pointer] = 'E';
+            } else {
+                gps_frame_buffer[gps_buffer_pointer] = 'W';
+            }
+            gps_buffer_pointer++;
+
+            strcpy(gps_frame_buffer + gps_buffer_pointer, ",,,");
+            gps_buffer_pointer += 3;
+
+            if (gui_gps_send_current_time) {
+
+                sprintf(gps_frame_buffer + gps_buffer_pointer, "%02d%02d%02d", t->tm_mday, t->tm_mon, t->tm_year - 100);
+
+            } else {
+                strcpy(gps_frame_buffer + gps_buffer_pointer, "000000");
+
+                int day = atoi(gui_gps_custom_day);
+                int mon = atoi(gui_gps_custom_month);
+                int year = atoi(gui_gps_custom_year);
+
+                if (!gui_gps_custom_day_on) {
+                    day = t->tm_mday;
+                }
+                if (!gui_gps_custom_month_on) {
+                    mon = t->tm_mon;
+                }
+                if (!gui_gps_custom_year_on) {
+                    year = t->tm_year - 100;
+                }
+
+                sprintf(gps_frame_buffer + gps_buffer_pointer, "%02d%02d%02d", day, mon, year);
+            }
+            gps_buffer_pointer += 6;
+
+            strcpy(gps_frame_buffer + gps_buffer_pointer, ",,,A*");
+            gps_buffer_pointer += 5;
+
+
+            uint8_t checksum = 0;
+            int i = 1;
+
+            while (gps_frame_buffer[i] && gps_frame_buffer[i] != '*') {
+                checksum ^= gps_frame_buffer[i];
+                i++;
+            }
+
+            sprintf(gps_frame_buffer + gps_buffer_pointer, "%02x\n", checksum);
+            gps_buffer_pointer += 3;
 
             gps_frame_buffer[gps_buffer_pointer] = '\0';
-            printf("%s\n", gps_frame_buffer);
+
+            gps_usart_send(gps_frame_buffer);
+            /* printf(gps_frame_buffer); */
 
         }
 
diff --git a/src/simulator/src/Gui/gui.c b/src/simulator/src/Gui/gui.c
index 72fa9ae..58bbb78 100644
--- a/src/simulator/src/Gui/gui.c
+++ b/src/simulator/src/Gui/gui.c
@@ -67,6 +67,24 @@ int gui_gps_lon_len = 10;
 static const char *gps_EW[] = {"East", "West"};
 int gui_gps_lon_hem = 0;
 int gui_gps_send_current_time = 1;
+int gui_gps_custom_hour_on = 0;
+int gui_gps_custom_min_on = 0;
+int gui_gps_custom_sec_on = 0;
+int gui_gps_custom_day_on = 0;
+int gui_gps_custom_month_on = 0;
+int gui_gps_custom_year_on = 0;
+char gui_gps_custom_hour[4] = "12";
+int gui_gps_custom_hour_len = 2;
+char gui_gps_custom_min[4] = "30";
+int gui_gps_custom_min_len = 2;
+char gui_gps_custom_sec[4] = "45";
+int gui_gps_custom_sec_len = 2;
+char gui_gps_custom_day[4] = "01";
+int gui_gps_custom_day_len = 2;
+char gui_gps_custom_month[4] = "06";
+int gui_gps_custom_month_len = 2;
+char gui_gps_custom_year[4] = "16";
+int gui_gps_custom_year_len = 2;
 
 struct XWindow {
     Display *dpy;
@@ -327,7 +345,7 @@ int main_gui() {
             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)) {
+            if (nk_begin(ctx, &layout, "LEDs", nk_rect(50, 360, 100, 155), NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE)) {
 
                 nk_layout_row_static(ctx, 20, 20, 3);
 
@@ -382,7 +400,7 @@ int main_gui() {
             }
             nk_end(ctx);
 
-            if (nk_begin(ctx, &layout, "GPS", nk_rect(460, 210, 200, 455), NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE)) {
+            if (nk_begin(ctx, &layout, "GPS", nk_rect(460, 50, 200, 465), NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE)) {
 
                 nk_layout_row_dynamic(ctx, 30, 1);
                 nk_checkbox_label(ctx, "Send frames", &gui_gps_send_frame);
@@ -403,6 +421,58 @@ int main_gui() {
                 nk_layout_row_dynamic(ctx, 30, 1);
                 nk_checkbox_label(ctx, "Send current time", &gui_gps_send_current_time);
 
+                if (!gui_gps_send_current_time) {
+
+                    nk_label(ctx, "Custom values:", NK_TEXT_LEFT);
+
+                    nk_layout_row_dynamic(ctx, 30, 3);
+
+                    nk_label(ctx, "Hour", NK_TEXT_LEFT);
+                    nk_checkbox_label(ctx, "", &gui_gps_custom_hour_on);
+                    if (gui_gps_custom_hour_on) {
+                        nk_edit_string(ctx, NK_EDIT_SIMPLE, gui_gps_custom_hour, &gui_gps_custom_hour_len, 3, nk_filter_decimal);
+                    } else {
+                        nk_label(ctx, "", NK_TEXT_LEFT);
+                    }
+                    nk_label(ctx, "Minutes", NK_TEXT_LEFT);
+                    nk_checkbox_label(ctx, "", &gui_gps_custom_min_on);
+                    if (gui_gps_custom_min_on) {
+                        nk_edit_string(ctx, NK_EDIT_SIMPLE, gui_gps_custom_min, &gui_gps_custom_min_len, 3, nk_filter_decimal);
+                    } else {
+                        nk_label(ctx, "", NK_TEXT_LEFT);
+                    }
+                    nk_label(ctx, "Secondes", NK_TEXT_LEFT);
+                    nk_checkbox_label(ctx, "", &gui_gps_custom_sec_on);
+                    if (gui_gps_custom_sec_on) {
+                        nk_edit_string(ctx, NK_EDIT_SIMPLE, gui_gps_custom_sec, &gui_gps_custom_sec_len, 3, nk_filter_decimal);
+                    } else {
+                        nk_label(ctx, "", NK_TEXT_LEFT);
+                    }
+                    nk_label(ctx, "Day", NK_TEXT_LEFT);
+                    nk_checkbox_label(ctx, "", &gui_gps_custom_day_on);
+                    if (gui_gps_custom_day_on) {
+                        nk_edit_string(ctx, NK_EDIT_SIMPLE, gui_gps_custom_day, &gui_gps_custom_day_len, 3, nk_filter_decimal);
+                    } else {
+                        nk_label(ctx, "", NK_TEXT_LEFT);
+                    }
+                    nk_label(ctx, "Month", NK_TEXT_LEFT);
+                    nk_checkbox_label(ctx, "", &gui_gps_custom_month_on);
+                    if (gui_gps_custom_month_on) {
+                        nk_edit_string(ctx, NK_EDIT_SIMPLE, gui_gps_custom_month, &gui_gps_custom_month_len, 3, nk_filter_decimal);
+                    } else {
+                        nk_label(ctx, "", NK_TEXT_LEFT);
+                    }
+                    nk_label(ctx, "Year", NK_TEXT_LEFT);
+                    nk_checkbox_label(ctx, "", &gui_gps_custom_year_on);
+                    if (gui_gps_custom_year_on) {
+                        nk_edit_string(ctx, NK_EDIT_SIMPLE, gui_gps_custom_year, &gui_gps_custom_year_len, 3, nk_filter_decimal);
+                    } else {
+                        nk_label(ctx, "", NK_TEXT_LEFT);
+                    }
+
+
+                }
+
             }
             nk_end(ctx);
 
-- 
cgit v1.2.3