Class: I2CDevice::MPL115A2

Inherits:
I2CDevice show all
Defined in:
lib/i2c/device/mpl115a2.rb

Constant Summary

Constants inherited from I2CDevice

VERSION

Instance Attribute Summary

Attributes inherited from I2CDevice

#address

Instance Method Summary collapse

Methods inherited from I2CDevice

#i2cget, #i2cset

Constructor Details

#initialize(args = {}) ⇒ MPL115A2

Returns a new instance of MPL115A2.



5
6
7
8
9
10
11
12
13
14
# File 'lib/i2c/device/mpl115a2.rb', line 5

def initialize(args={})
	args[:address] = 0x60
	super
	coefficient = i2cget(0x04, 8).unpack("n*")

	@a0  = fixed_point(coefficient[0], 12)
	@b1  = fixed_point(coefficient[1], 2)
	@b2  = fixed_point(coefficient[2], 1)
	@c12 = fixed_point(coefficient[3], 0) / (1<<9)
end

Instance Method Details

#calculate_hPaObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/i2c/device/mpl115a2.rb', line 26

def calculate_hPa
	i2cset(0x12, 0x01) # CONVERT

	sleep 0.003

	data = i2cget(0x00, 4).unpack("n*")

	p_adc = (data[0]) >> 6
	t_adc = (data[1]) >> 6

	p_comp = @a0 + (@b1 + @c12 * t_adc) * p_adc + @b2 * t_adc
	hPa = p_comp * ( (1150 - 500) / 1023.0) + 500;
end

#fixed_point(fixed, int_bits) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/i2c/device/mpl115a2.rb', line 16

def fixed_point(fixed, int_bits)
	msb = 15
	deno = (1<<(msb-int_bits)).to_f
	if (fixed & (1<<15)).zero?
		fixed / deno
	else
		-( ( (~fixed & 0xffff) + 1) / deno )
	end
end