improve refactor socket options, add _state to module export and bind all functions in pins and chip

master
David Kebler 2018-02-17 18:25:27 -08:00
parent 3c21bd66da
commit 342c6f98d2
7 changed files with 73 additions and 110 deletions

View File

@ -4,10 +4,19 @@
*/
import Base from '../../uci-base/src/base'
import { spawn } from 'child_process'
const PATH = '/opt/sockets/mcp.sock'
const delay = time => new Promise(res=>setTimeout(()=>res(),time))
const i2cbus = spawn('node',['-r', '@std/esm', './examples/bus'])
i2cbus.stdout.on('data', function(buf) {
console.log('[I2C BUS]', String(buf))
})
;
(async () => {

View File

@ -8,9 +8,15 @@ import MCP230XX from '../src/mcp230xx-packet'
;
(async () => {
let mcp_chip = new MCP230XX({id:'mcp23008-27', chip17:true, address:0x26, bus:{host:'sbc'} })
// let chip8 = new MCP230XX({id:'mcp8-27', address:0x27, nmcp: { path: '/opt/sockets/mcp2.sock' }})
let chip17 = new MCP230XX({id:'mcp17-26', chip17:true, address:0x26})
await mcp_chip.init()
await chip17.init()
// await chip8.init()
console.log(await chip17.pin.cfg({pins:'all'}))
// console.log(await chip17.pin.cfg({pins:'all', cfg:'toggle_switch'}))
// console.log(await chip17.pin.cfg({pins:'all', port:'B', cfg:'toggle_switch'}))
// console.log(await chip8.pin.cfg({pins:'all', cfg:'toggle_switch'}))
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)

View File

@ -1,42 +0,0 @@
/*
* 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'})
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

@ -1,16 +0,0 @@
/*
* 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'
;
(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

@ -1,19 +1,15 @@
{
"name": "@uci/mcp",
"main": "src/mcp23008-17.js",
"main": "src/mcp230xx-packet.mjs",
"version": "0.1.2",
"description": "Classes and Helper Functions for using the MCP chip on I2C Bus",
"scripts": {
"mcp": "SOCKETS_DIR=/opt/sockets node_modules/.bin/nodemon --require @std/esm examples/mcp",
"mcpl": "DEBUG=true SOCKETS_DIR=/opt/sockets node_modules/.bin/nodemon --require @std/esm examples/mcp",
"relays": "SOCKETS_DIR=/opt/sockets node --require @std/esm examples/relays",
"test": "./node_modules/.bin/mocha --reporter list --timeout 30000",
"testw": "./node_modules/.bin/mocha --reporter list -- watch --timeout 30000",
"testd": "DEBUG='1:*' ./node_modules/.bin/mocha --reporter list --watch --timeout 30000",
"testd2": "DEBUG='1:*,2:*' ./node_modules/.bin/mocha --reporter list --watch --timeout 30000",
"testd3": "DEBUG='1:*,2:*,3:*' ./node_modules/.bin/mocha --reporter list --watch --timeout 30000",
"testibc": "istanbul cover ./node_modules/.bin/_mocha test/ --report lcovonly -- -R spec --recursive && codecov || true",
"sample": "node demo/sample.js",
"inter": "sudo node demo/interrupt.js"
"inter": "sudo node -r @std/esm examples/interrupt"
},
"author": "David Kebler",
"license": "MIT",
@ -31,10 +27,9 @@
},
"homepage": "https://github.com/uCOMmandIt/uci-mcp#readme",
"dependencies": {
"@uci/i2c": "^0.1.0",
"@uci/utils": "^0.1.0",
"aggregation": "^1.2.0",
"bitwise": "^1.3.0"
"@uci/i2c-device": "^0.1.1",
"@uci/logger": "0.0.1",
"@uci/utils": "^0.1.1"
},
"@std/esm": "cjs",
"devDependencies": {

View File

@ -1,5 +1,6 @@
// import _ from '@uci/utils/src/byte'
import _ from '../../archive/uci-utils/src/byte'
// import _ from '../../archive/uci-utils/src/byte'
import _ from '@uci/utils/src/byte'
import { CHIP, PIN } from './config'
export const chip = {
@ -41,7 +42,7 @@ export const pin = {
for(let name of Object.keys(PIN.setting)) {
let op = cfg[name] ? 'on' : 'off'
// console.log(name, op)
let busreply = await state.bind(this)(packet,op,PIN.setting[name])
let busreply = await this.pin._state(packet,op,PIN.setting[name])
if (busreply.error) return busreply
reply.status[name] = busreply.status
}
@ -52,7 +53,7 @@ export const pin = {
status: async function (packet) {
let reg = packet.reg ? PIN.cmd[packet.reg] : PIN.cmd.gpio
if (!reg) return {error:`unknown register ${packet.reg}`, packet:packet }
let reply = { cmd:'reply', _req:packet}
let reply = { cmd:'reply'}
let pins = parsePins(packet.pins)
let state = new _.Byte()
let bus = await this.bus.read(sreg(reg, packet.port))
@ -67,30 +68,51 @@ export const pin = {
}
return reply
},
_state: async function(packet,op,reg){
reg = (reg!==undefined)? reg : PIN.cmd.gpio
// console.log(op, reg, packet)
let reply = { cmd:'reply'}
let pins = parsePins(packet.pins)
let state = new _.Byte()
let bus = await this.bus.read(sreg(reg,packet.port))
if (bus.error) return bus
state.value = bus.response
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.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'})
return reply
},
// threse three only for output pins
state : {
on: async function (packet) {
return state.bind(this)(packet,'on')
return this.pin._state(packet,'on')
},
off: async function (packet) {
return state.bind(this)(packet,'off')
return this.pin._state(packet,'off')
},
toggle: async function (packet) {
return state.bind(this)(packet,'toggle')
return this.pin._state(packet,'toggle')
}
},
// // will create packet to determine pin caused interrupt, packet will come from interrupt module
// interrupt: {
// find: packet =>{},
// report: packet=>{} // come here after determining which pin to report to requester
// }
// will create packet to determine pin caused interrupt, packet will come from interrupt module
interrupt: {
find: async function (packet) {
console.log('interrupt fired',packet)
return Promise.resolve({cmd:null})
},
report: ()=>{}, // come here after determining which pin to report to requester
async: async ()=>{}
}
}
const parsePins = function(pins) {
if (typeof pins==='number') pins = [pins]
if (typeof pins==='string') {
if (pins==='all') pins = _.byteFormat(255,{in:'DEC', out:'PLC'})
else pins = pins.split(',')
else pins = pins.split(/[,:\s]+/).map(Number).filter( (x) => !Number.isNaN(x))
}
return new _.Byte(pins,'PLC')
}
@ -98,21 +120,3 @@ const parsePins = function(pins) {
const sreg = (reg,port) => {
return reg + ((port==='B') ? 0x10 : 0)
}
const state = async function(packet,op,reg){
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.bus.read(sreg(reg,packet.port))
if (bus.error) return bus
state.value = bus.response
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.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'})
return reply
}

View File

@ -1,8 +1,9 @@
// import Base from '@uci/base'
import Device from '../../uci-i2c-device/src/device-packet'
import Device from '@uci/i2c-device'
// import Device from '../../uci-i2c-device/src/device-packet'
import { pin, chip } from './commands'
import logger from '../../uci-logger/src/logger'
// import logger from '../../uci-logger/src/logger'
import logger from '@uci/logger'
let log = {}
const LOG_OPTS = (id) => {
return {
@ -16,23 +17,29 @@ const LOG_OPTS = (id) => {
export default class MCP230XX extends Device {
constructor(opts) {
opts.nmcp = opts.nmcp || {path: (process.env.SOCKETS_DIR || __dirname) + '/mcp.sock'}
opts.sockets = 'nmcp#s>n,tmcp#s>t,'
// console.log(opts)
if(opts.nmcp) {
if (typeof opts.nmcp.path!=='string') opts.nmcp = {path: (process.env.SOCKETS_DIR || __dirname) + '/mcp.sock'}
opts.sockets = (opts.sockets ? (opts.sockets+',') : '') + 'nmcp#s>n'
}
if(opts.tmcp) {
if (typeof opts.nmcp.port!=='number') opts.tmcp.port = 1776
opts.sockets = (opts.sockets ? (opts.sockets+',') : '') + 'tmcp#s>t'
}
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.pin = this.bindFuncs(pin)
this.chip = this.bindFuncs(chip)
}
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)}`
let res = await this.chip.cfg.bind(this)({})
let res = await this.chip.cfg({})
// console.log(res)
let cfg = this.chip17 ?'10100010':'00100010'
if (res.response !==cfg ) throw `could not configure mcp chip at ${this.address}=0x${this.address.toString(16)}`
}