summaryrefslogtreecommitdiff
path: root/PIPET-1
diff options
context:
space:
mode:
Diffstat (limited to 'PIPET-1')
-rw-r--r--PIPET-1/main.c276
-rw-r--r--PIPET-1/pipet_view.c92
-rw-r--r--PIPET-1/pipet_view.h42
3 files changed, 408 insertions, 2 deletions
diff --git a/PIPET-1/main.c b/PIPET-1/main.c
index ff09eb6..45c9b43 100644
--- a/PIPET-1/main.c
+++ b/PIPET-1/main.c
@@ -2,14 +2,286 @@
// main.c
// PIPET-1
//
-// Created by dianshi on 4/1/20.
+// Created by dianshi on 3/28/20.
// Copyright © 2020 dianshi. All rights reserved.
//
#include <stdio.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <strings.h>
+#include <getopt.h>
+#include <signal.h>
+#include <fcntl.h>
+#include <pthread.h>
+
+#include <SDL2/SDL.h>
+#include <SDL2_ttf/SDL_ttf.h>
+
+#include "pipet_view.h"
+
+#define SCREEN_WIDTH 200
+#define SCREEN_HEIGHT 100
+
+
+typedef struct {
+ int f_output; //show statistics of pipe
+ int f_stopzero; //stop if 0 bytes in input
+ int f_nowriteout; // dont write to output
+ int f_graphmode; //show graph
+} pipet_params;
+
+typedef struct {
+ uint64_t cnt_in;
+ uint64_t prev_cnt_in;
+ uint64_t cnt_out;
+} pipet_stats;
+
+typedef struct {
+ int fd_in;
+ int fd_out;
+ pipet_stats *ps;
+} thread_params;
+
+//global_params
+static pipet_params g_params;
+static pipet_stats g_stats;
+static pipet_view g_view;
+static int quit=0;
+static SDL_Window *window = NULL;
+static SDL_Renderer *renderer = NULL;
+
+
+void helper(char *progname)
+{
+ printf("Usage: %s [OPTS]\n\n"
+ "-o show output stats\n"
+ "-s stop if there is 0 bytes in read\n"
+ "-n dont ouput input data\n"
+ "-g graphical interface\n"
+ "Version: 0.0.2 \n"
+ "\n"
+ , progname);
+}
+
+void sig_handler(int signo)
+{
+ switch(signo)
+ {
+ case SIGINT:
+ printf("Catch SIGINT Exit\n");
+ if (g_params.f_output)
+ {
+ printf("Data in %d bytes Data out %d bytes",g_stats.cnt_in,g_stats.cnt_out);
+ }
+ default:
+ printf("Unknown signal %d\n",signo);
+ }
+ exit(0);
+}
+
+void *thread_pipe(void *param)
+{
+ thread_params *tp = (thread_params *)param;
+ int cur_read=-1;
+ int cur_write=-1;
+ const uint64_t BUF_SZ=1024*4;
+ uint8_t buf[BUF_SZ];
+
+
+ printf("Start thread\n");
+
+ while (quit == 0)
+ {
+ cur_read = read(tp->fd_in,buf, BUF_SZ);
+ if (g_params.f_stopzero)
+ {
+ if (cur_read < 1)
+ {
+ quit = 1;
+ break;
+ }
+ }
+ g_stats.cnt_in += cur_read;// printf("Read %d\n", cur_read);
+
+ if (!g_params.f_nowriteout)
+ {
+ cur_write = write(tp->fd_out, buf, cur_read);
+ g_stats.cnt_out += cur_write;
+ }
+ }
+
+ //exit(0);
+ pthread_exit(NULL);
+
+}
int main(int argc, const char * argv[]) {
+ int c;
+ int rc;
+
+ int fd_stdin=-1,fd_stdout=-1;
+ int cur_read,cur_write;
+ int prev_val=0;
+
+ pthread_t thread;
+ SDL_Event event;
+
+ const uint64_t BUF_SZ=1024*4;
+ uint8_t buf[BUF_SZ];
+
+ thread_params tp;
+
// insert code here...
- printf("Hello, World!\n");
+
+ memset(&g_params,0,sizeof(pipet_params));
+ memset(&g_stats,0,sizeof(pipet_stats));
+ memset(&g_view,0,sizeof(pipet_view));
+ memset(&tp,0,sizeof(thread_params));
+
+
+ pv_init(&g_view, 200, SCREEN_WIDTH, SCREEN_HEIGHT);
+
+ fd_stdin = fileno(stdin);
+ fd_stdout = fileno(stdout);
+
+ //int flags = fcntl(fd_stdin, F_GETFL, 0);
+ //fcntl(fd_stdin, F_SETFL, flags | O_NONBLOCK);
+
+
+ if (signal(SIGINT,sig_handler) == SIG_ERR)
+ {
+ printf("cannot register signal handler\n");
+ exit(1);
+ }
+
+ //process arguments
+ while ((c = getopt(argc, argv, "osng")) != -1)
+ {
+ switch (c)
+ {
+ case 'o':
+ g_params.f_output = 1;
+ break;
+ case 's':
+ g_params.f_stopzero = 1;
+ break;
+ case 'n':
+ g_params.f_nowriteout = 1;
+ break;
+ case 'g':
+ g_params.f_graphmode = 1;
+ break;
+ default:
+ helper(argv[0]);
+ exit(1);
+ }
+ }
+
+ if ( argc < 2 )
+ {
+ helper(argv[0]);
+ exit(1);
+ }
+
+ //start graph
+ if (g_params.f_graphmode)
+ {
+ printf("Start thread\n");
+ SDL_Init(SDL_INIT_VIDEO);
+ window = SDL_CreateWindow("PIPET-1", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN|SDL_WINDOW_OPENGL);
+ renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
+ SDL_SetRenderDrawColor(renderer, 0xff, 0x99, 0x99, 0x99);
+
+
+ //int rc = pthread_create(&thread, NULL, thread_graph, NULL);
+ //if (rc)
+ {
+ // printf("Cannot start thread\n");
+ }
+ }
+
+ tp.fd_in = dup(fd_stdin);
+ tp.fd_out = dup(fd_stdout);
+
+ rc = pthread_create(&thread, NULL, thread_pipe, &tp);
+ if (rc)
+ {
+ printf("Cannot lunch pipe thread\n");
+ }
+
+ //main loop
+ int i=0;
+ while (quit == 0)
+ {
+ i+=10;
+ /*
+ //printf("!!!\n");
+ cur_read = read(fd_stdin,buf, BUF_SZ);
+ if (g_params.f_stopzero)
+ {
+ if (cur_read < 1)
+ {
+ quit = 1;
+ break;
+ }
+ }
+ g_stats.cnt_in += cur_read;// printf("Read %d\n", cur_read);
+
+ if (!g_params.f_nowriteout)
+ {
+ cur_write = write(fd_stdout, buf, cur_read);
+ g_stats.cnt_out += cur_write;
+ }
+ */
+
+ //sdl thread for now its here
+ if (g_params.f_graphmode)
+ {
+ //printf("SDL loop\n");
+
+ while (SDL_PollEvent(&event))
+ {
+
+ switch (event.type)
+ {
+ case SDL_QUIT:
+ {
+ quit = 1;
+ break;
+ }
+ //case SDL_KE
+ }
+
+ //sleep(1);
+ }
+ SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
+ SDL_RenderClear(renderer);
+ pv_render(&g_view, renderer);
+ SDL_RenderPresent(renderer);
+
+ pv_add_dot(&g_view, g_stats.cnt_in-g_stats.prev_cnt_in);
+ g_stats.prev_cnt_in = g_stats.cnt_in;
+
+ }
+ sleep(1);
+
+ }
+
+ if (g_params.f_output)
+ {
+ printf("Data in %d bytes Data out %d bytes\n", g_stats.cnt_in, g_stats.cnt_out);
+ }
+
+ if (g_params.f_graphmode)
+ {
+ SDL_DestroyRenderer(renderer);
+ SDL_DestroyWindow(window);
+ SDL_Quit();
+ }
+
+ //pthread_join(&thread,NULL);
+ //pthread_exit(NULL);
return 0;
}
diff --git a/PIPET-1/pipet_view.c b/PIPET-1/pipet_view.c
new file mode 100644
index 0000000..2bb1380
--- /dev/null
+++ b/PIPET-1/pipet_view.c
@@ -0,0 +1,92 @@
+//
+// pipet_view.c
+// PIPET-1
+//
+// Created by dianshi on 3/28/20.
+// Copyright © 2020 dianshi. All rights reserved.
+//
+
+#include "pipet_view.h"
+
+int pv_init(pipet_view *pv, int32_t max_dots, int32_t w, int32_t h)
+{
+ pv->dots_size = max_dots;
+
+ pv->dots = malloc(sizeof(uint64_t)*max_dots);
+ memset(pv->dots,0,sizeof(uint64_t)*max_dots);
+
+ pv->dots_relative = malloc(sizeof(float)*max_dots);
+ memset(pv->dots_relative,0,sizeof(float)*max_dots);
+
+ pv->width = w;
+ pv->height = h;
+ pv->cur_dot = 0;
+
+ return 0;
+}
+
+int pv_set_color(pipet_view *pv, int32_t color)
+{
+ pv->color = color;
+
+ return 0;
+}
+
+int pv_add_dot(pipet_view *pv, uint64_t val)
+{
+ int i=0;
+ float f = 0.0f;
+
+ pv->dots[pv->cur_dot] = val;
+ pv->cur_dot = (pv->cur_dot+1)%pv->dots_size;
+
+ if (val > pv->max_dot_value)
+ {
+ pv->max_dot_value = val;
+ //printf("New max value %d\n",val);
+ }
+
+ for (i=0;i<pv->dots_size;i++)
+ {
+ f = 1.0f*pv->dots[i]/pv->max_dot_value;
+
+ //pv->dots_relative[i] = (uint8_t)(f*255);
+ pv->dots_relative[i] = f;
+ //printf("%.3f %f (%d-%d) ",f, pv->dots_relative[i], pv->dots[i], pv->max_dot_value);
+
+ }
+ printf("Max:%d K Val:%d K \n",pv->max_dot_value/1024,val/1024);
+
+ return 0;
+}
+
+
+int pv_destroy(pipet_view *pv)
+{
+ free(pv->dots);
+ free(pv->dots_relative);
+ return 0;
+}
+
+int pv_draw_texture(pipet_view *pv, SDL_Renderer *renderer, int x, int y)
+{
+ SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
+ SDL_RenderDrawPoint(renderer,x,y);
+ return 0;
+}
+
+
+int pv_render(pipet_view *pv, SDL_Renderer *renderer)
+{
+ int i;
+ float f=1.0f*pv->width/pv->dots_size;
+ float fx = 0.0f;
+
+ for (i=0;i<pv->width;i++)
+ {
+ fx+=f;
+ pv_draw_texture(pv, renderer, fx, (pv->height-pv->height*pv->dots_relative[i]-1));
+ }
+
+ return 0;
+}
diff --git a/PIPET-1/pipet_view.h b/PIPET-1/pipet_view.h
new file mode 100644
index 0000000..1685ac1
--- /dev/null
+++ b/PIPET-1/pipet_view.h
@@ -0,0 +1,42 @@
+//
+// pipet_view.h
+// PIPET-1
+//
+// Created by dianshi on 3/28/20.
+// Copyright © 2020 dianshi. All rights reserved.
+//
+
+#ifndef pipet_view_h
+#define pipet_view_h
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+
+#include <SDL2/SDL.h>
+
+typedef struct pipet_view {
+ int32_t width;
+ int32_t height;
+ int32_t delay;
+ int32_t color; //rgb
+ int64_t max_dot_value;
+ int64_t last_max_dot;
+ float *dots_relative; //buf of dots values relative
+ uint64_t *dots;
+ int32_t cur_dot;
+ int32_t dots_size;
+} pipet_view;
+
+int pv_init(pipet_view *pv, int32_t max_dots, int32_t w, int32_t h);
+int pv_set_color(pipet_view *pv, int32_t color);
+int pv_add_dot(pipet_view *pv, uint64_t val);
+int pv_destroy(pipet_view *pv);
+
+
+int pv_draw_texture(pipet_view *pv, SDL_Renderer *renderer, int x, int y);
+int pv_render(pipet_view *pv, SDL_Renderer *renderer);
+
+
+
+#endif /* pipet_view_h */