Last active
March 31, 2017 10:30
-
-
Save rtanote/c8aa7e9f3e3ab398a30bc55adf3100e5 to your computer and use it in GitHub Desktop.
i2c communication for KX023 in Swift3 + Konashi SDK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// KonashiI2CTest | |
// | |
// Specification http://kionixfs.kionix.com/en/datasheet/KX023-1025%20Specifications%20Rev%2012.0.pdf | |
import UIKit | |
import konashi_ios_sdk | |
extension Data { | |
init<T>(fromArray values: [T]) { | |
var values = values | |
self.init(buffer: UnsafeBufferPointer(start: &values, count: values.count)) | |
} | |
func toArray<T>(type: T.Type) -> [T] { | |
return self.withUnsafeBytes { | |
[T](UnsafeBufferPointer(start: $0, count: self.count/MemoryLayout<T>.stride)) | |
} | |
} | |
} | |
let i2cAddress:UInt8 = 0x1F | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
Konashi.find() | |
Konashi.shared().readyHandler = { () -> Void in | |
print("ready!!") | |
Konashi.i2cMode(.enable400K) | |
self.configSensor() | |
} | |
NotificationCenter.default.addObserver(self, selector: #selector(onReady), name: NSNotification.Name(rawValue: KonashiEventI2CReadCompleteNotification), object: nil) | |
} | |
deinit { | |
NotificationCenter.default.removeObserver(self) | |
} | |
@objc private func onReady() { | |
let data = Konashi.i2cReadData() | |
let bytes = data?.toArray(type: UInt8.self) | |
print(bytes!) | |
Konashi.i2cStopCondition() | |
} | |
func configSensor() { | |
// 1 | |
Konashi.i2cStartCondition() | |
Thread.sleep(forTimeInterval: 0.01) | |
let standByMode = Data(bytes: [0x18, 0x40]) | |
Konashi.i2cWrite(standByMode, address: i2cAddress) | |
Thread.sleep(forTimeInterval: 0.01) | |
Konashi.i2cStopCondition() | |
Thread.sleep(forTimeInterval: 0.01) | |
// 2 | |
Konashi.i2cStartCondition() | |
Thread.sleep(forTimeInterval: 0.01) | |
let outputDataRate = Data(bytes: [0x1B, 0x02]) | |
Konashi.i2cWrite(outputDataRate, address: i2cAddress) | |
Thread.sleep(forTimeInterval: 0.01) | |
Konashi.i2cStopCondition() | |
Thread.sleep(forTimeInterval: 0.01) | |
// 3 | |
Konashi.i2cStartCondition() | |
Thread.sleep(forTimeInterval: 0.01) | |
let wakeUp = Data(bytes: [0x18, 0xC0]) | |
Konashi.i2cWrite(wakeUp, address: i2cAddress) | |
Thread.sleep(forTimeInterval: 0.01) | |
Konashi.i2cStopCondition() | |
Thread.sleep(forTimeInterval: 0.01) | |
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block:{ (timer) -> Void in | |
Konashi.i2cStartCondition() | |
Thread.sleep(forTimeInterval: 0.01) | |
let regAddressXOUTL = Data(bytes: [0x06]) | |
Konashi.i2cWrite(regAddressXOUTL, address: i2cAddress) | |
Thread.sleep(forTimeInterval: 0.01) | |
Konashi.i2cRestartCondition() | |
Thread.sleep(forTimeInterval: 0.01) | |
Konashi.i2cReadRequest(2, address: i2cAddress) | |
Thread.sleep(forTimeInterval: 0.01) | |
}) | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment