RxMotionKit

RxMotionKit - reactive motion sensors managing library 🏃

46
1
Swift

RxMotionKit

Platform
CocoaPods Compatible
Carthage compatible

RxMotionKit is a motion sensors managing library written in Swift.

Features

  • [x] CoreMotion RxSwift support
  • [x] Swifty interface
  • [x] Pleasant data model

Description

Library wraps CoreMotion and RxSwift frameworks and provides pleasant way to obtain data from sensors.

Requirements

  • Xcode 8.1+
  • Swift 3.0+

Installation

CocoaPods

RxMotionKit is available through CocoaPods. To install
it, simply specify it in your Podfile:

pod 'RxMotionKit', '~> 0.7.0'

Then, run the following command:

$ pod install

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate RxMotionKit into your Xcode project using Carthage, specify it in your Cartfile:

github "rsobolewski/RxMotionKit" ~> 0.7.0

Run carthage update to build the frameworks and drag them into your Xcode project.

Usage

Start motions source updates

To start updating data from sensors import RxMotionKit module and get reference to singleton instance of MotionManager:

import RxSwift
import RxMotionKit

unowned let motionManager = MotionManager.shared

subscribe an Observable:

motionManager.rx_didUpdateMotionData
	.subscribe(onNext: { (data) in
		// Handle received data
	})

and start updating selected sources:

motionManager.startUpdating([
    .accelerometr,
    .gyroscope,
    .magnetometer,
    .deviceMotion
    ],
    withInterval: 5.0
)

For convenient usage when you want to obtain few updates you can use take to automatically stop updates when defined limit is reached:

motionManager.rx_didUpdateMotionData
	.take(4)
	.subscribe(onNext: { (data) in
		// Handle received data
	})

Stop motions source updates

To stop updates from selected sources:

motionManager.stopUpdating([
    .accelerometr,
    .gyroscope,
    .magnetometer,
    .deviceMotion
])

Types of motion source

All motion sources that are available:

  • accelerometr - Accelerometer
  • gyroscope - Gyroscope
  • magnetometer - Magnetometer
  • deviceMotion - DeviceMotion

Also you can conveniently specify all motion sources using MotionSourceType.all.

Types of Observables

There are several types of Observables provided by MotionManager:

  • rx_didUpdateMotionData - general observable for motion data updates, that merges all events from all source updates
  • rx_didUpdateAccelerometerData - accelerometer updates
  • rx_didUpdateGyroscopeData - gyroscope updates
  • rx_didUpdateMagnetometerData - magnetometer updates
  • rx_didUpdateDeviceMotionData - device motion updates

Updates configuration

All sources require the update time interval, that must be provided in method which start updates:

let updateTimeInterval = 1.0 // 1 sec
startUpdating([.accelerometer], withInterval: updateTimeInterval)

For device motion updates you can choose a reference frame for attitude, which will be default if you not provide any.

startUpdating([.accelerometer], withInterval: updateTimeInterval, referenceFrame: .xArbitraryZVertical)

Communication

If you want to contribute, please submit a pull request. For code consistency please use SwiftLint with configuration from this repository.

Author

Robert Sobolewski, contact me on Twitter

License

RxMotionKit is available under the MIT license. See the LICENSE file for more info.