-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAxis.pde
70 lines (59 loc) · 1.9 KB
/
Axis.pde
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class Axis
{
String label;
int value;
int value_min, value_max;
int signal_group = 0;
boolean is_instrument = true;
float[] last_values_buffer;
Axis(String l, boolean instr, int sg) {
label = l;
value = 0;
value_min = Integer.MAX_VALUE;
value_max = Integer.MIN_VALUE;
is_instrument = instr;
signal_group = sg;
last_values_buffer = new float[LENGTH_OF_PAST_VALUES];
for(int t=0; t<LENGTH_OF_PAST_VALUES; t++) {
last_values_buffer[t] = 0.0;
}
}
String status_string() {
String status = this.label;
if(!this.is_instrument) label += " (ctrl)";
status += ": "+this.value;
return status;
}
void update_min_and_max() {
if(this.value < this.value_min) this.value_min = this.value;
if(this.value > this.value_max) this.value_max = this.value;
// println("DEBUG: min,max of #"+j+" are: "+this.value_min+","+this.value_max);
}
void update_vector_of_past_values() {
for(int t=LENGTH_OF_PAST_VALUES-1; t>0; t--) {
this.last_values_buffer[t] = this.last_values_buffer[t-1];
}
this.last_values_buffer[0] = this.normalized_value();
}
float normalized_value() {
// return zero if max and min values make no sense (i.e. are not set yet)
if ((1.*this.value_max-this.value_min) <= 0) return 0;
// if not, return so that min and max are top and bottom of window
return (1.*this.value-this.value_min) / (1.*this.value_max-this.value_min);
}
float normalized_old_value() {
return this.last_values_buffer[1];
}
float velocity() {
return abs(this.normalized_value() - this.normalized_old_value());
}
float average_of_last_values(int length) {
if(length < 1) { return 0.0; }
float sum = 0.0;
int actual_length = max(length, LENGTH_OF_PAST_VALUES-1);
for(int t=1; t<=actual_length; t++) {
sum += this.last_values_buffer[t];
}
return sum/actual_length;
}
}