uci-mcp/examples/mcp-switch-relay.js

66 lines
1.8 KiB
JavaScript

/*
*
*
*/
import { MCP230XXi } from '../src'
import { MCP230XX } from '../src'
const BUS_HOST = 'sbc'
const BUS_PORT = 1776 // 1776 is default port for i2c-bus module
const SW_ADDRESS = 0x25 // 1776 is default port for i2c-bus module
const RY_ADDRESS = 0x27 // 1776 is default port for i2c-bus module
// let chip8 = new MCP230XX({id:'mcp8-27', address:0x27, nmcp: { path: '/opt/sockets/mcp2.sock' }})
// let sw17 = new MCP230XXi([9,10],{id:'sw17', chip17:true, host:BUS_HOST, address:SW_ADDRESS, iport:9000, 10:{mport:'B'}})
let sw17 = new MCP230XXi([9,10],{id:'sw17', chip17:true, host:BUS_HOST, address:SW_ADDRESS+1, iport:true, 10:{mport:'B'}})
let sw8 = new MCP230XXi([24],{id:'sw8', host:BUS_HOST, address:SW_ADDRESS, iport:true})
let ry8 = new MCP230XX({id:'ry8', host:BUS_HOST, address:RY_ADDRESS})
sw17.reply = () => {}
ry8.reply = () => {}
sw8.reply = () => {}
function iprocess(details) {
console.log('--common interrupt processor--')
console.log(details)
action(details)
}
async function action (details) {
if (details.id === 'sw17') {
await ry8.pin.state.toggle({pins:details.pin})
}
if (details.id === 'sw8') {
if (details.pin===1) {
if (details.state==='off') await ry8.pin.state.off({pins:'all'})
else ry8.pin.state.on({pins:'all'})
}
if (details.pin===8) {
if (details.state==='off') await ry8.pin.state.off({pins:'1,3,5,7'})
else await ry8.pin.state.on({pins:'1,3,5,7'})
}
}
}
(async () => {
await sw17.init()
let cfg = {port:'B',pins:'1', cfg:'toggle_switch_pulldown'}
console.log(await sw17.pin.cfg(cfg))
sw17.interruptProcessor(iprocess)
await sw8.init()
sw8.interruptProcessor(iprocess)
await ry8.init()
let packet = {pins:'all', cfg:'output'}
await ry8.pin.cfg(packet)
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
})