1765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang/*
2765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang * Contributions: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
4765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang * Copyright (c) 2014 Intel Corporation.
5765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang *
6765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang * Permission is hereby granted, free of charge, to any person obtaining
7765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang * a copy of this software and associated documentation files (the
8765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang * "Software"), to deal in the Software without restriction, including
9765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang * without limitation the rights to use, copy, modify, merge, publish,
10765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang * distribute, sublicense, and/or sell copies of the Software, and to
11765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang * permit persons to whom the Software is furnished to do so, subject to
12765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang * the following conditions:
13765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang *
14765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang * The above copyright notice and this permission notice shall be
15765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang * included in all copies or substantial portions of the Software.
16765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang *
17765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang */
25765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang
26765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang#include <unistd.h>
27765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang#include "hmc5883l.h"
28765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang
29765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhangint
30765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhangmain(int argc, char **argv)
31765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang{
32765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang//! [Interesting]
33765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang    // Instantiate on I2C
34765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang    upm::Hmc5883l* compass = new upm::Hmc5883l(0);
35765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang    int16_t *pos;
36765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang
37765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang    compass->set_declination(0.2749); // Set your declination from true north in radians
38765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang
39765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang    // Print out the coordinates, heading, and direction every second
40765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang    while(true){
41765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang        compass->update(); // Update the coordinates
42765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang        pos = compass->coordinates();
43765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang        fprintf(stdout, "coor: %5d %5d %5d ", pos[0], pos[1], pos[2]);
44765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang        fprintf(stdout, "heading: %5.2f direction: %3.2f\n", compass->heading(), compass->direction());
45765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang        sleep(1);
46765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang    }
47765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang//! [Interesting]
48765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang
49765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang    return 0;
50765adb95dc941c32690d6c43bc08b9d07d197fcbJianxun Zhang}
51