auto_plot.c 2.23 KB
Newer Older
1
#ifdef REAL_TIME
2
3

#include "auto_plot.h"
4
#include "useful_functions.h"
5
6
7
8
9

#include <stdlib.h>
#include <string.h>

/*! \brief initialize the AutoPlot structure
10
 *
11
12
13
14
15
16
 * \param[in] max_nb maximum number of curves to plot
 * \return AutoPlot structure initialized
 */
AutoPlot* init_auto_plot(int max_nb)
{
    int i;
17

18
19
20
    AutoPlot *auto_plot;

    // memory allocation
21
    auto_plot = (AutoPlot*)malloc(sizeof(AutoPlot));
22
23
24

    auto_plot->label_curves = get_char_tab(max_nb, 50);

25
    auto_plot->y_curves = (double*)malloc(max_nb * sizeof(double));
26

27
    for (i = 0; i < max_nb; i++)
28
29
30
31
32
33
34
35
36
37
38
39
40
    {
        auto_plot->y_curves[i] = 0.0;
    }

    auto_plot->nb = 0;
    auto_plot->max_nb = max_nb;

    auto_plot->nb_new_curves = 0;

    return auto_plot;
}

/*! \brief release memory for AutoPlot
41
 *
42
43
44
45
46
47
48
49
50
51
 * \param[out] auto_plot AutoPlot structure to free
 */
void free_auto_plot(AutoPlot *auto_plot)
{
    free_char_tab(auto_plot->label_curves);
    free(auto_plot->y_curves);
    free(auto_plot);
}

/*! \brief update auto_plot when a 'set_plot' is encountered
52
 *
53
54
55
56
 * \param[out] auto_plot AutoPlot structure
 * \param[in] value current curve value
 * \param[in] label current curve label
 */
57
void update_auto_plot(AutoPlot *auto_plot, double value, const char* label)
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
{
    int nb;
    int index;

    index = index_plot_string(auto_plot, label);
    nb = auto_plot->nb;

    // curve already known
    if (index >= 0)
    {
        auto_plot->y_curves[index] = value;
    }
    else // new curve
    {
        if (nb >= auto_plot->max_nb) // maximal number of curves already reached
        {
            return;
        }
        else // add a new curve
        {
78
            auto_plot->label_curves[nb] = (char*)label;
79
80
81
82
83
84
85
86
87
            auto_plot->y_curves[nb] = value;

            auto_plot->nb++;
            auto_plot->nb_new_curves++;
        }
    }
}

/*! \brief get index for a curve with its label
88
 *
89
90
91
92
 * \param[in] auto_plot AutoPlot structure
 * \param[in] label label of the curve
 * \return index of the curve, -1 if not known
 */
93
int index_plot_string(AutoPlot *auto_plot, const char* label)
94
95
96
{
    int i;

97
    for (i = 0; i < auto_plot->nb; i++)
98
99
100
101
102
103
104
105
106
107
    {
        if (strcmp(label, auto_plot->label_curves[i]) == 0)
        {
            return i;
        }
    }

    return -1;
}

108

109
#endif