2013-04-13 00:32:35 +00:00
|
|
|
var configuration_received = false;
|
2014-01-04 09:08:13 +00:00
|
|
|
var CLI_active = false;
|
2014-01-22 19:51:12 +00:00
|
|
|
var CLI_valid = false;
|
2013-04-08 15:29:52 +00:00
|
|
|
|
2014-03-08 05:25:15 +00:00
|
|
|
$(document).ready(function() {
|
2013-04-08 15:29:52 +00:00
|
|
|
$('div#port-picker a.connect').click(function() {
|
2013-11-14 06:27:35 +00:00
|
|
|
if (GUI.connect_lock != true) { // GUI control overrides the user control
|
|
|
|
var clicks = $(this).data('clicks');
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-12-15 17:41:25 +00:00
|
|
|
var selected_port = String($('div#port-picker .port select').val());
|
|
|
|
var selected_baud = parseInt($('div#port-picker #baud').val());
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-11-14 06:27:35 +00:00
|
|
|
if (selected_port != '0') {
|
2013-11-20 11:26:00 +00:00
|
|
|
if (!clicks) {
|
|
|
|
console.log('Connecting to: ' + selected_port);
|
|
|
|
GUI.connecting_to = selected_port;
|
2014-03-08 05:25:15 +00:00
|
|
|
|
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);
|
2014-05-06 14:19:30 +00:00
|
|
|
$('div#port-picker a.connect').text(chrome.i18n.getMessage('connecting'));
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-01-18 11:14:33 +00:00
|
|
|
serial.connect(selected_port, {bitrate: selected_baud}, onOpen);
|
2013-11-20 11:26:00 +00:00
|
|
|
} else {
|
2013-11-14 06:27:35 +00:00
|
|
|
// Disable any active "data pulling" timer
|
2014-02-21 20:12:19 +00:00
|
|
|
GUI.interval_kill_all();
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-11-14 06:27:35 +00:00
|
|
|
GUI.tab_switch_cleanup();
|
2013-12-11 17:57:29 +00:00
|
|
|
GUI.timeout_remove('connecting');
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-01-18 11:14:33 +00:00
|
|
|
serial.disconnect(onClosed);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-11-20 11:26:00 +00:00
|
|
|
GUI.connected_to = false;
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-12-15 17:45:46 +00:00
|
|
|
// Reset various UI elements
|
|
|
|
$('.software-version').html('0.0');
|
|
|
|
$('span.cycle-time').html('0');
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-01-26 17:24:46 +00:00
|
|
|
MSP.disconnect_cleanup();
|
2014-03-28 23:59:39 +00:00
|
|
|
PortUsage.reset();
|
2013-12-11 17:57:29 +00:00
|
|
|
configuration_received = false; // reset valid config received variable (used to block tabs while not connected properly)
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-11-22 10:59:38 +00:00
|
|
|
// unlock port select & baud
|
2013-12-29 10:15:04 +00:00
|
|
|
$('div#port-picker #port').prop('disabled', false);
|
|
|
|
if (!GUI.auto_connect) $('div#port-picker #baud').prop('disabled', false);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-05-06 14:19:30 +00:00
|
|
|
$(this).text(chrome.i18n.getMessage('connect'));
|
2014-02-03 08:15:13 +00:00
|
|
|
$(this).removeClass('active');
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-03 08:15:13 +00:00
|
|
|
sensor_status(sensors_detected = 0); // reset active sensor indicators
|
|
|
|
$('#tabs > ul li').removeClass('active'); // de-select any selected tabs
|
2014-03-15 14:43:08 +00:00
|
|
|
|
|
|
|
// detach listeners and remove element data
|
|
|
|
$('#content').empty();
|
|
|
|
|
|
|
|
// load default html
|
2014-02-03 08:15:13 +00:00
|
|
|
tab_initialize_default();
|
2013-11-14 06:27:35 +00:00
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-11-14 06:27:35 +00:00
|
|
|
$(this).data("clicks", !clicks);
|
2013-04-08 15:29:52 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-29 10:15:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// auto-connect
|
|
|
|
chrome.storage.local.get('auto_connect', function(result) {
|
|
|
|
if (typeof result.auto_connect === 'undefined') {
|
|
|
|
// auto_connect wasn't saved yet, save and push true to the GUI
|
|
|
|
GUI.auto_connect = true;
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-03-04 23:45:25 +00:00
|
|
|
$('input.auto_connect').prop('checked', true);
|
2014-05-06 14:19:30 +00:00
|
|
|
$('input.auto_connect, span.auto_connect').prop('title', chrome.i18n.getMessage('autoConnectEnabled'));
|
2013-12-29 10:15:04 +00:00
|
|
|
$('select#baud').val(115200).prop('disabled', true);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-03-04 23:45:25 +00:00
|
|
|
// save
|
|
|
|
chrome.storage.local.set({'auto_connect': true});
|
2013-12-29 10:15:04 +00:00
|
|
|
} else {
|
2014-03-08 05:25:15 +00:00
|
|
|
if (result.auto_connect) {
|
2013-12-29 10:15:04 +00:00
|
|
|
// enabled by user
|
|
|
|
GUI.auto_connect = true;
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-12-29 10:15:04 +00:00
|
|
|
$('input.auto_connect').prop('checked', true);
|
2014-05-06 14:19:30 +00:00
|
|
|
$('input.auto_connect, span.auto_connect').prop('title', chrome.i18n.getMessage('autoConnectEnabled'));
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-12-29 10:15:04 +00:00
|
|
|
$('select#baud').val(115200).prop('disabled', true);
|
2014-03-08 05:25:15 +00:00
|
|
|
} else {
|
2013-12-29 10:15:04 +00:00
|
|
|
// disabled by user
|
|
|
|
GUI.auto_connect = false;
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-12-29 10:15:04 +00:00
|
|
|
$('input.auto_connect').prop('checked', false);
|
2014-05-06 14:19:30 +00:00
|
|
|
$('input.auto_connect, span.auto_connect').prop('title', chrome.i18n.getMessage('autoConnectDisabled'));
|
2013-12-29 10:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-12-29 10:15:04 +00:00
|
|
|
// bind UI hook to auto-connect checkbos
|
|
|
|
$('input.auto_connect').change(function() {
|
|
|
|
GUI.auto_connect = $(this).is(':checked');
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-12-29 10:15:04 +00:00
|
|
|
// update title/tooltip
|
|
|
|
if (GUI.auto_connect) {
|
2014-05-06 14:19:30 +00:00
|
|
|
$('input.auto_connect, span.auto_connect').prop('title', chrome.i18n.getMessage('autoConnectEnabled'));
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-12-29 10:15:04 +00:00
|
|
|
$('select#baud').val(115200).prop('disabled', true);
|
|
|
|
} else {
|
2014-05-06 14:19:30 +00:00
|
|
|
$('input.auto_connect, span.auto_connect').prop('title', chrome.i18n.getMessage('autoConnectDisabled'));
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-12-29 10:15:04 +00:00
|
|
|
if (!GUI.connected_to && !GUI.connecting_to) $('select#baud').prop('disabled', false);
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-03-04 23:45:25 +00:00
|
|
|
chrome.storage.local.set({'auto_connect': GUI.auto_connect});
|
2013-12-29 10:15:04 +00:00
|
|
|
});
|
2014-02-21 13:47:30 +00:00
|
|
|
});
|
2014-03-28 23:59:39 +00:00
|
|
|
|
2014-02-21 13:47:30 +00:00
|
|
|
PortHandler.initialize();
|
2014-03-28 23:59:39 +00:00
|
|
|
PortUsage.initialize();
|
2014-03-08 05:25:15 +00:00
|
|
|
});
|
2013-04-08 15:29:52 +00:00
|
|
|
|
2014-03-08 05:25:15 +00:00
|
|
|
function onOpen(openInfo) {
|
2014-02-03 09:07:54 +00:00
|
|
|
if (openInfo) {
|
2013-11-20 11:26:00 +00:00
|
|
|
// update connected_to
|
|
|
|
GUI.connected_to = GUI.connecting_to;
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-11-20 11:26:00 +00:00
|
|
|
// reset connecting_to
|
|
|
|
GUI.connecting_to = false;
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-05-06 14:19:30 +00:00
|
|
|
GUI.log(chrome.i18n.getMessage('serialPortOpened', [openInfo.connectionId]));
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-06-18 18:13:26 +00:00
|
|
|
// 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') {
|
2013-12-15 17:41:25 +00:00
|
|
|
if (result.last_used_port != GUI.connected_to) {
|
2013-06-18 18:13:26 +00:00
|
|
|
// last used port doesn't match the one found in local db, we will store the new one
|
2014-05-06 14:19:30 +00:00
|
|
|
chrome.storage.local.set({'last_used_port': GUI.connected_to});
|
2013-06-18 18:13:26 +00:00
|
|
|
}
|
2013-06-18 23:21:09 +00:00
|
|
|
} else {
|
|
|
|
// variable isn't stored yet, saving
|
2014-05-06 14:19:30 +00:00
|
|
|
chrome.storage.local.set({'last_used_port': GUI.connected_to});
|
2013-06-18 18:13:26 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-01-18 11:14:33 +00:00
|
|
|
serial.onReceive.addListener(read_serial);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-12-15 17:41:25 +00:00
|
|
|
// disconnect after 10 seconds with error if we don't get IDENT data
|
|
|
|
GUI.timeout_add('connecting', function() {
|
|
|
|
if (!configuration_received) {
|
2014-05-06 14:19:30 +00:00
|
|
|
GUI.log(chrome.i18n.getMessage('noConfigurationReceived'));
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-12-15 17:41:25 +00:00
|
|
|
$('div#port-picker a.connect').click(); // disconnect
|
|
|
|
}
|
|
|
|
}, 10000);
|
|
|
|
|
|
|
|
// request configuration data
|
2014-04-11 13:11:59 +00:00
|
|
|
send_message(MSP_codes.MSP_UID, false, false, function() {
|
2014-05-06 14:30:53 +00:00
|
|
|
GUI.log(chrome.i18n.getMessage('uniqueDeviceIdReceived', [CONFIG.uid[0].toString(16) + CONFIG.uid[1].toString(16) + CONFIG.uid[2].toString(16)]));
|
2014-04-11 13:11:59 +00:00
|
|
|
send_message(MSP_codes.MSP_IDENT, false, false, function() {
|
2014-01-18 11:55:09 +00:00
|
|
|
GUI.timeout_remove('connecting'); // kill connecting timer
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-05-06 14:30:53 +00:00
|
|
|
GUI.log(chrome.i18n.getMessage('firmwareVersion', [CONFIG.version]));
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-03-30 10:05:18 +00:00
|
|
|
if (CONFIG.version >= firmware_version_accepted) {
|
2014-02-25 13:37:34 +00:00
|
|
|
configuration_received = true;
|
2014-03-30 10:05:18 +00:00
|
|
|
|
2014-05-06 14:19:30 +00:00
|
|
|
$('div#port-picker a.connect').text(chrome.i18n.getMessage('disconnect')).addClass('active');
|
2014-02-25 13:37:34 +00:00
|
|
|
$('#tabs li a:first').click();
|
|
|
|
} else {
|
2014-05-06 14:19:30 +00:00
|
|
|
GUI.log(chrome.i18n.getMessage('firmwareVersionNotSupported', [firmware_version_accepted]));
|
2014-02-25 13:37:34 +00:00
|
|
|
$('div#port-picker a.connect').click(); // disconnect
|
|
|
|
}
|
2014-01-18 11:55:09 +00:00
|
|
|
});
|
2013-12-15 17:41:25 +00:00
|
|
|
});
|
2013-11-08 14:34:52 +00:00
|
|
|
} else {
|
|
|
|
console.log('Failed to open serial port');
|
2014-05-06 14:30:53 +00:00
|
|
|
GUI.log(chrome.i18n.getMessage('serialPortOpenFail'));
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-05-06 14:30:53 +00:00
|
|
|
$('div#port-picker a.connect').text(chrome.i18n.getMessage('connect'));
|
2014-03-08 05:25:15 +00:00
|
|
|
$('div#port-picker a.connect').removeClass('active');
|
|
|
|
|
2013-12-05 11:40:10 +00:00
|
|
|
// unlock port select & baud
|
|
|
|
$('div#port-picker #port, div#port-picker #baud, div#port-picker #delay').prop('disabled', false);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-11-08 14:34:52 +00:00
|
|
|
// reset data
|
|
|
|
$('div#port-picker a.connect').data("clicks", false);
|
2013-04-08 15:29:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onClosed(result) {
|
2014-02-03 08:15:13 +00:00
|
|
|
if (result) { // All went as expected
|
2014-05-06 14:19:30 +00:00
|
|
|
GUI.log(chrome.i18n.getMessage('serialPortClosedOk'));
|
2013-04-08 15:29:52 +00:00
|
|
|
} else { // Something went wrong
|
2014-05-06 14:19:30 +00:00
|
|
|
GUI.log(chrome.i18n.getMessage('serialPortClosedFail'));
|
2014-03-08 05:25:15 +00:00
|
|
|
}
|
2013-04-08 15:29:52 +00:00
|
|
|
}
|
|
|
|
|
2014-01-18 11:14:33 +00:00
|
|
|
function read_serial(info) {
|
2014-01-22 19:35:37 +00:00
|
|
|
if (!CLI_active) {
|
2014-03-02 17:36:28 +00:00
|
|
|
MSP.read(info);
|
2014-01-22 19:35:37 +00:00
|
|
|
} else {
|
|
|
|
handle_CLI(info);
|
|
|
|
}
|
2013-04-08 18:07:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function sensor_status(sensors_detected) {
|
2013-12-24 18:17:59 +00:00
|
|
|
// initialize variable (if it wasn't)
|
|
|
|
if (typeof sensor_status.previous_sensors_detected == 'undefined') {
|
|
|
|
sensor_status.previous_sensors_detected = 0;
|
2013-04-08 18:07:47 +00:00
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-12-24 18:17:59 +00:00
|
|
|
// update UI (if necessary)
|
|
|
|
if (sensor_status.previous_sensors_detected != sensors_detected) {
|
|
|
|
var e_sensor_status = $('div#sensor-status');
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-12-24 18:17:59 +00:00
|
|
|
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');
|
|
|
|
}
|
2013-04-08 18:07:47 +00:00
|
|
|
|
2013-12-24 18:17:59 +00:00
|
|
|
if (bit_check(sensors_detected, 1)) { // Barometer detected
|
|
|
|
$('.baro', e_sensor_status).addClass('on');
|
|
|
|
} else {
|
|
|
|
$('.baro', e_sensor_status).removeClass('on');
|
2014-03-08 05:25:15 +00:00
|
|
|
}
|
2013-12-24 18:17:59 +00:00
|
|
|
|
|
|
|
if (bit_check(sensors_detected, 2)) { // Magnetometer detected
|
|
|
|
$('.mag', e_sensor_status).addClass('on');
|
|
|
|
} else {
|
|
|
|
$('.mag', e_sensor_status).removeClass('on');
|
2014-03-08 05:25:15 +00:00
|
|
|
}
|
2013-12-24 18:17:59 +00:00
|
|
|
|
|
|
|
if (bit_check(sensors_detected, 3)) { // GPS detected
|
|
|
|
$('.gps', e_sensor_status).addClass('on');
|
|
|
|
} else {
|
|
|
|
$('.gps', e_sensor_status).removeClass('on');
|
2014-03-08 05:25:15 +00:00
|
|
|
}
|
2013-12-24 18:17:59 +00:00
|
|
|
|
|
|
|
if (bit_check(sensors_detected, 4)) { // Sonar detected
|
|
|
|
$('.sonar', e_sensor_status).addClass('on');
|
|
|
|
} else {
|
|
|
|
$('.sonar', e_sensor_status).removeClass('on');
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-12-24 18:17:59 +00:00
|
|
|
// set current value
|
|
|
|
sensor_status.previous_sensors_detected = sensors_detected;
|
|
|
|
}
|
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) {
|
2013-12-12 16:10:10 +00:00
|
|
|
return ((num >> bit) % 2 != 0);
|
2013-04-10 13:31:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|