mirror of
https://github.com/iNavFlight/inav-configurator.git
synced 2025-07-16 04:45:18 +03:00
Init
This commit is contained in:
parent
939f5af04b
commit
f24ccfc637
96 changed files with 16438 additions and 8058 deletions
|
@ -1,139 +1,103 @@
|
|||
'use strict'
|
||||
|
||||
const { SerialPortStream } = require('@serialport/stream');
|
||||
const { autoDetect } = require('@serialport/bindings-cpp')
|
||||
const binding = autoDetect();
|
||||
|
||||
class ConnectionSerial extends Connection {
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this._failed = 0;
|
||||
this._serialport = null;
|
||||
this._errorListeners = [];
|
||||
this._onReceiveListeners = [];
|
||||
this._onErrorListener = [];
|
||||
}
|
||||
|
||||
connectImplementation(path, options, callback) {
|
||||
chrome.serial.connect(path, options, (connectionInfo) => {
|
||||
this.checkChromeLastError();
|
||||
if (connectionInfo && !this._openCanceled) {
|
||||
this.addOnReceiveErrorListener(info => {
|
||||
console.error(info);
|
||||
googleAnalytics.sendException('Serial: ' + info.error, false);
|
||||
|
||||
switch (info.error) {
|
||||
case 'system_error': // we might be able to recover from this one
|
||||
if (!this._failed++) {
|
||||
chrome.serial.setPaused(this._connectionId, false, function () {
|
||||
SerialCom.getInfo((info) => {
|
||||
if (info) {
|
||||
if (!info.paused) {
|
||||
console.log('SERIAL: Connection recovered from last onReceiveError');
|
||||
googleAnalytics.sendException('Serial: onReceiveError - recovered', false);
|
||||
|
||||
this._failed = 0;
|
||||
} else {
|
||||
console.log('SERIAL: Connection did not recover from last onReceiveError, disconnecting');
|
||||
GUI.log(chrome.i18n.getMessage('serialPortUnrecoverable'));
|
||||
googleAnalytics.sendException('Serial: onReceiveError - unrecoverable', false);
|
||||
|
||||
this.abort();
|
||||
}
|
||||
} else {
|
||||
this.checkChromeLastError();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
case 'break': // This occurs on F1 boards with old firmware during reboot
|
||||
case 'overrun':
|
||||
case 'frame_error': //Got disconnected
|
||||
// wait 50 ms and attempt recovery
|
||||
var error = info.error;
|
||||
setTimeout(() => {
|
||||
chrome.serial.setPaused(info.connectionId, false, function() {
|
||||
SerialCom.getInfo(function (info) {
|
||||
if (info) {
|
||||
if (info.paused) {
|
||||
// assume unrecoverable, disconnect
|
||||
console.log('SERIAL: Connection did not recover from ' + error + ' condition, disconnecting');
|
||||
GUI.log(chrome.i18n.getMessage('serialPortUnrecoverable'));;
|
||||
googleAnalytics.sendException('Serial: ' + error + ' - unrecoverable', false);
|
||||
|
||||
this.abort();
|
||||
} else {
|
||||
console.log('SERIAL: Connection recovered from ' + error + ' condition');
|
||||
googleAnalytics.sendException('Serial: ' + error + ' - recovered', false);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}, 50);
|
||||
break;
|
||||
|
||||
case 'timeout':
|
||||
// TODO
|
||||
break;
|
||||
|
||||
case 'device_lost':
|
||||
case 'disconnected':
|
||||
default:
|
||||
this.abort();
|
||||
}
|
||||
connectImplementation(path, options, callback) {
|
||||
this._serialport = new SerialPortStream({binding, path: path, baudRate: options.bitrate, autoOpen: true}, () => {
|
||||
|
||||
this._serialport.on('data', buffer => {
|
||||
this._onReceiveListeners.forEach(listener => {
|
||||
listener({
|
||||
connectionId: this._connectionId,
|
||||
data: buffer
|
||||
});
|
||||
});
|
||||
GUI.log(chrome.i18n.getMessage('connectionConnected', [path]));
|
||||
}
|
||||
})
|
||||
|
||||
this._serialport.on('error', error => {
|
||||
console.log("Serial error: " + error);
|
||||
this._onReceiveErrorListeners.forEach(listener => {
|
||||
listener(error);
|
||||
});
|
||||
});
|
||||
|
||||
if (callback) {
|
||||
callback(connectionInfo);
|
||||
callback({
|
||||
connectionId: ++this._connectionId,
|
||||
bitrate: options.bitrate
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
disconnectImplementation(callback) {
|
||||
chrome.serial.disconnect(this._connectionId, (result) => {
|
||||
if (callback) {
|
||||
callback(result);
|
||||
}
|
||||
});
|
||||
if (this._serialport && this._serialport.isOpen) {
|
||||
this._serialport.close(error => {
|
||||
if (error) {
|
||||
console.log("Unable to close serial: " + error)
|
||||
}
|
||||
if (callback) {
|
||||
callback(error ? false : true);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
sendImplementation(data, callback) {
|
||||
chrome.serial.send(this._connectionId, data, callback);
|
||||
}
|
||||
|
||||
addOnReceiveCallback(callback){
|
||||
chrome.serial.onReceive.addListener(callback);
|
||||
}
|
||||
|
||||
removeOnReceiveCallback(callback){
|
||||
chrome.serial.onReceive.removeListener(callback);
|
||||
}
|
||||
|
||||
addOnReceiveErrorCallback(callback) {
|
||||
chrome.serial.onReceiveError.addListener(callback);
|
||||
}
|
||||
|
||||
removeOnReceiveErrorCallback(callback) {
|
||||
chrome.serial.onReceiveError.removeListener(callback);
|
||||
}
|
||||
|
||||
static getDevices(callback) {
|
||||
chrome.serial.getDevices((devices_array) => {
|
||||
var devices = [];
|
||||
devices_array.forEach((device) => {
|
||||
devices.push(device.path);
|
||||
});
|
||||
callback(devices);
|
||||
this._serialport.write(Buffer.from(data), error => {
|
||||
var result = 0;
|
||||
if (error) {
|
||||
result = 1;
|
||||
console.log("Serial wrire error: " + error)
|
||||
}
|
||||
if (callback) {
|
||||
callback({
|
||||
bytesSent: data.byteLength,
|
||||
resultCode: result
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static getInfo(connectionId, callback) {
|
||||
chrome.serial.getInfo(connectionId, callback);
|
||||
addOnReceiveCallback(callback){
|
||||
this._onReceiveErrorListeners.push(callback);
|
||||
}
|
||||
|
||||
static getControlSignals(connectionId, callback) {
|
||||
chrome.serial.getControlSignals(connectionId, callback);
|
||||
removeOnReceiveCallback(callback){
|
||||
this._onReceiveListeners = this._onReceiveErrorListeners.filter(listener => listener !== callback);
|
||||
}
|
||||
|
||||
static setControlSignals(connectionId, signals, callback) {
|
||||
chrome.serial.setControlSignals(connectionId, signals, callback);
|
||||
addOnReceiveErrorCallback(callback) {
|
||||
this._onReceiveErrorListeners.push(callback);
|
||||
}
|
||||
|
||||
removeOnReceiveErrorCallback(callback) {
|
||||
this._onReceiveErrorListeners = this._onReceiveErrorListeners.filter(listener => listener !== callback);
|
||||
}
|
||||
|
||||
static async getDevices(callback) {
|
||||
SerialPort.list().then((ports, error) => {
|
||||
var devices = [];
|
||||
if (error) {
|
||||
GUI.log("Unable to list serial ports.");
|
||||
} else {
|
||||
ports.forEach(port => {
|
||||
devices.push(port.path);
|
||||
});
|
||||
}
|
||||
if (callback)
|
||||
callback(devices);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue