-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeasurement_result.vala
150 lines (136 loc) · 3.49 KB
/
measurement_result.vala
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**********************************************************************
owon-tools - Copyright (C) 2017 - Andreas Kemnade
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
***********************************************************************/
/*
result format
1-4:value
5=space
6= decimal point pos
7: 08 = AC
10 = DC
20 = Auto
01 = running
02 = hold
04 = delta
8: 20 = MAX
10 = MIN
9: 40 =m
80 =µ
20 =k
10 =M
08 =Beep
04 =Diode
02 =%
10: 02 = °C
04 = F
08 = Hz
10 = HFE
20 = Ohm
40 = A
80 = V
11: 80 = -
lower bits: bargraph value
*/
public class MeasurementResult : GLib.Object {
public bool negative;
public uint8 [] rawdata;
public int exponent;
public string value_string;
public double value_noexp;
public double value;
public string unit;
public bool autorange;
public bool valid;
public bool nan;
public bool max;
public bool min;
public bool hold;
public bool delta;
public bool diode;
public bool running;
public bool beepmode;
public int bars;
[CCode (cheader_filename = "math.h", cname = "pow10")]
static extern double pow10(double y);
public MeasurementResult(uint8[] data) {
int i,j;
rawdata = data;
if (data.length != 14) {
valid = false;
return;
}
max = 0 != (data[8] & 0x20);
min = 0 != (data[8] & 0x10);
hold = 0 != (data[7] & 0x02);
delta = 0 != (data[7] & 0x04);
running = 0 != (data[7] & 0x01);
negative = data[0] == '-';
autorange = 0 != (data[7] & 0x20);
diode = 0 != (data[9] & 0x04);
beepmode = 0 != (data[9] & 0x08);
bars = data[11] & 0x3f;
nan = (data[1] == '?');
switch(data[9] & 0xf0) {
case 0x10: exponent = 6; break;
case 0x20: exponent = 3; break;
case 0x40: exponent = -3; break;
case 0x80: exponent = -6; break;
default: exponent = 0; break;
}
uint8[] val = new uint8[7];
val[0] = negative ? '-' : 0x20;
int pointpos = 4;
switch(data[6]) {
case 0x30: pointpos = 5; break;
case 0x31: pointpos = 2; break;
case 0x32: pointpos = 3; break;
case 0x34: pointpos = 4; break;
}
for(i = 1, j = 1; i < 5; i++, j++) {
if ((!nan) && (j == pointpos)) {
val[j] = '.';
j++;
}
val[j] = rawdata[i];
}
val[j] = 0;
value_string = (string)val;
if (nan) {
value_noexp = double.NAN;
value = double.NAN;
} else {
value_noexp = double.parse(value_string);
value = value_noexp * pow10(exponent);
}
switch(data[10]) {
case 0x80: unit = "V " + ((0 != (data[7] & 0x8)) ? "AC" : "DC"); break;
case 0x20: unit = "Ohm"; break;
case 0x04: unit = "F"; exponent = -9; break;
case 0x08: unit = "Hz"; break;
case 0x10: unit = "HFE"; break;
case 0x02: unit = "°C"; break;
case 0x40: unit = "A " + ((0 != (data[7] & 0x8)) ? "AC" : "DC"); break;
case 0x00: if (0 != (data[9] & 0x02)) unit="%"; break;
}
valid = true;
}
public string print_value() {
string prefix = "";
switch(exponent) {
case 3: prefix = "k"; break;
case 6: prefix = "M"; break;
case -3: prefix = "m"; break;
case -6: prefix = "µ"; break;
case -9: prefix = "n"; break;
}
return value_string + " " + prefix + unit;
}
}