diff options
-rw-r--r-- | PIPET-1.xcodeproj/project.pbxproj | 30 | ||||
-rw-r--r-- | PIPET-1/main.c | 276 | ||||
-rw-r--r-- | PIPET-1/pipet_view.c | 92 | ||||
-rw-r--r-- | PIPET-1/pipet_view.h | 42 |
4 files changed, 438 insertions, 2 deletions
diff --git a/PIPET-1.xcodeproj/project.pbxproj b/PIPET-1.xcodeproj/project.pbxproj index b6e3f57..7c1c6d5 100644 --- a/PIPET-1.xcodeproj/project.pbxproj +++ b/PIPET-1.xcodeproj/project.pbxproj @@ -8,6 +8,8 @@ /* Begin PBXBuildFile section */ A94B89502434F70400B7DC09 /* main.c in Sources */ = {isa = PBXBuildFile; fileRef = A94B894F2434F70400B7DC09 /* main.c */; }; + A94B89582434F73800B7DC09 /* pipet_view.c in Sources */ = {isa = PBXBuildFile; fileRef = A94B89572434F73800B7DC09 /* pipet_view.c */; }; + A94B895D2434F76800B7DC09 /* SDL2.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A94B895C2434F76800B7DC09 /* SDL2.framework */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -25,6 +27,11 @@ /* Begin PBXFileReference section */ A94B894C2434F70400B7DC09 /* PIPET-1 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "PIPET-1"; sourceTree = BUILT_PRODUCTS_DIR; }; A94B894F2434F70400B7DC09 /* main.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = main.c; sourceTree = "<group>"; }; + A94B89562434F73800B7DC09 /* pipet_view.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = pipet_view.h; sourceTree = "<group>"; }; + A94B89572434F73800B7DC09 /* pipet_view.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = pipet_view.c; sourceTree = "<group>"; }; + A94B895A2434F75400B7DC09 /* Library */ = {isa = PBXFileReference; lastKnownFileType = folder; name = Library; path = ../../../../../Library; sourceTree = "<group>"; }; + A94B895C2434F76800B7DC09 /* SDL2.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SDL2.framework; path = ../../../../../Library/Frameworks/SDL2.framework; sourceTree = "<group>"; }; + A94B895E2434F77C00B7DC09 /* SDL2_ttf.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SDL2_ttf.framework; path = ../../../../../Library/Frameworks/SDL2_ttf.framework; sourceTree = "<group>"; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -32,6 +39,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + A94B895D2434F76800B7DC09 /* SDL2.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -43,6 +51,7 @@ children = ( A94B894E2434F70400B7DC09 /* PIPET-1 */, A94B894D2434F70400B7DC09 /* Products */, + A94B89592434F75400B7DC09 /* Frameworks */, ); sourceTree = "<group>"; }; @@ -57,11 +66,23 @@ A94B894E2434F70400B7DC09 /* PIPET-1 */ = { isa = PBXGroup; children = ( + A94B89572434F73800B7DC09 /* pipet_view.c */, + A94B89562434F73800B7DC09 /* pipet_view.h */, A94B894F2434F70400B7DC09 /* main.c */, ); path = "PIPET-1"; sourceTree = "<group>"; }; + A94B89592434F75400B7DC09 /* Frameworks */ = { + isa = PBXGroup; + children = ( + A94B895E2434F77C00B7DC09 /* SDL2_ttf.framework */, + A94B895C2434F76800B7DC09 /* SDL2.framework */, + A94B895A2434F75400B7DC09 /* Library */, + ); + name = Frameworks; + sourceTree = "<group>"; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -120,6 +141,7 @@ buildActionMask = 2147483647; files = ( A94B89502434F70400B7DC09 /* main.c in Sources */, + A94B89582434F73800B7DC09 /* pipet_view.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -239,6 +261,10 @@ isa = XCBuildConfiguration; buildSettings = { CODE_SIGN_STYLE = Automatic; + FRAMEWORK_SEARCH_PATHS = ( + "$(inherited)", + "$(LOCAL_LIBRARY_DIR)/Frameworks", + ); PRODUCT_NAME = "$(TARGET_NAME)"; }; name = Debug; @@ -247,6 +273,10 @@ isa = XCBuildConfiguration; buildSettings = { CODE_SIGN_STYLE = Automatic; + FRAMEWORK_SEARCH_PATHS = ( + "$(inherited)", + "$(LOCAL_LIBRARY_DIR)/Frameworks", + ); PRODUCT_NAME = "$(TARGET_NAME)"; }; name = Release; 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 */ |