o_gnuplot.cpp 4.89 KB
#include "blc_channel.h"
#include "blc_program.h"
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <sys/time.h>
#include "graph.h"
#include <deque> //We use deque instread of vector to warantly  address of objects

using namespace std;

deque <blc_channel> inputs;
blc_array history_array;

char const *with_option, *style_option;

int sx, sy;
int sampling_period;
int interactive_mode=0;
int final_columns_nb, final_rows_nb;
int offset=0;
int bands=1;
int dims_nb, *lengths;
double ymin, ymax;
enum {RUN, PAUSE};
int status=RUN;

void refresh_period_cb(char *argument, void*){
    sampling_period=strtol(argument, NULL, 10)*1000;
}

int main(int argc, char *argv[]){
    char const *refresh_string, *ymin_str, *ymax_str, *history_str, *sample_string, *verbatim;
    char **channel_names;
    char const *xmin_str, *xmax_str, *label_max_str;
    float xmin, xmax, lmax;
    int status=0, total_length;
    uint32_t type;
    int dims_nb;
    
    blc_program_set_description("Display the content of the blc_channel depending on its type and format");
    blc_program_add_option(&ymin_str, 'm', "min", "FL32", "minimal value", NULL);
    blc_program_add_option(&ymax_str, 'M', "max", "FL32", "maximal value", NULL);
    blc_program_add_option(&sample_string, 'p', "period", "UI32", "sampling period in ms or history", "10");
    blc_program_add_option(&style_option, 's', "style", "string", "gnuplot option set option (style, boxwidth,  ...)", "fill solid");
    blc_program_add_option(&verbatim, 't', "text", "string", "text directly put on the gnuplot set commandline", NULL);
    blc_program_add_option(&with_option, 'w', "with", "string", "gnuplot option with (lines, boxes, dots,...)", "lines");
    blc_program_add_option(&xmin_str, 'x', "xmin", "FL32", "minimal abscissa value", NULL);
    blc_program_add_option(&xmax_str, 'X', "xmax", "FL32", "maximal abscissa value", NULL);
    blc_program_add_option(&label_max_str, 'L', "label-max", "FL32", "display the absisca scale in order to have the last value as label-max ", NULL);
    blc_program_add_option(&history_str, 'H', "history", "UI32", "size of the history", NULL);
    blc_program_add_option(&refresh_string, 'P', "period", "UI32", "graph refresh period in ms", "100");
    blc_program_add_multiple_parameters(&channel_names, "blc_channel", -1, "channel you want to graph");
     
    blc_program_init(&argc, &argv, blc_quit);
    blc_command_forward_blc_channels();
    
    for (char *const*channel_name_pt = channel_names; *channel_name_pt!=nullptr; channel_name_pt++){
        inputs.emplace_back(*channel_name_pt, BLC_CHANNEL_READ);
        if (inputs.size()==1){ //First time
            type=inputs.back().type;
            dims_nb=inputs.back().dims_nb;
            total_length=inputs.back().total_length;
        }
        else{
            if (type!=inputs.back().type) EXIT_ON_ERROR("Type of different channels must be the same");
            if (total_length!=inputs.back().total_length) EXIT_ON_ERROR("Total_leength of different channels must be the same");
            if (dims_nb!=inputs.back().dims_nb) EXIT_ON_ERROR("Number of dims of different channels must be the same");
        }
    }
    
    if (ymin_str) ymin=strtof(ymin_str, NULL);
    else switch (type){
        case 'UIN8':case 'UI16':case 'UI32':case 'IN16':case 'IN32':case 'IN64':case 'FL32':case'FL64':ymin=0.0;break;
        case 'INT8':ymin=INT8_MIN;break;
        default: EXIT_ON_ERROR( "No default min value for type");
    }
    

    if (ymax_str) ymax=strtof(ymax_str, NULL);
    else switch (type){
        case 'UIN8':ymax=UINT8_MAX;break;
        case 'INT8':ymax=INT8_MAX;break;
        case 'UI16':ymax=UINT16_MAX;break;
        case 'IN16':ymax=INT16_MAX;break;
        case 'UI32':ymax=UINT32_MAX;break;
        case 'IN32':ymax=INT32_MAX;break;
        case 'FL32': case 'FL64':ymax=1.0;break;
        default: EXIT_ON_ERROR( "No default max value");
    }
    
    if (xmin_str) SSCANF(1, xmin_str, "%f", &xmin);
    else xmin=0;
    
    if (xmax_str) SSCANF(1, xmax_str, "%f", &xmax);
    else xmax=total_length;
    
    if (label_max_str) {
        SSCANF(1, label_max_str, "%f", &lmax);
        if (xmax_str==NULL) xmax=lmax; //We still want to see all the graph
    }
    else lmax=total_length;
    
    sampling_period=strtol(refresh_string, NULL, 10)*1000;
    
    fprintf(stderr, "=== %s: ", blc_program_name);
    if (history_str) fprintf(stderr, "sample period (%.3fms), history(%s), ",  sampling_period/1000.f, history_str);
    fprintf(stderr, "min(%.3lf), max(%.2lf) === \n", ymin, ymax);
    
    blc_command_add("p", refresh_period_cb,  "ms", "sampling period", NULL);
    
    if (history_str) create_history_graph(inputs, (char const*)inputs[0].name+1, strtol(history_str, NULL, 10), sampling_period, strtol(sample_string, NULL, 10)*1000, ymin, ymax, verbatim);
    else create_graph(inputs, inputs[0].name+1, sampling_period, ymin, ymax, xmin, xmax, lmax, verbatim);
    
    return (status);
}