mirror of
https://github.com/betaflight/betaflight-configurator.git
synced 2025-07-16 12:55:14 +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
156
js/serial.js
156
js/serial.js
|
@ -1,9 +1,6 @@
|
|||
'use strict';
|
||||
|
||||
var serial = {
|
||||
connectionType: 'serial', // 'serial' or 'tcp'
|
||||
connectionIP: '127.0.0.1',
|
||||
connectionPort: 2323,
|
||||
connectionId: false,
|
||||
openRequested: false,
|
||||
openCanceled: false,
|
||||
|
@ -11,79 +8,35 @@ var serial = {
|
|||
bytesReceived: 0,
|
||||
bytesSent: 0,
|
||||
failed: 0,
|
||||
connectionType: 'serial', // 'serial' or 'tcp'
|
||||
connectionIP: '127.0.0.1',
|
||||
connectionPort: 2323,
|
||||
|
||||
transmitting: false,
|
||||
outputBuffer: [],
|
||||
|
||||
LOGHEAD: 'SERIAL: ',
|
||||
logHead: 'SERIAL: ',
|
||||
|
||||
connect: function (path, options, callback) {
|
||||
var self = this;
|
||||
self.openRequested = true;
|
||||
|
||||
var testUrl = path.match(/tcp:\/\/(.*):(.*)/)
|
||||
var testUrl = path.match(/^tcp:\/\/([A-Za-z0-9\.-]+)(?:\:(\d+))?$/)
|
||||
if (testUrl) {
|
||||
self.connectionIP = testUrl[1];
|
||||
self.connectionPort = testUrl[2] || self.connectionPort;
|
||||
self.connectionPort = 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('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);
|
||||
}
|
||||
|
||||
});
|
||||
});
|
||||
var ip = testUrl[1];
|
||||
var port = testUrl[2] || self.connectionPort;
|
||||
port = parseInt(self.connectionPort);
|
||||
|
||||
console.log('connect to raw tcp:', ip + ':' + port)
|
||||
self.connectTcp(ip, port, options, callback);
|
||||
} else {
|
||||
self.connectionType = 'serial';
|
||||
self.LOGHEAD = 'SERIAL: ';
|
||||
self.connectSerial(path, options, callback);
|
||||
}
|
||||
},
|
||||
connectSerial: function (path, options, callback) {
|
||||
var self = this;
|
||||
self.openRequested = true;
|
||||
self.connectionType = 'serial';
|
||||
self.logHead = 'SERIAL: ';
|
||||
|
||||
chrome.serial.connect(path, options, function (connectionInfo) {
|
||||
if (chrome.runtime.lastError) {
|
||||
|
@ -211,7 +164,65 @@ var serial = {
|
|||
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) {
|
||||
var self = this;
|
||||
|
@ -234,10 +245,11 @@ var serial = {
|
|||
console.error(chrome.runtime.lastError.message);
|
||||
}
|
||||
|
||||
result = result || self.connectionType == 'tcp'
|
||||
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 {
|
||||
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;
|
||||
|
@ -262,14 +274,14 @@ var serial = {
|
|||
});
|
||||
},
|
||||
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);
|
||||
},
|
||||
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) {
|
||||
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) {
|
||||
var self = this;
|
||||
|
@ -302,7 +314,7 @@ var serial = {
|
|||
counter++;
|
||||
}
|
||||
|
||||
console.log(self.LOGHEAD + 'Send buffer overflowing, dropped: ' + counter + ' entries');
|
||||
console.log(self.logHead + 'Send buffer overflowing, dropped: ' + counter + ' entries');
|
||||
}
|
||||
|
||||
send();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue