var configuration _received = false ;
var CLI _active = false ;
var CLI _valid = false ;
$ ( document ) . ready ( function ( ) {
$ ( 'div#port-picker a.connect' ) . click ( function ( ) {
if ( GUI . connect _lock != true ) { // GUI control overrides the user control
var clicks = $ ( this ) . data ( 'clicks' ) ;
var selected _port = String ( $ ( 'div#port-picker .port select' ) . val ( ) ) ;
var selected _baud = parseInt ( $ ( 'div#port-picker #baud' ) . val ( ) ) ;
if ( selected _port != '0' ) {
if ( ! clicks ) {
console . log ( 'Connecting to: ' + selected _port ) ;
GUI . connecting _to = selected _port ;
// 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' ) ;
serial . connect ( selected _port , { bitrate : selected _baud } , onOpen ) ;
} else {
// Disable any active "data pulling" timer
GUI . interval _kill _all ( ) ;
GUI . tab _switch _cleanup ( ) ;
GUI . timeout _remove ( 'connecting' ) ;
serial . disconnect ( onClosed ) ;
GUI . connected _to = false ;
// Reset various UI elements
$ ( '.software-version' ) . html ( '0.0' ) ;
$ ( 'span.cycle-time' ) . html ( '0' ) ;
MSP . disconnect _cleanup ( ) ;
PortUsage . reset ( ) ;
configuration _received = false ; // reset valid config received variable (used to block tabs while not connected properly)
// unlock port select & baud
$ ( 'div#port-picker #port' ) . prop ( 'disabled' , false ) ;
if ( ! GUI . auto _connect ) $ ( 'div#port-picker #baud' ) . prop ( 'disabled' , false ) ;
$ ( this ) . text ( 'Connect' ) ;
$ ( this ) . removeClass ( 'active' ) ;
sensor _status ( sensors _detected = 0 ) ; // reset active sensor indicators
$ ( '#tabs > ul li' ) . removeClass ( 'active' ) ; // de-select any selected tabs
// detach listeners and remove element data
$ ( '#content' ) . empty ( ) ;
// load default html
tab _initialize _default ( ) ;
}
$ ( this ) . data ( "clicks" , ! clicks ) ;
}
}
} ) ;
// 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 ;
$ ( 'input.auto_connect' ) . prop ( 'checked' , true ) ;
$ ( 'input.auto_connect, span.auto_connect' ) . prop ( 'title' , 'Auto-Connect: Enabled - Configurator automatically tries to connect when new port is detected' ) ;
$ ( 'select#baud' ) . val ( 115200 ) . prop ( 'disabled' , true ) ;
// save
chrome . storage . local . set ( { 'auto_connect' : true } ) ;
} else {
if ( result . auto _connect ) {
// enabled by user
GUI . auto _connect = true ;
$ ( 'input.auto_connect' ) . prop ( 'checked' , true ) ;
$ ( 'input.auto_connect, span.auto_connect' ) . prop ( 'title' , 'Auto-Connect: Enabled - Configurator automatically tries to connect when new serial port is detected' ) ;
$ ( 'select#baud' ) . val ( 115200 ) . prop ( 'disabled' , true ) ;
} else {
// disabled by user
GUI . auto _connect = false ;
$ ( 'input.auto_connect' ) . prop ( 'checked' , false ) ;
$ ( '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' ) ;
}
}
// 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 ) {
$ ( 'input.auto_connect, span.auto_connect' ) . prop ( 'title' , 'Auto-Connect: Enabled - Configurator automatically tries to connect when new port is detected' ) ;
$ ( 'select#baud' ) . val ( 115200 ) . prop ( 'disabled' , true ) ;
} else {
$ ( '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' ) ;
if ( ! GUI . connected _to && ! GUI . connecting _to ) $ ( 'select#baud' ) . prop ( 'disabled' , false ) ;
}
chrome . storage . local . set ( { 'auto_connect' : GUI . auto _connect } ) ;
} ) ;
} ) ;
PortHandler . initialize ( ) ;
PortUsage . initialize ( ) ;
} ) ;
function onOpen ( openInfo ) {
if ( openInfo ) {
// update connected_to
GUI . connected _to = GUI . connecting _to ;
// reset connecting_to
GUI . connecting _to = false ;
GUI . log ( 'Serial port <span style="color: green">successfully</span> opened with ID: ' + openInfo . 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 != GUI . connected _to ) {
// 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' : GUI . connected _to } , 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' : GUI . connected _to } , 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.');
} ) ;
}
} ) ;
serial . onReceive . addListener ( read _serial ) ;
// disconnect after 10 seconds with error if we don't get IDENT data
GUI . timeout _add ( 'connecting' , function ( ) {
if ( ! configuration _received ) {
GUI . log ( 'No configuration received within <span style="color: red">10 seconds</span>, communication <span style="color: red">failed</span>' ) ;
$ ( 'div#port-picker a.connect' ) . click ( ) ; // disconnect
}
} , 10000 ) ;
// request configuration data
send _message ( MSP _codes . MSP _UID , MSP _codes . MSP _UID , false , function ( ) {
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>' ) ;
send _message ( MSP _codes . MSP _IDENT , MSP _codes . MSP _IDENT , false , function ( ) {
GUI . timeout _remove ( 'connecting' ) ; // kill connecting timer
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
}
} ) ;
} ) ;
} else {
console . log ( 'Failed to open serial port' ) ;
GUI . log ( 'Failed to open serial port' , 'red' ) ;
$ ( 'div#port-picker a.connect' ) . text ( 'Connect' ) ;
$ ( 'div#port-picker a.connect' ) . removeClass ( 'active' ) ;
// unlock port select & baud
$ ( 'div#port-picker #port, div#port-picker #baud, div#port-picker #delay' ) . prop ( 'disabled' , false ) ;
// reset data
$ ( 'div#port-picker a.connect' ) . data ( "clicks" , false ) ;
}
}
function onClosed ( result ) {
if ( result ) { // All went as expected
GUI . log ( 'Serial port <span style="color: green">successfully</span> closed' ) ;
} else { // Something went wrong
GUI . log ( '<span style="color: red">Failed</span> to close serial port' ) ;
}
}
function read _serial ( info ) {
if ( ! CLI _active ) {
MSP . read ( info ) ;
} else {
handle _CLI ( info ) ;
}
}
function sensor _status ( sensors _detected ) {
// initialize variable (if it wasn't)
if ( typeof sensor _status . previous _sensors _detected == 'undefined' ) {
sensor _status . previous _sensors _detected = 0 ;
}
// 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' ) ;
}
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 ;
}
}
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 ) ;
}