refactor bus and device class, testing of i2c r/w
parent
34b951f528
commit
635d7fc35e
|
@ -15,7 +15,7 @@ class MCP23008 extends Device {
|
||||||
this.ports.A = new Port(opts)
|
this.ports.A = new Port(opts)
|
||||||
// if not specified there RPi is not managing the interrupt
|
// 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
|
// 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
|
} // end constructor
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
|
@ -49,7 +49,53 @@ class MCP23008 extends Device {
|
||||||
return pSeries(jobs)
|
return pSeries(jobs)
|
||||||
} // end writePinsCfg
|
} // 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) {
|
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;
|
let mcpdev = this;
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
let cmd = opts.cmd ? opts.cmd : 'gpio'
|
let cmd = opts.cmd ? opts.cmd : 'gpio'
|
||||||
|
@ -84,10 +130,9 @@ class MCP23017 extends MCP23008 {
|
||||||
constructor(busObj, i2cAddress, opts) {
|
constructor(busObj, i2cAddress, opts) {
|
||||||
super(busObj, i2cAddress, opts)
|
super(busObj, i2cAddress, opts)
|
||||||
// add a second port
|
// add a second port
|
||||||
|
|
||||||
opts.portID = 'B'
|
opts.portID = 'B'
|
||||||
this.ports.B = new Port(opts)
|
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) {
|
pin(id, port) {
|
||||||
|
|
|
@ -49,43 +49,55 @@
|
||||||
|
|
||||||
it('can set and get a single pin config on both ports', function () {
|
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(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)
|
expect(mcp17.pin(8).cfg.dir, "pin config getter failed").to.equal(1)
|
||||||
// mcp17.pin(8).cfg.dir = 1
|
mcp17.pin(8).cfg.dir = 0
|
||||||
// expect(mcp17.pin(8).config.dir, "pin address setter failed").to.equal(1)
|
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(0)
|
expect(mcp17.pin(8, 'B').cfg.dir, "pin address getter port B failed").to.equal(1)
|
||||||
// mcp17.pin(8, 'B').cfg.dir = 1
|
mcp17.pin(8, 'B').cfg.dir = 0
|
||||||
// expect(mcp17.pin(8, 'B').config.dir, "pin address setter failed").to.equal(1)
|
expect(mcp17.pin(8, 'B').config.dir, "pin address setter failed").to.equal(0)
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let mcp8 = new MCP.MCP23008(bus1, 0x21, {
|
let mcp8 = new MCP.MCP23008(bus1, 0x21, {
|
||||||
pin_cfg_default: 'toggle_switch',
|
pin_cfg_default: 'toggle_switch',
|
||||||
name: 'relay 1-8'
|
name: 'test 1-8'
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('MCP23008 Class - ', function () {
|
describe('MCP23008 Class - ', function () {
|
||||||
|
|
||||||
it('can set and get a single pin config', 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(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.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.ivrt, "pin address getter failed").to.equal(1)
|
||||||
// expect(mcp8.pin(8).cfg.usedef, "pin address getter failed").to.equal(0)
|
expect(mcp8.pin(8).cfg.usedef, "pin address getter failed").to.equal(0)
|
||||||
// mcp8.pin(8).cfg.dir = 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.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.ivrt, "pin address getter failed").to.equal(1)
|
||||||
});
|
});
|
||||||
|
|
||||||
const ADDR = 0x21,
|
const ADDR = 0x20,
|
||||||
DIR = 0x00,
|
W = 0x09,
|
||||||
RW = 0x09,
|
R = 0x0A,
|
||||||
ALLPINS = 0x40,
|
PINSON = 129,
|
||||||
NOPINS = 0x00
|
PINSOFF = 0x00
|
||||||
|
|
||||||
mcp8.write(DIR, 0x00).then(() =>
|
let testout = new MCP.MCP23008(bus1, ADDR, {
|
||||||
mcp8.write(RW, ALLPINS).then(() =>
|
pin_cfg_default: 'output',
|
||||||
mcp8.read(RW, ALLPINS).then((resp) => {
|
name: 'outputs 1-8'
|
||||||
console.log('relays on', resp)
|
})
|
||||||
|
|
||||||
|
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)
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue