57 lines
2.1 KiB
JavaScript
57 lines
2.1 KiB
JavaScript
// TODO add uci logging debug
|
|
|
|
export default {
|
|
|
|
ack: async function (){
|
|
// TODO if mux channel used check it as well
|
|
let bus = await this.send(this.i2cbusName,{ 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() {
|
|
if (this.channel) await this._setChannel()
|
|
return await this.send(this.i2cbusName,{ cmd:'receive', args: {address:this.address}})
|
|
},
|
|
|
|
send: async function(byte) {
|
|
if (this.channel) await this._setChannel()
|
|
return await this.send(this.i2cbusName,{ cmd:'send', args: {address:this.address, byte:byte }})
|
|
},
|
|
|
|
// for devices needing a buffer/stream
|
|
readRaw: async function (length, buffer) {
|
|
if (this.channel) await this._setChannel()
|
|
return await this.send(this.i2cbusName,{ cmd:'readRaw', args: {address:this.address, length:length, buffer:buffer }})
|
|
},
|
|
|
|
writeRaw: async function (length, buffer) {
|
|
if (this.channel) await this._setChannel()
|
|
return await this.send(this.i2cbusName,{ 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) {
|
|
if (this.channel) await this._setChannel()
|
|
return await this.send(this.i2cbusName,{ cmd:'read', args: {address:this.address, cmd:cmd }})
|
|
},
|
|
|
|
write: async function (cmd, byte) {
|
|
if (this.channel) await this._setChannel()
|
|
return await this.send(this.i2cbusName,{ cmd:'write', args: {address:this.address, cmd:cmd, byte:byte }})
|
|
},
|
|
|
|
// for I2C devices that use a word length packackage
|
|
read2: async function (cmd) {
|
|
if (this.channel) await this._setChannel()
|
|
return await this.send(this.i2cbusName,{ cmd:'read2', args: {address:this.address, cmd:cmd }})
|
|
},
|
|
|
|
write2: async function (cmd, bytes) {
|
|
if (this.channel) await this._setChannel()
|
|
return await this.send(this.i2cbusName,{ cmd:'write2', args: {address:this.address, cmd:cmd, byte:bytes }})
|
|
}
|
|
}
|