improve refactor socket options, add _state to module export and bind all functions in pins and chip
parent
3c21bd66da
commit
342c6f98d2
|
@ -4,10 +4,19 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import Base from '../../uci-base/src/base'
|
import Base from '../../uci-base/src/base'
|
||||||
|
import { spawn } from 'child_process'
|
||||||
|
|
||||||
const PATH = '/opt/sockets/mcp.sock'
|
const PATH = '/opt/sockets/mcp.sock'
|
||||||
|
|
||||||
const delay = time => new Promise(res=>setTimeout(()=>res(),time))
|
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 () => {
|
(async () => {
|
||||||
|
|
||||||
|
|
|
@ -8,9 +8,15 @@ import MCP230XX from '../src/mcp230xx-packet'
|
||||||
;
|
;
|
||||||
(async () => {
|
(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 => {
|
})().catch(err => {
|
||||||
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
|
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
|
||||||
|
|
|
@ -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')
|
|
||||||
})
|
|
|
@ -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)
|
|
||||||
})
|
|
17
package.json
17
package.json
|
@ -1,19 +1,15 @@
|
||||||
{
|
{
|
||||||
"name": "@uci/mcp",
|
"name": "@uci/mcp",
|
||||||
"main": "src/mcp23008-17.js",
|
"main": "src/mcp230xx-packet.mjs",
|
||||||
"version": "0.1.2",
|
"version": "0.1.2",
|
||||||
"description": "Classes and Helper Functions for using the MCP chip on I2C Bus",
|
"description": "Classes and Helper Functions for using the MCP chip on I2C Bus",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"mcp": "SOCKETS_DIR=/opt/sockets node_modules/.bin/nodemon --require @std/esm examples/mcp",
|
"relays": "SOCKETS_DIR=/opt/sockets node --require @std/esm examples/relays",
|
||||||
"mcpl": "DEBUG=true SOCKETS_DIR=/opt/sockets node_modules/.bin/nodemon --require @std/esm examples/mcp",
|
|
||||||
"test": "./node_modules/.bin/mocha --reporter list --timeout 30000",
|
"test": "./node_modules/.bin/mocha --reporter list --timeout 30000",
|
||||||
"testw": "./node_modules/.bin/mocha --reporter list -- watch --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",
|
"testibc": "istanbul cover ./node_modules/.bin/_mocha test/ --report lcovonly -- -R spec --recursive && codecov || true",
|
||||||
"sample": "node demo/sample.js",
|
"sample": "node demo/sample.js",
|
||||||
"inter": "sudo node demo/interrupt.js"
|
"inter": "sudo node -r @std/esm examples/interrupt"
|
||||||
},
|
},
|
||||||
"author": "David Kebler",
|
"author": "David Kebler",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@ -31,10 +27,9 @@
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/uCOMmandIt/uci-mcp#readme",
|
"homepage": "https://github.com/uCOMmandIt/uci-mcp#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@uci/i2c": "^0.1.0",
|
"@uci/i2c-device": "^0.1.1",
|
||||||
"@uci/utils": "^0.1.0",
|
"@uci/logger": "0.0.1",
|
||||||
"aggregation": "^1.2.0",
|
"@uci/utils": "^0.1.1"
|
||||||
"bitwise": "^1.3.0"
|
|
||||||
},
|
},
|
||||||
"@std/esm": "cjs",
|
"@std/esm": "cjs",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
// import _ from '@uci/utils/src/byte'
|
// 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'
|
import { CHIP, PIN } from './config'
|
||||||
|
|
||||||
export const chip = {
|
export const chip = {
|
||||||
|
@ -41,7 +42,7 @@ export const pin = {
|
||||||
for(let name of Object.keys(PIN.setting)) {
|
for(let name of Object.keys(PIN.setting)) {
|
||||||
let op = cfg[name] ? 'on' : 'off'
|
let op = cfg[name] ? 'on' : 'off'
|
||||||
// console.log(name, op)
|
// 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
|
if (busreply.error) return busreply
|
||||||
reply.status[name] = busreply.status
|
reply.status[name] = busreply.status
|
||||||
}
|
}
|
||||||
|
@ -52,7 +53,7 @@ export const pin = {
|
||||||
status: async function (packet) {
|
status: async function (packet) {
|
||||||
let reg = packet.reg ? PIN.cmd[packet.reg] : PIN.cmd.gpio
|
let reg = packet.reg ? PIN.cmd[packet.reg] : PIN.cmd.gpio
|
||||||
if (!reg) return {error:`unknown register ${packet.reg}`, packet:packet }
|
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 pins = parsePins(packet.pins)
|
||||||
let state = new _.Byte()
|
let state = new _.Byte()
|
||||||
let bus = await this.bus.read(sreg(reg, packet.port))
|
let bus = await this.bus.read(sreg(reg, packet.port))
|
||||||
|
@ -67,30 +68,51 @@ export const pin = {
|
||||||
}
|
}
|
||||||
return reply
|
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
|
// threse three only for output pins
|
||||||
state : {
|
state : {
|
||||||
on: async function (packet) {
|
on: async function (packet) {
|
||||||
return state.bind(this)(packet,'on')
|
return this.pin._state(packet,'on')
|
||||||
},
|
},
|
||||||
off: async function (packet) {
|
off: async function (packet) {
|
||||||
return state.bind(this)(packet,'off')
|
return this.pin._state(packet,'off')
|
||||||
},
|
},
|
||||||
toggle: async function (packet) {
|
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
|
// will create packet to determine pin caused interrupt, packet will come from interrupt module
|
||||||
// interrupt: {
|
interrupt: {
|
||||||
// find: packet =>{},
|
find: async function (packet) {
|
||||||
// report: packet=>{} // come here after determining which pin to report to requester
|
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) {
|
const parsePins = function(pins) {
|
||||||
if (typeof pins==='number') pins = [pins]
|
if (typeof pins==='number') pins = [pins]
|
||||||
if (typeof pins==='string') {
|
if (typeof pins==='string') {
|
||||||
if (pins==='all') pins = _.byteFormat(255,{in:'DEC', out:'PLC'})
|
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')
|
return new _.Byte(pins,'PLC')
|
||||||
}
|
}
|
||||||
|
@ -98,21 +120,3 @@ const parsePins = function(pins) {
|
||||||
const sreg = (reg,port) => {
|
const sreg = (reg,port) => {
|
||||||
return reg + ((port==='B') ? 0x10 : 0)
|
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
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
// import Base from '@uci/base'
|
import Device from '@uci/i2c-device'
|
||||||
import Device from '../../uci-i2c-device/src/device-packet'
|
// 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'
|
||||||
|
import logger from '@uci/logger'
|
||||||
let log = {}
|
let log = {}
|
||||||
const LOG_OPTS = (id) => {
|
const LOG_OPTS = (id) => {
|
||||||
return {
|
return {
|
||||||
|
@ -16,23 +17,29 @@ const LOG_OPTS = (id) => {
|
||||||
|
|
||||||
export default class MCP230XX extends Device {
|
export default class MCP230XX extends Device {
|
||||||
constructor(opts) {
|
constructor(opts) {
|
||||||
opts.nmcp = opts.nmcp || {path: (process.env.SOCKETS_DIR || __dirname) + '/mcp.sock'}
|
if(opts.nmcp) {
|
||||||
opts.sockets = 'nmcp#s>n,tmcp#s>t,'
|
if (typeof opts.nmcp.path!=='string') opts.nmcp = {path: (process.env.SOCKETS_DIR || __dirname) + '/mcp.sock'}
|
||||||
// console.log(opts)
|
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)
|
super(opts)
|
||||||
log = logger.child(LOG_OPTS(this.id))
|
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 = this.bindFuncs(pin)
|
||||||
this.chip = chip
|
this.chip = this.bindFuncs(chip)
|
||||||
}
|
}
|
||||||
|
|
||||||
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)}`
|
||||||
let res = await this.chip.cfg.bind(this)({})
|
let res = await this.chip.cfg({})
|
||||||
|
// console.log(res)
|
||||||
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)}`
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue