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-22
Revision:
2:e09ad9c1f751
Parent:
1:2507a3379f17
Child:
3:3e9586433ce5

File content as of revision 2:e09ad9c1f751:

/**
 * @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 : Mbed Library
 */
#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.
 * more information : http://www.robot-electronics.co.uk/htm/cmps3tech.htm
 */
class CMPS03 {

protected :

    I2C* _i2c;
    InterruptIn _boussole;
    Timer _tim;

    void rise();
    void fall();

    int  _i2cAddress;
    long _startTime, _stopTime, _pwmBearing;

public :

    /**
     * Constructor of a CMPS03 Compass object.
     * @note If not used, the two I2C pins 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 : I2C adress of the compass
     */
    CMPS03(PinName pwm, PinName sda, PinName scl, int address = 0xC0);

    /**
     * Reads the software revision register of the compass (I2C register 0).
     *
     * @return Software revision register (as a byte).
     */
    char readSoftwareRevision(void);

    /**
     * Reads the current bearing of the compass (I2C registers 2 and 3).
     *
     * @return Current bearing of the compass in degree (value between 0.0 and 359.9).
     */
    double readBearing(void);

    /**
     * Reads the last bearing of the compass from PWM signal (1 bearing each 100ms approx.).
     *
     * @return Current bearing of the compass in degree (0.00 and 359.99)
     */
    double getBearing(void);

    /** 
     * A short hand for getBearing
     */
    operator double();
    
};

#endif /* CMPS03_H */