138 lines
3.9 KiB
JavaScript
138 lines
3.9 KiB
JavaScript
'use strict'
|
|
|
|
const expect = require('chai').expect,
|
|
i2c = require('uci-i2c'),
|
|
_u = require('uci-utils'),
|
|
MCP = require('../lib/mcp23008-17')
|
|
|
|
let bus1 = new i2c.Bus(1)
|
|
|
|
const config_sets = {
|
|
output: {
|
|
dir: 0, // 0 output,1 input
|
|
ivrt: 0,
|
|
pullup: 0,
|
|
intr: 0, // if intr = 0 usedef,deval not used
|
|
usedef: 0, // if usedef = 0 defval not used
|
|
defval: 0
|
|
},
|
|
toggle_switch: {
|
|
dir: 1, // 0 output,1 input
|
|
ivrt: 1, // for reading let 1 be zero and vice versa
|
|
pullup: 1,
|
|
intr: 1, // if intr = 0 usedef,deval not used
|
|
usedef: 0, // if usedef = 0 defval not used
|
|
defval: 0
|
|
},
|
|
momentary_switch: {
|
|
dir: 1, // 0 output,1 input
|
|
ivrt: 1, // for reading let 1 be zero and vice versa
|
|
pullup: 1,
|
|
intr: 1, // if intr = 0 usedef,deval not used
|
|
usedef: 1, // if usedef = 0 defval not used
|
|
defval: 1
|
|
}
|
|
}
|
|
|
|
// console.log(bus1)
|
|
// bus1.scan((devs)=>{console.log('devices: ', devs)})
|
|
//bus1.scan().then(results=>console.log(results))
|
|
|
|
describe('I2C Device Classes - ', function () {
|
|
|
|
let mcp17 = new MCP.MCP23017(bus1, 0x20, {
|
|
pin_cfg_default: 'momentary_switch',
|
|
name: 'switches 1-16'
|
|
})
|
|
|
|
describe('MCP23017 Class - ', 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(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: '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).cfg.ivrt, "pin address getter failed").to.equal(1)
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
let ADDR = 0x20,
|
|
W = 0x09,
|
|
R = 0x0A,
|
|
PINSON = 0xa5,
|
|
PINSOFF = 0x00
|
|
|
|
let testout = new MCP.MCP23008(bus1, ADDR, {
|
|
pin_cfg_default: 'output',
|
|
name: 'outputs 1-8'
|
|
})
|
|
|
|
function tstout() {
|
|
return testout.init()
|
|
.then(() => {
|
|
console.log('testing device write and read')
|
|
return testout.write(W, PINSON)
|
|
.then(() => {
|
|
return testout.read(R)
|
|
.then((resp) => {
|
|
console.log('Pins Set On', _u.byteFormat(resp, { in: 'DEC', out: 'PLC' }))
|
|
return _u.pDelay(1000)
|
|
.then(() => {
|
|
console.log('Reset Pins')
|
|
return testout.write(W, PINSOFF)
|
|
})
|
|
})
|
|
})
|
|
})
|
|
.catch(err => console.log('errors:', err))
|
|
}
|
|
|
|
const
|
|
ADDR17 = 0x27,
|
|
port = { A: 0x09, B: 0x19 }
|
|
|
|
let testin = new MCP.MCP23017(bus1, ADDR17, {
|
|
pin_cfg_default: 'toggle_switch',
|
|
name: 'read inputs 1-16'
|
|
})
|
|
|
|
function tstin(p) {
|
|
return testin.init()
|
|
.then(() => {
|
|
console.log('port', port[p])
|
|
return testin.read(port[p])
|
|
.then((resp) => {
|
|
console.log('Port ', p, ' Pins', _u.byteFormat(resp, { in: 'DEC', out: 'PLC' }))
|
|
})
|
|
})
|
|
.catch(err => console.log('errors:', err))
|
|
}
|
|
|
|
// tstout().then(() => tstin())
|
|
bus1.qAdd(tstin('A'))
|
|
bus1.qAdd(tstin('B'))
|