Skip to content

Instantly share code, notes, and snippets.

@appsforartists
Last active January 9, 2017 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save appsforartists/0df4f27ac8f37668813bd2a70e2b18ee to your computer and use it in GitHub Desktop.
Save appsforartists/0df4f27ac8f37668813bd2a70e2b18ee to your computer and use it in GitHub Desktop.
Testing Android Accessory Development Kit in JavaScript
/** @license
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const {
Board,
Button,
Led,
Light,
Thermometer,
} = require('johnny-five');
const board = new Board();
// Demo Shield pins: http://jeffreysambells.com/2011/05/17/understanding-the-demokit-pde-arduino-sketch
board.on(
'ready',
() => {
const led1 = new Led.RGB({
pins: {
red: 8,
blue: 9,
green: 10,
},
isAnode: true
});
const led2 = new Led.RGB({
pins: {
red: 5,
blue: 6,
green: 7,
},
isAnode: true
});
const led3 = new Led.RGB({
pins: {
red: 2,
blue: 3,
green: 4,
},
isAnode: true
});
led1.color('#673AB7');
led2.color('#FFA726');
led3.color('#DCE775');
const button1 = new Button({
pin: 'A6',
isPullup: true,
});
const button2 = new Button({
pin: 'A7',
isPullup: true,
});
const button3 = new Button({
pin: 'A8',
isPullup: true,
});
button1.on(
'down',
() => {
console.log('button 1 down');
led1.toggle();
}
);
button2.on(
'down',
() => {
console.log('button 2 down');
led2.toggle();
}
);
button3.on(
'down',
() => {
console.log('button 3 down');
led3.toggle();
}
);
// Joystick uses an I2C, which JohnnyFive doesn't seem to support, so we
// can only read if its button is being pressed
const joystickButton = new Button({
pin: 'A9',
isPullup: true,
});
joystickButton.on(
'down',
() => {
console.log('joystick down');
led1.toggle();
led2.toggle();
led3.toggle();
}
);
const photo = new Light('A2');
photo.on(
'change',
() => {
console.log(
'photo: ',
Math.round(photo.value / 1000),
photo.level
);
}
);
// // seems to stop changing when the LEDs go out?
// const temperature = new Thermometer({
// controller: 'LM35',
// pin: 'A3',
// });
// temperature.on('change', () => console.log('temperature: ', temperature.F));
// touchIn.on('change', onChange);
// touchOut.on('change', onChange);
// function onChange() {
// console.log(
// touchOut.value,
// touchIn.value,
// touchIn.value - touchOut.value
// );
// }
}
);
@soundanalogous
Copy link

Johnny-five supports i2c via firmata.js. Here's an example. You should simply be able to call either board.i2cConfig, board.i2cRead, etc or board.io.i2cConfig, board.io.i2cRead, etc from within your J5 application since J5 uses firmata.js to communicate with the Arduino board. You'll find the J5 example for the same accelerometer here. Note the Controller pattern used to create a hardware abstraction layer (HAL) in the J5 architecture.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment