Code refactor
This commit is contained in:
@@ -82,33 +82,25 @@ function getMapIndex(array, input) {
|
|||||||
channel.addListener("onMessage", async function (message) {
|
channel.addListener("onMessage", async function (message) {
|
||||||
switch (message.id) {
|
switch (message.id) {
|
||||||
case can_conf.selector.id:
|
case can_conf.selector.id:
|
||||||
console.log("==INVOKE SELECTOR==");
|
|
||||||
if (Buffer.compare(message.data, can_conf.selector.command) == 0) {
|
if (Buffer.compare(message.data, can_conf.selector.command) == 0) {
|
||||||
console.log("enable");
|
|
||||||
enabled = true;
|
enabled = true;
|
||||||
} else {
|
} else {
|
||||||
console.log("disable");
|
|
||||||
enabled = false;
|
enabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case can_conf.apps_id:
|
case can_conf.apps_id:
|
||||||
console.log("==INVOKE APPS==");
|
|
||||||
const apps_percentage = ((message.data[1] << 8) | message.data[0]) / 1000;
|
const apps_percentage = ((message.data[1] << 8) | message.data[0]) / 1000;
|
||||||
|
|
||||||
throttle_output = loaded_map_data[getMapIndex(map_throttle, apps_percentage)][2];
|
throttle_output = loaded_map_data[getMapIndex(map_throttle, apps_percentage)][2];
|
||||||
console.log(throttle_output);
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case erpm_id:
|
case erpm_id:
|
||||||
console.log("==INVOKE ERPM==");
|
|
||||||
if (enabled) {
|
if (enabled) {
|
||||||
console.log("enabled");
|
|
||||||
if (fs.existsSync(path.join(__dirname, map_conf.map_storage_path, "selected"))) {
|
if (fs.existsSync(path.join(__dirname, map_conf.map_storage_path, "selected"))) {
|
||||||
const enabled_map_name = fs.readFileSync(path.join(__dirname, map_conf.map_storage_path, "selected"));
|
const enabled_map_name = fs.readFileSync(path.join(__dirname, map_conf.map_storage_path, "selected"));
|
||||||
|
|
||||||
if (enabled_map_name != "") {
|
if (enabled_map_name != "") {
|
||||||
console.log("map selected");
|
|
||||||
if (loaded_map_name != enabled_map_name) {
|
if (loaded_map_name != enabled_map_name) {
|
||||||
[loaded_map_name, loaded_map_data] = await parseMap(enabled_map_name);
|
[loaded_map_name, loaded_map_data] = await parseMap(enabled_map_name);
|
||||||
}
|
}
|
||||||
@@ -117,15 +109,12 @@ channel.addListener("onMessage", async function (message) {
|
|||||||
|
|
||||||
let max_current = loaded_map_data[getMapIndex(map_rpm, ERPM / can_conf.motor_poles)][(throttle_output > 0) ? 0 : 1];
|
let max_current = loaded_map_data[getMapIndex(map_rpm, ERPM / can_conf.motor_poles)][(throttle_output > 0) ? 0 : 1];
|
||||||
let current = throttle_output * max_current * 10;
|
let current = throttle_output * max_current * 10;
|
||||||
console.log(max_current);
|
|
||||||
console.log(current);
|
|
||||||
channel.send({
|
channel.send({
|
||||||
id: (throttle_output > 0) ? current_id : brake_current_id,
|
id: (throttle_output > 0) ? current_id : brake_current_id,
|
||||||
ext: false,
|
ext: false,
|
||||||
data: Buffer.from([(current >> 8) & 0xFF, current & 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
|
data: Buffer.from([(current >> 8) & 0xFF, current & 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
console.log("map not selected");
|
|
||||||
channel.send({
|
channel.send({
|
||||||
id: brake_current_id,
|
id: brake_current_id,
|
||||||
ext: false,
|
ext: false,
|
||||||
|
|||||||
@@ -73,7 +73,6 @@ setEventListeners(commit, async () => {
|
|||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
alert(`Add unsuccessful. ${error.message}`);
|
alert(`Add unsuccessful. ${error.message}`);
|
||||||
console.error(error.message);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
alert("Nothing to commit.");
|
alert("Nothing to commit.");
|
||||||
|
|||||||
@@ -59,7 +59,6 @@ setEventListeners(remove, async () => {
|
|||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
alert(`Remove unsuccessful. ${error.message}`);
|
alert(`Remove unsuccessful. ${error.message}`);
|
||||||
console.error(error.message);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -115,7 +114,6 @@ setEventListeners(commit, async () => {
|
|||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
alert(`Edit unsuccessful. ${error.message}`);
|
alert(`Edit unsuccessful. ${error.message}`);
|
||||||
console.error(error.message);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
alert("Nothing to commit.");
|
alert("Nothing to commit.");
|
||||||
|
|||||||
Reference in New Issue
Block a user