blitflash
Version:
A JavaScript implementation of the 32blit flash tools
125 lines (124 loc) • 4.55 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BlitFlasher = void 0;
const BlitConnection_1 = require("./BlitConnection");
const util_1 = require("./util");
const BLIT_DEVICE_FILTER = { usbVendorId: 0x0483, usbProductId: 0x5740 };
const BLIT_DEVICE_FILTERS = [BLIT_DEVICE_FILTER];
class BlitFlasher {
constructor(port, options = { debug: false }) {
this.port = port;
this.options = options;
this.port = port;
}
/**
* Opens a connection to the 32Blit.
*/
open() {
return __awaiter(this, void 0, void 0, function* () {
yield this.port.open({
baudRate: 115200,
dataBits: 8,
stopBits: 1,
});
this.connection = new BlitConnection_1.BlitConnection(this.port.readable.getReader(), this.port.writable.getWriter(), { debug: this.options.debug });
});
}
/**
* Closes the connection to the 32Blit.
*/
close() {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.close());
yield ((_b = this.port) === null || _b === void 0 ? void 0 : _b.close());
});
}
/**
* Returns the 32Blit status.
*/
status() {
return __awaiter(this, void 0, void 0, function* () {
return this.connection.status();
});
}
/**
* Lists blit files installed on the 32Blit flash.
*/
list() {
return __awaiter(this, void 0, void 0, function* () {
return this.connection.list();
});
}
/**
* Sends a blit file to the 32blit.
*/
sendFile(data, drive, filename, directory = '') {
return __awaiter(this, void 0, void 0, function* () {
return this.connection.sendFile(data, drive, filename, directory);
});
}
/**
* Resets the 32Blit.
*/
reset() {
return __awaiter(this, void 0, void 0, function* () {
if (this.connection) {
yield this.connection.reset();
yield this.connection.close();
yield this.port.close();
}
yield util_1.sleep(1000);
yield this.open();
});
}
/**
* Return true if the browser is able to connect to a 32Blit over the serial port.
*/
static supportsWebSerial() {
return 'serial' in navigator;
}
/**
* Returns a previously approved serial port for the 32Blit or requests the user to authorise one.
*/
static getOrRequestPort() {
return __awaiter(this, void 0, void 0, function* () {
const ports = yield this.getPorts();
if (ports.length > 0) {
return ports[0];
}
return yield this.requestPort();
});
}
/**
* Retrieves previously approved serial ports for the 32Blit.
*/
static getPorts() {
return __awaiter(this, void 0, void 0, function* () {
const ports = yield navigator.serial.getPorts();
return ports.filter((port) => {
const info = port.getInfo();
return info.usbProductId == BLIT_DEVICE_FILTER.usbProductId &&
info.usbVendorId === BLIT_DEVICE_FILTER.usbVendorId;
});
});
}
/**
* Asks the user to authorise a serial port for the 32Blit.
*/
static requestPort() {
return __awaiter(this, void 0, void 0, function* () {
return yield navigator.serial.requestPort({ filters: BLIT_DEVICE_FILTERS });
});
}
}
exports.BlitFlasher = BlitFlasher;