Skip to content

Instantly share code, notes, and snippets.

@DrewDahlman
Last active March 12, 2017 15:16
Show Gist options
  • Save DrewDahlman/8d48b3fb4e06730fab75c066f230fe95 to your computer and use it in GitHub Desktop.
Save DrewDahlman/8d48b3fb4e06730fab75c066f230fe95 to your computer and use it in GitHub Desktop.
const REGISTRY = require('./registry');
class i2cInterface {
/*
------------------------------------------
| construct:void (-)
------------------------------------------ */
constructor(){
this._readRegisters( REGISTRY.OSC_CALIBRATE_VAL, 16, (err, data) => {
console.log(data.readInt16BE(8));
});
}
/*
------------------------------------------
| _readRegisters:void (-)
|
| Read the registers via I2C.
------------------------------------------ */
_readRegisters(addressToRead, bytesToRead, callback){
this.queue.place( () => {
this.i2c.transfer( new Buffer([addressToRead]), bytesToRead, (err, data) => {
this.queue.next();
if( callback ){
callback(err, data);
}
})
});
}
/*
------------------------------------------
| _writeRegisters:void (-)
|
| Write to the register via I2C.
------------------------------------------ */
_writeRegisters(addressToRead, dataToWrite, callback){
// console.log(addressToRead, dataToWrite, callback);
this.queue.place( () => {
this.i2c.send( new Buffer([addressToRead, dataToWrite]), (err, data) => {
this.queue.next();
if( callback ){
callback(err, data);
}
})
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment