Skip to content

Commit

Permalink
Merge pull request #90 from kekcheburec/master
Browse files Browse the repository at this point in the history
Fix initialization (for Barometer v2)
  • Loading branch information
kekcheburec authored Sep 17, 2021
2 parents 9fd4c48 + 094cd07 commit 2e15a03
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions modules/@amperka/barometer.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
/**
* Класс для работы с датчиком давления
* Pressure sensor class
*/
// Инициализация класса
var LPS331 = function(opts) {
// Class initialization
var Barometer = function(opts) {
opts = opts || {};
this._i2c = opts.i2c || I2C1;
this._address = opts.address || 0x5c;
};

// Метод записывает данные data в регистр reg
LPS331.prototype.writeI2C = function(reg, data) {
// The method writes data to the reg register
Barometer.prototype.writeI2C = function(reg, data) {
this._i2c.writeTo(this._address, [reg, data]);
};

// Метод производит чтение из регистра reg количестов байт count
LPS331.prototype.readI2C = function(reg, count) {
// The method reads from the reg register the number of bytes count
Barometer.prototype.readI2C = function(reg, count) {
if (count === undefined) {
count = 1;
}
this._i2c.writeTo(this._address, reg | 0x80);
return this._i2c.readFrom(this._address, count);
};

// Старт модуля
LPS331.prototype.init = function() {
// Module start
Barometer.prototype.init = function() {
if (this.whoAmI() === 0xbd) {
this.writeI2C(0x20, 0xc0);
var t = getTime() + 0.1;
while (getTime() < t);
return;
}
this.writeI2C(0x20, 0xe0);
};

// Температура
LPS331.prototype.temperature = function(units) {
// Temperature
Barometer.prototype.temperature = function(units) {
var data = this.readI2C(0x2b, 2);
var temp = data[0] | (data[1] << 8);
if (temp >= 32767) {
Expand All @@ -40,8 +46,8 @@ LPS331.prototype.temperature = function(units) {
return temp;
};

// Давление
LPS331.prototype.read = function(units) {
// Pressure
Barometer.prototype.read = function(units) {
var data = this.readI2C(0x28, 3);
var baro = (data[1] << 8) | (data[2] << 16) || data[0];
if (baro > 2147483647) {
Expand All @@ -58,12 +64,12 @@ LPS331.prototype.read = function(units) {
return baro;
};

// Метод возвращает идентификатор устройства
LPS331.prototype.whoAmI = function() {
// The method returns the device identifier
Barometer.prototype.whoAmI = function() {
return this.readI2C(0x0f)[0];
};

// Экспортируем класс
// Exporting the class
exports.connect = function(opts) {
return new LPS331(opts);
return new Barometer(opts);
};

0 comments on commit 2e15a03

Please sign in to comment.