remove busSend now using base's send and sockets await send

add chip option chip17 when using 230017
add relay example
master
David Kebler 2018-02-13 18:18:03 -08:00
parent f605508982
commit da862a7c1a
6 changed files with 100 additions and 69 deletions

View File

@ -13,46 +13,34 @@ const delay = time => new Promise(res=>setTimeout(()=>res(),time))
let mcpclient = new Base({id:'mcpclient', sockets:'uc#c>n', uc:{path:PATH}})
mcpclient.reply = function (packet) {
console.log('for request ',packet._header.request)
console.log('mcp status is ',packet.status)
}
await mcpclient.init()
console.log('=============sending============')
// let packet = {cmd:'chip.ack'}
// console.dir(packet)
// for (let i=0;i<20;i++){
// await delay(100)
// packet.num = i
// console.log('--------->',i)
// mcpclient.send(packet)
// }
// const pins='2,3,4'
const pins=3
// const pins=3
// const pins='all'
// const pins=[1,3,5,7]
let packet = {cmd:'pin.cfg', pins:'all'}
console.dir(packet)
mcpclient.send(packet)
await delay(1000)
await mcpclient.send(packet)
packet = {cmd:'pin.cfg', pins:'all', port:'B'}
console.dir(packet)
mcpclient.send(packet)
await delay(1000)
await mcpclient.send(packet)
packet = {cmd:'pin.state.off', pins:'all' }
console.dir(packet)
await delay(1000)
mcpclient.send(packet)
await mcpclient.send(packet)
packet = {cmd:'pin.state.off', pins:'all', port:'B' }
console.dir(packet)
mcpclient.send(packet)
await delay(1000)
// packet = {cmd:'pin.state.on', pins:pins}
// console.dir(packet)
// mcpclient.send(packet)
await mcpclient.send(packet)
packet = {cmd:'pin.state.on', pins:'1,7', port:'B' }
console.dir(packet)
mcpclient.send(packet)
await mcpclient.send(packet)
packet = {cmd:'pin.state.on', pins:'3,4'}
console.dir(packet)
mcpclient.send(packet)
await mcpclient.send(packet)
packet = {cmd:'pin.status', pins:'all'}
await mcpclient.send(packet)
packet = {cmd:'pin.status', pins:'all', port:'B'}
await mcpclient.send(packet)
// mcpclient.send(packet)
// await delay(1000)

View File

@ -8,7 +8,7 @@ import MCP230XX from '../src/mcp230xx-packet'
;
(async () => {
let mcp_chip = new MCP230XX({id:'mcp23008-27', address:0x26, bus:{host:'sbc'} })
let mcp_chip = new MCP230XX({id:'mcp23008-27', chip17:true address:0x26, bus:{host:'sbc'} })
await mcp_chip.init()

40
examples/relays-test.mjs Normal file
View File

@ -0,0 +1,40 @@
/*
* i2c bus unix socket and client in one for demo
*
*/
import Base from '../../uci-base/src/base'
const PATH = '/opt/sockets/mcp.sock'
const delay = time => new Promise(res=>setTimeout(()=>res(),time))
;
(async () => {
let relays = new Base({id:'relays', sockets:'uc#c>n', uc:{path:PATH}})
relays.reply = function (packet) {
// console.log('for request ',packet._header.request)
// console.log('mcp status is ',packet.status)
}
await relays.init()
console.log('=============sending============')
await relays.send({cmd:'pin.cfg', pins:'all'})
await relays.send({cmd:'pin.state.off', pins:'all' })
for(let i=1;i<=8;i +=2){
await relays.send({cmd:'pin.state.on', pins:i})
}
console.log((await relays.send({cmd:'pin.status', pins:'all'})).status)
console.log((await relays.send({cmd:'pin.status', pins:'3'})).status.pins)
await delay(3000)
await relays.send({cmd:'pin.state.off', pins:'all'})
process.kill(process.pid, 'SIGTERM')
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
})

17
examples/relays.mjs Normal file
View File

@ -0,0 +1,17 @@
/*
* i2c bus with both unix and tcp socket using defaults. For TCP that is host OS name and port 8080
*
*/
import MCP230XX from '../src/mcp230xx-packet'
// const PATH = ''
;
(async () => {
let relays = new MCP230XX({id:'relays', address:0x27, bus:{host:'sbc'} })
await relays.init()
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
})

View File

@ -3,10 +3,10 @@ import _ from '../../archive/uci-utils/src/byte'
import { CHIP, PIN } from './config'
export const chip = {
ack: async function(packet){
let bus = await this.busSend({ cmd:'scan'})
ack: async function(){
let bus = await this.send('bus',{ cmd:'scan'})
if (bus.error) return bus
let res = { cmd:'reply', _req:packet, _reqBus:{cmd:'scan'}, ack: false, address:this.address, scan:bus.response}
let res = { cmd:'reply', ack: false, address:this.address, scan:bus.response}
if (bus.response.indexOf(this.address) !== -1) res.ack = true
return res
},
@ -15,7 +15,7 @@ export const chip = {
cfg: async function(packet){
busPacket = busPacket.bind(this)
// first make sure chip is in set to BANK=0 if not already
let bus = await this.busSend(busPacket('write',0x05,0))
let bus = await this.send('bus',busPacket('write',0x05,0))
if (bus.error) return bus
let setting = {}
let cfg = packet.cfg || 'default'
@ -26,11 +26,12 @@ export const chip = {
}
let byte = _.byteFormat(setting.val, { in: setting.fmt, out: 'DEC' })
if (byte < 128) byte += 128 // make sure BANK=1 remains on
bus = await this.busSend(busPacket('write',0x0A,byte))
let reg = this.chip17 ? 0x0A : 0x05
bus = await this.send('bus',busPacket('write',reg,byte))
if (bus.error) return bus
bus = await this.busSend(busPacket('read',0x05))
bus = await this.send('bus',busPacket('read',0x05))
if (bus.error) return bus
return { cmd:'reply', _req:packet, response:_.byteFormat(bus.response,{in:'DEC',out:'STR'}) }
return { cmd:'reply', response:_.byteFormat(bus.response,{in:'DEC',out:'STR'}) }
}
}
@ -41,7 +42,7 @@ export const chip = {
export const pin = {
cfg: async function(packet){
let cfg = {}
let reply = { cmd:'reply', _req:packet, response:{} }
let reply = { cmd:'reply', status:{} }
packet.cfg = packet.cfg || 'output'
if (packet.cfg==='custom') cfg = packet.set
else cfg = PIN.cfgset[packet.cfg]
@ -50,7 +51,7 @@ export const pin = {
// console.log(name, op)
let busreply = await state.bind(this)(packet,op,PIN.setting[name])
if (busreply.error) return busreply
reply.response[name] = busreply.status
reply.status[name] = busreply.status
}
return reply
},
@ -62,14 +63,16 @@ export const pin = {
let reply = { cmd:'reply', _req:packet}
let pins = parsePins(packet.pins)
let state = new _.Byte()
let bus = await this.busSend(busPacket.bind(this)('read',reg, packet.port))
let bus = await this.send('bus',busPacket.bind(this)('read',reg, packet.port))
if (bus.error) return bus
state.value = bus.response
reply.port = state.toFmt('ARY')
reply.pins = pins.value.map(pin => {
reply.status =
{ port:state.toFmt('ARY'),
pins: pins.value.map(pin => {
if (state.toFmt('PLC').indexOf(pin) !==-1) return [pin, 'on']
else return [pin,'off']
})
}
return reply
},
// threse three only for output pins
@ -114,12 +117,12 @@ const state = async function(packet,op,reg){
let reply = { cmd:'reply', _req:packet}
let pins = parsePins(packet.pins)
let state = new _.Byte()
let bus = await this.busSend(busPacket('read',reg, packet.port))
let bus = await this.send('bus',busPacket('read',reg, packet.port))
if (bus.error) return bus
state.value = bus.response
bus = await this.busSend(busPacket('write',reg,state.bwOp(pins.value,op,{in:'PLC', out:'DEC'}),packet.port))
bus = await this.send('bus',busPacket('write',reg,state.bwOp(pins.value,op,{in:'PLC', out:'DEC'}),packet.port))
if (bus.error) return bus
bus = await this.busSend(busPacket('read',reg, packet.port))
bus = await this.send('bus',busPacket('read',reg, packet.port))
if (bus.error) return bus
state.value = bus.response
reply.status = state.bwOp(pins.value,'check',{in:'PLC', out:'PLC'})

View File

@ -32,9 +32,10 @@ export default class MCP230XX extends Base {
super(opts)
if (!opts.address) log.fatal({opts:opts},'no i2c bus address supplied' )
this.address = opts.address
this.chip17 = opts.chip17
this.pin = pin
this.chip = chip
this.busSend = this.busSend.bind(this)
// this.busSend = this.busSend.bind(this)
}
@ -43,7 +44,8 @@ export default class MCP230XX extends Base {
let res = await this.chip.ack.bind(this)() // move this to device class
if (!res.ack) throw `no device on this bus at address ${this.address}=0x${this.address.toString(16)}`
res = await this.chip.cfg.bind(this)({})
if (res.response !== '10100010') throw `could not configure mcp chip at ${this.address}=0x${this.address.toString(16)}`
let cfg = this.chip17 ?'10100010':'00100010'
if (res.response !==cfg ) throw `could not configure mcp chip at ${this.address}=0x${this.address.toString(16)}`
}
@ -52,23 +54,4 @@ export default class MCP230XX extends Base {
this.emit(bus_packet.id, bus_packet)
}
async busSend(packet) {
return new Promise( async (resolve) => {
setTimeout(() => {resolve({error:'no response from bus in 10sec'})},10000)
packet.id = Math.random().toString().slice(2)
// console.log('sending to bus', packet.id)
this.socket.bus.send(packet)
this.on(packet.id,function(bus_reply){
// console.log('reply emitted',bus_reply)
this.removeAllListeners(bus_reply.id)
delete bus_reply.id
resolve(bus_reply)
}) //end listener
})
}
// setBusListener(packet) {
// // this.on(packet.cmd+Math.random().toString().slice(1))
// }
} // end of MCP230XX Class