Port from Avnet's Internet Of Things full WiGo demo: SmartConfig - WebServer - Exosite - Android sensor Fusion App

Dependencies:   NVIC_set_all_priorities mbed cc3000_hostdriver_mbedsocket TEMT6200 TSI Wi-Go_eCompass_Lib_V3 WiGo_BattCharger

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MAG3110.cpp Source File

MAG3110.cpp

00001 /* Copyright (c) 2010-2011 mbed.org, MIT License
00002 *
00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004 * and associated documentation files (the "Software"), to deal in the Software without
00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00007 * Software is furnished to do so, subject to the following conditions:
00008 *
00009 * The above copyright notice and this permission notice shall be included in all copies or
00010 * substantial portions of the Software.
00011 *
00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 */
00018 
00019 #include "MAG3110.h"
00020 
00021 /******************************************************************************
00022  * Constructors
00023  ******************************************************************************/
00024 
00025 MAG3110::MAG3110(PinName sda, PinName scl, int addr): m_i2c(sda, scl),  m_addr(addr)
00026 {
00027     m_i2c.frequency(375000);
00028     unsigned char data[2] = {MAG_CTRL_REG2, MAG_3110_AUTO_MRST_EN};
00029     writeRegs(data, 2);
00030     data[0] = MAG_CTRL_REG1;
00031     data[1] = MAG_3110_ACTIVE;
00032     writeRegs(data, 2);
00033 }
00034 
00035 uint8_t MAG3110::isDataAvailable(void)
00036 {
00037     uint8_t status;
00038     status = readReg(MAG_DR_STATUS);
00039     return (status & 0x07);
00040 }
00041 
00042 void MAG3110::readXYZ(uint8_t * data)
00043 {
00044     char t[1] = {MAG_OUT_X_MSB};
00045     m_i2c.write(m_addr, t, 1, true);
00046     m_i2c.read(m_addr, (char *)data, 6);
00047 }
00048 
00049 uint8_t MAG3110::readReg(uint8_t addr) {
00050     char t[1] = {addr};
00051     m_i2c.write(m_addr, t, 1, true);
00052     m_i2c.read(m_addr, t, 1);
00053     return t[0];
00054 }
00055 
00056 void MAG3110::writeRegs(uint8_t * data, int len) {
00057     m_i2c.write(m_addr, (char *)data, len);
00058 }
00059 
00060 
00061