UNPKG

flashmagic.js

Version:
65 lines (64 loc) 2.6 kB
"use strict"; var Symbol = require('es6-symbol'); var InSystemProgramming_1 = require('./InSystemProgramming'); var Utilities = require('./Utilities'); var UUEncoder_1 = require('./UUEncoder'); var _addressSym = Symbol(); var RAMWriter = (function () { function RAMWriter(isp) { this.isp = isp; } Object.defineProperty(RAMWriter.prototype, "address", { get: function () { return this[_addressSym]; }, set: function (address) { this[_addressSym] = address; }, enumerable: true, configurable: true }); RAMWriter.prototype.writeToRAM = function (buffer) { var _this = this; var ret = this.isp.sendCommand("W " + this.address + " " + buffer.length) .then(function () { return _this.uploadChunk(buffer); }); if (InSystemProgramming_1.InSystemProgramming.VLAB_MODE) { ret = ret.then(function () { return _this.isp.assertSuccess(); }); } return ret.then(function () { _this.address = _this.address.increment(buffer.length); return _this; }); }; RAMWriter.prototype.uploadChunk = function (buffer) { var _this = this; return new Promise(function (resolve, reject) { var uue = new UUEncoder_1.UUEncoder(); var isp = _this.isp; var lineCount = 0; var index = 0; (function loop() { if (lineCount === Utilities.LINES_PER_UUENCODED_CHUNK || index >= buffer.length) { isp.sendLine(uue.checksum.toString()).then(function () { uue.reset(); lineCount = 0; return isp.assertOK(); }).then(function () { if (index < buffer.length) { process.nextTick(loop); } else { resolve(); } }).catch(function (error) { return reject(error); }); } else { var count_1 = Math.min(Utilities.BYTES_PER_UUENCODED_LINE, buffer.length - index); isp.sendLine(uue.encode(buffer, index, count_1)).then(function () { index += count_1; lineCount++; process.nextTick(loop); }).catch(function (error) { return reject(error); }); } })(); }); }; return RAMWriter; }()); exports.RAMWriter = RAMWriter;