2018-05-28 14:47:52 -07:00
import Base from '@uci/base'
import commands from './bus-device-commands'
2019-02-26 11:53:30 -08:00
import logger from '@uci-utils/logger'
2018-05-28 14:47:52 -07:00
let log = { }
2019-01-01 19:34:06 -08:00
class I2CDevice extends Base {
2018-05-28 14:47:52 -07:00
constructor ( opts ) {
2019-08-15 14:05:24 -07:00
opts . ndevice = opts . ndevice || { }
opts . tdevice = opts . tdevice || { }
2019-11-04 10:42:13 -08:00
opts . ndevice . path = opts . ndevice . path || opts . path || 'i2c-device'
opts . tdevice . port = opts . tdevice . port || opts . port || 1777
2018-05-28 14:47:52 -07:00
opts . bus = opts . bus || { }
2019-08-15 14:05:24 -07:00
if ( opts . bus . host ) opts . bus . port = opts . bus . port || 1776
else opts . bus . path = opts . bus . path || 'i2c-bus'
2018-05-28 14:47:52 -07:00
super ( opts )
2019-08-15 14:05:24 -07:00
this . addSocket ( 'bus' , 'c' , opts . bus . host ? 't' : 'n' , opts . bus )
2019-11-04 10:42:13 -08:00
this . addSocket ( opts . tdevice . name || 'device:tcp' , 's' , 't' , opts . tdevice )
this . addSocket ( opts . ndevice . name || 'device:np' , 's' , 'n' , opts . ndevice )
2019-08-15 14:05:24 -07:00
2019-01-01 19:34:06 -08:00
log = logger ( {
file : 'src/device-packet.js' ,
class : 'Device' ,
name : 'i2c-device' ,
id : this . id
} )
2019-04-26 11:09:33 -07:00
if ( ! opts . address ) log . fatal ( { opts : opts , method : 'constructor' , line : 26 , msg : 'no i2c bus address supplied' } )
2019-02-26 11:53:30 -08:00
this . address = + opts . address // make sure any passed number is number not a string
2019-04-27 15:47:37 -07:00
this . channel = + opts . channel // if using TAC9546A channel number on which device is attached
2018-05-28 14:47:52 -07:00
this . bus = this . bindFuncs ( commands )
2019-08-15 14:05:24 -07:00
this . _s . ack = async ( packet ) => { return Object . assign ( packet , await this . bus . ack ( ) ) } // give socket access to bus.ack
2017-10-13 12:26:36 -07:00
}
2019-01-01 19:34:06 -08:00
async init ( ) {
2019-08-15 14:05:24 -07:00
let res = await super . init ( )
if ( res . errors ) return { error : res . errors , msg : 'socket initialization' , }
res = await this . bus . ack ( )
2019-04-27 15:47:37 -07:00
let socket = this . getSocket ( 'bus' ) . opts
2018-05-28 14:47:52 -07:00
let connection = socket . path || socket . host + ':' + socket . port
2019-02-26 11:53:30 -08:00
if ( ! res . ack ) {
2019-04-26 11:09:33 -07:00
let err = { error : 'ack' , res : res , socket : socket , method : 'init' , line : 38 , connection : connection , address : this . address , msg : ` no bus or device on bus at address ${ this . address } =0x ${ this . address . toString ( 16 ) } ` }
log . error ( err )
2019-02-26 11:53:30 -08:00
return err
}
2018-05-28 14:47:52 -07:00
return res
2017-10-13 12:26:36 -07:00
}
2019-04-27 15:47:37 -07:00
// TODO enable mux board channel set
async _setChannel ( ) {
}
2019-04-26 11:09:33 -07:00
2018-05-28 14:47:52 -07:00
} // end of i2c Device Class
2019-01-01 19:34:06 -08:00
export default I2CDevice