浏览代码

Flashing from download

Andrew Chalkley 9 年之前
父节点
当前提交
d64e8b4583
共有 4 个文件被更改,包括 79 次插入110 次删除
  1. 3 1
      README.md
  2. 28 5
      manifestly/index.js
  3. 36 65
      manifestly/manifest.js
  4. 12 39
      manifestly/manifest.json

+ 3 - 1
README.md

@@ -1,14 +1,16 @@
 # Especially Flasher
 
-__Especially Flasher_ is a tool to get JavaScript running natively on
+_Especially Flasher_ is a tool to get JavaScript running natively on
 the Internet of Things device, ESP8266. This application runs on
 Windows, Mac OS X and Linux.
 
 This tool flashes (installs) the Espruino JavaScript run time on ESP8266
 EP-12 devices like the Adafruit Huzzah and Adadfruit Feather Huzzah.
 
+-------
 
 ## ROM Communication
+
 The ESP8266 is notoriously finicky about being flashed, we've done our best to abstract that for you.
 
 Here is an example of flashing the ESP8266 with the latest Espruino build.

+ 28 - 5
manifestly/index.js

@@ -2,14 +2,37 @@
 
 const fs = require("fs");
 const fetch = require("node-fetch");
-const ManifestPreparer = require("./manifest").ManifestPreparer;
+const prepareBinaries = require("./manifest");
+const log = require("../back-end/logger");
+const RomComm = require("../back-end/rom_comm");
 
-fs.readFile("./manifest.json", function(err, data){
+fs.readFile("./manifest.json", (err, data) => {
     if(err) throw err;
-    let manifest = Object.assign({}, JSON.parse(data), {fetch});
+    const manifest = JSON.parse(data);
+    prepareBinaries(manifest, (err, flashSpec) => {
+        if(err) throw err;
 
-    let preparer = new ManifestPreparer(manifest);
+        const esp = new RomComm({
+            portName: "/dev/cu.SLAB_USBtoUART",
+            baudRate: 115200
+        });
 
-    preparer.prepare();
+        esp.open().then((result) => {
+            log.info("ESP is open", result);
+            const firstSpec = flashSpec.shift();
+            let promise = esp.flashAddress(Number.parseInt(firstSpec.address), firstSpec.buffer);
+
+            flashSpec.forEach((spec) => {
+               promise = promise.then(()=> {
+                   return esp.flashAddress(Number.parseInt(spec.address), spec.buffer)
+               });
+            });
+
+            return promise.then(() => esp.close())
+                .then((result) => log.info("Flashed to latest Espruino build!", result));
+        }).catch((error) => {
+            log.error("Oh noes!", error);
+        });
+    });
 });
 

+ 36 - 65
manifestly/manifest.js

@@ -1,76 +1,47 @@
 "use strict";
 const http = require("http");
 const unzip = require("unzip");
-var targz = require('tar.gz');
-const url = require("url");
 const fs = require("fs");
 
-class ManifestPreparer {
-    constructor(options) {
-        this.steps = options.steps;
-        this.download = options.download;
-        this._validateSteps();
-    }
-
-    prepare() {
-        var unzipStep = this.steps[0]["unzip"];
-        var untarStep = this.steps[1]["untar"];
+function isBinaryFileRequired(flashSpecification, fileName) {
+    return flashSpecification.map(binary => binary.path).indexOf(fileName) !== -1;
+}
 
-        let fileName = `tmp/${this.download.split("/").pop()}`;
+function addBufferToBinary(flashSpecification, fileName, buffer) {
+    flashSpecification.forEach((element, index) => {
+        if (flashSpecification[index].path === fileName) {
+            flashSpecification[index].buffer = buffer;
+        }
+    });
+}
 
-        const downloadRequest = http.get(this.download, (response) => {
-            var body = "";
-            response.pipe(unzip.Parse()).on('entry', (entry) => {
-                const fileName = entry.path;
-                if (unzipStep.files.indexOf(fileName) !== -1) {
-                    entry.pipe(targz().createParseStream()).on('entry', (tarEntry) => {
-                        const fileName = tarEntry.path.split("/").pop();
-                        if (untarStep.files.indexOf(fileName) !== -1 ) {
-                            tarEntry.pipe(fs.createWriteStream(`tmp/${fileName}`));
-                        }
-                    });
-                } else {
-                    entry.autodrain();
-                }
-            });
 
-            response.on("error", (e) => console.error(e));
+function prepareBinaries(manifest, callback) {
+    const flashContents = manifest.flash;
+    const downloadRequest = http.get(manifest.download, (response) => {
+        response.pipe(unzip.Parse()).on('entry', (entry) => {
+            const fileName = entry.path;
+            if (isBinaryFileRequired(flashContents, fileName)) {
+                let body;
+                entry.on("data", function(data){
+                    if(body) {
+                        body = Buffer.concat([body, data])
+                    } else {
+                        body = data;
+                    }
+                }).on("end", () => {
+                    addBufferToBinary(flashContents, fileName, body);
+                }).on("error", callback);
+
+            } else {
+                entry.autodrain();
+            }
+        }).on("close", () => {
+            console.log("close");
+            callback(null, flashContents);
         });
-    }
-
-
-    unzip(source, files) {
-
-    }
-
-
-    untar(source, files) {
-
-    }
-
-    flash() {
-
-    }
-
-    /**
-     * Checks if the step functionality from the manifest.json file exists
-     * in the {ManifestPreparer}
-     * @private
-     */
-    _validateSteps() {
-        this.steps
-            .map(this._getStepName)
-            .forEach(step => {
-                if (!(typeof this[step] === "function")) {
-                    throw `${step} is not a valid step`;
-                }
-            });
-    }
-
-    _getStepName(step) {
-        return Object.keys(step)[0];
-    }
+        response.on("error", callback);
+    });
 }
 
-
-module.exports = {ManifestPreparer};
+module.exports = prepareBinaries;

+ 12 - 39
manifestly/manifest.json

@@ -2,50 +2,23 @@
   "name": "Espruino 1v85",
   "board": "ESP8266 ESP-12",
   "description": "Official Binaries for the Espruino Runtime for the ESP8266 MCU ESP-12",
-  "download": "http://www.espruino.com/files/espruino_1v85.zip",
-  "steps": [
+  "download": "http://0.0.0.0:8080/espruino_1v85.zip",
+  "flash": [
     {
-      "unzip": {
-        "source": "espruino_1v85.zip",
-        "files": ["espruino_1v85_esp8266.tgz"]
-      }
+      "address": "0x0000",
+      "path": "espruino_1v85_esp8266/boot_v1.4(b1).bin"
     },
     {
-      "untar": {
-        "source" :"espruino_1v85_esp8266.tgz",
-        "files": [
-          "boot_v1.4(b1).bin",
-          "espruino_esp8266_user1.bin",
-          "esp_init_data_default.bin",
-          "blank.bin"
-        ]
-      }
+      "address": "0x1000",
+      "path": "espruino_1v85_esp8266/espruino_esp8266_user1.bin"
     },
     {
-      "flash": {
-        "baud": 115200,
-        "frequency": "80m",
-        "mode": "qio",
-        "size": "32m",
-        "files": [
-        {
-          "address": "0x0000",
-          "binary": "boot_v1.4(b1).bin"
-        },
-        {
-          "address": "0x1000",
-          "binary": "espruino_esp8266_user1.bin"
-        },
-        {
-          "address": "0x3FC000",
-          "binary": "esp_init_data_default.bin"
-        },
-        {
-          "address": "0x3FE000",
-          "binary": "blank.bin"
-        }
-        ]
-      }
+      "address": "0x3FC000",
+      "path": "espruino_1v85_esp8266/esp_init_data_default.bin"
+    },
+    {
+      "address": "0x3FE000",
+      "path": "espruino_1v85_esp8266/blank.bin"
     }
   ]
 }