Browse Source

Fixed real-time file read and colors

Dmitry Yu Okunev 4 years ago
parent
commit
514fb50e14
5 changed files with 90 additions and 18 deletions
  1. 1 1
      GNUmakefile
  2. 11 2
      binary.c
  3. 1 0
      binary.h
  4. 1 1
      configuration.h
  5. 76 14
      main.c

+ 1 - 1
GNUmakefile

@@ -8,7 +8,7 @@ EXAMPLES ?= yes
 CSECFLAGS ?= -fstack-protector-all -Wall --param ssp-buffer-size=4 -D_FORTIFY_SOURCE=2 -fstack-check -DPARANOID -std=gnu99
 CFLAGS ?= -pipe -O2
 CFLAGS += $(CSECFLAGS) $(shell pkg-config --cflags gtk+-2.0) -flto
-DEBUGCFLAGS ?= -pipe -Wall -Werror -ggdb3 -export-dynamic -Wno-error=unused-variable -O0 -pipe $(CSECFLAGS)
+DEBUGCFLAGS ?= -pipe -Wall -Werror -ggdb3 -export-dynamic -Wno-error=unused-variable -O0 -pipe $(CSECFLAGS) $(shell pkg-config --cflags gtk+-2.0)
 
 CARCHFLAGS ?= -march=native
 

+ 11 - 2
binary.c

@@ -20,7 +20,9 @@
 
 #include <assert.h>	/* assert()	*/
 #include <unistd.h>
+#include <stdio.h>
 
+#include "configuration.h"
 #include "binary.h"
 
 
@@ -28,8 +30,15 @@
 	x ## _t get_ ## x (FILE *i_f) {		\
 		x ## _t r;				\
 							\
+							\
 		if (fread(&r, sizeof(r), 1, i_f) < 1) {	\
-			if (feof(i_f)) sleep(3600*24*30);	\
+			while (feof(i_f)) {		\
+				usleep(AUTOUPDATE_USECS);\
+				clearerr(i_f);\
+				if (fread(&r, sizeof(r), 1, i_f) < 1 && !feof(i_f))\
+					assert (ferror(i_f));		\
+				return r;\
+			}				\
 			assert (ferror(i_f));		\
 		}					\
 							\
@@ -39,5 +48,5 @@
 DECLARE_GET_X_t(uint64);
 DECLARE_GET_X_t(uint32);
 DECLARE_GET_X_t(uint16);
-//DECLARE_GET_X_t(uint8);
+DECLARE_GET_X_t(uint8);
 

+ 1 - 0
binary.h

@@ -23,4 +23,5 @@
 extern uint64_t get_uint64(FILE *i_f);
 extern uint32_t get_uint32(FILE *i_f);
 extern uint16_t get_uint16(FILE *i_f);
+extern uint8_t  get_uint8(FILE *i_f);
 

+ 1 - 1
configuration.h

@@ -24,5 +24,5 @@
 
 #define	OUTPUT_LOCK_TIMEOUT		1000
 
-#define PARSER_MAXELEMENTS		65536
+#define AUTOUPDATE_USECS		100000
 

+ 76 - 14
main.c

@@ -27,14 +27,15 @@
 //#include <fcntl.h>
 #include <gtk/gtk.h>
 #include <pthread.h>
+#include <errno.h>
 
+#include "configuration.h"
 #include "binary.h"
 #include "malloc.h"
 
 FILE *sensor;
 FILE *dump;
 
-#define AUTOUPDATE_USECS 100000
 #define HISTORY_SIZE (1 << 20)
 #define MAX_CHANNELS 1
 #define Y_BITS 12
@@ -59,13 +60,13 @@ enum trigger_mode {
 
 double x_userdiv    = 1.9E-3;
 double x_useroffset = 0;
-double y_userscale  = 14;
-double y_useroffset = -0.11;
+double y_userscale  = 7;
+double y_useroffset = -0.17;
 
 int trigger_start_mode = TG_RISE;
-int trigger_start_y    = 450;
+int trigger_start_y    = 695;
 int trigger_end_mode   = TG_RISE;
-int trigger_end_y      = 450;
+int trigger_end_y      = 695;
 
 void
 sensor_open()
@@ -105,10 +106,29 @@ sensor_close()
 }
 
 void
-dump_open()
+dump_open(char *dumppath, char tailonly)
 {
 	dump = stdin;
 
+	if (dumppath == NULL)
+		return;
+
+	if (*dumppath == 0)
+		return;
+
+	if (!strcmp(dumppath, "-"))
+		return;
+
+	dump = fopen(dumppath, "r");
+	if (dump == NULL) {
+		fprintf(stderr, "Cannot open file \"%s\": %s", dumppath, strerror(errno));
+		abort ();
+	}
+
+	if (tailonly) {
+		fseek(dump, 0, SEEK_END);
+	}
+
 	return;
 }
 
@@ -120,13 +140,27 @@ dump_fetch(history_t *p)
 	uint32_t value;
 
 	ts_parse  = get_uint64(dump);
+	while (ts_parse < 1437900000000000000 || ts_parse > 1537900000000000000) {
+		printf("dump_fetch() correction 0\n");
+		get_uint8(dump);
+		ts_parse  = get_uint64(dump);
+	}
+
 	ts_device = get_uint64(dump);
+
 	value     = get_uint32(dump);
+	while (value < 0 || value > 1024) {
+		printf("dump_fetch() correction 1\n");
+		value     = get_uint32(dump);
+	}
 
 	(void)ts_parse;
 	p->timestamp = ts_device;
 	p->value     = value;
 
+	//printf("V: %lu %lu %u\n", ts_parse, ts_device, value);
+	//sleep(1);
+
 	return 1;
 }
 
@@ -144,7 +178,7 @@ history_flush()
 	memcpy(history[0], &history[0][HISTORY_SIZE], sizeof(*history[0])*HISTORY_SIZE);
 	history_length[0] = HISTORY_SIZE;
 
-	//printf("history_flush()\n");
+	printf("history_flush()\n");
 	pthread_mutex_unlock(&history_mutex);
 	return;
 }
@@ -176,17 +210,19 @@ cb_expose (GtkWidget      *area,
 	height	= gdk_window_get_height (event->window);
 
 	cairo_rectangle(cr, 0, 0, width, height);
-	cairo_set_source_rgb(cr, 1, 1, 1);
+	cairo_set_source_rgb(cr, 0, 0, 0);
 	cairo_fill(cr);
 
 	cairo_set_line_width (cr, 2);
 
 	int history_end   = history_length[0]-2;
 
-	if (history_end >= HISTORY_SIZE) {
+	if (history_end >= (double)HISTORY_SIZE*x_userdiv) {
 		//printf("%u %u\n", HISTORY_SIZE, history_end);
 		pthread_mutex_lock(&history_mutex);
-		cairo_set_source_rgba (cr, 0, 0, 0.3, 0.8);
+		//cairo_set_source_rgba (cr, 0, 0, 0.3, 0.8);
+		//cairo_set_source_rgba (cr, 0.8, 0.8, 1, 0.8);
+		cairo_set_source_rgba (cr, 0.1, 1, 0.1, 0.8);
 		cairo_move_to(cr, -1, height/2);
 		int x;
 		int y;
@@ -198,7 +234,7 @@ cb_expose (GtkWidget      *area,
 		if (history_start == 0)
 			history_start++;
 
-		printf("h: %u %u\n", history_start, history_end);
+		//printf("h: %u %u\n", history_start, history_end);
 
 		char found;
 
@@ -255,7 +291,7 @@ cb_expose (GtkWidget      *area,
 			history_end = history_end_initial;
 		}
 
-		printf("H: %u %u\n", history_start, history_end);
+		//printf("H: %u %u\n", history_start, history_end);
 
 		uint64_t timestamp_start = history[0][history_start].timestamp;
 		uint64_t timestamp_end   = history[0][history_end  ].timestamp;
@@ -282,7 +318,8 @@ cb_expose (GtkWidget      *area,
 		pthread_mutex_unlock(&history_mutex);
 	}
 
-	cairo_set_source_rgba (cr, 0, 0, 0, 0.2);
+	//cairo_set_source_rgba (cr, 0, 0, 0, 0.2);
+	cairo_set_source_rgba (cr, 1, 1, 1, 0.2);
 	cairo_move_to(cr, 0,	 height/2);
 	cairo_line_to(cr, width, height/2);
 	cairo_stroke(cr);
@@ -311,11 +348,36 @@ main (int    argc,
 	int i;
 	pthread_t thread_fetcher;
 	pthread_t thread_autoupdate;
+	char *dumppath = NULL;
+	char tailonly = 0;
 	//sensor_open();
+
+	// Parsing arguments
+	char c;
+	while ((c = getopt (argc, argv, "i:tf")) != -1) {
+		char *arg;
+		arg = optarg;
+
+		switch (c)
+		{
+			case 'i':
+				dumppath = arg;
+				break;
+			case 't':
+				tailonly = 1;
+				break;
+			default:
+				abort ();
+		}
+	}
+
+
 	i = 0;
 	while (i < MAX_CHANNELS)
 		history[i++] = xcalloc(HISTORY_SIZE * 2 + 1, sizeof(history_t));
-	dump_open();
+
+
+	dump_open(dumppath, tailonly);
 
 	if (pthread_create(&thread_fetcher, NULL, history_fetcher, NULL)) {
 		fprintf(stderr, "Error creating thread\n");