Private
Public Access
2
0

JS examples: refactor for clarity and consistent style

Signed-off-by: Kassandra Perch <the@nodebotani.st>
Signed-off-by: Alex Tereschenko <alext.mkrs@gmail.com>
This commit is contained in:
Kas Perch
2017-02-23 00:57:19 -06:00
committed by Alex Tereschenko
parent 43d9f6c400
commit 075a7b1225
12 changed files with 145 additions and 127 deletions

View File

@@ -22,11 +22,13 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
var m = require('mraa'); //require mraa "use strict";
console.log('MRAA Version: ' + m.getVersion()); //write the mraa version to the console
var analogPin0 = new m.Aio(0); //setup access analog inpuput pin 0 const mraa = require('mraa'); //require mraa
var analogValue = analogPin0.read(); //read the value of the analog pin console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the console
var analogValueFloat = analogPin0.readFloat(); //read the pin value as a float
let analogPin0 = new mraa.Aio(0); //setup access analog inpuput pin 0
let analogValue = analogPin0.read(); //read the value of the analog pin
let analogValueFloat = analogPin0.readFloat(); //read the pin value as a float
console.log(analogValue); //write the value of the analog pin to the console console.log(analogValue); //write the value of the analog pin to the console
console.log(analogValueFloat.toFixed(5)); //write the value in the float format console.log(analogValueFloat.toFixed(5)); //write the value in the float format

View File

@@ -22,19 +22,18 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
var m = require('mraa'); //require mraa "use strict";
console.log('MRAA Version: ' + m.getVersion()); //write the mraa version to the console
var myLed = new m.Gpio(13); //LED hooked up to digital pin 13 (or built in pin on Galileo Gen1 & Gen2) const mraa = require('mraa'); //require mraa
myLed.dir(m.DIR_OUT); //set the gpio direction to output console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the console
var ledState = true; //Boolean to hold the state of Led
function periodicActivity() let myLed = new mraa.Gpio(13); //LED hooked up to digital pin 13 (or built in pin on Galileo Gen1 & Gen2)
{ myLed.dir(mraa.DIR_OUT); //set the gpio direction to output
let ledState = true; //Boolean to hold the state of Led
function periodicActivity() {
myLed.write(ledState ? 1 : 0); //if ledState is true then write a '1' (high) otherwise write a '0' (low) myLed.write(ledState ? 1 : 0); //if ledState is true then write a '1' (high) otherwise write a '0' (low)
ledState = !ledState; //invert the ledState ledState = !ledState; //invert the ledState
setTimeout(periodicActivity,1000); //call the indicated function after 1 second (1000 milliseconds)
} }
periodicActivity(); //call the periodicActivity function setInterval(periodicActivity, 1000); //call the periodicActivity function every second

View File

@@ -22,17 +22,17 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
var m = require('mraa'); //require mraa "use strict";
console.log('MRAA Version: ' + m.getVersion()); //write the mraa version to the console
var myDigitalPin = new m.Gpio(6); //setup digital read on pin 6 const mraa = require('mraa'); //require mraa
myDigitalPin.dir(m.DIR_IN); //set the gpio direction to input console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the console
periodicActivity(); //call the periodicActivity function let myDigitalPin = new mraa.Gpio(6); //setup digital read on pin 6
myDigitalPin.dir(mraa.DIR_IN); //set the gpio direction to input
function periodicActivity() // function periodicActivity() {
{ let myDigitalValue = myDigitalPin.read(); //read the digital value of the pin
var myDigitalValue = myDigitalPin.read(); //read the digital value of the pin console.log('Gpio value is ' + myDigitalValue); //write the read value out to the console
console.log('Gpio is ' + myDigitalValue); //write the read value out to the console
setTimeout(periodicActivity,1000); //call the indicated function after 1 second (1000 milliseconds)
} }
setInterval(periodicActivity, 1000); //call the indicated function every 1 second (1000 milliseconds)

View File

@@ -22,9 +22,11 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
var m = require('mraa'); //require mraa "use strict";
console.log('MRAA Version: ' + m.getVersion()); //write the mraa version to the console
var myDigitalPin = new m.Gpio(5); //setup digital read on pin 5 const mraa = require('mraa'); //require mraa
myDigitalPin.dir(m.DIR_OUT); //set the gpio direction to output console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the console
let myDigitalPin = new mraa.Gpio(5); //setup digital read on pin 5
myDigitalPin.dir(mraa.DIR_OUT); //set the gpio direction to output
myDigitalPin.write(1); //set the digital pin to high (1) myDigitalPin.write(1); //set the digital pin to high (1)

View File

@@ -24,29 +24,32 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
var m = require('mraa'); //require mraa "use strict";
x = new m.I2c(0) const mraa = require('mraa'); //require mraa
x.address(0x77)
let i2cDevice = new mraa.I2c(0);
i2cDevice.address(0x77);
// initialise device // initialise device
if (x.readReg(0xd0) != 0x55) { if (i2cDevice.readReg(0xd0) != 0x55) {
console.log("error"); console.log("error");
} }
// we want to read temperature so write 0x2e into control reg // we want to read temperature so write 0x2e into control reg
x.writeReg(0xf4, 0x2e) i2cDevice.writeReg(0xf4, 0x2e);
// read a 16bit reg, obviously it's uncalibrated so mostly a useless value :) // read a 16bit reg, obviously it's uncalibrated so mostly a useless value :)
console.log(x.readWordReg(0xf6)) console.log(i2cDevice.readWordReg(0xf6));
// and we can do the same thing with the read()/write() calls if we wished // and we can do the same thing with the read()/write() calls if we wished
// thought I'd really not recommend it! // thought I'd really not recommend it!
buf = new Buffer(2) let buf = new Buffer(2);
buf[0] = 0xf4 buf[0] = 0xf4;
buf[1] = 0x2e buf[1] = 0x2e;
console.log(buf.toString('hex')) console.log(buf.toString('hex'));
x.write(buf) i2cDevice.write(buf);
x.writeByte(0xf6) i2cDevice.writeByte(0xf6);
d = x.read(2) let result = i2cDevice.read(2);
console.log(result.toString('hex'));

View File

@@ -22,7 +22,9 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
var mraa = require('mraa'); "use strict";
const mraa = require('mraa');
console.log('MRAA Version: ' + mraa.getVersion()); console.log('MRAA Version: ' + mraa.getVersion());
// open connection to firmata // open connection to firmata

View File

@@ -22,7 +22,9 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
var mraa = require('mraa'); "use strict";
const mraa = require('mraa');
const readline = require('readline'); const readline = require('readline');
const rl = readline.createInterface({ const rl = readline.createInterface({
@@ -42,16 +44,16 @@ function getVersion() {
} }
function setPin() { function setPin() {
var pinNumber = arguments[0]; let pinNumber = arguments[0];
var pinValue = arguments[1]; let pinValue = arguments[1];
var pin = new mraa.Gpio(pinNumber); let pin = new mraa.Gpio(pinNumber);
pin.dir(mraa.DIR_OUT); pin.dir(mraa.DIR_OUT);
pin.write(pinNumber, pinValue); pin.write(pinNumber, pinValue);
} }
function getPin() { function getPin() {
var pinNumber = arguments[0]; let pinNumber = arguments[0];
var pin = new mraa.Gpio(pinNumber); let pin = new mraa.Gpio(pinNumber);
pin.dir(mraa.DIR_IN); pin.dir(mraa.DIR_IN);
console.log('Gpio ' + pinNumber + ' = ' + pin.read()); console.log('Gpio ' + pinNumber + ' = ' + pin.read());
} }
@@ -61,9 +63,9 @@ function onPinLevelChange() {
} }
function monitorPin() { function monitorPin() {
var pinNumber = arguments[0]; let pinNumber = arguments[0];
try { try {
var pin = new mraa.Gpio(pinNumber); let pin = new mraa.Gpio(pinNumber);
pin.dir(mraa.DIR_IN); pin.dir(mraa.DIR_IN);
pin.isr(mraa.EDGE_BOTH, onPinLevelChange); pin.isr(mraa.EDGE_BOTH, onPinLevelChange);
rl.question('Press ENTER to stop', function(answer) { rl.question('Press ENTER to stop', function(answer) {
@@ -77,23 +79,24 @@ function monitorPin() {
const args = process.argv; const args = process.argv;
const argc = args.length; const argc = args.length;
if (argc >= 3) { if (argc >= 3) {
const cmd = args[2]; if (argc > 3) {
const pinNumber = parseInt(args[3]);
}
switch (args[2]) { switch (args[2]) {
case "version": case "version":
getVersion(); getVersion();
break; break;
case "get": case "get":
var pinNumber = parseInt(args[3]);
getPin(pinNumber); getPin(pinNumber);
break; break;
case "set": case "set":
var pinNumber = parseInt(args[3]); let pinValue = parseInt(args[4]);
var pinValue = parseInt(args[4]);
getPin(pinNumber, pinValue); getPin(pinNumber, pinValue);
break; break;
case "monitor": case "monitor":
var pinNumber = parseInt(args[3]);
monitorPin(pinNumber); monitorPin(pinNumber);
break; break;
default: default:
@@ -104,4 +107,3 @@ if (argc >= 3) {
console.log("Command not specified"); console.log("Command not specified");
printUsage(); printUsage();
} }

View File

@@ -22,27 +22,29 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
var m = require('mraa'); "use strict";
const mraa = require('mraa');
// GPIO-PIN // GPIO-PIN
m.gpioFromDesc("gpio-1"); mraa.gpioFromDesc("gpio-1");
// GPIO-RAW-RAWPIN // GPIO-RAW-RAWPIN
m.gpioFromDesc("gpio-raw-131"); mraa.gpioFromDesc("gpio-raw-131");
// AIO-PIN // AIO-PIN
m.aioFromDesc("aio-1"); mraa.aioFromDesc("aio-1");
//PWM-PIN //PWM-PIN
m.pwmFromDesc("pwm-6"); mraa.pwmFromDesc("pwm-6");
// PWM-RAW-CHIPID-PIN // PWM-RAW-CHIPID-PIN
m.pwmFromDesc("pwm-raw-0-1") mraa.pwmFromDesc("pwm-raw-0-1")
// UART-INDEX: the index is the one represented internally in the uart_dev array // UART-INDEX: the index is the one represented internally in the uart_dev array
m.uartFromDesc("uart-0"); mraa.uartFromDesc("uart-0");
// UART-RAW-PATH // UART-RAW-PATH
m.uartFromDesc("uart-raw-/dev/ttyS0"); mraa.uartFromDesc("uart-raw-/dev/ttyS0");
// SPI-INDEX: same as UART // SPI-INDEX: same as UART
m.spiFromDesc("spi-0"); mraa.spiFromDesc("spi-0");
// SPI-RAW-BUS-CS: USED to open and use /dev/spidev<BUS>.<CS> // SPI-RAW-BUS-CS: USED to open and use /dev/spidev<BUS>.<CS>
m.spiFromDesc("spi-raw-0-1"); mraa.spiFromDesc("spi-raw-0-1");
// I2C-INDEX: same as UART // I2C-INDEX: same as UART
m.i2cFromDesc("i2c-0"); mraa.i2cFromDesc("i2c-0");
// I2C-RAW-BUS // I2C-RAW-BUS
m.i2cFromDesc("i2c-raw-0"); mraa.i2cFromDesc("i2c-raw-0");

View File

@@ -1,16 +1,18 @@
#!/usr/bin/env node #!/usr/bin/env node
var m = require('mraa') "use strict";
function h() { const mraa = require('mraa');
console.log("HELLO!!!!")
function hello() {
console.log("HELLO!!!!");
} }
x = new m.Gpio(14) let pin = new mraa.Gpio(14);
x.isr(m.EDGE_BOTH, h) pin.isr(mraa.EDGE_BOTH, hello);
setInterval(function() { setInterval(function() {
// It's important to refer to our GPIO context here, // It's important to refer to our GPIO context here,
// otherwise it will be garbage-collected // otherwise it will be garbage-collected
console.log("Waiting for an interrupt at GPIO pin " + x.getPin() + "...") console.log("Waiting for an interrupt at GPIO pin " + pin.getPin() + "...");
}, 10000) }, 10000);

View File

@@ -22,13 +22,15 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
var m = require('mraa'); //require mraa "use strict";
x = new m.I2c(0) const mraa = require('mraa'); //require mraa
x.address(0x62)
x.writeReg(0, 0)
x.writeReg(1, 0)
x.writeReg(0x08, 0xAA) let i2cDevice = new mraa.I2c(0);
x.writeReg(0x04, 255) i2cDevice.address(0x62);
x.writeReg(0x02, 255) i2cDevice.writeReg(0, 0);
i2cDevice.writeReg(1, 0);
i2cDevice.writeReg(0x08, 0xAA);
i2cDevice.writeReg(0x04, 255);
i2cDevice.writeReg(0x02, 255);

View File

@@ -24,15 +24,15 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
var m = require('mraa'); //require mraa "use strict";
x = new m.Spi(0)
buf = new Buffer(4)
buf[0] = 0xf4
buf[1] = 0x2e
buf[2] = 0x3e
buf[3] = 0x4e
buf2 = x.write(buf)
console.log("Sent: " + buf.toString('hex') + ". Received: " + buf2.toString('hex'))
const mraa = require('mraa'); //require mraa
let spiDevice = new mraa.Spi(0);
let buf = new Buffer(4);
buf[0] = 0xf4;
buf[1] = 0x2e;
buf[2] = 0x3e;
buf[3] = 0x4e;
let buf2 = spiDevice.write(buf);
console.log("Sent: " + buf.toString('hex') + ". Received: " + buf2.toString('hex'));

View File

@@ -22,9 +22,11 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
var m = require('mraa'); //require mraa "use strict";
console.log('MRAA Version: ' + m.getVersion());
u = new m.Uart(0) const mraa = require('mraa'); //require mraa
console.log('MRAA Version: ' + mraa.getVersion());
let uart = new mraa.Uart(0);
console.log("Note: connect Rx and Tx of UART with a wire before use"); console.log("Note: connect Rx and Tx of UART with a wire before use");
@@ -35,20 +37,20 @@ function sleep(delay) {
console.log("Set UART parameters"); console.log("Set UART parameters");
u.setBaudRate(115200); uart.setBaudRate(115200);
u.setMode(8, 0, 1); uart.setMode(8, 0, 1);
u.setFlowcontrol(false, false); uart.setFlowcontrol(false, false);
sleep(200); sleep(200);
console.log("First write-read circle:"); console.log("First write-read circle:");
u.writeStr("test\n"); uart.writeStr("test\n");
sleep(200); sleep(200);
console.log(u.readStr(6)); console.log(uart.readStr(6));
sleep(200); sleep(200);
console.log("Second write-read circle:"); console.log("Second write-read circle:");
u.writeStr("2nd test\n"); uart.writeStr("2nd test\n");
sleep(200); sleep(200);
console.log(u.readStr(10)); console.log(uart.readStr(10));