refactored to use new uci-i2c-device packet module
parent
da862a7c1a
commit
3c21bd66da
|
@ -33,57 +33,15 @@ const delay = time => new Promise(res=>setTimeout(()=>res(),time))
|
||||||
await mcpclient.send(packet)
|
await mcpclient.send(packet)
|
||||||
packet = {cmd:'pin.state.off', pins:'all', port:'B' }
|
packet = {cmd:'pin.state.off', pins:'all', port:'B' }
|
||||||
await mcpclient.send(packet)
|
await mcpclient.send(packet)
|
||||||
packet = {cmd:'pin.state.on', pins:'1,7', port:'B' }
|
packet = {cmd:'pin.state.on', pins:'2,7', port:'B' }
|
||||||
await mcpclient.send(packet)
|
await mcpclient.send(packet)
|
||||||
packet = {cmd:'pin.state.on', pins:'3,4'}
|
packet = {cmd:'pin.state.on', pins:'3,6'}
|
||||||
await mcpclient.send(packet)
|
await mcpclient.send(packet)
|
||||||
packet = {cmd:'pin.status', pins:'all'}
|
packet = {cmd:'pin.status', pins:'all'}
|
||||||
await mcpclient.send(packet)
|
await mcpclient.send(packet)
|
||||||
packet = {cmd:'pin.status', pins:'all', port:'B'}
|
packet = {cmd:'pin.status', pins:'all', port:'B'}
|
||||||
await mcpclient.send(packet)
|
await mcpclient.send(packet)
|
||||||
|
|
||||||
// mcpclient.send(packet)
|
|
||||||
// await delay(1000)
|
|
||||||
// packet = {cmd:'pin.state.on', pins:pins}
|
|
||||||
// console.dir(packet)
|
|
||||||
// mcpclient.send(packet)
|
|
||||||
// await delay(1000)
|
|
||||||
// packet = {cmd:'pin.status', reg:'xintf', pins:pins}
|
|
||||||
// console.dir(packet)
|
|
||||||
// mcpclient.send(packet)
|
|
||||||
// await delay(1000)
|
|
||||||
// packet = {cmd:'pin.status', pins:pins}
|
|
||||||
// console.dir(packet)
|
|
||||||
// mcpclient.send(packet)
|
|
||||||
// await delay(1000)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// packet = {cmd:'pin.cfg', pins:pins, cfg:'toggle_switch'}
|
|
||||||
// console.dir(packet)
|
|
||||||
// mcpclient.send(packet)
|
|
||||||
// await delay(1000)
|
|
||||||
// packet = {cmd:'pin.cfg', pins:pins}
|
|
||||||
// console.dir(packet)
|
|
||||||
// mcpclient.send(packet)
|
|
||||||
// await delay(1000)
|
|
||||||
// packet = {cmd:'pin.state.on', pins:pins, }
|
|
||||||
// console.dir(packet)
|
|
||||||
// mcpclient.send(packet)
|
|
||||||
// await delay(1000)
|
|
||||||
// packet = {cmd:'pin.state.toggle', pins:pins, }
|
|
||||||
// console.dir(packet)
|
|
||||||
// mcpclient.send(packet)
|
|
||||||
// await delay(1000)
|
|
||||||
// packet = {cmd:'pin.state.toggle', pins:pins, }
|
|
||||||
// console.dir(packet)
|
|
||||||
// mcpclient.send(packet)
|
|
||||||
// packet = {cmd:'pin.cfg', pins:3}
|
|
||||||
// console.dir(packet)
|
|
||||||
// mcpclient.send(packet)
|
|
||||||
|
|
||||||
|
|
||||||
await delay(10000)
|
|
||||||
process.kill(process.pid, 'SIGTERM')
|
process.kill(process.pid, 'SIGTERM')
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ import MCP230XX from '../src/mcp230xx-packet'
|
||||||
;
|
;
|
||||||
(async () => {
|
(async () => {
|
||||||
|
|
||||||
let mcp_chip = new MCP230XX({id:'mcp23008-27', chip17:true address:0x26, bus:{host:'sbc'} })
|
let mcp_chip = new MCP230XX({id:'mcp23008-27', chip17:true, address:0x26, bus:{host:'sbc'} })
|
||||||
|
|
||||||
await mcp_chip.init()
|
await mcp_chip.init()
|
||||||
|
|
||||||
|
|
|
@ -32,9 +32,11 @@ const delay = time => new Promise(res=>setTimeout(()=>res(),time))
|
||||||
await delay(3000)
|
await delay(3000)
|
||||||
await relays.send({cmd:'pin.state.off', pins:'all'})
|
await relays.send({cmd:'pin.state.off', pins:'all'})
|
||||||
|
|
||||||
|
relays.socket.uc.end()
|
||||||
process.kill(process.pid, 'SIGTERM')
|
process.kill(process.pid, 'SIGTERM')
|
||||||
|
|
||||||
|
|
||||||
})().catch(err => {
|
})().catch(err => {
|
||||||
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
|
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
|
||||||
|
process.kill(process.pid, 'SIGTERM')
|
||||||
})
|
})
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
import MCP230XX from '../src/mcp230xx-packet'
|
import MCP230XX from '../src/mcp230xx-packet'
|
||||||
// const PATH = ''
|
|
||||||
|
|
||||||
;
|
;
|
||||||
(async () => {
|
(async () => {
|
||||||
|
|
|
@ -3,19 +3,11 @@ import _ from '../../archive/uci-utils/src/byte'
|
||||||
import { CHIP, PIN } from './config'
|
import { CHIP, PIN } from './config'
|
||||||
|
|
||||||
export const chip = {
|
export const chip = {
|
||||||
ack: async function(){
|
|
||||||
let bus = await this.send('bus',{ cmd:'scan'})
|
|
||||||
if (bus.error) return bus
|
|
||||||
let res = { cmd:'reply', ack: false, address:this.address, scan:bus.response}
|
|
||||||
if (bus.response.indexOf(this.address) !== -1) res.ack = true
|
|
||||||
return res
|
|
||||||
},
|
|
||||||
// for custom chip configuration set packet.cfg='custom' then packet.setting should be a
|
// for custom chip configuration set packet.cfg='custom' then packet.setting should be a
|
||||||
// configuration byte with given format ('STR' by defaul).
|
// configuration byte with given format ('STR' by defaul).
|
||||||
cfg: async function(packet){
|
cfg: async function(packet){
|
||||||
busPacket = busPacket.bind(this)
|
|
||||||
// first make sure chip is in set to BANK=0 if not already
|
// first make sure chip is in set to BANK=0 if not already
|
||||||
let bus = await this.send('bus',busPacket('write',0x05,0))
|
let bus = await this.bus.write(0x05,0)
|
||||||
if (bus.error) return bus
|
if (bus.error) return bus
|
||||||
let setting = {}
|
let setting = {}
|
||||||
let cfg = packet.cfg || 'default'
|
let cfg = packet.cfg || 'default'
|
||||||
|
@ -27,9 +19,9 @@ export const chip = {
|
||||||
let byte = _.byteFormat(setting.val, { in: setting.fmt, out: 'DEC' })
|
let byte = _.byteFormat(setting.val, { in: setting.fmt, out: 'DEC' })
|
||||||
if (byte < 128) byte += 128 // make sure BANK=1 remains on
|
if (byte < 128) byte += 128 // make sure BANK=1 remains on
|
||||||
let reg = this.chip17 ? 0x0A : 0x05
|
let reg = this.chip17 ? 0x0A : 0x05
|
||||||
bus = await this.send('bus',busPacket('write',reg,byte))
|
bus = await this.bus.write(reg,byte)
|
||||||
if (bus.error) return bus
|
if (bus.error) return bus
|
||||||
bus = await this.send('bus',busPacket('read',0x05))
|
bus = await this.bus.read(0x05)
|
||||||
if (bus.error) return bus
|
if (bus.error) return bus
|
||||||
return { cmd:'reply', response:_.byteFormat(bus.response,{in:'DEC',out:'STR'}) }
|
return { cmd:'reply', response:_.byteFormat(bus.response,{in:'DEC',out:'STR'}) }
|
||||||
}
|
}
|
||||||
|
@ -63,7 +55,7 @@ export const pin = {
|
||||||
let reply = { cmd:'reply', _req:packet}
|
let reply = { cmd:'reply', _req:packet}
|
||||||
let pins = parsePins(packet.pins)
|
let pins = parsePins(packet.pins)
|
||||||
let state = new _.Byte()
|
let state = new _.Byte()
|
||||||
let bus = await this.send('bus',busPacket.bind(this)('read',reg, packet.port))
|
let bus = await this.bus.read(sreg(reg, packet.port))
|
||||||
if (bus.error) return bus
|
if (bus.error) return bus
|
||||||
state.value = bus.response
|
state.value = bus.response
|
||||||
reply.status =
|
reply.status =
|
||||||
|
@ -103,26 +95,22 @@ const parsePins = function(pins) {
|
||||||
return new _.Byte(pins,'PLC')
|
return new _.Byte(pins,'PLC')
|
||||||
}
|
}
|
||||||
|
|
||||||
let busPacket = function (cmd,reg,byte,port) {
|
const sreg = (reg,port) => {
|
||||||
if (typeof byte==='string') port = byte
|
return reg + ((port==='B') ? 0x10 : 0)
|
||||||
let shift = (port==='B') ? 0x10 : 0
|
|
||||||
let packet = { cmd:cmd, args: {address:this.address, cmd:reg+shift, byte:byte } }
|
|
||||||
return packet
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const state = async function(packet,op,reg){
|
const state = async function(packet,op,reg){
|
||||||
busPacket = busPacket.bind(this)
|
|
||||||
reg = (reg!==undefined)? reg : PIN.cmd.gpio
|
reg = (reg!==undefined)? reg : PIN.cmd.gpio
|
||||||
// console.log(op, reg)
|
// console.log(op, reg)
|
||||||
let reply = { cmd:'reply', _req:packet}
|
let reply = { cmd:'reply', _req:packet}
|
||||||
let pins = parsePins(packet.pins)
|
let pins = parsePins(packet.pins)
|
||||||
let state = new _.Byte()
|
let state = new _.Byte()
|
||||||
let bus = await this.send('bus',busPacket('read',reg, packet.port))
|
let bus = await this.bus.read(sreg(reg,packet.port))
|
||||||
if (bus.error) return bus
|
if (bus.error) return bus
|
||||||
state.value = bus.response
|
state.value = bus.response
|
||||||
bus = await this.send('bus',busPacket('write',reg,state.bwOp(pins.value,op,{in:'PLC', out:'DEC'}),packet.port))
|
bus = await this.bus.write(sreg(reg,packet.port),state.bwOp(pins.value,op,{in:'PLC', out:'DEC'}))
|
||||||
if (bus.error) return bus
|
if (bus.error) return bus
|
||||||
bus = await this.send('bus',busPacket('read',reg, packet.port))
|
bus = await this.bus.read(sreg(reg,packet.port))
|
||||||
if (bus.error) return bus
|
if (bus.error) return bus
|
||||||
state.value = bus.response
|
state.value = bus.response
|
||||||
reply.status = state.bwOp(pins.value,'check',{in:'PLC', out:'PLC'})
|
reply.status = state.bwOp(pins.value,'check',{in:'PLC', out:'PLC'})
|
||||||
|
|
|
@ -1,57 +1,46 @@
|
||||||
// import Base from '@uci/base'
|
// import Base from '@uci/base'
|
||||||
import Base from '../../uci-base/src/base'
|
import Device from '../../uci-i2c-device/src/device-packet'
|
||||||
import { pin, chip } from './commands'
|
import { pin, chip } from './commands'
|
||||||
|
|
||||||
import logger from '../../uci-logger/src/logger'
|
import logger from '../../uci-logger/src/logger'
|
||||||
let log = {}
|
let log = {}
|
||||||
const LOG_OPTS = {
|
const LOG_OPTS = (id) => {
|
||||||
repo:'uci-mcp',
|
return {
|
||||||
npm:'@uci/mcp',
|
repo:'uci-mcp',
|
||||||
file:'src/mcp230xx-packet.mjs',
|
npm:'@uci/mcp',
|
||||||
class:'MCP230XX',
|
file:'src/mcp230xx-packet.mjs',
|
||||||
id:this.id,
|
class:'MCP230XX',
|
||||||
instance_created:new Date().getTime()
|
id:id,
|
||||||
}
|
instance_created:new Date().getTime()
|
||||||
|
}}
|
||||||
|
|
||||||
export default class MCP230XX extends Base {
|
export default class MCP230XX extends Device {
|
||||||
constructor(opts) {
|
constructor(opts) {
|
||||||
log = logger.child(LOG_OPTS)
|
|
||||||
if (opts.bus) {
|
|
||||||
if (opts.bus.host) {
|
|
||||||
opts.bus.socket = 'bus#c>t'
|
|
||||||
opts.bus.port = opts.bus.port || 1776
|
|
||||||
}
|
|
||||||
if (opts.bus.path) opts.bus.socket = 'bus#c>n'
|
|
||||||
} else {
|
|
||||||
opts.bus = { path : (process.env.SOCKETS_DIR || __dirname) + '/i2c-bus.sock' }
|
|
||||||
opts.bus.socket = 'bus#c>n'
|
|
||||||
}
|
|
||||||
opts.nmcp = opts.nmcp || {path: (process.env.SOCKETS_DIR || __dirname) + '/mcp.sock'}
|
opts.nmcp = opts.nmcp || {path: (process.env.SOCKETS_DIR || __dirname) + '/mcp.sock'}
|
||||||
opts.sockets = 'nmcp#s>n,tmcp#s>t,'+ opts.bus.socket
|
opts.sockets = 'nmcp#s>n,tmcp#s>t,'
|
||||||
console.log(opts)
|
// console.log(opts)
|
||||||
super(opts)
|
super(opts)
|
||||||
|
log = logger.child(LOG_OPTS(this.id))
|
||||||
if (!opts.address) log.fatal({opts:opts},'no i2c bus address supplied' )
|
if (!opts.address) log.fatal({opts:opts},'no i2c bus address supplied' )
|
||||||
this.address = opts.address
|
this.address = opts.address
|
||||||
this.chip17 = opts.chip17
|
this.chip17 = opts.chip17
|
||||||
this.pin = pin
|
this.pin = pin
|
||||||
this.chip = chip
|
this.chip = chip
|
||||||
// this.busSend = this.busSend.bind(this)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async init(){
|
async init(){
|
||||||
await super.init()
|
await super.init()
|
||||||
let res = await this.chip.ack.bind(this)() // move this to device class
|
// 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)}`
|
// 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)({})
|
let res = await this.chip.cfg.bind(this)({})
|
||||||
let cfg = this.chip17 ?'10100010':'00100010'
|
let cfg = this.chip17 ?'10100010':'00100010'
|
||||||
if (res.response !==cfg ) throw `could not configure mcp chip at ${this.address}=0x${this.address.toString(16)}`
|
if (res.response !==cfg ) throw `could not configure mcp chip at ${this.address}=0x${this.address.toString(16)}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// this services the reply from the bus
|
// this overwrites the default cmd 'reply' from the bus
|
||||||
reply (bus_packet) {
|
reply (packet) {
|
||||||
this.emit(bus_packet.id, bus_packet)
|
// console.log(packet._header.request, packet.response)
|
||||||
}
|
}
|
||||||
|
|
||||||
} // end of MCP230XX Class
|
} // end of MCP230XX Class
|
||||||
|
|
Loading…
Reference in New Issue