CMPS03 Compass library with only PWM support. I2C support will be added shortly, while it will arrive, you may use MBED component library if you wish to use CMPS03 I2C interface

Dependents:   TestBoussole FRC_2018 0hackton_08_06_18 lib_FRC_2019 ... more

CMPS03.h

Committer:
haarkon
Date:
2018-05-18
Revision:
0:41ff46c0f65a
Child:
1:2507a3379f17

File content as of revision 0:41ff46c0f65a:

/**
 * @author Hugues Angelis, based on Aaron Berk
 *
 * @section LICENSE
 *
 * Copyright (c) 2010 ARM Limited
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @section DESCRIPTION
 *
 * CMPS03 digital compass module I2C.
 *
 * Datasheet:
 *
 * http://www.robot-electronics.co.uk/htm/cmps3tech.htm
 */

#ifndef CMPS03_H
#define CMPS03_H

/**
 * Includes
 */
#include "mbed.h"

/**
 * Defines
 */
#define CMPS03_DEFAULT_I2C_ADDRESS 0xC0

//-----------
// Registers
//-----------
#define SOFTWARE_REVISION_REG    0x0
#define COMPASS_BEARING_WORD_REG 0x2

/**
 * CMPS03 digital compass module.
 */
class CMPS03 {

protected :

    I2C* _i2c;
    InterruptIn _boussole;
    Timer _tim;

    void rise();
    void fall();

    int  _i2cAddress;
    long _startTime, _stopTime, _pwmBearing;

public :

    /**
     * Constructor.
     * If not used, the 2 I2C pin should be pulled up.
     *
     * @param pwm mbed pin to use to get the bearing Pulse
     * @param sda mbed pin to use as the I2C serial data
     * @param scl mbed pin to use as the I2C serial clock
     * @param address is the I2C adress of the compass
     */
    CMPS03(PinName pwm, PinName sda, PinName scl, int address = 0xC0);

    /**
     * Reads the software revision register [register 0] on the device.
     *
     * @return The contents of the software revision register as a byte.
     */
    char readSoftwareRevision(void);

    /**
     * Reads the current bearing of the compass.
     *
     * @return The current bearing of the compass as a value between 0.0 and 359.9,
     *         representing 0 - 359.9 degrees.
     */
    float readBearing(void);

    /**
     * Reads the last bearing of the compass thru PWM signal (1 bearing each 100ms approx.).
     *
     * @return The current bearing of the compass as a value between 0.00 and 359.99,
     *         representing 0 - 359.99 degrees.
     */
    float getBearing(void);
    
};

#endif /* CMPS03_H */