bind device bus command and separate into module
fix socket options use npm modules instead of local pathmaster
parent
8fb4dbd788
commit
c564ce208d
|
@ -2,7 +2,7 @@
|
|||
"name": "@uci/i2c-device",
|
||||
"version": "0.1.1",
|
||||
"description": "Device Classes for I2C Interfacing",
|
||||
"main": "src/",
|
||||
"main": "src/device-packet.mjs",
|
||||
"scripts": {
|
||||
"testw": "./node_modules/.bin/mocha --reporter list --recursive --watch",
|
||||
"test": "istanbul cover ./node_modules/.bin/_mocha test/ --report lcovonly -- -R spec --recursive && codecov || true"
|
||||
|
@ -23,12 +23,13 @@
|
|||
"url": "https://github.com/uCOMmandIt/i2c/issues"
|
||||
},
|
||||
"homepage": "https://github.com/uCOMmandIt/i2c#readme",
|
||||
"@std/esm": "cjs",
|
||||
|
||||
"dependencies": {
|
||||
"@std/esm": "^0.18.0",
|
||||
"i2c-bus": "^1.x",
|
||||
|
||||
},
|
||||
"@std/esm": "cjs",
|
||||
"devDependencies": {
|
||||
"@std/esm": "^0.18.0",
|
||||
"chai": "^3.5.0",
|
||||
"chai-as-promised": "^6.0.0",
|
||||
"codecov": "^1.0.1",
|
||||
|
|
|
@ -7,4 +7,4 @@
|
|||
[![devDependencies](https://img.shields.io/david/dev/uCOMmandIt/uci-pkg-template.svg)](https://david-dm.org/uCOMmandIt/uci-pkg-template?type=dev)
|
||||
[![codecov](https://img.shields.io/codecov/c/github/uCOMmandIt/uci-pkg-template/master.svg)](https://codecov.io/gh/uCOMmandIt/uci-pkg-template)
|
||||
|
||||
A promise wrapper for an I2C arm C library methods and a Device Class that is the base class for an I2C device
|
||||
A promise wrapper for an I2C arm C library methods and a Device Class that extends the base case and writes packets to the ic2-bus class
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
export default {
|
||||
|
||||
ack: async function (){
|
||||
// TODO if mux channel used check it as well
|
||||
let bus = await this.send(this._bus_name,{ 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
|
||||
},
|
||||
|
||||
receive: async function() {
|
||||
await this._setChannel()
|
||||
return await this.send(this._bus_name, { cmd:'receive', args: {address:this.address}})
|
||||
},
|
||||
|
||||
send: async function(byte) {
|
||||
await this._setChannel()
|
||||
return await this.send(this._bus_name, { cmd:'send', args: {address:this.address, byte:byte }})
|
||||
},
|
||||
|
||||
// for devices needing a buffer/stream
|
||||
readRaw: async function (length, buffer) {
|
||||
await this._setChannel()
|
||||
return await this.send(this._bus_name, { cmd:'readRaw', args: {address:this.address, length:length, buffer:buffer }})
|
||||
},
|
||||
|
||||
writeRaw: async function (length, buffer) {
|
||||
await this._setChannel()
|
||||
return await this.send(this._bus_name, { cmd:'writeRaw', args: {address:this.address, length:length, buffer:buffer }})
|
||||
},
|
||||
|
||||
// both cmd and byte should be a single byte as a decimal or hex
|
||||
read: async function (cmd) {
|
||||
await this._setChannel()
|
||||
// console.log('after set before read',this.address,this.id,this._channel,await this.socket.bus.send(getState)
|
||||
return await this.send(this._bus_name, { cmd:'read', args: {address:this.address, cmd:cmd }})
|
||||
},
|
||||
|
||||
write: async function (cmd, byte) {
|
||||
await this._setChannel()
|
||||
// console.log('after set, before write',this.address,this.id,this._channel,await this.socket.bus.send(getState)
|
||||
return await this.send(this._bus_name, { cmd:'write', args: {address:this.address, cmd:cmd, byte:byte }})
|
||||
},
|
||||
|
||||
// for I2C devices that use a word length packackage
|
||||
read2: async function (cmd) {
|
||||
await this._setChannel()
|
||||
return await this.send(this._bus_name, { cmd:'read2', args: {address:this.address, cmd:cmd }})
|
||||
},
|
||||
|
||||
write2: async function (cmd, bytes) {
|
||||
await this._setChannel()
|
||||
return await this.send(this._bus_name, { cmd:'write2', args: {address:this.address, cmd:cmd, byte:bytes }})
|
||||
}
|
||||
|
||||
}
|
|
@ -1,7 +1,8 @@
|
|||
// import Base from '@uci/base'
|
||||
import Base from '../../uci-base/src/base'
|
||||
|
||||
import logger from '../../uci-logger/src/logger'
|
||||
import Base from '@uci/base'
|
||||
// import Base from '../../uci-base/src/base'
|
||||
import commands from './bus-device-commands'
|
||||
// import logger from '../../uci-logger/src/logger'
|
||||
import logger from '@uci/logger'
|
||||
let log = {}
|
||||
const LOG_OPTS = (id) => {
|
||||
return {
|
||||
|
@ -20,24 +21,20 @@ export default class Device extends Base {
|
|||
// either device process instance runs on same host (use named pipe) or not (the host of bus must be given)
|
||||
if (opts[opts.bus.name].host) opts[opts.bus.name].port = opts[opts.bus.name].port || 1776
|
||||
else opts[opts.bus.name].path = opts[opts.bus.name].path || (process.env.SOCKETS_DIR || __dirname) + '/i2c-bus.sock'
|
||||
opts.sockets = (opts.sockets || '') + opts.bus.name + '#c>' + ((opts[opts.bus.name].path) ? 'n' :'t')
|
||||
console.log(opts)
|
||||
opts.sockets = (opts.sockets ? (opts.sockets+',') : '') + opts.bus.name + '#c>' + ((opts[opts.bus.name].path) ? 'n' :'t')
|
||||
super(opts)
|
||||
console.log({opts:opts},'created i2c device with these options')
|
||||
log = logger.child(LOG_OPTS(this.id))
|
||||
if (!opts.address) log.fatal({opts:opts},'no i2c bus address supplied' )
|
||||
this.address = opts.address
|
||||
this._bus_name = opts.bus.name
|
||||
this._channel = opts.channel // if using TAC9546A channel number on which device is attached
|
||||
this.bus = device_funcs
|
||||
for(const func in this.bus) { // autobind
|
||||
this.bus[func] = this.bus[func].bind(this)
|
||||
}
|
||||
|
||||
this.bus = this.bindFuncs(commands)
|
||||
}
|
||||
|
||||
async init(){
|
||||
await super.init()
|
||||
let res = await this.bus.ack() // move this to device class
|
||||
let res = await this.bus.ack()
|
||||
let socket = this.socket[this._bus_name].opts
|
||||
let connection = socket.path || socket.host + ':' + socket.port
|
||||
if (!res.ack) throw `no device operational on bus '${socket.id}' (${connection}) at address ${this.address}=0x${this.address.toString(16)}`
|
||||
|
@ -56,62 +53,3 @@ export default class Device extends Base {
|
|||
|
||||
|
||||
} // end of MCP230XX Class
|
||||
|
||||
|
||||
const device_funcs = {
|
||||
|
||||
ack: async function (){
|
||||
// TODO if mux channel used check it as well
|
||||
let bus = await this.send(this._bus_name,{ 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
|
||||
},
|
||||
|
||||
receive: async function() {
|
||||
await this._setChannel()
|
||||
return await this.send(this._bus_name, { cmd:'receive', args: {address:this.address}})
|
||||
},
|
||||
|
||||
send: async function(byte) {
|
||||
await this._setChannel()
|
||||
return await this.send(this._bus_name, { cmd:'send', args: {address:this.address, byte:byte }})
|
||||
},
|
||||
|
||||
// for devices needing a buffer/stream
|
||||
readRaw: async function (length, buffer) {
|
||||
await this._setChannel()
|
||||
return await this.send(this._bus_name, { cmd:'readRaw', args: {address:this.address, length:length, buffer:buffer }})
|
||||
},
|
||||
|
||||
writeRaw: async function (length, buffer) {
|
||||
await this._setChannel()
|
||||
return await this.send(this._bus_name, { cmd:'writeRaw', args: {address:this.address, length:length, buffer:buffer }})
|
||||
},
|
||||
|
||||
// both cmd and byte should be a single byte as a decimal or hex
|
||||
read: async function (cmd) {
|
||||
await this._setChannel()
|
||||
// console.log('after set before read',this.address,this.id,this._channel,await this.socket.bus.send(getState)
|
||||
return await this.send(this._bus_name, { cmd:'read', args: {address:this.address, cmd:cmd }})
|
||||
},
|
||||
|
||||
write: async function (cmd, byte) {
|
||||
await this._setChannel()
|
||||
// console.log('after set, before write',this.address,this.id,this._channel,await this.socket.bus.send(getState)
|
||||
return await this.send(this._bus_name, { cmd:'write', args: {address:this.address, cmd:cmd, byte:byte }})
|
||||
},
|
||||
|
||||
// for I2C devices that use a word length packackage
|
||||
read2: async function (cmd) {
|
||||
await this._setChannel()
|
||||
return await this.send(this._bus_name, { cmd:'read2', args: {address:this.address, cmd:cmd }})
|
||||
},
|
||||
|
||||
write2: async function (cmd, bytes) {
|
||||
await this._setChannel()
|
||||
return await this.send(this._bus_name, { cmd:'write2', args: {address:this.address, cmd:cmd, byte:bytes }})
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue