Skip to content

Instantly share code, notes, and snippets.

@philippeantoine
Created May 1, 2018 20:16
Show Gist options
  • Save philippeantoine/6a97382617cb6fa2ffbbd3fc443f3656 to your computer and use it in GitHub Desktop.
Save philippeantoine/6a97382617cb6fa2ffbbd3fc443f3656 to your computer and use it in GitHub Desktop.
2018-05-01-ArduinoButton
var argv = require('optimist')
.usage('Arduino deploy button')
.demand('d')
.describe('d', 'project directory')
.argv
var five = require("johnny-five")
var spawn = require('child_process').spawn
var board = new five.Board()
var dir = argv.d
function startDeploy(cb) {
console.log("starting deploy")
var sh = spawn("git", ["push", "heroku", "master"], {cwd:dir})
sh.stdout.setEncoding('utf8')
sh.stderr.setEncoding('utf8')
sh.on('close', function(exitCode) {
if (exitCode == 0) {
cb(null, true)
} else {
cb("deploy failed, exit code: " + exitCode, true)
}
})
sh.stdout.on('data', function(data) {
console.log(data)
})
sh.stderr.on('data', function(data) {
console.log(data)
})
}
board.on("ready", function() {
var greenLed = new five.Led(2)
var redLed = new five.Led(3)
var button = new five.Button(4)
var inProgress = false
function go() {
if (inProgress) {
console.log("deploy already in progress")
return
}
inProgress = true
redLed.strobe()
greenLed.stop().off()
startDeploy(function(err) {
inProgress = false
if (err) {
console.log("deploy failed!")
redLed.stop()
redLed.on()
greenLed.stop().off()
return
}
console.log("deploy success!")
redLed.stop().off()
greenLed.on()
})
}
// Start deploy when button is released
button.on("up", function() {
go()
})
})
{
"name": "2018-05-01-ArduinoButton",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {},
"devDependencies": {
"johnny-five": "^0.14.3",
"optimist": "^0.6.1"
},
"scripts": {
"start": "node index.js -d node-js-getting-started",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "MIT"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment