Last active
June 5, 2016 02:03
-
-
Save vicatcu/1ed8e88463878943de9108dcae9fdc6c 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 promiseDoWhilst = require('promise-do-whilst'); | |
var Board = require('firmata'); | |
var Promise = require('bluebird'); | |
let gBoard = null; | |
// returns a promised of a connectable port | |
let firmataRequestPort = () => { | |
return Promise.try(() => { | |
return new Promise((resolve, reject) => { | |
Board.requestPort((err, port) => { | |
if(err){ | |
reject(err); | |
} | |
else{ | |
resolve(port); | |
} | |
}); | |
}); | |
}) | |
}; | |
// returns a promised of a board | |
let createFirmataBoardFromPort = (port) => { | |
let thePort = port; | |
return Promise.try(() => { | |
let board = new Board(thePort.comName); | |
gBoard = board; | |
board.on("string", function (message) { | |
console.log(`Info: Received String: ${message}`); | |
}); | |
board.on("disconnect", () => { | |
console.log(`Info: Board Disconnected!`); | |
}); | |
return new Promise((resolve, reject) => { | |
board.on("ready", () => { | |
console.log("Info: Firmata Ready! Good news."); | |
resolve(board); | |
}); | |
}); | |
}); | |
}; | |
let setPinMode = (pin, mode) => { | |
return Promise.try(() => { | |
if(variableIsNormalInteger(mode) && variableIsNormalInteger(pin)){ | |
if(gBoard && gBoard.pins && gBoard.pins[pin]){ | |
if(gBoard.pins[pin].mode !== mode){ | |
gBoard.pinMode(pin, mode); | |
} | |
} | |
} | |
}); | |
}; | |
let configureAndRegisterInterestInInputs = () => { | |
return Promise.try(() => { | |
const IDENTITY_PINS = [7, 8, 19]; | |
return IDENTITY_PINS; | |
}).each((pin) => { | |
let thePin = pin; | |
return Promise.try(() => { | |
return setPinMode(thePin, gBoard.MODES.INPUT); | |
}).then(() => { | |
gBoard.digitalWrite(thePin, 1); // configure internal pullups | |
gBoard.reportDigitalPin(thePin, 1); // configure reporting | |
}); | |
}); | |
}; | |
let myDelay = (ms) => { | |
let milliseconds = ms; | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
resolve(); | |
}, milliseconds) | |
}); | |
}; | |
let pollInputsForever = () => { | |
return promiseDoWhilst(() => { | |
// ... do this action | |
return Promise.try(() => { | |
console.log(gBoard.pins) | |
}).then(() => { | |
return myDelay(1000); // delay for a second | |
}).catch((error) => { | |
console.log(error); | |
}); | |
}, () => { | |
return true; // forever | |
// ... while this condition holds | |
}); | |
}; | |
// putting it all together | |
Promise.try(() => { | |
return firmataRequestPort(); | |
}).then((port) => { | |
return createFirmataBoardFromPort(port); | |
}).then(() => { | |
return configureAndRegisterInterestInInputs(); | |
}).then(() =>{ | |
return pollInputsForever(); | |
}).catch((error) => { | |
console.log(error); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment