Version 1.0 - Library for Parallax OFN module 27903

Fork of OFN by Neha Kadam

Import library

Public Member Functions

OFN (PinName sda, PinName scl)
Constructor.
bool checkMotion ()
Function to check if motion has been detected Checks the 7th bit of MOTION register and Returns true if motion detected else returns false.
int readX ()
Function to get displacement in the X direction Returns an integer value.
int readY ()
Function to get displacement in the X direction Returns an integer value.
int readOrientation ()
Function to read the Orientation register Returns an integer value 1 - default orientation 0 - rotated by 90 degrees.
Committer:
nkadam
Date:
Mon Mar 09 03:18:22 2015 +0000
Revision:
0:c559e09a7b5d
Version 1.0 of Library for Parallax OFN module 27903

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nkadam 0:c559e09a7b5d 1
nkadam 0:c559e09a7b5d 2 //@Authors: Guru Das Srinagesh, Neha Kadam
nkadam 0:c559e09a7b5d 3 /* Copyright (c) March, 2015 MIT License
nkadam 0:c559e09a7b5d 4 *
nkadam 0:c559e09a7b5d 5 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
nkadam 0:c559e09a7b5d 6 * and associated documentation files (the "Software"), to deal in the Software without restriction,
nkadam 0:c559e09a7b5d 7 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
nkadam 0:c559e09a7b5d 8 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
nkadam 0:c559e09a7b5d 9 * furnished to do so, subject to the following conditions:
nkadam 0:c559e09a7b5d 10 *
nkadam 0:c559e09a7b5d 11 * The above copyright notice and this permission notice shall be included in all copies or
nkadam 0:c559e09a7b5d 12 * substantial portions of the Software.
nkadam 0:c559e09a7b5d 13 *
nkadam 0:c559e09a7b5d 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
nkadam 0:c559e09a7b5d 15 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
nkadam 0:c559e09a7b5d 16 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
nkadam 0:c559e09a7b5d 17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
nkadam 0:c559e09a7b5d 18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
nkadam 0:c559e09a7b5d 19 */
nkadam 0:c559e09a7b5d 20
nkadam 0:c559e09a7b5d 21 #include "ofn.h"
nkadam 0:c559e09a7b5d 22
nkadam 0:c559e09a7b5d 23 OFN::OFN(PinName sda, PinName scl): i2c(sda, scl){
nkadam 0:c559e09a7b5d 24 i2c.frequency(400000); // Set I2C frequency to 400kHz (as per datasheet)
nkadam 0:c559e09a7b5d 25 MOTREG_ADDR = 0x02;
nkadam 0:c559e09a7b5d 26 DELTA_X_ADDR = 0x03;
nkadam 0:c559e09a7b5d 27 DELTA_Y_ADDR = 0x04;
nkadam 0:c559e09a7b5d 28 }
nkadam 0:c559e09a7b5d 29
nkadam 0:c559e09a7b5d 30 bool OFN::checkMotion(){
nkadam 0:c559e09a7b5d 31 char tx[1], rx[1];
nkadam 0:c559e09a7b5d 32 tx[0] = MOTREG_ADDR;
nkadam 0:c559e09a7b5d 33 i2c.write((DEVICE_ADDR<<1)&0xFE, tx, 1, true);
nkadam 0:c559e09a7b5d 34 i2c.read((DEVICE_ADDR<<1)|0x01, rx, 1);
nkadam 0:c559e09a7b5d 35 MotReg = rx[0];
nkadam 0:c559e09a7b5d 36
nkadam 0:c559e09a7b5d 37 moved = MotReg & 0x80;
nkadam 0:c559e09a7b5d 38
nkadam 0:c559e09a7b5d 39 return moved;
nkadam 0:c559e09a7b5d 40 }
nkadam 0:c559e09a7b5d 41
nkadam 0:c559e09a7b5d 42 int OFN::readX(){
nkadam 0:c559e09a7b5d 43 char dxAdd[1];
nkadam 0:c559e09a7b5d 44 dxAdd[0] = DELTA_X_ADDR;
nkadam 0:c559e09a7b5d 45 if(moved){
nkadam 0:c559e09a7b5d 46 i2c.write((DEVICE_ADDR<<1)&0xFE, dxAdd , 1, true);
nkadam 0:c559e09a7b5d 47 i2c.read((DEVICE_ADDR<<1)|0x01, raw_delta_X, 1);
nkadam 0:c559e09a7b5d 48 delta_X = (raw_delta_X[0] > 127)? raw_delta_X[0] - 255 : raw_delta_X[0];
nkadam 0:c559e09a7b5d 49 }
nkadam 0:c559e09a7b5d 50 return delta_X;
nkadam 0:c559e09a7b5d 51 }
nkadam 0:c559e09a7b5d 52
nkadam 0:c559e09a7b5d 53 int OFN::readY(){
nkadam 0:c559e09a7b5d 54 char dyAdd[1];
nkadam 0:c559e09a7b5d 55 dyAdd[0] = DELTA_Y_ADDR;
nkadam 0:c559e09a7b5d 56 if(moved){
nkadam 0:c559e09a7b5d 57 i2c.write((DEVICE_ADDR<<1)&0xFE, dyAdd, 1, true);
nkadam 0:c559e09a7b5d 58 i2c.read((DEVICE_ADDR<<1)|0x01, raw_delta_Y, 1);
nkadam 0:c559e09a7b5d 59 delta_Y = (raw_delta_Y[0] > 127)? raw_delta_Y[0] - 255 : raw_delta_Y[0];
nkadam 0:c559e09a7b5d 60 }
nkadam 0:c559e09a7b5d 61 return delta_Y;
nkadam 0:c559e09a7b5d 62 }
nkadam 0:c559e09a7b5d 63
nkadam 0:c559e09a7b5d 64 int OFN::readOrientation(){
nkadam 0:c559e09a7b5d 65 char tx[1], rx[1];
nkadam 0:c559e09a7b5d 66 int orientReg;
nkadam 0:c559e09a7b5d 67 tx[0] = OFN_ORIENTATION_CTRL;
nkadam 0:c559e09a7b5d 68 i2c.write((DEVICE_ADDR<<1)&0xFE, tx, 1, true);
nkadam 0:c559e09a7b5d 69 i2c.read((DEVICE_ADDR<<1)|0x01, rx, 1);
nkadam 0:c559e09a7b5d 70 orientReg = rx[0];
nkadam 0:c559e09a7b5d 71 return orientReg;
nkadam 0:c559e09a7b5d 72 }