uci-mcp/test/mcp.test.js

81 lines
2.5 KiB
JavaScript
Raw Normal View History

2017-01-12 20:05:20 -08:00
'use strict'
const expect = require('chai').expect,
2017-01-14 00:04:34 -08:00
_u = require('uci-utils'),
MCP = require('../lib/mcp23008-17')
2017-01-12 20:05:20 -08:00
2017-01-16 22:44:10 -08:00
const bus = {}
2017-01-12 20:05:20 -08:00
2017-01-14 00:04:34 -08:00
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
}
}
2017-01-12 20:05:20 -08:00
// console.log(bus1)
// bus1.scan((devs)=>{console.log('devices: ', devs)})
//bus1.scan().then(results=>console.log(results))
describe('I2C Device Classes - ', function () {
2017-01-16 22:44:10 -08:00
let mcp17 = new MCP.MCP23017(bus, 0x20, {
2017-01-14 00:04:34 -08:00
pin_cfg_default: 'momentary_switch',
2017-01-12 20:05:20 -08:00
name: 'switches 1-16'
})
describe('MCP23017 Class - ', function () {
2017-01-14 00:04:34 -08:00
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)
2017-01-12 20:05:20 -08:00
});
});
2017-01-16 22:44:10 -08:00
let mcp8 = new MCP.MCP23008(bus, 0x21, {
2017-01-14 00:04:34 -08:00
pin_cfg_default: 'toggle_switch',
name: 'test 1-8'
2017-01-12 20:05:20 -08:00
})
describe('MCP23008 Class - ', function () {
it('can set and get a single pin config', function () {
2017-01-14 00:04:34 -08:00
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)
2017-01-12 20:05:20 -08:00
});
});
});