From 635d7fc35e239dfb55258edd765f3616b0ba4076 Mon Sep 17 00:00:00 2001 From: David Kebler Date: Sun, 15 Jan 2017 17:59:50 -0800 Subject: [PATCH] refactor bus and device class, testing of i2c r/w --- lib/mcp23008-17.js | 51 ++++++++++++++++++++++++++++++++++++++--- test/mcp.test.js | 56 ++++++++++++++++++++++++++++------------------ 2 files changed, 82 insertions(+), 25 deletions(-) diff --git a/lib/mcp23008-17.js b/lib/mcp23008-17.js index a818858..0673d6e 100644 --- a/lib/mcp23008-17.js +++ b/lib/mcp23008-17.js @@ -15,7 +15,7 @@ class MCP23008 extends Device { this.ports.A = new Port(opts) // if not specified there RPi is not managing the interrupt // pin number on RPi that is connected to and services the interrupt. 4/17/27/22 = 7/11/13/15 - this.ports.A.interPin = opts.interPin + this.ports.A.interPin = opts.interPin.A ? opts.interPin.A : opts.interPin } // end constructor init() { @@ -49,7 +49,53 @@ class MCP23008 extends Device { return pSeries(jobs) } // end writePinsCfg + // reads all pins in all ports + read(cmd, fmt) { + let jobs = [] + cmd = cmd ? cmd : 'gpio' + let reg = registers.pin_cmd[cmd] + for (let port in this.ports) { + jobs.push(() => super.read(reg)) + reg += 0x10 + } + return pSeries(jobs) + + } + + write() { + + } + pinRead(id, opts) { + let mcpdev = this; + return new Promise(function (resolve, reject) { + let cmd = opts.cmd ? opts.cmd : 'gpio' + let fmt = opts.fmt ? opts.fmt : { in: 'PLC', out: 'PLC' } + let reg = registers.pin_cmd[cmd] + let gpio = mcpdev.pin(id) + if (!gpio) { + reject('no pin found for given id') + } + if (gpio.port === 'B') { + reg = reg + 16 + } + // call device class read (response is always decimal) + console.log('port - reg', gpio.port, reg) + return mcpdev.read(reg).then(resp => { + resp = _u.byteFormat(resp, { in: 'DEC', + out: 'ARY' + }) + let addr = gpio.pin.address.toFmt('ARY') + console.log(addr) + console.log(resp) + resolve(_u.sum(_u.and(addr, resp))) // resolve 1 or 0 + }) + }) + .then(state => console.log(`pin state ${state}`)) + .catch(err => console.log(err)) // end Promise + } // end pinRead + + pinWrite(id, opts) { let mcpdev = this; return new Promise(function (resolve, reject) { let cmd = opts.cmd ? opts.cmd : 'gpio' @@ -84,10 +130,9 @@ class MCP23017 extends MCP23008 { constructor(busObj, i2cAddress, opts) { super(busObj, i2cAddress, opts) // add a second port - opts.portID = 'B' this.ports.B = new Port(opts) - this.ports.A.interPin = opts.interPin // if not specified the RPi is not managing the interrupt + this.ports.B.interPin = opts.interPin.B ? opts.interPin.B : opts.interPin } pin(id, port) { diff --git a/test/mcp.test.js b/test/mcp.test.js index 8e3c6e1..4e013ce 100644 --- a/test/mcp.test.js +++ b/test/mcp.test.js @@ -49,43 +49,55 @@ it('can set and get a single pin config on both ports', function () { expect(mcp17.pin(1).cfg, "pin configs getter failed").to.deep.equal(config_sets.momentary_switch) - // expect(mcp17.pin(8).cfg.dir, "pin config getter failed").to.equal(0) - // mcp17.pin(8).cfg.dir = 1 - // expect(mcp17.pin(8).config.dir, "pin address setter failed").to.equal(1) - // expect(mcp17.pin(8, 'B').cfg.dir, "pin address getter port B failed").to.equal(0) - // mcp17.pin(8, 'B').cfg.dir = 1 - // expect(mcp17.pin(8, 'B').config.dir, "pin address setter failed").to.equal(1) + expect(mcp17.pin(8).cfg.dir, "pin config getter failed").to.equal(1) + mcp17.pin(8).cfg.dir = 0 + expect(mcp17.pin(8).cfg.dir, "pin address setter failed").to.equal(0) + expect(mcp17.pin(8, 'B').cfg.dir, "pin address getter port B failed").to.equal(1) + mcp17.pin(8, 'B').cfg.dir = 0 + expect(mcp17.pin(8, 'B').config.dir, "pin address setter failed").to.equal(0) }); }); let mcp8 = new MCP.MCP23008(bus1, 0x21, { pin_cfg_default: 'toggle_switch', - name: 'relay 1-8' + name: 'test 1-8' }) describe('MCP23008 Class - ', function () { it('can set and get a single pin config', function () { expect(mcp8.pin(1).cfg, "pin configs getter failed").to.deep.equal(config_sets.toggle_switch) - // expect(mcp8.pin(8).cfg.dir, "pin address getter failed").to.equal(1) - // expect(mcp8.pin(8).cfg.ivrt, "pin address getter failed").to.equal(1) - // expect(mcp8.pin(8).cfg.usedef, "pin address getter failed").to.equal(0) - // mcp8.pin(8).cfg.dir = 0 - // expect(mcp8.pin(8).cfg.dir, "pin address setter failed").to.equal(0) - // expect(mcp8.pin(8).ivrt, "pin address getter failed").to.equal(1) + expect(mcp8.pin(8).cfg.dir, "pin address getter failed").to.equal(1) + expect(mcp8.pin(8).cfg.ivrt, "pin address getter failed").to.equal(1) + expect(mcp8.pin(8).cfg.usedef, "pin address getter failed").to.equal(0) + mcp8.pin(8).cfg.dir = 0 + expect(mcp8.pin(8).cfg.dir, "pin address setter failed").to.equal(0) + expect(mcp8.pin(8).cfg.ivrt, "pin address getter failed").to.equal(1) }); - const ADDR = 0x21, - DIR = 0x00, - RW = 0x09, - ALLPINS = 0x40, - NOPINS = 0x00 + const ADDR = 0x20, + W = 0x09, + R = 0x0A, + PINSON = 129, + PINSOFF = 0x00 - mcp8.write(DIR, 0x00).then(() => - mcp8.write(RW, ALLPINS).then(() => - mcp8.read(RW, ALLPINS).then((resp) => { - console.log('relays on', resp) + let testout = new MCP.MCP23008(bus1, ADDR, { + pin_cfg_default: 'output', + name: 'outputs 1-8' + }) + + let tasks = [ + testout.init(), + testout.write(W, PINSON) + + ] + + testout.init().then(() => + testout.write(W, PINSON).then(() => + testout.read(R).then((resp) => { + console.log('Pins Set On', resp) + testout.write(W, PINSOFF) }) ) )