Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit 0dc9d88

Browse files
committed
make linechart work with rpcWiFi.
1 parent a31134d commit 0dc9d88

File tree

3 files changed

+44
-46
lines changed

3 files changed

+44
-46
lines changed

examples/basic/basic.ino

+1-27
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@ int brightness;
88

99
void setup()
1010
{
11-
Serial.begin(115200);
12-
while (!Serial)
13-
{
14-
/* code */
15-
}
16-
delay(1000);
17-
1811
pinMode(A0, INPUT);
1912
tft.begin();
2013
tft.setRotation(3);
@@ -25,23 +18,13 @@ void loop()
2518
{
2619
brightness = analogRead(A0);
2720

28-
if (data.size() == MAX_SIZE)
21+
if (data.size() > MAX_SIZE) // keep the old line chart front
2922
{
3023
data.pop(); // this is used to remove the first read variable
3124
}
3225

3326
data.push(brightness); // read variables and store in data
3427

35-
36-
Serial.print("AAAAAAAA: ");
37-
for(int i =0; i < data.size();i++)
38-
{
39-
Serial.printf("%f ", data.front());
40-
data.push(data.front());
41-
data.pop();
42-
}
43-
Serial.println();
44-
4528
// Settings for the line graph title
4629
auto header = text(0, 0)
4730
.value("Light Sensor Readings")
@@ -62,18 +45,9 @@ void loop()
6245
.show_circle(false) // drawing a cirle at each point, default is on.
6346
.value(data) // passing through the data to line graph
6447
.max_size(MAX_SIZE)
65-
.show_circle(true)
6648
.color(TFT_RED) // Setting the color for the line
6749
.backgroud(TFT_WHITE) // Setting the color for the backgroud
6850
.draw(&tft);
6951

70-
Serial.print("BBBBBBBB: ");
71-
for(int i =0; i < data.size();i++)
72-
{
73-
Serial.printf("%f ", data.front());
74-
data.push(data.front());
75-
data.pop();
76-
}
77-
Serial.println();
7852
delay(200);
7953
}

examples/basic_spr/basic_spr.ino

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
TFT_eSPI tft;
44
TFT_eSprite spr = TFT_eSprite(&tft); // Sprite
55

6-
#define max_size 30 // maximum size of data
6+
#define MAX_SIZE 30 // maximum size of data
77
doubles data; // Initilising a doubles type to store data
88
int brightness;
99

@@ -21,7 +21,7 @@ void loop()
2121
spr.fillSprite(TFT_WHITE);
2222
brightness = analogRead(A0);
2323

24-
if (data.size() == max_size)
24+
if (data.size() > MAX_SIZE)
2525
{
2626
data.pop(); // this is used to remove the first read variable
2727
}
@@ -46,7 +46,9 @@ void loop()
4646
.based_on(0.0) // Starting point of y-axis, must be a float
4747
.show_circle(false) // drawing a cirle at each point, default is on.
4848
.value(data) // passing through the data to line graph
49+
.max_size(MAX_SIZE)
4950
.color(TFT_RED) // Setting the color for the line
51+
.backgroud(TFT_WHITE)
5052
.draw(&spr);
5153

5254
spr.pushSprite(0, 0);

seeed_line_chart.h

+39-17
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class range
1010
size_t max_count;
1111

1212
template <class type>
13-
range(type &list, int skip = 0)
13+
range(type &list, size_t max_size = 0, size_t cut = 0)
1414
{
1515
max_value = 0;
1616
min_value = 0;
@@ -21,26 +21,44 @@ class range
2121
return;
2222
}
2323

24-
min_value = max_value = list[0].front();
24+
size_t skip = list[0].size() > max_size ? list[0].size() - max_size : 0;
25+
26+
for (size_t i = 0; i < list[0].size(); i++)
27+
{
28+
auto value = list[0].front();
29+
list[0].push(value);
30+
list[0].pop();
31+
if(i == skip)
32+
{
33+
min_value = max_value = value;
34+
}
35+
}
2536

2637
for (size_t i = 0; i < list.size(); i++)
2738
{
28-
if (max_count < list[i].size())
39+
size_t skip = list[i].size() > max_size ? list[i].size() - max_size : 0;
40+
size_t size = list[i].size() - skip - cut;
41+
if (max_count < size)
2942
{
30-
max_count = list[i].size() - skip;
43+
max_count = size;
3144
}
3245
for (size_t j = 0; j < list[i].size(); j++)
3346
{
34-
if (max_value < list[i].front() && (j != max_count))
47+
auto value = list[i].front();
48+
list[i].push(value);
49+
list[i].pop();
50+
if (j < skip)
3551
{
36-
max_value = list[i].front();
52+
continue;
3753
}
38-
else if (min_value > list[i].front() && (j != max_count))
54+
if ((max_value < value) && (j < list[i].size() - cut))
3955
{
40-
min_value = list[i].front();
56+
max_value = value;
57+
}
58+
else if ((min_value > value) && (j < list[i].size() - cut))
59+
{
60+
min_value = value;
4161
}
42-
list[i].push(list[i].front());
43-
list[i].pop();
4462
}
4563
}
4664
}
@@ -298,11 +316,11 @@ struct line_chart
298316

299317
void clear(TFT_eSPI *canvans)
300318
{
301-
//·绘制y轴/x轴
319+
//·绘制y轴/x轴
302320
//·绘制x轴刻度
303321
auto y_tick_value_template = text().origin(right).vorigin(vcenter);
304322
auto x_tick_value_template = text().origin(center).vorigin(top);
305-
auto r = range(_value, 1);
323+
auto r = range(_value, _max_size + 1, 1);
306324
auto m = match_tick(r.max_value, r.min_value, _based_on, _y_max_tick_count, _y_min_tick_count);
307325
auto w = pix_t(0);
308326
auto max_y_tick_pix_width = 0;
@@ -416,7 +434,7 @@ struct line_chart
416434
}
417435
}
418436
}
419-
line(x_start, x_end).color(_backgroud).thickness(_x_role_thickness).draw(canvans);
437+
//line(x_start, x_end).color(_backgroud).thickness(_x_role_thickness).draw(canvans);
420438
line(y_start, y_end).color(_backgroud).thickness(_y_role_thickness).draw(canvans);
421439
}
422440

@@ -428,7 +446,7 @@ struct line_chart
428446
//·绘制x轴刻度
429447
auto y_tick_value_template = text().origin(right).vorigin(vcenter);
430448
auto x_tick_value_template = text().origin(center).vorigin(top);
431-
auto r = range(_value);
449+
auto r = range(_value, _max_size);
432450
auto m = match_tick(r.max_value, r.min_value, _based_on, _y_max_tick_count, _y_min_tick_count);
433451
auto w = pix_t(0);
434452
auto max_y_tick_pix_width = 0;
@@ -516,17 +534,21 @@ struct line_chart
516534
auto default_color = _color[std::min(i, _color.size() - 1)];
517535
auto show_circle = _show_circle[std::min(i, _show_circle.size() - 1)];
518536
std::vector<point> value_point;
537+
size_t skip = cur.size() > _max_size ? cur.size() - _max_size : 0;
519538
for (int j = 0; j < cur.size() - 1; j++)
520539
{
521540
cur.push(cur.front());
522541
auto a = cur.front();
523542
cur.pop();
524-
543+
if (j < skip)
544+
{
545+
continue;
546+
}
525547
auto b = cur.front();
526548
auto ha = pos_t(round((a - m.start_value) / m.abs_value * height));
527549
auto hb = pos_t(round((b - m.start_value) / m.abs_value * height));
528-
auto pa = origin(pos_t((j + x_skip_tick) * x_step + x_offset), -(pos_t)ha);
529-
auto pb = origin(pos_t((j + x_skip_tick + 1) * x_step + x_offset), -(pos_t)hb);
550+
auto pa = origin(pos_t((j - skip + x_skip_tick) * x_step + x_offset), -(pos_t)ha);
551+
auto pb = origin(pos_t((j - skip + x_skip_tick + 1) * x_step + x_offset), -(pos_t)hb);
530552
line(pa, pb).color(default_color).draw(canvans);
531553
if (j == 0)
532554
{

0 commit comments

Comments
 (0)