/* Copyright 2012, Sebastian Reichel * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ public class HMC5883L : I2CDevice, Compass { public static const int COUNTS_PER_MGAUSS = 1090; public HMC5883L(uint8 dev, uint8 addr = 0x1e) throws I2CError { setup(dev, addr); devsetup(); } private void devsetup() throws I2CError { if(get_byte(0x0a) != 'H' || get_byte(0x0b) != '4' || get_byte(0x0c) != '3') log("HMC5883L", LogLevelFlags.LEVEL_ERROR, @"invalid device id"); else log("HMC5883L", LogLevelFlags.LEVEL_DEBUG, @"correct device id"); set_byte(0x00, 0x10); /* default mode */ set_byte(0x01, 0x20); /* set gain: +/- 1.3Ga (default) */ set_byte(0x02, 0x00); /* continous conversion */ } public void init(KeyFile cfg) throws KeyFileError, I2CError { var adapter = cfg.get_uint64("HMC5883L", "i2c-adapter"); var address = cfg.get_uint64("HMC5883L", "i2c-address"); setup((uint8) adapter, (uint8) address); devsetup(); } public void get_data(out int16 x, out int16 y, out int16 z) throws I2CError { var data = get_block(0x03, 6); x = data[0] << 8 | data[1]; y = data[2] << 8 | data[3]; z = data[4] << 8 | data[5]; } } public Type register_plugin(Module module) { // types are registered automatically return typeof(HMC5883L); }