extends uci-i2c-device to support mcp 230xx chips via UCI packets
 
Go to file
David Kebler c83c0914b7 aggreate class with event emitter, add bubble up event emitter from interrupts to class 2017-05-30 13:26:49 -07:00
demo aggreate class with event emitter, add bubble up event emitter from interrupts to class 2017-05-30 13:26:49 -07:00
src aggreate class with event emitter, add bubble up event emitter from interrupts to class 2017-05-30 13:26:49 -07:00
test aggreate class with event emitter, add bubble up event emitter from interrupts to class 2017-05-30 13:26:49 -07:00
.gitignore initial 2017-01-12 20:05:20 -08:00
.npmignore initial 2017-01-12 20:05:20 -08:00
.travis.yml initial 2017-01-12 20:05:20 -08:00
package.json aggreate class with event emitter, add bubble up event emitter from interrupts to class 2017-05-30 13:26:49 -07:00
readme.md improved testing, significant start to readme, added sample demo 2017-05-28 00:00:26 -07:00

readme.md

uCOMmandIt MCP - An I2C Micro Chip NodeJS Class Library

Build Status Inline docs Dependencies devDependencies codecov

What is it

This module contains ES6 Classes for the Micro Chip (MCP) I2C GPIO (general purpose input output) expander chips. The chips contain 8 GPIO pins organized in ports. The MCP23008 has a single port and the MCP23017 has two ports (A,B). Each port has a single interrupt pin which can be used to determine when any pin of a port has changed state. With this module after creating an instance of a chip class you'll be able to easily read and write to the chip's pins with simple methods such as .read(),.on()',.off(), and .toggle()

What's it good for

Lots of I2C devices make use of these chips. In particular many of Control Everything's boards use these chips including their relay boards and their general purpose DIO boards. To control and read them (and any other board using these chips) using nodejs all you need is this module!

Prerequisites

  1. You'll need a computer with an I2C interface which is typically a set of reserved GPIO pins found on most ARM based single board computer (SBC) like the Raspberry Pi. You'll probably want a pin header shield (e.g. Control Everything's RPi shield) to make connecting your IC2 MCP device to your SBC easy.

  2. You'll need to have an I2C bus interface library with NodeJS bindings and it will need to compile on your specific SBC. One such library, Fivdi's I2C-Bus, already exists for the Raspberry Pi and will likely compile on many other ARM7 SBCs (e.g. BeagleBone Black). This module makes use the Fivdi I2C library via the UCI-I2C module. It is possible that other I2C libraries with a nodeJS binding could be used but for now the Fivdi library is the only one.

Getting Started

  1. Prepare an SBC (e.g. Raspberry Pi) with an appropriate OS (e.g. Raspbian).

  2. Connect your MCP based I2C device and check that the SBC can talk to it usingi2cdetect which is part of the i2ctools linux package. Assuming your I2c linux device is number 1 that would be

    sudo i2cdetect -y 1

  3. Install the latest nodeJS. NOTE! You MUST be using at least node 7.6 for this module to work so just load and maintain the latest version for the best result. Node source has the easiest way to do that.

    Using Ubuntu
    curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
    Using Debian, as root
    curl -sL https://deb.nodesource.com/setup_7.x | bash -
    apt-get install -y nodejs
    
  4. Start an npm project and install the uci mpc module

npm init npm install @uci/mcp

  1. Now simply run the sample.js code in the demo/ directory by typing

npm run sample

if all went well the device should have been written to and read back and you can confirm the writes and reads were correct by looking at the console output. A more robust way to see that you've succeeded in talking to the mcp chip is the run the test suite

npm run test

Using the module

and here is the code

const Bus = require('@uci/i2c').Bus,
	MCP = require('../lib/mcp23008-17'),
	// use this require when using your own code
	// MCP = require('@uci/mcp')
	pause = require('@uci/utils').pPause

let bus = new Bus()

let ADDR = 0x24
let CHIP = 'MCP23017'
let MS = 500 // so humans can watch the light otherwise set to zero
let PORT = 'A'

let mcp = new MCP[CHIP](bus, ADDR);

(async function () {

	await mcp.init()
	await mcp.allOff(PORT) // start clean
	console.log('all pins off')
	await pause(MS)

	let pins = [1, 7, 8]
	await mcp.on(pins, PORT, 'PLC')
	let result = await mcp.readPort(PORT, { format: 'PLC' })
	console.log(`${pins} on = current state: ${result.sort()}`)
	await pause(MS)

	pins = [2, 1, 6]
	await mcp.toggle(pins, PORT, 'PLC')
	result = await mcp.readPort(PORT, { format: 'PLC' })
	console.log(`${pins} toggle = current state: ${result.sort()}`)
	await pause(MS)

	pins = [2, 1, 7]
	await mcp.off(pins, PORT, 'PLC')
	result = await mcp.readPort(PORT, { format: 'PLC' })
	console.log(`${pins} off = current state: ${result.sort()}`)
	await pause(MS)

	console.log('all pins off')
	await mcp.allOff(PORT) // clear port after each test

})()