OpenCores
URL https://opencores.org/ocsvn/riscv_vhdl/riscv_vhdl/trunk

Subversion Repositories riscv_vhdl

[/] [riscv_vhdl/] [trunk/] [debugger/] [src/] [gui_plugin/] [GnssWidgets/] [linecommon.cpp] - Blame information for rev 5

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 sergeykhbr
/**
2
 * @file
3
 * @copyright  Copyright 2017 GNSS Sensor Ltd. All right reserved.
4
 * @author     Sergey Khabarov - sergeykhbr@gmail.com
5
 * @brief      Plot's Line common class.
6
 */
7
 
8
#include "api_core.h"
9
#include "linecommon.h"
10
 
11
namespace debugger {
12
 
13
LineCommon::LineCommon(AttributeType &descr) {
14
    if (descr.is_dict()) {
15
        descr_ = descr;
16
    } else {
17
        descr_.make_dict();
18
        descr_["Name"].make_string("NoName");
19
        descr_["Format"].make_string("%.1f");
20
        descr_["RingLength"].make_int64(256);
21
        descr_["FixedMinY"].make_boolean(true);
22
        descr_["FixedMinYVal"].make_floating(0.0);
23
        descr_["FixedMaxY"].make_boolean(false);
24
        descr_["FixedMaxYVal"].make_floating(0.0);
25
        descr_["Color"].make_string("#FF0000");
26
    }
27
    strcpy(color_, descr_["Color"].to_string());
28
    strcpy(format_, descr_["Format"].to_string());
29
 
30
    plot_w = 0;
31
    plot_h = 0;
32
    dx = 0;
33
    dy = 0;
34
    sel_start_idx = 0;
35
    sel_cnt = 0;
36
 
37
    is_ring_ = false;
38
    len_ = 1024;
39
    cnt_ = 0;
40
    start_ = 0;
41
    if (descr_["RingLength"].to_uint32()) {
42
        is_ring_ = true;
43
        len_ = descr_["RingLength"].to_int();
44
    }
45
 
46
    for (int i = 0; i < 2; i++) {
47
        axis_[i].data = new double[len_];
48
        axis_[i].accum = 0;
49
        axis_[i].minVal = 0;
50
        axis_[i].maxVal = 0;
51
    }
52
}
53
 
54
const AttributeType &LineCommon::getDescription() {
55
    return descr_;
56
}
57
 
58
unsigned LineCommon::size() {
59
    return cnt_;
60
}
61
void LineCommon::append(double y) {
62
    append(size(), y);
63
}
64
 
65
void LineCommon::append(double x, double y) {
66
    if (cnt_ == 0) {
67
        axis_[0].minVal = x;
68
        axis_[0].maxVal = x;
69
        axis_[1].minVal = y;
70
        if (descr_["FixedMinY"].to_bool()) {
71
            axis_[1].minVal = descr_["FixedMinYVal"].to_float();
72
        }
73
        axis_[1].maxVal = y;
74
        if (descr_["FixedMaxY"].to_bool()) {
75
            axis_[1].maxVal = descr_["FixedMaxYVal"].to_float();
76
        }
77
    }
78
 
79
 
80
    int wridx;
81
    if (is_ring_) {
82
        if (cnt_ < len_) {
83
            wridx = cnt_;
84
        } else  {
85
            if (++start_ >= len_) {
86
                start_ = 0;
87
            }
88
            wridx = start_ - 1;
89
            if (wridx < 0) {
90
                wridx += len_;
91
            }
92
        }
93
    } else {
94
        if (cnt_ >= len_) {
95
            len_ *= 2;
96
            for (int i = 0; i < 2; i++) {
97
                double *tbuf = new double[len_];
98
                memcpy(tbuf, axis_[i].data, cnt_*sizeof(double));
99
                delete []axis_[i].data;
100
                axis_[i].data = tbuf;
101
            }
102
        }
103
        wridx = cnt_;
104
    }
105
    axis_[0].data[wridx] = x;
106
    axis_[1].data[wridx] = y;
107
    if (cnt_ < len_) {
108
        cnt_++;
109
    }
110
 
111
    // X-axis
112
    axis_[0].accum += x;
113
    if (x < axis_[0].minVal) {
114
        axis_[0].minVal = x;
115
    }
116
    if (x > axis_[0].maxVal) {
117
        axis_[0].maxVal = x;
118
    }
119
 
120
    // Y-axis
121
    axis_[1].accum += y;
122
    if (y < axis_[1].minVal && !descr_["FixedMinY"].to_bool()) {
123
        axis_[1].minVal = y;
124
    }
125
    if (y > axis_[1].maxVal && !descr_["FixedMaxY"].to_bool()) {
126
        axis_[1].maxVal = y;
127
    }
128
}
129
 
130
void LineCommon::setPlotSize(int w, int h) {
131
    plot_w = w;
132
    plot_h = h;
133
}
134
 
135
 
136
void LineCommon::selectData(int start_idx, int total) {
137
    sel_start_idx = start_idx;
138
    sel_cnt = start_idx;
139
    if (plot_w == 0 || plot_h == 0) {
140
        dx = 0;
141
        dy = 0;
142
        return;
143
    }
144
    if (total > 1) {
145
        dx = plot_w / static_cast<double>(total - 1);
146
    } else {
147
        dx = 0;
148
    }
149
    double diff_y = axis_[1].maxVal - axis_[1].minVal;
150
    if (diff_y) {
151
        dy = plot_h / diff_y;
152
    } else {
153
        dy = 0;
154
    }
155
}
156
 
157
bool LineCommon::getNext(int &x, int &y) {
158
    double val;
159
    if (!getAxisValue(1, sel_cnt, val)) {
160
        return false;
161
    }
162
    x = static_cast<int>((sel_cnt - sel_start_idx) * dx + 0.5);
163
    y = static_cast<int>((axis_[1].maxVal - val) * dy + 0.5);
164
    sel_cnt++;
165
    return true;
166
}
167
 
168
bool LineCommon::getXY(int idx, int &x, int &y) {
169
    double val;
170
    if (!getAxisValue(1, idx, val)) {
171
        return false;
172
    }
173
    x = static_cast<int>((idx - sel_start_idx) * dx + 0.5);
174
    y = static_cast<int>((axis_[1].maxVal - val) * dy + 0.5);
175
    return true;
176
}
177
 
178
int LineCommon::getNearestByX(int x) {
179
    if (x < 0 || dx == 0) {
180
        return sel_start_idx;
181
    }
182
    double idx = static_cast<double>(x) / dx;
183
    return sel_start_idx + static_cast<int>(idx + 0.5);
184
}
185
 
186
bool LineCommon::getAxisValue(int axis, int idx, double &outval) {
187
    if (idx >= 0 && idx < cnt_) {
188
        int n = (start_ + idx) % len_;
189
        outval = axis_[axis].data[n];
190
        return true;
191
    }
192
    return false;
193
}
194
 
195
bool LineCommon::getAxisValue(int axis, int idx, char *outbuf, size_t bufsz) {
196
    if (idx >= 0 && idx < cnt_) {
197
        int n = (start_ + idx) % len_;
198
        double outval = axis_[axis].data[n];
199
        RISCV_sprintf(outbuf, bufsz, format_, outval);
200
        return true;
201
    }
202
    return false;
203
}
204
 
205
void LineCommon::getAxisMin(int axis, char *outbuf, size_t bufsz) {
206
    RISCV_sprintf(outbuf, bufsz, format_, axis_[axis].minVal);
207
}
208
 
209
void LineCommon::getAxisMax(int axis, char *outbuf, size_t bufsz) {
210
    RISCV_sprintf(outbuf, bufsz, format_, axis_[axis].maxVal);
211
}
212
 
213
}  // namespace debugger

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.