Last active
October 27, 2016 02:38
-
-
Save ril3y/7b21a10d4d18e7549e85e23c86053344 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var serialport = require("serialport") | |
| var color = require("colors") | |
| var EventEmitter = require('events').EventEmitter; | |
| var util = require('util'); | |
| var q = require('q'); | |
| var fs = require('fs'); | |
| const commandLineArgs = require("command-line-args"); | |
| /* | |
| on("read") can then call needsWrite(), and needsWrite(), and on('written') can call read() | |
| read(), later on('readDone') calls write(), later on('writeDone') calls read() | |
| eventually write() calls promise.fulfill() | |
| you don't *need* promises. events are good for what events do. | |
| promise to open port | |
| promise to flash board | |
| use events (functions) inside of the flash_board | |
| */ | |
| const optionDefinitions = [ | |
| { name: 'port', alias:"p", | |
| file: 'file', alias: "f"} | |
| //{ name: 'src', type: String, multiple: true, defaultOption: true }, | |
| //{ name: 'timeout', alias: 't', type: Number } | |
| ] | |
| const options = commandLineArgs(optionDefinitions); | |
| var atmel_bootloader_Options = { | |
| baudRate: 1200, | |
| flowcontrol: ['RTSCTS'], | |
| // Provide our own custom parser: | |
| timedSendsOnly: false | |
| }; | |
| var atmel_flashing_Options = { | |
| baudRate: 115200, | |
| flowcontrol: ['RTSCTS'], | |
| // Provide our own custom parser: | |
| timedSendsOnly: false | |
| }; | |
| // var PORT = "/dev/cu.usbmodem14221" | |
| var PORT = options.port; | |
| if(PORT == true) //dumb hack needed for vsc | |
| PORT = '/dev/cu.usbmodem142111'; | |
| var enterBootloaderMode = function(){ | |
| return q.Promise(function(resolve, reject, notify){ | |
| var _p = new serialport(PORT, atmel_bootloader_Options) | |
| _p.on('open',function(){ | |
| //Close when we open to kick board into bootloader | |
| console.log("1200 baud Port Opened"); | |
| _p.close(); | |
| }) | |
| _p.on('close', function(){ | |
| console.log("Closed 1200 baud"); | |
| resolve(); | |
| }) | |
| }) | |
| } | |
| var getPort = function(){ | |
| return q.Promise(function(resolve, reject, notify){ | |
| var sProm = enterBootloaderMode().then(function(){ | |
| var s = new serialport(PORT, atmel_flashing_Options) | |
| s.on('open', function(s){ | |
| console.log("Port Opened"); | |
| resolve(this); | |
| }); //end s.on() | |
| }); //then end | |
| }); //end promise | |
| } | |
| var startFlash = function(port){ | |
| var self = this; | |
| var _w = function(msg, data){ | |
| console.log("[W] " + msg + " " + data); | |
| this.write(data); | |
| } | |
| console.log("Starting to flash board @ " + port.path); | |
| _w("Set board to binary ", "%N"); | |
| } | |
| var s = getPort() | |
| s.then(function(val){ | |
| startFlash(val) | |
| }); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment