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
2013-12-15 17:41:25 +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' ) ;
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 ( ) ) ;
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 ;
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-20 11:26:00 +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 ( ) ;
2013-11-14 06:27:35 +00:00
GUI . tab _switch _cleanup ( ) ;
2013-12-11 17:57:29 +00:00
GUI . timeout _remove ( 'connecting' ) ;
2013-11-14 06:27:35 +00:00
2014-01-18 11:14:33 +00:00
serial . disconnect ( onClosed ) ;
2013-11-14 06:27:35 +00:00
2013-11-20 11:26:00 +00:00
GUI . connected _to = false ;
2013-12-15 17:45:46 +00:00
// Reset various UI elements
2013-11-14 06:27:35 +00:00
$ ( 'span.port-usage' ) . html ( '0%' ) ;
2013-12-15 17:45:46 +00:00
$ ( '.software-version' ) . html ( '0.0' ) ;
$ ( 'span.cycle-time' ) . html ( '0' ) ;
2013-11-14 06:27:35 +00:00
2014-01-26 17:24:46 +00:00
MSP . disconnect _cleanup ( ) ;
2013-12-11 17:57:29 +00:00
configuration _received = false ; // reset valid config received variable (used to block tabs while not connected properly)
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 ) ;
2013-11-22 10:59:38 +00:00
2013-11-14 06:27:35 +00:00
$ ( this ) . text ( 'Connect' ) ;
2014-02-03 08:15:13 +00:00
$ ( this ) . removeClass ( 'active' ) ;
sensor _status ( sensors _detected = 0 ) ; // reset active sensor indicators
$ ( '#tabs > ul li' ) . removeClass ( 'active' ) ; // de-select any selected tabs
tab _initialize _default ( ) ;
2013-11-14 06:27:35 +00:00
}
2013-04-08 15:29:52 +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
chrome . storage . local . set ( { 'auto_connect' : true } ) ;
GUI . auto _connect = true ;
$ ( 'select#baud' ) . val ( 115200 ) . prop ( 'disabled' , true ) ;
} else {
if ( result . auto _connect ) {
// enabled by user
GUI . auto _connect = true ;
$ ( 'input.auto_connect' ) . prop ( 'checked' , true ) ;
2013-12-29 10:32:21 +00:00
$ ( 'input.auto_connect, span.auto_connect' ) . prop ( 'title' , 'Auto-Connect: Enabled - Configurator automatically tries to connect when new serial port is detected' ) ;
2013-12-29 10:15:04 +00:00
$ ( 'select#baud' ) . val ( 115200 ) . prop ( 'disabled' , true ) ;
} else {
// disabled by user
GUI . auto _connect = false ;
$ ( 'input.auto_connect' ) . prop ( 'checked' , false ) ;
2013-12-29 10:32:21 +00:00
$ ( 'input.auto_connect, span.auto_connect' ) . prop ( 'title' , 'Auto-Connect: Disabled - User needs to select the correct serial port and click "Connect" button on its own' ) ;
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' ) ;
// update title/tooltip
if ( GUI . auto _connect ) {
2013-12-29 10:32:21 +00:00
$ ( 'input.auto_connect, span.auto_connect' ) . prop ( 'title' , 'Auto-Connect: Enabled - Configurator automatically tries to connect when new port is detected' ) ;
2013-12-29 10:15:04 +00:00
$ ( 'select#baud' ) . val ( 115200 ) . prop ( 'disabled' , true ) ;
} else {
2013-12-29 10:32:21 +00:00
$ ( 'input.auto_connect, span.auto_connect' ) . prop ( 'title' , 'Auto-Connect: Disabled - User needs to select the correct serial port and click "Connect" button on its own' ) ;
2013-12-29 10:15:04 +00:00
if ( ! GUI . connected _to && ! GUI . connecting _to ) $ ( 'select#baud' ) . prop ( 'disabled' , false ) ;
}
chrome . storage . local . set ( { 'auto_connect' : GUI . auto _connect } , function ( ) { } ) ;
} ) ;
2014-02-21 13:47:30 +00:00
} ) ;
PortHandler . initialize ( ) ;
2013-04-08 15:29:52 +00:00
} ) ;
2013-12-06 17:32:32 +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 ;
// reset connecting_to
GUI . connecting _to = false ;
2014-02-03 08:15:13 +00:00
GUI . log ( 'Serial port <span style="color: green">successfully</span> opened with ID: ' + openInfo . connectionId ) ;
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
2013-12-15 17:41:25 +00:00
chrome . storage . local . set ( { 'last_used_port' : GUI . connected _to } , function ( ) {
2013-06-18 18:13:26 +00:00
// 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-06-18 23:21:09 +00:00
} else {
// variable isn't stored yet, saving
2013-12-15 17:41:25 +00:00
chrome . storage . local . set ( { 'last_used_port' : GUI . connected _to } , function ( ) {
2013-06-18 23:21:09 +00:00
// 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-06-18 18:13:26 +00:00
}
} ) ;
2014-01-18 11:14:33 +00:00
serial . onReceive . addListener ( read _serial ) ;
2014-01-18 11:55:09 +00:00
GUI . interval _add ( 'port_usage' , port _usage , 1000 , true ) ;
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-02-03 08:15:13 +00:00
GUI . log ( 'No configuration received within <span style="color: red">10 seconds</span>, communication <span style="color: red">failed</span>' ) ;
2013-12-05 09:17:49 +00:00
2013-12-15 17:41:25 +00:00
$ ( 'div#port-picker a.connect' ) . click ( ) ; // disconnect
}
} , 10000 ) ;
// request configuration data
2014-01-18 11:55:09 +00:00
send _message ( MSP _codes . MSP _UID , MSP _codes . MSP _UID , false , function ( ) {
2014-02-04 18:16:53 +00:00
GUI . log ( 'Unique device ID <span style="color: green">received</span> - <strong>0x' + CONFIG . uid [ 0 ] . toString ( 16 ) + CONFIG . uid [ 1 ] . toString ( 16 ) + CONFIG . uid [ 2 ] . toString ( 16 ) + '</strong>' ) ;
2014-01-18 11:55:09 +00:00
send _message ( MSP _codes . MSP _IDENT , MSP _codes . MSP _IDENT , false , function ( ) {
GUI . timeout _remove ( 'connecting' ) ; // kill connecting timer
2014-02-25 13:37:34 +00:00
if ( CONFIG . version >= firmware _version _accepted ) {
// Update UI elements that doesn't need consistent refreshing
$ ( '.software-version' ) . html ( CONFIG . version ) ;
configuration _received = true ;
$ ( 'div#port-picker a.connect' ) . text ( 'Disconnect' ) . addClass ( 'active' ) ;
$ ( '#tabs li a:first' ) . click ( ) ;
} else {
GUI . log ( 'This firmware version is <span style="color: red">not supported</span>. Please upgrade to version <strong>' + firmware _version _accepted + '</strong> or higher.' ) ;
$ ( '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-02-03 07:45:21 +00:00
GUI . log ( 'Failed to open serial port' , 'red' ) ;
2013-11-08 14:34:52 +00:00
$ ( 'div#port-picker a.connect' ) . text ( 'Connect' ) ;
$ ( '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 ) ;
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
GUI . log ( 'Serial port <span style="color: green">successfully</span> closed' ) ;
2013-04-08 15:29:52 +00:00
} else { // Something went wrong
2014-02-03 08:15:13 +00:00
GUI . log ( '<span style="color: red">Failed</span> to close serial port' ) ;
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 ) {
MSP _char _read ( info ) ;
} else {
handle _CLI ( info ) ;
}
2013-04-08 18:07:47 +00:00
}
2013-04-10 16:49:25 +00:00
function port _usage ( ) {
2013-12-15 17:41:25 +00:00
var port _usage = ( char _counter * 10 / parseInt ( $ ( 'div#port-picker #baud' ) . val ( ) ) ) * 100 ;
2013-04-10 16:49:25 +00:00
$ ( 'span.port-usage' ) . html ( parseInt ( port _usage ) + '%' ) ;
// reset counter
char _counter = 0 ;
}
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
}
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' ) ;
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' ) ;
}
if ( bit _check ( sensors _detected , 2 ) ) { // Magnetometer detected
$ ( '.mag' , e _sensor _status ) . addClass ( 'on' ) ;
} else {
$ ( '.mag' , e _sensor _status ) . removeClass ( 'on' ) ;
}
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' ) ;
}
// 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
}