Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 972447e

Browse files
authored
docs: update doc for gravity API and example to get raw device acceleration (react-native-sensors#400)
1 parent 88e79ea commit 972447e

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

.all-contributorsrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,16 @@
325325
"contributions": [
326326
"code"
327327
]
328+
},
329+
{
330+
"login": "Ehsan0Hejazi",
331+
"name": "Ehsan Hejazi",
332+
"avatar_url": "https://avatars.githubusercontent.com/u/70890584?v=4",
333+
"profile": "https://github.com/Ehsan0Hejazi",
334+
"contributions": [
335+
"code",
336+
"doc"
337+
]
328338
}
329339
],
330340
"repoType": "github"

docs/API.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ const subscription = accelerometer.subscribe(({ x, y, z, timestamp }) => console
1818

1919
It might be interesting to note that the gravity of the earth is not removed from the sensoric values.
2020
Dependening on the position of the phone you will need to substract this from the values if you are interested in the raw values.
21+
See example : [device acceleration](https://react-native-sensors.github.io/docs/Usage.html#Raw-device-acceleration)
22+
23+
## gravity: Observable<{x: number, y: number, z: number, timestamp: string}>
24+
25+
```js
26+
import { gravity } from "react-native-sensors";
27+
28+
const subscription = gravity.subscribe(({ x, y, z, timestamp }) => console.log({ x, y, z, timestamp }));
29+
```
2130

2231
## gyroscope: Observable<{x: number, y: number, z: number, timestamp: string}>
2332

docs/Usage.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ sidebar_label: General
66

77
You can access your sensor data through a [RxJS Observable](http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html). This way you have the maximum of control over your data stream. You can add the values up, filter them, only react if a certain value is reached: You have the choice. Here is a small example showing the relatively small API interface. If you would like to learn more, please see the [API specification](/docs/API.html).
88

9+
10+
## Device speed
11+
912
```javascript
1013
import { accelerometer, gyroscope, setUpdateIntervalForType, SensorTypes } from "react-native-sensors";
1114
import { map, filter } from "rxjs/operators";
@@ -29,3 +32,36 @@ setTimeout(() => {
2932
subscription.unsubscribe();
3033
}, 1000);
3134
```
35+
36+
## Raw device acceleration
37+
38+
You can get user raw acceleration which is calculated with (accelerometer - gravity) formula, to achieve this you can use RxJs combineLatest
39+
40+
```javascript
41+
import { accelerometer, gravity, setUpdateIntervalForType, SensorTypes } from "react-native-sensors";
42+
import { combineLatest } from "rxjs";
43+
import { map } from 'rxjs/operators';
44+
45+
const interval = 100;
46+
47+
setUpdateIntervalForType(SensorTypes.accelerometer, interval);
48+
setUpdateIntervalForType(SensorTypes.gravity, interval);
49+
50+
const userAccelerationStream = combineLatest(accelerometer, gravity).pipe(
51+
map(([accelerometerValue, gravityValue]) => ({
52+
accelerometer: accelerometerValue,
53+
gravity: gravityValue,
54+
}))
55+
)
56+
57+
const subscription = userAccelerationStream.subscribe(event => console.log(
58+
'x:', event.accelerometer.x - event.gravity.x,
59+
'y:', event.accelerometer.y - event.gravity.y,
60+
'z:', event.accelerometer.z - event.gravity.z,
61+
));
62+
63+
setTimeout(() => {
64+
subscription.unsubscribe();
65+
}, 1000);
66+
```
67+

0 commit comments

Comments
 (0)