refactored to use new uci-i2c-device packet module

master
David Kebler 2018-02-14 19:01:08 -08:00
parent da862a7c1a
commit 3c21bd66da
6 changed files with 34 additions and 98 deletions

View File

@ -33,57 +33,15 @@ const delay = time => new Promise(res=>setTimeout(()=>res(),time))
await mcpclient.send(packet)
packet = {cmd:'pin.state.off', pins:'all', port:'B' }
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)
packet = {cmd:'pin.state.on', pins:'3,4'}
packet = {cmd:'pin.state.on', pins:'3,6'}
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)
// 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')

View File

@ -8,7 +8,7 @@ import MCP230XX from '../src/mcp230xx-packet'
;
(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()

View File

@ -32,9 +32,11 @@ const delay = time => new Promise(res=>setTimeout(()=>res(),time))
await delay(3000)
await relays.send({cmd:'pin.state.off', pins:'all'})
relays.socket.uc.end()
process.kill(process.pid, 'SIGTERM')
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
process.kill(process.pid, 'SIGTERM')
})

View File

@ -3,7 +3,6 @@
*
*/
import MCP230XX from '../src/mcp230xx-packet'
// const PATH = ''
;
(async () => {

View File

@ -3,19 +3,11 @@ import _ from '../../archive/uci-utils/src/byte'
import { CHIP, PIN } from './config'
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
// configuration byte with given format ('STR' by defaul).
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.send('bus',busPacket('write',0x05,0))
let bus = await this.bus.write(0x05,0)
if (bus.error) return bus
let setting = {}
let cfg = packet.cfg || 'default'
@ -27,9 +19,9 @@ export const chip = {
let byte = _.byteFormat(setting.val, { in: setting.fmt, out: 'DEC' })
if (byte < 128) byte += 128 // make sure BANK=1 remains on
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
bus = await this.send('bus',busPacket('read',0x05))
bus = await this.bus.read(0x05)
if (bus.error) return bus
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 pins = parsePins(packet.pins)
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
state.value = bus.response
reply.status =
@ -103,26 +95,22 @@ const parsePins = function(pins) {
return new _.Byte(pins,'PLC')
}
let busPacket = function (cmd,reg,byte,port) {
if (typeof byte==='string') port = byte
let shift = (port==='B') ? 0x10 : 0
let packet = { cmd:cmd, args: {address:this.address, cmd:reg+shift, byte:byte } }
return packet
const sreg = (reg,port) => {
return reg + ((port==='B') ? 0x10 : 0)
}
const state = async function(packet,op,reg){
busPacket = busPacket.bind(this)
reg = (reg!==undefined)? reg : PIN.cmd.gpio
// console.log(op, reg)
let reply = { cmd:'reply', _req:packet}
let pins = parsePins(packet.pins)
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
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
bus = await this.send('bus',busPacket('read',reg, packet.port))
bus = await this.bus.read(sreg(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

@ -1,57 +1,46 @@
// 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 logger from '../../uci-logger/src/logger'
let log = {}
const LOG_OPTS = {
repo:'uci-mcp',
npm:'@uci/mcp',
file:'src/mcp230xx-packet.mjs',
class:'MCP230XX',
id:this.id,
instance_created:new Date().getTime()
}
const LOG_OPTS = (id) => {
return {
repo:'uci-mcp',
npm:'@uci/mcp',
file:'src/mcp230xx-packet.mjs',
class:'MCP230XX',
id:id,
instance_created:new Date().getTime()
}}
export default class MCP230XX extends Base {
export default class MCP230XX extends Device {
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.sockets = 'nmcp#s>n,tmcp#s>t,'+ opts.bus.socket
console.log(opts)
opts.sockets = 'nmcp#s>n,tmcp#s>t,'
// console.log(opts)
super(opts)
log = logger.child(LOG_OPTS(this.id))
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)
}
async init(){
await super.init()
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)({})
// 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)}`
let res = await this.chip.cfg.bind(this)({})
let cfg = this.chip17 ?'10100010':'00100010'
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
reply (bus_packet) {
this.emit(bus_packet.id, bus_packet)
// this overwrites the default cmd 'reply' from the bus
reply (packet) {
// console.log(packet._header.request, packet.response)
}
} // end of MCP230XX Class