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

66 lines
1.8 KiB
JavaScript
Raw Normal View History

/*
*
*
*/
import { MCP230XXi } from '../src'
import { MCP230XX } from '../src'
import Base from '@uci/base'
const BUS_HOST = 'sbc' // if ihost not given will be the same as host:
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 announce = new Base({id:'switch:announce'})
let sw17 = new MCP230XXi([9,10],{id:'sw17', chip17:true, host:BUS_HOST, address:SW_ADDRESS+1})
// let sw8 = new MCP230XXi([24],{id:'sw8', host:BUS_HOST, address:SW_ADDRESS})
// let ry8 = new MCP230XX({id:'ry8', host:BUS_HOST, address:RY_ADDRESS})
const reply = { reply: () => {} }
// ry8.c = reply;
sw17.c=reply
// sw8.c= reply
function iprocess(details) {
console.log('--common interrupt processor--')
console.log(details)
announce.push()
// action function can do whatever but best would be to push the details to any attached client (mqtt or some database that then pushes
// local_action(details)
}
// just something to do
async function local_action (details) {
if (details.id === 'sw17') {
await ry8.pin.state.toggle({pins:'2,4,6,8'})
}
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()
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)
})