Class: BH1750
- Inherits:
-
Object
- Object
- BH1750
- Defined in:
- lib/bh1750.rb
Overview
BH1750 - ambient light sensor
Constant Summary collapse
- DEVICE =
I2C sensor address
0x23- POWER_DOWN =
off state
0x00- POWER_ON =
on state
0x01- RESET =
reset state: POWER_DOWN = 0x00
0x07- CONTINUOUS_LOW_RES_MODE =
Measure with resolution 4.0 lx and timing ~16 ms
0x13- CONTINUOUS_HIGH_RES_MODE_1 =
Measure with resolution 1.0 lx and timing ~120 ms
0x10- CONTINUOUS_HIGH_RES_MODE_2 =
Measure with resolution 0.5 lx and timing ~120 ms
0x11- ONE_TIME_LOW_RES_MODE =
Measure with resolution 4.0 lx, POWER DOWN after measuring
0x23- ONE_TIME_HIGH_RES_MODE_1 =
Measure with resolution 1.0 lx, POWER DOWN after measuring
0x20- ONE_TIME_HIGH_RES_MODE_2 =
Measure with resolution 0.5 lx, POWER DOWN after measuring
0x21
Instance Method Summary collapse
-
#initialize(i2c_bus = '/dev/i2c-1') ⇒ BH1750
constructor
A new instance of BH1750.
-
#lux ⇒ Object
Return value in Lux.
-
#read_sensor ⇒ Object
Read raw data from sensor and convert it to numeric.
Constructor Details
#initialize(i2c_bus = '/dev/i2c-1') ⇒ BH1750
Returns a new instance of BH1750.
17 18 19 20 21 22 23 24 |
# File 'lib/bh1750.rb', line 17 def initialize(i2c_bus='/dev/i2c-1') @sensor_name = 'BH1750' @i2c_bus = i2c_bus @device = I2CDevice.new(address: DEVICE, driver: I2CDevice::Driver::I2CDev.new(@i2c_bus)) @resolution = ONE_TIME_HIGH_RES_MODE_1 @length = 2 @value = read_sensor end |
Instance Method Details
#lux ⇒ Object
Return value in Lux
33 34 35 36 |
# File 'lib/bh1750.rb', line 33 def lux read_sensor @value end |
#read_sensor ⇒ Object
Read raw data from sensor and convert it to numeric
27 28 29 30 |
# File 'lib/bh1750.rb', line 27 def read_sensor data = @device.i2cget(@resolution, @length) @value = to_f(data) end |