Skip to content

Commit

Permalink
Simplify and rename concatToBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
majgis committed Aug 1, 2016
1 parent 0708e40 commit 9e79635
Showing 1 changed file with 7 additions and 23 deletions.
30 changes: 7 additions & 23 deletions lib/flash.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function address(addr) {

// Generate a mediatek factory partition
function factoryPartition(mac1, mac2) {
return concatToBuffer(
return toBuffer(
[0x20, 0x76, 0x03, 0x01],
mac1,
Array(30).fill(0xff),
Expand All @@ -36,7 +36,7 @@ function transaction(usbConnection, write, read, status_poll, wren, next) {
var flags = (status_poll ? 1 : 0) | ((wren ? 1 : 0) << 1);
var hdr = [(read >> 0) & 0xff, (read >> 8) & 0xff, (read >> 16) & 0xff, flags];

var data = concatToBuffer(hdr, write);
var data = toBuffer(hdr, write);

usbConnection.epOut.transfer(data, (err) => {
if (err) {
Expand Down Expand Up @@ -109,26 +109,10 @@ function write(usbConnection, writeAddress, data, sliceStart, next) {
});
}

function concatToBuffer() {
var totalLength = 0;
var buffers = [];
var buffer;
for (var i = 0; i < arguments.length; i++) {
var arg = arguments[i];
buffer = new Buffer(arg);
totalLength += buffer.length;
buffers.push(buffer);
}
var result = new Buffer(totalLength);

var byteIndex = 0;
for (i = 0; i < buffers.length; i++) {
buffer = buffers[i];
buffer.copy(result, byteIndex);
byteIndex += buffer.length;
}

return result;
function toBuffer() {
return Buffer.concat( Array.from(arguments).map (
(arg) => Array.isArray(arg) ? new Buffer(arg) : arg
));
}

// Write a page to flash
Expand All @@ -137,7 +121,7 @@ function writePage(usbConnection, addr, data, next) {
var wren = true;
transaction(
usbConnection,
concatToBuffer([0x12], address(addr), data),
toBuffer([0x12], address(addr), data),
null,
status_poll,
wren,
Expand Down

0 comments on commit 9e79635

Please sign in to comment.