mirror of
https://github.com/betaflight/betaflight-configurator.git
synced 2025-07-17 13:25:24 +03:00
fixes / improvements some variable / branch
This commit is contained in:
parent
a2adfa0457
commit
2f505314f5
1 changed files with 84 additions and 72 deletions
150
js/serial.js
150
js/serial.js
|
@ -1,9 +1,6 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var serial = {
|
var serial = {
|
||||||
connectionType: 'serial', // 'serial' or 'tcp'
|
|
||||||
connectionIP: '127.0.0.1',
|
|
||||||
connectionPort: 2323,
|
|
||||||
connectionId: false,
|
connectionId: false,
|
||||||
openRequested: false,
|
openRequested: false,
|
||||||
openCanceled: false,
|
openCanceled: false,
|
||||||
|
@ -11,79 +8,35 @@ var serial = {
|
||||||
bytesReceived: 0,
|
bytesReceived: 0,
|
||||||
bytesSent: 0,
|
bytesSent: 0,
|
||||||
failed: 0,
|
failed: 0,
|
||||||
|
connectionType: 'serial', // 'serial' or 'tcp'
|
||||||
|
connectionIP: '127.0.0.1',
|
||||||
|
connectionPort: 2323,
|
||||||
|
|
||||||
transmitting: false,
|
transmitting: false,
|
||||||
outputBuffer: [],
|
outputBuffer: [],
|
||||||
|
|
||||||
LOGHEAD: 'SERIAL: ',
|
logHead: 'SERIAL: ',
|
||||||
|
|
||||||
connect: function (path, options, callback) {
|
connect: function (path, options, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
self.openRequested = true;
|
|
||||||
|
|
||||||
var testUrl = path.match(/tcp:\/\/(.*):(.*)/)
|
var testUrl = path.match(/^tcp:\/\/([A-Za-z0-9\.-]+)(?:\:(\d+))?$/)
|
||||||
if (testUrl) {
|
if (testUrl) {
|
||||||
self.connectionIP = testUrl[1];
|
var ip = testUrl[1];
|
||||||
self.connectionPort = testUrl[2] || self.connectionPort;
|
var port = testUrl[2] || self.connectionPort;
|
||||||
self.connectionPort = parseInt(self.connectionPort);
|
port = parseInt(self.connectionPort);
|
||||||
self.connectionType = 'tcp';
|
|
||||||
self.LOGHEAD = 'SERIAL-TCP: ';
|
|
||||||
console.log('connect to raw tcp:', self.connectionIP + ':' + self.connectionPort)
|
|
||||||
|
|
||||||
chrome.sockets.tcp.create({}, function(createInfo) {
|
console.log('connect to raw tcp:', ip + ':' + port)
|
||||||
console.log('chrome.sockets.tcp.create', createInfo)
|
self.connectTcp(ip, port, options, callback);
|
||||||
if (createInfo && !self.openCanceled) {
|
|
||||||
self.connectionId = createInfo.socketId;
|
|
||||||
self.bitrate = 115200; // fake
|
|
||||||
self.bytesReceived = 0;
|
|
||||||
self.bytesSent = 0;
|
|
||||||
self.failed = 0;
|
|
||||||
self.openRequested = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
chrome.sockets.tcp.connect(createInfo.socketId, self.connectionIP, self.connectionPort, function (result){
|
|
||||||
if (chrome.runtime.lastError) {
|
|
||||||
console.error('onConnectedCallback', chrome.runtime.lastError.message);
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('onConnectedCallback', result)
|
|
||||||
if(result == 0) {
|
|
||||||
chrome.sockets.tcp.setNoDelay(createInfo.socketId, true, function (noDelayResult){
|
|
||||||
if (chrome.runtime.lastError) {
|
|
||||||
console.error('setNoDelay', chrome.runtime.lastError.message);
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('setNoDelay', noDelayResult)
|
|
||||||
if(noDelayResult != 0) {
|
|
||||||
self.openRequested = false;
|
|
||||||
console.log(self.LOGHEAD + 'Failed to setNoDelay');
|
|
||||||
}
|
|
||||||
self.onReceive.addListener(function log_bytesReceived(info) {
|
|
||||||
if (info.socketId != self.connectionId) return;
|
|
||||||
self.bytesReceived += info.data.byteLength;
|
|
||||||
});
|
|
||||||
self.onReceiveError.addListener(function watch_for_on_receive_errors(info) {
|
|
||||||
console.error(info);
|
|
||||||
if (info.socketId != self.connectionId) return;
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log(self.LOGHEAD + 'Connection opened with ID: ' + createInfo.socketId + ', url: ' + self.connectionIP + ':' + self.connectionPort);
|
|
||||||
|
|
||||||
if (callback) callback(createInfo);
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
self.openRequested = false;
|
self.connectSerial(path, options, callback);
|
||||||
console.log(self.LOGHEAD + 'Failed to connect');
|
|
||||||
if (callback) callback(false);
|
|
||||||
}
|
}
|
||||||
|
},
|
||||||
});
|
connectSerial: function (path, options, callback) {
|
||||||
});
|
var self = this;
|
||||||
|
self.openRequested = true;
|
||||||
} else {
|
|
||||||
self.connectionType = 'serial';
|
self.connectionType = 'serial';
|
||||||
self.LOGHEAD = 'SERIAL: ';
|
self.logHead = 'SERIAL: ';
|
||||||
|
|
||||||
chrome.serial.connect(path, options, function (connectionInfo) {
|
chrome.serial.connect(path, options, function (connectionInfo) {
|
||||||
if (chrome.runtime.lastError) {
|
if (chrome.runtime.lastError) {
|
||||||
|
@ -211,7 +164,65 @@ var serial = {
|
||||||
if (callback) callback(false);
|
if (callback) callback(false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
},
|
||||||
|
connectTcp: function (ip, port, options, callback) {
|
||||||
|
var self = this;
|
||||||
|
self.openRequested = true;
|
||||||
|
self.connectionIP = ip;
|
||||||
|
self.connectionPort = port || 2323;
|
||||||
|
self.connectionPort = parseInt(self.connectionPort);
|
||||||
|
self.connectionType = 'tcp';
|
||||||
|
self.logHead = 'SERIAL-TCP: ';
|
||||||
|
|
||||||
|
chrome.sockets.tcp.create({}, function(createInfo) {
|
||||||
|
console.log('chrome.sockets.tcp.create', createInfo)
|
||||||
|
if (createInfo && !self.openCanceled) {
|
||||||
|
self.connectionId = createInfo.socketId;
|
||||||
|
self.bitrate = 115200; // fake
|
||||||
|
self.bytesReceived = 0;
|
||||||
|
self.bytesSent = 0;
|
||||||
|
self.failed = 0;
|
||||||
|
self.openRequested = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
chrome.sockets.tcp.connect(createInfo.socketId, self.connectionIP, self.connectionPort, function (result){
|
||||||
|
if (chrome.runtime.lastError) {
|
||||||
|
console.error('onConnectedCallback', chrome.runtime.lastError.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('onConnectedCallback', result)
|
||||||
|
if(result == 0) {
|
||||||
|
chrome.sockets.tcp.setNoDelay(createInfo.socketId, true, function (noDelayResult){
|
||||||
|
if (chrome.runtime.lastError) {
|
||||||
|
console.error('setNoDelay', chrome.runtime.lastError.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('setNoDelay', noDelayResult)
|
||||||
|
if(noDelayResult != 0) {
|
||||||
|
self.openRequested = false;
|
||||||
|
console.log(self.logHead + 'Failed to setNoDelay');
|
||||||
|
}
|
||||||
|
self.onReceive.addListener(function log_bytesReceived(info) {
|
||||||
|
if (info.socketId != self.connectionId) return;
|
||||||
|
self.bytesReceived += info.data.byteLength;
|
||||||
|
});
|
||||||
|
self.onReceiveError.addListener(function watch_for_on_receive_errors(info) {
|
||||||
|
console.error(info);
|
||||||
|
if (info.socketId != self.connectionId) return;
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(self.logHead + 'Connection opened with ID: ' + createInfo.socketId + ', url: ' + self.connectionIP + ':' + self.connectionPort);
|
||||||
|
|
||||||
|
if (callback) callback(createInfo);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
self.openRequested = false;
|
||||||
|
console.log(self.logHead + 'Failed to connect');
|
||||||
|
if (callback) callback(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
},
|
},
|
||||||
disconnect: function (callback) {
|
disconnect: function (callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
@ -234,10 +245,11 @@ var serial = {
|
||||||
console.error(chrome.runtime.lastError.message);
|
console.error(chrome.runtime.lastError.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result = result || self.connectionType == 'tcp'
|
||||||
if (result) {
|
if (result) {
|
||||||
console.log(self.LOGHEAD + 'Connection with ID: ' + self.connectionId + ' closed, Sent: ' + self.bytesSent + ' bytes, Received: ' + self.bytesReceived + ' bytes');
|
console.log(self.logHead + 'Connection with ID: ' + self.connectionId + ' closed, Sent: ' + self.bytesSent + ' bytes, Received: ' + self.bytesReceived + ' bytes');
|
||||||
} else {
|
} else {
|
||||||
console.log(self.LOGHEAD + 'Failed to close connection with ID: ' + self.connectionId + ' closed, Sent: ' + self.bytesSent + ' bytes, Received: ' + self.bytesReceived + ' bytes');
|
console.log(self.logHead + 'Failed to close connection with ID: ' + self.connectionId + ' closed, Sent: ' + self.bytesSent + ' bytes, Received: ' + self.bytesReceived + ' bytes');
|
||||||
}
|
}
|
||||||
|
|
||||||
self.connectionId = false;
|
self.connectionId = false;
|
||||||
|
@ -262,14 +274,14 @@ var serial = {
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
getInfo: function (callback) {
|
getInfo: function (callback) {
|
||||||
var chromeType = (self.connectionType == 'serial') ? chrome.serial : chrome.sockets.tcp;
|
var chromeType = (this.connectionType == 'serial') ? chrome.serial : chrome.sockets.tcp;
|
||||||
chromeType.getInfo(this.connectionId, callback);
|
chromeType.getInfo(this.connectionId, callback);
|
||||||
},
|
},
|
||||||
getControlSignals: function (callback) {
|
getControlSignals: function (callback) {
|
||||||
if (self.connectionType == 'serial') chrome.serial.getControlSignals(this.connectionId, callback);
|
if (this.connectionType == 'serial') chrome.serial.getControlSignals(this.connectionId, callback);
|
||||||
},
|
},
|
||||||
setControlSignals: function (signals, callback) {
|
setControlSignals: function (signals, callback) {
|
||||||
if (self.connectionType == 'serial') chrome.serial.setControlSignals(this.connectionId, signals, callback);
|
if (this.connectionType == 'serial') chrome.serial.setControlSignals(this.connectionId, signals, callback);
|
||||||
},
|
},
|
||||||
send: function (data, callback) {
|
send: function (data, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
@ -302,7 +314,7 @@ var serial = {
|
||||||
counter++;
|
counter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(self.LOGHEAD + 'Send buffer overflowing, dropped: ' + counter + ' entries');
|
console.log(self.logHead + 'Send buffer overflowing, dropped: ' + counter + ' entries');
|
||||||
}
|
}
|
||||||
|
|
||||||
send();
|
send();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue