GithubHelp home page GithubHelp logo

hmc5883l.c's Introduction

HMC5883L.c

Simple wrapper for HMC5883L compass on I2C.

Tested on a Raspberry Pi 2.

Build

Build, and optionally install, with scons.

scons
sudo scons install

Usage

All functions take a file handle as first parameter (obtained with open and released with close), and return 0 when successful or -1 on failure.

Example:

static int file;

static int closeAndExit(int code)
{
	close(file);
	return code;
}

int main(void)
{
	file = open("/dev/i2c-1", O_RDWR);
	if (file < 0)
		return -1;

	if (HMC5883L_Init(file, HMC5883L_ID, true))
		return closeAndExit(-1);

    struct HMC5883L conf = {
        .gain = HMC5883L_GAIN_1090,
        .measurementMode = HMC5883L_MEASUREMENTMODE_NORMAL,
        .outputRate = HMC5883L_OUTPUTRATE_30,
        .samples = HMC5883L_SAMPLES_2,
    };
    if (HMC5883L_Configure(file, &conf))
		return closeAndExit(-1);

    if (HMC5883L_SetContinuousMeasurement(file))
		return closeAndExit(-1);

	short x, y, z;
	if (HMC5883L_ReadData(file, &x, &y, &z))
		return closeAndExit(-1);

	return closeAndExit(0);
}

HMC5883L_Init(int file, unsigned char id, bool check)

id is the device I2C address. It is usually HMC5883L_ID (0x1E).

If check is true, the function reads the device identification registers and checks against the expected values. This inherently checks that the device and the I2C bus are working.

License

Copyright 2016 Bloutiouf

MIT License

hmc5883l.c's People

Contributors

koltesdigital avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

stefaanblondeel

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.