forked from iNavFlight/inav-configurator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutputMapping.js
211 lines (167 loc) · 5.8 KB
/
outputMapping.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
'use strict';
const BitHelper = require("./bitHelper");
var OutputMappingCollection = function () {
let self = {},
data = [],
timerOverrides = {};
const colorTable = [
"#8ecae6",
"#2a9d8f",
"#e9c46a",
"#f4a261",
"#e76f51",
"#ef476f",
"#ffc300"
];
const TIM_USE_ANY = 0;
const TIM_USE_PPM = 0;
const TIM_USE_PWM = 1;
const TIM_USE_MOTOR = 2; // Motor output
const TIM_USE_SERVO = 3; // Servo output (i.e. TRI)
//const TIM_USE_MC_CHNFW = 4; // Deprecated and not used after removal of CHANNEL_FORWARDING feature
//const TIM_USE_FW_MOTOR = 5;
//const TIM_USE_FW_SERVO = 6;
const TIM_USE_LED = 24;
const TIM_USE_BEEPER = 25;
const OUTPUT_TYPE_MOTOR = 0;
const OUTPUT_TYPE_SERVO = 1;
const OUTPUT_TYPE_LED = 2;
const SPECIAL_LABEL_LED = 1;
self.TIMER_OUTPUT_MODE_AUTO = 0;
self.TIMER_OUTPUT_MODE_MOTORS = 1;
self.TIMER_OUTPUT_MODE_SERVOS = 2;
self.TIMER_OUTPUT_MODE_LED = 3;
self.flushTimerOverrides = function() {
timerOverrides = {};
}
self.setTimerOverride = function (timer, outputMode) {
timerOverrides[timer] = outputMode;
}
self.getTimerOverride = function (timer) {
return timerOverrides[timer];
}
self.getTimerColor = function (timer) {
let timerIndex = self.getUsedTimerIds().indexOf(String(timer));
return colorTable[timerIndex % colorTable.length];
}
self.isLedPin = function(timer) {
return data[timer].specialLabels == SPECIAL_LABEL_LED;
}
self.getOutputTimerColor = function (output) {
let timerId = self.getTimerId(output);
return self.getTimerColor(timerId);
}
self.getUsedTimerIds = function (timer) {
let used = {};
let outputCount = self.getOutputCount();
for (let i = 0; i < outputCount; ++i) {
let timerId = self.getTimerId(i);
used[timerId] = 1;
}
return Object.keys(used).sort((a, b) => a - b);
}
function getTimerMap(isMR, motors, servos) {
let timerMap = [],
motorsToGo = motors,
servosToGo = servos;
for (let i = 0; i < data.length; i++) {
timerMap[i] = null;
if (servosToGo > 0 && BitHelper.bit_check(data[i]['usageFlags'], TIM_USE_LED)) {
console.log(i + ": LED");
timerMap[i] = OUTPUT_TYPE_LED;
} else if (servosToGo > 0 && BitHelper.bit_check(data[i]['usageFlags'], TIM_USE_SERVO)) {
console.log(i + ": SERVO");
servosToGo--;
timerMap[i] = OUTPUT_TYPE_SERVO;
} else if (motorsToGo > 0 && BitHelper.bit_check(data[i]['usageFlags'], TIM_USE_MOTOR)) {
console.log(i + ": MOTOR");
motorsToGo--;
timerMap[i] = OUTPUT_TYPE_MOTOR;
}
}
return timerMap;
};
self.getOutputTable = function (isMR, motors, servos) {
let currentMotorIndex = 1,
currentServoIndex = 0,
timerMap = getTimerMap(isMR, motors, servos.length),
outputMap = [],
offset = getFirstOutputOffset();
console.log("Offset: " + offset)
for (let i = 0; i < self.getOutputCount(); i++) {
let assignment = timerMap[i + offset];
if (assignment === null) {
outputMap[i] = "-";
} else if (assignment == OUTPUT_TYPE_MOTOR) {
outputMap[i] = "Motor " + currentMotorIndex;
currentMotorIndex++;
} else if (assignment == OUTPUT_TYPE_SERVO) {
outputMap[i] = "Servo " + servos[currentServoIndex];
currentServoIndex++;
} else if (assignment == OUTPUT_TYPE_LED) {
outputMap[i] = "Led";
}
}
return outputMap;
};
self.flush = function () {
data = [];
};
self.put = function (element) {
data.push(element);
};
self.getOutputCount = function () {
let retVal = 0;
for (let i = 0; i < data.length; i++) {
let flags = data[i]['usageFlags'];
if (
BitHelper.bit_check(flags, TIM_USE_MOTOR) ||
BitHelper.bit_check(flags, TIM_USE_SERVO) ||
BitHelper.bit_check(flags, TIM_USE_LED)
) {
retVal++;
};
}
return retVal;
}
function getFirstOutputOffset() {
for (let i = 0; i < data.length; i++) {
if (
BitHelper.bit_check(data[i]['usageFlags'], TIM_USE_MOTOR) ||
BitHelper.bit_check(data[i]['usageFlags'], TIM_USE_SERVO) ||
BitHelper.bit_check(data[i]['usageFlags'], TIM_USE_LED)
) {
return i;
}
}
return 0;
}
function getTimerId(outputIndex) {
return data[outputIndex]['timerId'];
}
function getOutput(servoIndex, bit) {
let offset = getFirstOutputOffset();
let lastFound = 0;
for (let i = offset; i < data.length; i++) {
if (BitHelper.bit_check(data[i]['usageFlags'], bit)) {
if (lastFound == servoIndex) {
return i - offset + 1;
} else {
lastFound++;
}
}
}
return null;
}
self.getTimerId = function(outputIndex) {
return getTimerId(outputIndex)
}
self.getFwServoOutput = function (servoIndex) {
return getOutput(servoIndex, TIM_USE_SERVO);
};
self.getMrServoOutput = function (index) {
return getOutput(index, TIM_USE_SERVO);
};
return self;
}
module.exports = OutputMappingCollection;