From 8a08b9dce9cfc161494ddb07a1c979c7796780ff Mon Sep 17 00:00:00 2001 From: Sebastian Reichel Date: Fri, 15 Jun 2012 22:56:00 +0200 Subject: restructure code it is now loading its components from plugins --- sensors/accelerometer.vala | 1 + sensors/accelerometer/Makefile | 14 +++ sensors/accelerometer/accelerometer-adxl345.vala | 44 ------- sensors/accelerometer/adxl345.vala | 55 +++++++++ sensors/barometer.vala | 1 + sensors/barometer/Makefile | 14 +++ sensors/barometer/barometer-bmp085.vala | 128 --------------------- sensors/barometer/bmp085.vala | 140 +++++++++++++++++++++++ sensors/compass.vala | 1 + sensors/compass/Makefile | 14 +++ sensors/compass/compass-hmc5843.vala | 26 ----- sensors/compass/hmc5843.vala | 37 ++++++ sensors/gyroscope.vala | 13 ++- sensors/gyroscope/Makefile | 14 +++ sensors/gyroscope/gyroscope-itg3200.vala | 48 -------- sensors/gyroscope/itg3200.vala | 63 ++++++++++ 16 files changed, 366 insertions(+), 247 deletions(-) create mode 100644 sensors/accelerometer/Makefile delete mode 100644 sensors/accelerometer/accelerometer-adxl345.vala create mode 100644 sensors/accelerometer/adxl345.vala create mode 100644 sensors/barometer/Makefile delete mode 100644 sensors/barometer/barometer-bmp085.vala create mode 100644 sensors/barometer/bmp085.vala create mode 100644 sensors/compass/Makefile delete mode 100644 sensors/compass/compass-hmc5843.vala create mode 100644 sensors/compass/hmc5843.vala create mode 100644 sensors/gyroscope/Makefile delete mode 100644 sensors/gyroscope/gyroscope-itg3200.vala create mode 100644 sensors/gyroscope/itg3200.vala (limited to 'sensors') diff --git a/sensors/accelerometer.vala b/sensors/accelerometer.vala index 27b47f0..d9563f9 100644 --- a/sensors/accelerometer.vala +++ b/sensors/accelerometer.vala @@ -14,5 +14,6 @@ */ public interface Accelerometer : Device { + public abstract void init(KeyFile cfg) throws Error; public abstract void get_data(out uint16 x, out uint16 y, out uint16 z) throws Error; } diff --git a/sensors/accelerometer/Makefile b/sensors/accelerometer/Makefile new file mode 100644 index 0000000..1bd2a03 --- /dev/null +++ b/sensors/accelerometer/Makefile @@ -0,0 +1,14 @@ +include ../../config.mk + +all: ADXL345.so + +clean: + rm -f *.c *.so + +adxl345.c: adxl345.vala + $(VALAC) --pkg gmodule-2.0 --pkg posix --pkg linux -C $< ../accelerometer.vala ../../hw/i2c-device.vala ../../hw/device.vala + +ADXL345.so: adxl345.c + $(CC) -shared -fPIC `pkg-config --cflags --libs glib-2.0 gobject-2.0 gmodule-2.0` -o $@ $< + +.PHONY: all clean diff --git a/sensors/accelerometer/accelerometer-adxl345.vala b/sensors/accelerometer/accelerometer-adxl345.vala deleted file mode 100644 index ed9947f..0000000 --- a/sensors/accelerometer/accelerometer-adxl345.vala +++ /dev/null @@ -1,44 +0,0 @@ -/* 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 ADXL345 : I2CDevice, Accelerometer { - public ADXL345(uint8 dev, uint8 addr = 0x53) throws I2CError { - base(dev, addr); - } - - public uint8 get_address() throws I2CError { - var addr = get_byte(0x00); - assert(addr == 0xe5); - return addr; - } - - public void get_offset(out uint8 x, out uint8 y, out uint8 z) throws I2CError { - x = get_byte(0x1E); - y = get_byte(0x1F); - z = get_byte(0x20); - } - - public void set_offset(uint8 x, uint8 y, uint8 z) throws I2CError { - set_byte(0x1E, x); - set_byte(0x1F, y); - set_byte(0x20, z); - } - - public void get_data(out uint16 x, out uint16 y, out uint16 z) throws I2CError { - x = get_big_word(0x32); - y = get_big_word(0x34); - z = get_big_word(0x36); - } -} diff --git a/sensors/accelerometer/adxl345.vala b/sensors/accelerometer/adxl345.vala new file mode 100644 index 0000000..b8aa5e0 --- /dev/null +++ b/sensors/accelerometer/adxl345.vala @@ -0,0 +1,55 @@ +/* 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 ADXL345 : I2CDevice, Accelerometer { + public ADXL345(uint8 dev, uint8 addr = 0x53) throws I2CError { + setup(dev, addr); + } + + public void init(KeyFile cfg) throws KeyFileError, I2CError { + var adapter = cfg.get_uint64("ADXL345", "i2c-adapter"); + var address = cfg.get_uint64("ADXL345", "i2c-address"); + setup((uint8) adapter, (uint8) address); + } + + public uint8 get_address() throws I2CError { + var addr = get_byte(0x00); + assert(addr == 0xe5); + return addr; + } + + public void get_offset(out uint8 x, out uint8 y, out uint8 z) throws I2CError { + x = get_byte(0x1E); + y = get_byte(0x1F); + z = get_byte(0x20); + } + + public void set_offset(uint8 x, uint8 y, uint8 z) throws I2CError { + set_byte(0x1E, x); + set_byte(0x1F, y); + set_byte(0x20, z); + } + + public void get_data(out uint16 x, out uint16 y, out uint16 z) throws I2CError { + x = get_big_word(0x32); + y = get_big_word(0x34); + z = get_big_word(0x36); + } +} + +public Type register_plugin (Module module) { + // types are registered automatically + return typeof(ADXL345); +} diff --git a/sensors/barometer.vala b/sensors/barometer.vala index 5c1f4f9..d891ca0 100644 --- a/sensors/barometer.vala +++ b/sensors/barometer.vala @@ -14,6 +14,7 @@ */ public interface Barometer : Device { + public abstract void init(KeyFile cfg) throws Error; public abstract int32 get_pressure() throws Error; public double get_altitude(int32 base_pressure) throws Error { diff --git a/sensors/barometer/Makefile b/sensors/barometer/Makefile new file mode 100644 index 0000000..9a23671 --- /dev/null +++ b/sensors/barometer/Makefile @@ -0,0 +1,14 @@ +include ../../config.mk + +all: BMP085.so + +clean: + rm -f *.c *.so + +bmp085.c: bmp085.vala + $(VALAC) --pkg gmodule-2.0 --pkg posix --pkg linux -C $< ../barometer.vala ../../hw/i2c-device.vala ../../hw/device.vala + +BMP085.so: bmp085.c + $(CC) -shared -fPIC `pkg-config --cflags --libs glib-2.0 gobject-2.0 gmodule-2.0` -o $@ $< + +.PHONY: all clean diff --git a/sensors/barometer/barometer-bmp085.vala b/sensors/barometer/barometer-bmp085.vala deleted file mode 100644 index 92ea917..0000000 --- a/sensors/barometer/barometer-bmp085.vala +++ /dev/null @@ -1,128 +0,0 @@ -/* 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 BMP085 : I2CDevice, Barometer { - public uint8 oversampling_setting = 0x03; - private Calibration cal = Calibration(); - - private int64 next_temp_measurement; - private int raw_temp; - private int raw_pressure; - - private int64 temp_correction_coefficient; - - struct Calibration { - int16 AC1; - int16 AC2; - int16 AC3; - uint16 AC4; - uint16 AC5; - uint16 AC6; - int16 B1; - int16 B2; - int16 MB; - int16 MC; - int16 MD; - } - - public BMP085(uint8 dev, uint8 addr = 0x77) throws I2CError { - base(dev, addr); - read_calibration_values(); - } - - private void read_calibration_values() throws I2CError { - var data = get_block(0xaa, 22); - cal.AC1 = data[0] << 8 | data[1]; - cal.AC2 = data[2] << 8 | data[3]; - cal.AC3 = data[4] << 8 | data[5]; - cal.AC4 = data[6] << 8 | data[7]; - cal.AC5 = data[8] << 8 | data[9]; - cal.AC6 = data[10] << 8 | data[11]; - cal.B1 = data[12] << 8 | data[13]; - cal.B2 = data[14] << 8 | data[15]; - cal.MB = data[16] << 8 | data[17]; - cal.MC = data[18] << 8 | data[19]; - - cal.MD = data[20] << 8 | data[21]; - } - - private void msleep(uint msecs) { - Posix.usleep(msecs*1000); - } - - private void update_raw_temperature() throws I2CError { - set_byte(0xf4, 0x2e); - msleep(5); - var data = get_block(0xf6, 2); - raw_temp = data[0] << 8 | data[1]; - next_temp_measurement = time_t() + 1; - } - - private void update_raw_pressure() throws I2CError { - set_byte(0xf4, 0x34 + (oversampling_setting<<6)); - msleep(2+(3 << oversampling_setting<<1)); - var data = get_block(0xf6, 3); - raw_pressure = data[0] << 16 | data[1] << 8 | data[2]; - raw_pressure >>= 8-oversampling_setting; - } - - public int32 get_temperature() throws I2CError { - if(next_temp_measurement+1 < time_t()) - update_raw_temperature(); - - int64 x1 = ((raw_temp - cal.AC6) * cal.AC5) >> 15; - int64 x2 = (cal.MC << 11) / (x1 + cal.MD); - temp_correction_coefficient = x1 + x2 - 4000; - - return (int32) (x1+x2+8) >> 4; - } - - public int32 get_pressure() throws I2CError { - int64 x1, x2, x3, b3, p; - uint64 b4, b7; - - if(next_temp_measurement+1 < time_t()) - get_temperature(); - - update_raw_pressure(); - - x1 = (temp_correction_coefficient * temp_correction_coefficient) >> 12; - x1 *= cal.B2; - x1 >>= 11; - - x2 = cal.AC2 * temp_correction_coefficient; - x2 >>= 11; - - x3 = x1 + x2; - - b3 = (((((int64)cal.AC1) * 4 + x3) << oversampling_setting) + 2) >> 2; - - x1 = (cal.AC3 * temp_correction_coefficient) >> 13; - x2 = (cal.B1 * ((temp_correction_coefficient * temp_correction_coefficient) >> 12)) >> 16; - x3 = (x1 + x2 + 2) >> 2; - b4 = (cal.AC4 * (uint64)(x3 + 32768)) >> 15; - - b7 = ((uint64)raw_pressure - b3) * (50000 >> oversampling_setting); - p = (int64) ((b7 < 0x80000000) ? ((b7 << 1) / b4) : ((b7 / b4) * 2)); - - x1 = p >> 8; - x1 *= x1; - x1 = (x1 * 3038) >> 16; - x2 = (-7357 * p) >> 16; - p += (x1 + x2 + 3791) >> 4; - - return (int32) p; - } -} diff --git a/sensors/barometer/bmp085.vala b/sensors/barometer/bmp085.vala new file mode 100644 index 0000000..f0da790 --- /dev/null +++ b/sensors/barometer/bmp085.vala @@ -0,0 +1,140 @@ +/* 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 BMP085 : I2CDevice, Barometer { + public uint8 oversampling_setting = 0x03; + private Calibration cal = Calibration(); + + private int64 next_temp_measurement; + private int raw_temp; + private int raw_pressure; + + private int64 temp_correction_coefficient; + + struct Calibration { + int16 AC1; + int16 AC2; + int16 AC3; + uint16 AC4; + uint16 AC5; + uint16 AC6; + int16 B1; + int16 B2; + int16 MB; + int16 MC; + int16 MD; + } + + public BMP085(uint8 dev, uint8 addr = 0x77) throws I2CError { + setup(dev, addr); + read_calibration_values(); + } + + public void init(KeyFile cfg) throws KeyFileError, I2CError { + var adapter = cfg.get_uint64("BMP085", "i2c-adapter"); + var address = cfg.get_uint64("BMP085", "i2c-address"); + setup((uint8) adapter, (uint8) address); + read_calibration_values(); + } + + private void read_calibration_values() throws I2CError { + var data = get_block(0xaa, 22); + cal.AC1 = data[0] << 8 | data[1]; + cal.AC2 = data[2] << 8 | data[3]; + cal.AC3 = data[4] << 8 | data[5]; + cal.AC4 = data[6] << 8 | data[7]; + cal.AC5 = data[8] << 8 | data[9]; + cal.AC6 = data[10] << 8 | data[11]; + cal.B1 = data[12] << 8 | data[13]; + cal.B2 = data[14] << 8 | data[15]; + cal.MB = data[16] << 8 | data[17]; + cal.MC = data[18] << 8 | data[19]; + + cal.MD = data[20] << 8 | data[21]; + } + + private void msleep(uint msecs) { + Posix.usleep(msecs*1000); + } + + private void update_raw_temperature() throws I2CError { + set_byte(0xf4, 0x2e); + msleep(5); + var data = get_block(0xf6, 2); + raw_temp = data[0] << 8 | data[1]; + next_temp_measurement = time_t() + 1; + } + + private void update_raw_pressure() throws I2CError { + set_byte(0xf4, 0x34 + (oversampling_setting<<6)); + msleep(2+(3 << oversampling_setting<<1)); + var data = get_block(0xf6, 3); + raw_pressure = data[0] << 16 | data[1] << 8 | data[2]; + raw_pressure >>= 8-oversampling_setting; + } + + public int32 get_temperature() throws I2CError { + if(next_temp_measurement+1 < time_t()) + update_raw_temperature(); + + int64 x1 = ((raw_temp - cal.AC6) * cal.AC5) >> 15; + int64 x2 = (cal.MC << 11) / (x1 + cal.MD); + temp_correction_coefficient = x1 + x2 - 4000; + + return (int32) (x1+x2+8) >> 4; + } + + public int32 get_pressure() throws I2CError { + int64 x1, x2, x3, b3, p; + uint64 b4, b7; + + if(next_temp_measurement+1 < time_t()) + get_temperature(); + + update_raw_pressure(); + + x1 = (temp_correction_coefficient * temp_correction_coefficient) >> 12; + x1 *= cal.B2; + x1 >>= 11; + + x2 = cal.AC2 * temp_correction_coefficient; + x2 >>= 11; + + x3 = x1 + x2; + + b3 = (((((int64)cal.AC1) * 4 + x3) << oversampling_setting) + 2) >> 2; + + x1 = (cal.AC3 * temp_correction_coefficient) >> 13; + x2 = (cal.B1 * ((temp_correction_coefficient * temp_correction_coefficient) >> 12)) >> 16; + x3 = (x1 + x2 + 2) >> 2; + b4 = (cal.AC4 * (uint64)(x3 + 32768)) >> 15; + + b7 = ((uint64)raw_pressure - b3) * (50000 >> oversampling_setting); + p = (int64) ((b7 < 0x80000000) ? ((b7 << 1) / b4) : ((b7 / b4) * 2)); + + x1 = p >> 8; + x1 *= x1; + x1 = (x1 * 3038) >> 16; + x2 = (-7357 * p) >> 16; + p += (x1 + x2 + 3791) >> 4; + + return (int32) p; + } +} + +public Type register_plugin (Module module) { + // types are registered automatically + return typeof(BMP085); +} diff --git a/sensors/compass.vala b/sensors/compass.vala index b62cb3c..42d43ad 100644 --- a/sensors/compass.vala +++ b/sensors/compass.vala @@ -14,5 +14,6 @@ */ public interface Compass : Device { + public abstract void init(KeyFile cfg) throws Error; public abstract void get_data(out uint16 x, out uint16 y, out uint16 z) throws Error; } diff --git a/sensors/compass/Makefile b/sensors/compass/Makefile new file mode 100644 index 0000000..178ae55 --- /dev/null +++ b/sensors/compass/Makefile @@ -0,0 +1,14 @@ +include ../../config.mk + +all: HMC5843.so + +clean: + rm -f *.c *.so + +hmc5843.c: hmc5843.vala + $(VALAC) --pkg gmodule-2.0 --pkg posix --pkg linux -C $< ../compass.vala ../../hw/i2c-device.vala ../../hw/device.vala + +HMC5843.so: hmc5843.c + $(CC) -shared -fPIC `pkg-config --cflags --libs glib-2.0 gobject-2.0 gmodule-2.0` -o $@ $< + +.PHONY: all clean diff --git a/sensors/compass/compass-hmc5843.vala b/sensors/compass/compass-hmc5843.vala deleted file mode 100644 index b09b054..0000000 --- a/sensors/compass/compass-hmc5843.vala +++ /dev/null @@ -1,26 +0,0 @@ -/* 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 HMC5843 : I2CDevice, Compass { - public HMC5843(uint8 dev, uint8 addr = 0x1e) throws I2CError { - base(dev, addr); - } - - public void get_data(out uint16 x, out uint16 y, out uint16 z) throws I2CError { - x = get_big_word(0x03); - y = get_big_word(0x05); - z = get_big_word(0x07); - } -} diff --git a/sensors/compass/hmc5843.vala b/sensors/compass/hmc5843.vala new file mode 100644 index 0000000..393f985 --- /dev/null +++ b/sensors/compass/hmc5843.vala @@ -0,0 +1,37 @@ +/* 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 HMC5843 : I2CDevice, Compass { + public HMC5843(uint8 dev, uint8 addr = 0x1e) throws I2CError { + setup(dev, addr); + } + + public void init(KeyFile cfg) throws KeyFileError, I2CError { + var adapter = cfg.get_uint64("HMC5843", "i2c-adapter"); + var address = cfg.get_uint64("HMC5843", "i2c-address"); + setup((uint8) adapter, (uint8) address); + } + + public void get_data(out uint16 x, out uint16 y, out uint16 z) throws I2CError { + x = get_big_word(0x03); + y = get_big_word(0x05); + z = get_big_word(0x07); + } +} + +public Type register_plugin (Module module) { + // types are registered automatically + return typeof(HMC5843); +} diff --git a/sensors/gyroscope.vala b/sensors/gyroscope.vala index 165df73..8cdc0cd 100644 --- a/sensors/gyroscope.vala +++ b/sensors/gyroscope.vala @@ -14,5 +14,16 @@ */ public interface Gyroscope : Device { - public abstract void get_data(out int16 x, out int16 y, out int16 z) throws Error; +#if 0 + double[] rate = {0.0, 0.0, 0.0}; + int[] zero = {0, 0, 0}; + long[] sample = {0, 0, 0}; + double smoothFactor = 1.0; + double scaleFactor = 0.0; + double heading = 0.0; + ulong gyroLastMesuredTime = 0; +#endif + public abstract void init(KeyFile cfg) throws Error; + public abstract void get_data(out int16 x, out int16 y, out int16 z) throws I2CError; + public abstract int[] rate { get; set; } } diff --git a/sensors/gyroscope/Makefile b/sensors/gyroscope/Makefile new file mode 100644 index 0000000..f7add97 --- /dev/null +++ b/sensors/gyroscope/Makefile @@ -0,0 +1,14 @@ +include ../../config.mk + +all: ITG3200.so + +clean: + rm -f *.c *.so + +itg3200.c: itg3200.vala + $(VALAC) -g --pkg gmodule-2.0 --pkg posix --pkg linux -C $< ../gyroscope.vala ../../hw/i2c-device.vala ../../hw/device.vala + +ITG3200.so: itg3200.c + $(CC) -g -shared -fPIC `pkg-config --cflags --libs glib-2.0 gobject-2.0 gmodule-2.0` -o $@ $< + +.PHONY: all clean diff --git a/sensors/gyroscope/gyroscope-itg3200.vala b/sensors/gyroscope/gyroscope-itg3200.vala deleted file mode 100644 index c57aa55..0000000 --- a/sensors/gyroscope/gyroscope-itg3200.vala +++ /dev/null @@ -1,48 +0,0 @@ -/* 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 ITG3200 : I2CDevice, Gyroscope { - public ITG3200(uint8 dev, uint8 addr = 0x68) throws I2CError { - base(dev, addr); - } - - public uint8 get_address() throws I2CError { - return (uint8) get_byte(0x00); - } - - /* TODO: add method for register 0x16 (frequency + range) */ - /* TODO: add method for register 0x17 (interrupt config) */ - /* TODO: add method for register 0x3E (power management) */ - - public uint8 get_sample_rate_divider() throws I2CError { - /* sample rate = internal frequency / (divider + 1) */ - return (uint8) get_byte(0x15); - } - - public void set_sample_rate_divider(uint8 val) throws I2CError { - set_byte(0x15, val); - } - - public int16 get_temperature() throws I2CError { - int16 raw = (int16) get_big_word(0x1b); - return 350 + ((raw + 13200) / 28); - } - - public void get_data(out int16 x, out int16 y, out int16 z) throws I2CError { - x = (int16) get_big_word(0x1d); - y = (int16) get_big_word(0x1f); - z = (int16) get_big_word(0x21); - } -} diff --git a/sensors/gyroscope/itg3200.vala b/sensors/gyroscope/itg3200.vala new file mode 100644 index 0000000..2b7f03e --- /dev/null +++ b/sensors/gyroscope/itg3200.vala @@ -0,0 +1,63 @@ +/* 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 ITG3200 : I2CDevice, Gyroscope { + public int[] rate { get; set; } + + public ITG3200(uint8 dev, uint8 addr = 0x68) throws I2CError { + setup(dev, addr); + } + + public void init(KeyFile cfg) throws KeyFileError, I2CError { + rate = new int[3]; + + var adapter = cfg.get_uint64("ITG3200", "i2c-adapter"); + var address = cfg.get_uint64("ITG3200", "i2c-address"); + setup((uint8) adapter, (uint8) address); + } + + public uint8 get_address() throws I2CError { + return (uint8) get_byte(0x00); + } + + /* TODO: add method for register 0x16 (frequency + range) */ + /* TODO: add method for register 0x17 (interrupt config) */ + /* TODO: add method for register 0x3E (power management) */ + + public uint8 get_sample_rate_divider() throws I2CError { + /* sample rate = internal frequency / (divider + 1) */ + return (uint8) get_byte(0x15); + } + + public void set_sample_rate_divider(uint8 val) throws I2CError { + set_byte(0x15, val); + } + + public int16 get_temperature() throws I2CError { + int16 raw = (int16) get_big_word(0x1b); + return 350 + ((raw + 13200) / 28); + } + + public void get_data(out int16 x, out int16 y, out int16 z) throws I2CError { + x = (int16) get_big_word(0x1d); + y = (int16) get_big_word(0x1f); + z = (int16) get_big_word(0x21); + } +} + +public Type register_plugin (Module module) { + // types are registered automatically + return typeof(ITG3200); +} -- cgit v1.2.3