From b5af4d6a8644fd4aee5f04efeb7010af6495dcad Mon Sep 17 00:00:00 2001 From: Bartosz Kostrzewa Date: Wed, 17 Nov 2010 12:38:53 +0100 Subject: [PATCH] improvements to console and how the frames are stored --- clients/plasma.py | 23 +++---- frameserver/Server.cpp | 141 +++++++++++++++++++++++++++++++++-------- frameserver/Server.h | 9 ++- frameserver/defines.h | 14 +++- 4 files changed, 141 insertions(+), 46 deletions(-) diff --git a/clients/plasma.py b/clients/plasma.py index 2495afa..ff9d12f 100644 --- a/clients/plasma.py +++ b/clients/plasma.py @@ -26,33 +26,26 @@ segments = open('segments','r') alpha = 1 -z_buffer = "1" + "\n" +z_buffer = chr(1) + "\n" width = 7 height = 12 # Send messages -sleeptime = 0.04 +sleeptime = 0.03 t = 0 -frequency = 2*pi/40 +frequency = 2*pi/200 +max = 0.0 + while (1): #zero out the data buffer data = z_buffer for i in range(0,width): for j in range(0,height): - pixel = sin(1.5*pi*(float(i)/width)+t*frequency)*sin(1.5*pi*(float(j)/height)+t*frequency) - if pixel < 0.25: - pixel = '.' - elif pixel < 0.5: - pixel = '-' - elif pixel < 0.75: - pixel = '+' - elif pixel <= 1.0: - pixel = '#' - else: - pixel = '*' - data = data + pixel + str(alpha) + pixel = fabs(sin(2*pi*(float(i)/width)+t*frequency)*sin(2*pi*(float(j)/height)+t*frequency)) + data = data + chr(int(254*pixel)) + chr(alpha) data = data + "\n" t+=1 + #print( data ) #data += segments.read() if not data: break diff --git a/frameserver/Server.cpp b/frameserver/Server.cpp index c7f2cf7..6df2f64 100644 --- a/frameserver/Server.cpp +++ b/frameserver/Server.cpp @@ -6,6 +6,8 @@ Server::Server(int _port ) displaycounter = 0; packetcounter = 0; consoleinit = false; + mode = FRAME; + //test(); } Server::~Server() @@ -50,7 +52,7 @@ void Server::listen() while (1) { // creating the buffer each time is faster than zeroing it out - boost::array recv_buf; + boost::array recv_buf; udp::endpoint remote_endpoint; boost::system::error_code error; @@ -95,14 +97,6 @@ void Server::listen() continue; } - if( packetcounter % 10000 == 0 ) - { - //cout << endl << "packets received " << packetcounter << endl; - /*cout << remote_endpoint << endl; - for(int i = 0; i < BUFLEN; i++) - cout << recv_buf[i]; - cout << endl;//*/ - } packetcounter++; frame.z = recv_buf[0]; @@ -113,6 +107,7 @@ void Server::listen() for(int a = 0; a < CHANNELS; a++) { frame.windows[i][j][a] = recv_buf[HEADEROFFSET+ i*(CHANNELS*WIDTH+1) + j*CHANNELS + a]; + //cout << int( recv_buf[HEADEROFFSET+ i*(CHANNELS*WIDTH+1) + j*CHANNELS + a] ) << endl; } } } @@ -227,20 +222,20 @@ void Server::mix() } } - /*if( counter % 100 == 0 && x == size-1 ) + + /* if( counter % 100 == 0 && x == size-1 ) { - cout << counter << endl; for(int i = 0; i < HEIGHT; i++) { for(int j = 0; j < WIDTH; j++) { - cout << frame.windows[i][j][0]; + cout << brtoc(frame.windows[i][j][0]); } - cout << endl; + //cout << endl; } - cout << endl; + //cout << endl; - for(int w = 0; w < SEGWIDTH; w++) + /*for(int w = 0; w < SEGWIDTH; w++) { for(int n = 0; n < SEGNUM; n++) { @@ -272,14 +267,18 @@ void Server::console() { { // we'll be accessing some data to provide statistics, lock the Server - Glib::Mutex::Lock lock(mutex_); - mvprintw(0,0,"Clients\t %d \t| F1 Frame | F2 Stats | F3 Clients | input: %d ", buffers.size(),console_input ); - switch(console_input) + Glib::Mutex::Lock lock(mutex_); + mvprintw(0,0,"Clients %d | F2 Frame | F3 Values | F4 Stats | F5 Clients | input: %d ", buffers.size(),console_input ); + switch(mode) { - case KEY_F(1): - console_printframe(); + case FRAME: + console_printframe(frame); + break; + case FRAME_VALUES: + console_printframe_values(frame); + break; default: - console_printframe(); + console_printframe(frame); } refresh(); /* Print it on to the real screen */ } @@ -299,36 +298,59 @@ void Server::input() // now we need to lock data structure because we're going to use shared objects { Glib::Mutex::Lock lock(mutex_); - console_input = c; + switch(c) + { + case KEY_F(2): + mode = FRAME; + clear(); + break; + case KEY_F(3): + mode = FRAME_VALUES; + clear(); + break; + default: + console_input = c; + } } - } } /* the console functions should only be used in the console thread, they don't * implement their own locking and they need ncurses to be initialised */ -void Server::console_printframe() +void Server::console_printframe(frame_t _frame) { // output the current screen contents for(int i = 0; i < HEIGHT; i++) { for(int j = 0; j < WIDTH; j++) { - mvprintw(i+2,j,"%c",frame.windows[i][j][0]); + mvprintw(i+2,j,"%c", brtoc(_frame.windows[i][j][0]) ); } } + //TODO print a nicer 7 segment display with colours and brightness for(int w = 0; w < SEGWIDTH; w++) { for(int n = 0; n < SEGNUM; n++) { for(int a = 0; a < SEGCHANNELS; a++) { - mvprintw(HEIGHT+3+w,(n*SEGCHANNELS)+a,"%c",frame.segments[w][n][a]); + mvprintw(HEIGHT+3+w,(n*SEGCHANNELS)+a,"%c", _frame.segments[w][n][a]); } } } - +} + +void Server::console_printframe_values(frame_t _frame) +{ + // output the current screen contents + for(int i = 0; i < HEIGHT; i++) + { + for(int j = 0; j < WIDTH; j++) + { + mvprintw(i*2+2,j*4," %d ", brtoi(_frame.windows[i][j][0]) ); + } + } } void Server::console_printstats() @@ -370,3 +392,68 @@ void Server::expire() usleep( 1000000 ); } } + +int Server::brtoi(unsigned char br) +{ + return (int)br; +} + +// converts a given brightness value (0-255) to a character +// relatively efficiently +char Server::brtoc(unsigned char br) +{ + static char chars[11] = " ._-~:+*#@"; + if(br > 124) + { + if( br > 202 ) + { + if( br > 228 ) + return chars[9]; + else + return chars[8]; + } + else + { + if( br > 176 ) + return chars[7]; + else + return chars[6]; + } + } + else + { + if( br > 72 ) + { + if( br > 124 ) + return chars[5]; + if( br > 98 ) + return chars[4]; + else + return chars[3]; + } + else + { + if( br > 46 ) + return chars[2]; + if( br > 20 ) + return chars[1]; + else + return chars[0]; + } + } +} + +// implementation dependent behaviour of unsigned char?! +void Server::test() +{ + char i = 0; + unsigned char j = 0; + while(1) + { + cout << int(i) << endl; + cout << int(j) << endl; + i--; + j = i; + usleep(20000); + } +} diff --git a/frameserver/Server.h b/frameserver/Server.h index f9f316b..f197fd5 100644 --- a/frameserver/Server.h +++ b/frameserver/Server.h @@ -32,7 +32,13 @@ public: private: void console(); void input(); - void console_printframe(); + void test(); + + char brtoc(unsigned char); + int brtoi(unsigned char); + + void console_printframe(frame_t _frame); + void console_printframe_values(frame_t _frame); void console_printstats(); void listen(); @@ -58,6 +64,7 @@ private: frame_t frame; int port; + char mode; }; #endif diff --git a/frameserver/defines.h b/frameserver/defines.h index a5d39ad..f48a639 100644 --- a/frameserver/defines.h +++ b/frameserver/defines.h @@ -26,11 +26,19 @@ // not used for simplicity //#define SEGHEIGHT 1 + + struct frame_t { - char z; - char windows[HEIGHT][WIDTH][CHANNELS]; - char segments[SEGWIDTH][SEGNUM][SEGCHANNELS]; + unsigned char z; + unsigned char windows[HEIGHT][WIDTH][CHANNELS]; + unsigned char segments[SEGWIDTH][SEGNUM][SEGCHANNELS]; +}; + +enum modes +{ + FRAME, + FRAME_VALUES }; #endif