Skip to content

Commit

Permalink
Examples: modernize example program code syntax
Browse files Browse the repository at this point in the history
Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
  • Loading branch information
rwaldron committed Apr 24, 2017
1 parent 70c45de commit aaa23fd
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 125 deletions.
19 changes: 10 additions & 9 deletions eg/analog-read.js
@@ -1,16 +1,17 @@
// process.env.IS_TEST_MODE = true;
var Tessel = require("../lib/");
var board = new Tessel();
"use strict";

board.on("ready", function() {
const Tessel = require("../lib/");
const board = new Tessel();
board.on("ready", () => {
console.log("Ready");
this.pinMode("a7", this.MODES.ANALOG);
this.analogRead("a7", function(data) {
process.stdout.write("\r \033[36mA\033[m " + padLeft(data, 4, 0));
board.pinMode("a7", board.MODES.ANALOG);
board.analogRead("a7", (data) => {
process.stdout.write(`\r \u001b[36mA\u001b[39m ${padStart(data, 4, 0)}`);
});
});

function padLeft(value, l, c) {
value = String(value);
return Array(l - value.length + 1).join(c || " ") + value;
function padStart(v, l, c) {
v = String(v);
return Array(l - v.length + 1).join(c || " ") + v;
}
12 changes: 7 additions & 5 deletions eg/digital-read.js
@@ -1,12 +1,14 @@
// process.env.IS_TEST_MODE = true
var Tessel = require("../lib/");
var board = new Tessel();
"use strict";

board.on("ready", function() {
const Tessel = require("../lib/");
const board = new Tessel();

board.on("ready", () => {
console.log("Ready");

this.pinMode("b2", this.MODES.INPUT);
this.digitalRead("b2", function(data) {
board.pinMode("b2", board.MODES.INPUT);
board.digitalRead("b2", data => {
// Connect a button or some similar digital signal control
console.log(data);
});
Expand Down
18 changes: 10 additions & 8 deletions eg/digital-write.js
@@ -1,15 +1,17 @@
// process.env.IS_TEST_MODE = true;
var Tessel = require("../lib/");
var board = new Tessel();
"use strict";

board.on("ready", function() {
const Tessel = require("../lib/");
const board = new Tessel();

board.on("ready", () => {
console.log("Ready");

this.pinMode("b0", this.MODES.OUTPUT);
this.digitalWrite("b0", this.HIGH);
board.pinMode("b0", board.MODES.OUTPUT);
board.digitalWrite("b0", board.HIGH);

setTimeout(function() {
this.digitalWrite("b0", this.LOW);
setTimeout(() => {
board.digitalWrite("b0", board.LOW);
process.exit(0);
}.bind(this), 1000);
}, 1000);
});
74 changes: 32 additions & 42 deletions eg/i2c-blinkm-controller.js
@@ -1,24 +1,20 @@
// process.env.IS_TEST_MODE = true;
var Tessel = require("../lib/");
var board = new Tessel();
"use strict";

var BlinkM = {
0x09: "ADDRESS",
0x63: "FADE_TO_RGB",
0x43: "FADE_TO_RANDOM_RGB",
0x70: "SCRIPT_PLAY",
0x6f: "SCRIPT_STOP",
0x6e: "SET_RGB",
0x67: "GET_RGB",
};
const Tessel = require("../lib/");
const board = new Tessel();

Object.keys(BlinkM).forEach(function(key) {
// Turn the value into a key and
// the key into an int value
BlinkM[BlinkM[key]] = key | 0;
});
const BlinkM = {
ADDRESS: 0x09,
FADE_TO_RGB: 0x63,
FADE_TO_RANDOM_RGB: 0x43,
SCRIPT_PLAY: 0x70,
SCRIPT_STOP: 0x6f,
SET_RGB: 0x6e,
GET_RGB: 0x67,
};

var rgb = {
const rgb = {
red: [0xff, 0x00, 0x00],
orange: [0xff, 0x7f, 0x00],
yellow: [0xff, 0xff, 0x00],
Expand All @@ -29,48 +25,42 @@ var rgb = {
white: [0xff, 0xff, 0xff],
};

var rainbow = Object.keys(rgb).reduce(function(colors, color) {
const rainbow = Object.keys(rgb).reduce((colors, color) => {
// While testing, I found that the BlinkM produced
// more vibrant colors when provided a 7 bit value.
return (colors[color] = rgb[color].map(to7bit), colors);
return (colors[color] = rgb[color].map(v => v >> 1), colors);
}, {});

var colors = Object.keys(rainbow);
var index = 0;
const colors = Object.keys(rainbow);
let index = 0;

board.on("ready", function() {
board.on("ready", () => {
console.log("READY");

this.i2cConfig({
board.i2cConfig({
address: BlinkM.ADDRESS,
bus: "A"
});

// http://thingm.com/fileadmin/thingm/downloads/BlinkM_datasheet.pdf
this.i2cWrite(BlinkM.ADDRESS, BlinkM.SCRIPT_STOP);
board.i2cWrite(BlinkM.ADDRESS, BlinkM.SCRIPT_STOP);
board.i2cWrite(BlinkM.ADDRESS, BlinkM.SET_RGB, [0, 0, 0]);

this.i2cWrite(BlinkM.ADDRESS, BlinkM.SET_RGB, [0, 0, 0]);
const cycle = () => {
if (index === colors.length) {
index = 0;
}

setInterval(function() {
var color = colors[index++];
const color = colors[index++];

this.i2cWrite(BlinkM.ADDRESS, BlinkM.FADE_TO_RGB, rainbow[color]);
board.i2cWrite(BlinkM.ADDRESS, BlinkM.FADE_TO_RGB, rainbow[color]);
board.i2cReadOnce(BlinkM.ADDRESS, BlinkM.GET_RGB, 3, (data) => {
console.log(`(${Date.now()}) RGB: [${data}]`, Date.now());

this.i2cReadOnce(BlinkM.ADDRESS, BlinkM.GET_RGB, 3, function(data) {
console.log("(%d) RGB: [%s]", Date.now(), data);
setTimeout(() => cycle, 1000);
});
};

if (index === colors.length) {
index = 0;
}
}.bind(this), 1000);
cycle();
});

function to7bit(value) {
return scale(value, 0, 255, 0, 127) | 0;
}

function scale(value, inMin, inMax, outMin, outMax) {
return (value - inMin) * (outMax - outMin) /
(inMax - inMin) + outMin;
}
30 changes: 16 additions & 14 deletions eg/muilt-function.js
@@ -1,28 +1,30 @@
var Tessel = require("../lib/");
var board = new Tessel();
"use strict";

board.on("ready", function() {
const Tessel = require("../lib/");
const board = new Tessel();

board.on("ready", () => {
console.log("Ready");

// LED -> PORT A Pin 5
this.pinMode("a5", this.MODES.OUTPUT);
board.pinMode("a5", board.MODES.OUTPUT);
// Button -> PORT A Pin 6
this.pinMode("a6", this.MODES.INPUT);
this.digitalRead("a6", function(data) {
if (this.pins[5].value === 0 && data === 1) {
this.digitalWrite("a5", this.HIGH);
board.pinMode("a6", board.MODES.INPUT);
board.digitalRead("a6", data => {
if (board.pins[5].value === 0 && data === 1) {
board.digitalWrite("a5", board.HIGH);
} else {
if (this.pins[5].value === 1 && data === 0) {
this.digitalWrite("a5", this.LOW);
if (board.pins[5].value === 1 && data === 0) {
board.digitalWrite("a5", board.LOW);
}
}
});

// LED -> PORT B Pin 7
this.pinMode("b7", this.MODES.PWM);
board.pinMode("b7", board.MODES.PWM);
// Potentiometer -> PORT A Pin 7
this.pinMode("a7", this.MODES.ANALOG);
this.analogRead("a7", function(data) {
this.pwmWrite("b7", data >> 2);
board.pinMode("a7", board.MODES.ANALOG);
board.analogRead("a7", data => {
board.pwmWrite("b7", data >> 2);
});
});
19 changes: 12 additions & 7 deletions eg/onboard-led.js
@@ -1,15 +1,20 @@
// process.env.IS_TEST_MODE = true;
var Tessel = require("../lib/");
var board = new Tessel();
"use strict";

board.on("ready", function() {
const Tessel = require("../lib/");
const board = new Tessel();

board.on("ready", () => {
console.log("Ready");

var leds = ["L0", "L1", "L2", "L3"];
const leds = ["L0", "L1", "L2", "L3"];
let state = 1;


leds.forEach(led => this.digitalWrite(led, this.HIGH));
leds.forEach(led => board.digitalWrite(led, state));

setTimeout(() => {
leds.forEach(led => this.digitalWrite(led, this.LOW));
setInterval(() => {
state ^= 1;
leds.forEach(led => board.digitalWrite(led, state));
}, 1000);
});
59 changes: 26 additions & 33 deletions eg/pwm-rgb-led.js
@@ -1,39 +1,32 @@
var Tessel = require('../lib/');
var five = require('johnny-five');
"use strict";

var board = new five.Board({
io: new Tessel()
});

board.on("ready", function(){
var led = new five.Led.RGB(["a5","a6","b5"]);
const Tessel = require("../lib/");
const five = require("johnny-five");

// use led.<function> in repl
// after color step-through is over
this.repl.inject({
led: led
});
var colors = [
'red',
'orange',
'yellow',
'green',
'blue'
];

led.on();
const board = new five.Board({
io: new Tessel()
});

var index = 0;
this.loop(1000, function(done){
board.on("ready", () => {
const rgb = new five.Led.RGB(["a5", "a6", "b5"]);
const colors = [
"red",
"orange",
"yellow",
"green",
"blue"
];

if (index >= colors.length){
led.off();
done();
} else {
led.color(colors[index]);
index++;
}
});
rgb.on();

let index = 0;
board.loop(1000, (done) => {
if (index >= colors.length) {
rgb.off();
done();
} else {
rgb.color(colors[index]);
index++;
}
});
});

16 changes: 9 additions & 7 deletions eg/pwm-write.js
@@ -1,14 +1,16 @@
// process.env.IS_TEST_MODE = true;
var Tessel = require("../lib/");
var board = new Tessel();
"use strict";

board.on("ready", function() {
const Tessel = require("../lib/");
const board = new Tessel();

board.on("ready", () => {
console.log("Ready");

this.pinMode("a7", this.MODES.ANALOG);
this.pinMode("b7", this.MODES.PWM);
board.pinMode("a7", board.MODES.ANALOG);
board.pinMode("b7", board.MODES.PWM);

this.analogRead("a7", function(data) {
this.pwmWrite("b7", data >> 2);
board.analogRead("a7", data => {
board.pwmWrite("b7", data >> 2);
});
});

0 comments on commit aaa23fd

Please sign in to comment.