Skip to content

Commit

Permalink
docs(README): Update readme to show better examples
Browse files Browse the repository at this point in the history
  • Loading branch information
AlejandroHerr committed Dec 30, 2018
1 parent 33fbeb9 commit b02a42e
Showing 1 changed file with 113 additions and 4 deletions.
117 changes: 113 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,132 @@ And build the module with `yarn build` or `npm run build`.

## Usage

The `BMP280` takes as argument and instance of the `BusInterface`:
The [`BMP280` factory](https://alejandroherr.github.io/async-bmp280/globals.html#bmp280) takes as argument an instance of the [`BusInterface`](https://alejandroherr.github.io/async-i2c-bus/interfaces/businterface.html) and returns an instance of the [`BMP280Interface`](https://alejandroherr.github.io/async-bmp280/interfaces/bmp280interface.html).

```javascript
function BMP280({ bus }: { bus: BusInterface }): BMP280Interface;
```

The next step is to `init` the device to reset, acquire temperature/pressure compensation and configure the device:
The `BMP280Interfaces` inherits from [`DeviceInterface`](https://alejandroherr.github.io/async-i2c-bus/interfaces/deviceinterface.html), hence all the low level methods such as `writeByte`, `readByte`,... are available to work with the device.

But it also offers some specific methods to work with the sensor.

### `init` and configuration

The `init` method performs a reset of the device, acquires temperature/pressure correction and configures the device with the values selected (if none present, it will use the default ones):

```javascript
init(params?: Partial<BMP280ControlMeasurement & BMP280Config>): Promise<BMP280Interface>;
```

The interfaces in the params are:

```javascript
interface BMP280ControlMeasurement {
temperatureOversampling: BMP280Oversampling;
pressureOversampling: BMP280Oversampling;
mode: BMP280Mode;
}
interface BMP280Config {
standbyTime: BMP280StandbyTime;
iirFilter: BMP280IirFilter;
}

// Types:
type BMP280Oversampling = 'x0' | 'x1' | 'x2' | 'x4' | 'x8' | 'x16';
type BMP280Mode = 'SLEEP' | 'FORCED' | 'NORMAL';
type BMP280StandbyTime = '500us' | '62ms' | '125ms' | '250ms' | '500ms' | '1s' | '2s' | '4s';
type BMP280IirFilter = 'x0' | 'x1' | 'x2' | 'x4' | 'x8' | 'x16';
```

**This is the recommended way of initializing the sensor.**

If you don't use it, be sure to call `readTemperatureCorrection` and `readPressureCorrection` to be able to read the right temperature/pressure values.

Example of `init` vs no `init`

```javascript
import { Bus } from 'async-i2c-bus';
import { BMP280, REGISTERS, OFFSETS, OVERSAMPLING, MODE, STANDBY_TIME, IIR_FILTER } from 'async-i2c-bus';

// ...

const bus = Bus();
await bus.open();

const bmp280 = BMP280({ bus });

// init version
await bmp280.init();

// no-init version;

await bmp280.reset();
await bmp280.readTemperatureCorrection();
await bmp280.readPressureCorrection();

await bmp280.writeByte(
REGISTERS.CTRL_MEAS,
(OVERSAMPLING.x1 << OFFSETS.OSRS_T) | (OVERSAMPLING.x1 << OFFSETS.OSRS_P) | MODE.NORMAL,
);
await bmp280.writeByte(REGISTERS.CONFIG, (STANDBY_TIME['500us'] << OFFSETS.T_SB) | (IIR_FILTER.x16 << OFFSETS.FILTER));
```

After this step, the device is ready to `readTemperature` and to `readPressure`.

For more details, check the full auto-generated [documentation](https://alejandroherr.github.io/async-bmp280/) and get familiar with [BMP280 datasheet](https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMP280-DS001.pdf).

### Example of `NORMAL` usage
### Read/write `config` and `ctrl_meas`

The module exports
There's two handy methods to read/write the registers `config` and `ctrl_meas`.

#### `write`

```javascript
writeControlMeasurement(controlMeasurement: Partial<BMP280ControlMeasurement>): Promise<BMP280Interface>
writeConfig(controlMeasurement: Partial<BMP280Config>): Promise<BMP280Interface>
```

Both functions will apply the values passed in the argument and apply them on the current value. That means that it is possible to change only one value or more in the register and leave the rest untouched.

```javascript
await bmp280.writeConfig({ iirFilter: 'x16' });

// it is equivalent as:

const currentValue = await bmp280.readByte(REGISTERS.config);
const nextValue = (currentValue ^ (MASKS.FILTER << OFFSETS.FILTER)) | (IIR_FILTER.x16 << OFFSETS.FILTER);
await bmp280.writeByte(REGISTERS.CONFIG, nextValue);
```

#### `read`

Read is the inverse function of the previous two functions:

```javascript
readControlMeasurement(): Promise<BMP280ControlMeasurement>
readConfig(): Promise<BMP280Config>
```

Example:

```javascript
await bmp280.writeConfig({ iirFilter: 'x16', standbyTime: '4s' });
await bmp280.readConfig(); // Returns { iirFilter: 'x16', standbyTime: '4s' }
```

### `readTemperature` and `readPressure`

```javascript
readTemperature(): Promise<number>
readPressure(): Promise<number>
```

Read temperature returns the celsius degrees.
Read pressure returns the pressure in Pascals.

### Full example of `NORMAL` (mode) usage

```javascript
import { Bus } from 'async-i2c-bus';
Expand Down Expand Up @@ -93,7 +202,7 @@ const main = async () => {
};
```

### Example of `FORCED` usage
### Example of `FORCED` (mode) usage

```javascript
import { Bus } from 'async-i2c-bus';
Expand Down

0 comments on commit b02a42e

Please sign in to comment.