-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphView.cpp
More file actions
55 lines (44 loc) · 1.63 KB
/
Copy pathGraphView.cpp
File metadata and controls
55 lines (44 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "widgets/chart/lv_chart.h"
#include "GraphView.hpp"
GraphView::GraphView(
std::shared_ptr<FPOscillator> osc,
uint16_t sampleCount,
uint32_t updateMs)
: m_osc(osc),
m_sampleCount(sampleCount),
m_updateMs(updateMs) {}
void GraphView::initChart(lv_obj_t* parent) {
// Create chart object
m_chart = lv_chart_create(parent);
lv_obj_set_size(m_chart, LV_PCT(100), LV_PCT(100));
lv_chart_set_type(m_chart, LV_CHART_TYPE_LINE);
lv_chart_set_update_mode(m_chart, LV_CHART_UPDATE_MODE_CIRCULAR);
lv_obj_set_style_size(m_chart, 0, 0, LV_PART_INDICATOR);
lv_chart_set_point_count(m_chart, m_sampleCount);
lv_chart_set_range(m_chart,
LV_CHART_AXIS_PRIMARY_Y,
0, 100);
// Series = actual waveform
m_series = lv_chart_add_series(m_chart,
lv_palette_main(LV_PALETTE_GREEN),
LV_CHART_AXIS_PRIMARY_Y);
// Initialize with baseline
for (int i = 0; i < m_sampleCount; i++)
m_series->y_points[i] = 50; // midline
}
void GraphView::update() {
float sample = m_osc->nextSample();
// clamp -1..1
if (sample < -1.0f) sample = -1.0f;
if (sample > 1.0f) sample = 1.0f;
// convert to 0..100
uint16_t y = (uint16_t)((sample + 1.0f) * 50.0f);
lv_chart_set_next_value(m_chart, m_series, y);
uint32_t p = lv_chart_get_point_count(m_chart);
uint32_t s = lv_chart_get_x_start_point(m_chart, m_series);
int32_t* a = lv_chart_get_series_y_array(m_chart, m_series);
a[(s + 1) % p] = LV_CHART_POINT_NONE;
a[(s + 2) % p] = LV_CHART_POINT_NONE;
a[(s + 2) % p] = LV_CHART_POINT_NONE;
//lv_chart_refresh(m_chart);
}