betaflight-configurator/js/serial_backend.js

377 lines
13 KiB
JavaScript
Raw Normal View History

2013-04-08 15:29:52 +00:00
var connectionId = -1;
var connection_delay = 0; // delay which defines "when" will the configurator request configurator data after connection was established
var configuration_received = false;
2013-04-08 15:29:52 +00:00
var CONFIG = {
2013-04-08 18:07:47 +00:00
version: 0,
multiType: 0,
2013-11-11 12:33:50 +00:00
msp_version: 0,
capability: 0,
2013-04-08 18:07:47 +00:00
cycleTime: 0,
i2cError: 0,
activeSensors: 0,
2013-04-15 08:03:34 +00:00
mode: 0,
profile: 0,
2013-04-15 08:03:34 +00:00
uid: [0, 0, 0],
accelerometerTrims: [0, 0]
2013-04-15 06:44:08 +00:00
}
var PIDs = new Array(10);
for (var i = 0; i < 10; i++) {
PIDs[i] = new Array(3);
}
var RC = {
roll: 0,
pitch: 0,
yaw: 0,
throttle: 0,
AUX1: 0,
AUX2: 0,
AUX3: 0,
AUX4: 0
2013-04-15 06:44:08 +00:00
}
var RC_tuning = {
RC_RATE: 0,
RC_EXPO: 0,
roll_pitch_rate: 0,
yaw_rate: 0,
dynamic_THR_PID: 0,
throttle_MID: 0,
throttle_EXPO: 0,
2013-04-15 06:44:08 +00:00
}
2013-04-10 11:53:48 +00:00
var AUX_CONFIG = new Array();
var AUX_CONFIG_values = new Array();
2013-11-08 09:24:12 +00:00
var SERVO_CONFIG = new Array();
var SENSOR_DATA = {
gyroscope: [0, 0, 0],
accelerometer: [0, 0, 0],
magnetometer: [0, 0, 0],
altitude: 0,
kinematicsX: 0.0,
kinematicsY: 0.0,
kinematicsZ: 0.0,
debug: [0, 0, 0, 0]
2013-04-15 06:44:08 +00:00
}
var MOTOR_DATA = new Array(8);
var SERVO_DATA = new Array(8);
2013-04-11 11:16:51 +00:00
var GPS_DATA = {
fix: 0,
numSat: 0,
lat: 0,
lon: 0,
alt: 0,
speed: 0,
ground_course: 0,
2013-04-11 11:16:51 +00:00
distanceToHome: 0,
ditectionToHome: 0,
update: 0,
// baseflight specific gps stuff
chn: new Array(),
svid: new Array(),
quality: new Array(),
cno: new Array()
2013-04-15 06:44:08 +00:00
}
var BATTERY = {
voltage: 0,
pMeterSum: 0,
}
2013-04-11 11:16:51 +00:00
2013-04-12 17:17:27 +00:00
var CLI_active = false;
2013-04-08 15:29:52 +00:00
$(document).ready(function() {
port_picker = $('div#port-picker .port select');
baud_picker = $('div#port-picker #baud');
delay_picker = $('div#port-picker #delay');
console.log('Scanning for new ports...');
update_ports();
2013-04-08 15:29:52 +00:00
$('div#port-picker a.connect').click(function() {
if (GUI.connect_lock != true) { // GUI control overrides the user control
var clicks = $(this).data('clicks');
selected_port = String($(port_picker).val());
selected_baud = parseInt(baud_picker.val());
connection_delay = parseInt(delay_picker.val());
if (selected_port != '0') {
if (!clicks) {
console.log('Connecting to: ' + selected_port);
GUI.connecting_to = selected_port;
2013-11-22 10:59:38 +00:00
// lock port select & baud while we are connecting / connected
$('div#port-picker #port, div#port-picker #baud, div#port-picker #delay').prop('disabled', true);
$('div#port-picker a.connect').text('Connecting');
2013-11-22 10:59:38 +00:00
chrome.serial.open(selected_port, {bitrate: selected_baud}, onOpen);
} else {
// Disable any active "data pulling" timer
disable_timers();
GUI.tab_switch_cleanup();
GUI.timeout_remove('connecting'); // kill connecting timer
chrome.serial.close(connectionId, onClosed);
GUI.connected_to = false;
clearTimeout(connection_delay);
clearInterval(serial_poll);
clearInterval(port_usage_poll);
// Change port utilization to 0
$('span.port-usage').html('0%');
2013-04-12 17:17:27 +00:00
// reset valid config received variable (used to block tabs while not connected properly)
configuration_received = false;
2013-11-22 10:59:38 +00:00
// unlock port select & baud
$('div#port-picker #port, div#port-picker #baud, div#port-picker #delay').prop('disabled', false);
$(this).text('Connect');
$(this).removeClass('active');
}
2013-04-08 15:29:52 +00:00
$(this).data("clicks", !clicks);
2013-04-08 15:29:52 +00:00
}
}
});
});
function onOpen(openInfo) {
connectionId = openInfo.connectionId;
2013-06-19 11:06:14 +00:00
backgroundPage.connectionId = openInfo.connectionId; // also pass connectionId to the background page
2013-04-08 15:29:52 +00:00
if (connectionId != -1) {
// update connected_to
GUI.connected_to = GUI.connecting_to;
// reset connecting_to
GUI.connecting_to = false;
2013-04-08 15:29:52 +00:00
console.log('Connection was opened with ID: ' + connectionId);
// save selected port with chrome.storage if the port differs
chrome.storage.local.get('last_used_port', function(result) {
if (typeof result.last_used_port != 'undefined') {
if (result.last_used_port != selected_port) {
// last used port doesn't match the one found in local db, we will store the new one
chrome.storage.local.set({'last_used_port': selected_port}, function() {
// Debug message is currently disabled (we dont need to spam the console log with that)
// console.log('Last selected port was saved in chrome.storage.');
});
}
} else {
// variable isn't stored yet, saving
chrome.storage.local.set({'last_used_port': selected_port}, function() {
// Debug message is currently disabled (we dont need to spam the console log with that)
// console.log('Last selected port was saved in chrome.storage.');
});
}
});
2013-04-08 15:29:52 +00:00
connection_delay = setTimeout(function() {
// start polling
serial_poll = setInterval(readPoll, 10);
port_usage_poll = setInterval(port_usage, 1000);
// disconnect after 10 seconds with error if we don't get IDENT data
GUI.timeout_add('connecting', function() {
if (!configuration_received) {
notify('Did not received configuration within <span style="color: red">10 seconds</span>, communication <span style="color: red">failed</span> - Disconnecting');
$('div#port-picker a.connect').click(); // disconnect
}
}, 10000);
2013-04-15 08:03:34 +00:00
// baseflight specific
send_message(MSP_codes.MSP_UID, MSP_codes.MSP_UID);
send_message(MSP_codes.MSP_ACC_TRIM, MSP_codes.MSP_ACC_TRIM);
// request configuration data
send_message(MSP_codes.MSP_IDENT, MSP_codes.MSP_IDENT);
2013-04-08 20:02:58 +00:00
send_message(MSP_codes.MSP_STATUS, MSP_codes.MSP_STATUS);
send_message(MSP_codes.MSP_PID, MSP_codes.MSP_PID);
send_message(MSP_codes.MSP_RC_TUNING, MSP_codes.MSP_RC_TUNING);
2013-04-08 15:29:52 +00:00
}, connection_delay * 1000);
} else {
console.log('Failed to open serial port');
notify('Failed to open serial port', 'red');
$('div#port-picker a.connect').text('Connect');
$('div#port-picker a.connect').removeClass('active');
// reset data
$('div#port-picker a.connect').data("clicks", false);
2013-04-08 15:29:52 +00:00
}
}
function onClosed(result) {
if (result) { // All went as expected
connectionId = -1; // reset connection id
2013-06-19 11:06:14 +00:00
backgroundPage.connectionId = connectionId; // also pass latest connectionId to the background page
2013-04-08 19:10:47 +00:00
2013-04-08 18:07:47 +00:00
sensor_status(sensors_detected = 0); // reset active sensor indicators
2013-04-08 19:10:47 +00:00
$('#tabs > ul li').removeClass('active'); // de-select any selected tabs
2013-10-30 16:41:29 +00:00
tab_initialize_default();
2013-04-08 19:10:47 +00:00
2013-04-08 15:29:52 +00:00
console.log('Connection closed successfully.');
} else { // Something went wrong
if (connectionId > 0) {
console.log('There was an error that happened during "connection-close" procedure.');
notify('Failed to close serial port', 'red');
2013-04-08 15:29:52 +00:00
}
}
}
function readPoll() {
chrome.serial.read(connectionId, 128, MSP_char_read);
2013-04-08 18:07:47 +00:00
}
function port_usage() {
var port_usage = (char_counter * 10 / selected_baud) * 100;
$('span.port-usage').html(parseInt(port_usage) + '%');
// reset counter
char_counter = 0;
}
function update_ports() {
var initial_ports = false;
GUI.interval_add('port-update', function() {
chrome.serial.getPorts(function(current_ports) {
if (initial_ports.length > current_ports.length || !initial_ports) {
// port got removed or initial_ports wasn't initialized yet
var removed_ports = array_difference(initial_ports, current_ports);
if (initial_ports != false) console.log('Port removed: ' + removed_ports);
// disconnect "UI" if necessary
if (GUI.connected_to != false && removed_ports[0] == GUI.connected_to) {
$('div#port-picker a.connect').click();
}
// update COM port list
update_port_select_menu(current_ports);
// auto-select last used port (only during initialization)
if (!initial_ports) {
chrome.storage.local.get('last_used_port', function(result) {
// if last_used_port was set, we try to select it
if (result.last_used_port) {
current_ports.forEach(function(port) {
if (port == result.last_used_port) {
console.log('Selecting last used port: ' + result.last_used_port);
$('div#port-picker .port select').val(result.last_used_port);
}
});
} else {
console.log('Last used port wasn\'t saved "yet", auto-select disabled.');
}
});
}
// reset initial_ports
initial_ports = current_ports;
}
var new_ports = array_difference(current_ports, initial_ports);
if (new_ports.length > 0) {
console.log('New port found: ' + new_ports[0]);
// generate new COM port list
update_port_select_menu(current_ports);
if (!GUI.connected_to) {
$('div#port-picker .port select').val(new_ports[0]);
} else {
$('div#port-picker .port select').val(GUI.connected_to);
}
// reset initial_ports
initial_ports = current_ports;
}
});
}, 100, true);
}
function update_port_select_menu(ports) {
$('div#port-picker .port select').html(''); // drop previous one
if (ports.length > 0) {
for (var i = 0; i < ports.length; i++) {
$('div#port-picker .port select').append($("<option/>", {value: ports[i], text: ports[i]}));
}
} else {
$('div#port-picker .port select').append($("<option/>", {value: 0, text: 'NOT FOUND'}));
}
}
2013-04-08 18:07:47 +00:00
function sensor_status(sensors_detected) {
var e_sensor_status = $('div#sensor-status');
if (bit_check(sensors_detected, 0)) { // Gyroscope & accel detected
$('.gyro', e_sensor_status).addClass('on');
$('.accel', e_sensor_status).addClass('on');
} else {
$('.gyro', e_sensor_status).removeClass('on');
$('.accel', e_sensor_status).removeClass('on');
}
if (bit_check(sensors_detected, 1)) { // Barometer detected
$('.baro', e_sensor_status).addClass('on');
} else {
$('.baro', e_sensor_status).removeClass('on');
}
if (bit_check(sensors_detected, 2)) { // Magnetometer detected
$('.mag', e_sensor_status).addClass('on');
} else {
$('.mag', e_sensor_status).removeClass('on');
2013-04-08 20:02:58 +00:00
}
if (bit_check(sensors_detected, 3)) { // GPS detected
$('.gps', e_sensor_status).addClass('on');
} else {
$('.gps', e_sensor_status).removeClass('on');
}
if (bit_check(sensors_detected, 4)) { // Sonar detected
$('.sonar', e_sensor_status).addClass('on');
} else {
$('.sonar', e_sensor_status).removeClass('on');
}
2013-04-08 18:07:47 +00:00
}
function highByte(num) {
return num >> 8;
}
function lowByte(num) {
return 0x00FF & num;
}
function bit_check(num, bit) {
return ((num >> bit) % 2 != 0)
}
function bit_set(num, bit) {
return num | 1 << bit;
}
function bit_clear(num, bit) {
return num & ~(1 << bit);
2013-04-08 15:29:52 +00:00
}