-
Notifications
You must be signed in to change notification settings - Fork 67
/
parameter.ts
99 lines (80 loc) · 2.85 KB
/
parameter.ts
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
export enum ParamType {
MAV_PARAM_TYPE_UINT8 = 'MAV_PARAM_TYPE_UINT8',
MAV_PARAM_TYPE_INT8 = 'MAV_PARAM_TYPE_INT8',
MAV_PARAM_TYPE_UINT16 = 'MAV_PARAM_TYPE_UINT16',
MAV_PARAM_TYPE_INT16 = 'MAV_PARAM_TYPE_INT16',
MAV_PARAM_TYPE_UINT32 = 'MAV_PARAM_TYPE_UINT32',
MAV_PARAM_TYPE_INT32 = 'MAV_PARAM_TYPE_INT32',
MAV_PARAM_TYPE_UINT64 = 'MAV_PARAM_TYPE_UINT64',
MAV_PARAM_TYPE_INT64 = 'MAV_PARAM_TYPE_INT64',
MAV_PARAM_TYPE_REAL32 = 'MAV_PARAM_TYPE_REAL32',
MAV_PARAM_TYPE_REAL64 = 'MAV_PARAM_TYPE_REAL64',
}
export default interface Parameter {
name: string
value: number
readonly: boolean
description: string
shortDescription: string
options?: {[key:number] : string}
bitmask?: {[key:number] : string}
rebootRequired: boolean
id: number
range?: {high: number, low: number}
increment?: number
paramType: { type: ParamType }
units?: string
}
export function printParam(param: Parameter | undefined): string | undefined {
if (param === undefined) {
return undefined
}
// Show device id as an hexadecimal value
if (param.name.includes('_DEV_ID') || param.name.includes('_DEVID')) {
return `0x${param.value.toString(16).padStart(8, '0')}`
}
// Check if there are options but zero does not cover it
// Or if it's a bitmask, where no flags is 'None'
const option_zero_does_not_exist = param.options !== undefined && param.options?.[0] === undefined
if ((param.bitmask || option_zero_does_not_exist) && param.value === 0) {
return 'None'
}
// Bitmask can have options for default values, so this needs to go first
// E.g: Option: 830 = default, bitmask = ATTITUDE_MED, GPS, PM, CTUN..
// TODO: fix this so it doesnt show text for values such as 2.5 (rounding down to 2)
const option_value = param.options?.[param.value]
if (option_value) {
return option_value
}
// if options is a float...
const float_option = Object.entries(param.options ?? {}).find(
([key, _name]) => parseFloat(key) === param.value,
)
if (float_option) {
return float_option[1]
}
if (param.bitmask) {
const bitmask_result = []
// We check up to 64 bits to make sure that we are going to cover all possible bits
// Including the ones not listed in the bitmask
for (let bit = 0; bit < 64; bit += 1) {
const bitmask_value = 2 ** bit
// eslint-disable-next-line no-bitwise
if (param.value & bitmask_value) {
bitmask_result.push(param.bitmask[bit] ?? `UNKNOWN (Bit ${bit})`)
}
}
return bitmask_result.join(', ')
}
try {
if (Math.abs(param.value) > 1e4) {
return param.value.toExponential()
}
if (Math.abs(param.value) < 0.01 && param.value !== 0) {
return param.value.toExponential()
}
return param.value.toFixed(param.paramType.type.includes('INT') ? 0 : 2)
} catch {
return 'N/A'
}
}