Class: Tinkerforge::BrickletAmbientLightV2

Inherits:
Device
  • Object
show all
Defined in:
lib/tinkerforge/bricklet_ambient_light_v2.rb

Overview

Measures ambient light up to 64000lux

Constant Summary collapse

DEVICE_IDENTIFIER =

:nodoc:

259
DEVICE_DISPLAY_NAME =

:nodoc:

'Ambient Light Bricklet 2.0'
CALLBACK_ILLUMINANCE =

This callback is triggered periodically with the period that is set by BrickletAmbientLightV2#set_illuminance_callback_period. The parameter is the illuminance of the ambient light sensor.

The CALLBACK_ILLUMINANCE callback is only triggered if the illuminance has changed since the last triggering.

10
CALLBACK_ILLUMINANCE_REACHED =

This callback is triggered when the threshold as set by BrickletAmbientLightV2#set_illuminance_callback_threshold is reached. The parameter is the illuminance of the ambient light sensor.

If the threshold keeps being reached, the callback is triggered periodically with the period as set by BrickletAmbientLightV2#set_debounce_period.

11
FUNCTION_GET_ILLUMINANCE =

:nodoc:

1
FUNCTION_SET_ILLUMINANCE_CALLBACK_PERIOD =

:nodoc:

2
FUNCTION_GET_ILLUMINANCE_CALLBACK_PERIOD =

:nodoc:

3
FUNCTION_SET_ILLUMINANCE_CALLBACK_THRESHOLD =

:nodoc:

4
FUNCTION_GET_ILLUMINANCE_CALLBACK_THRESHOLD =

:nodoc:

5
FUNCTION_SET_DEBOUNCE_PERIOD =

:nodoc:

6
FUNCTION_GET_DEBOUNCE_PERIOD =

:nodoc:

7
FUNCTION_SET_CONFIGURATION =

:nodoc:

8
FUNCTION_GET_CONFIGURATION =

:nodoc:

9
FUNCTION_GET_IDENTITY =

:nodoc:

255
THRESHOLD_OPTION_OFF =

:nodoc:

'x'
THRESHOLD_OPTION_OUTSIDE =

:nodoc:

'o'
THRESHOLD_OPTION_INSIDE =

:nodoc:

'i'
THRESHOLD_OPTION_SMALLER =

:nodoc:

'<'
THRESHOLD_OPTION_GREATER =

:nodoc:

'>'
ILLUMINANCE_RANGE_UNLIMITED =

:nodoc:

6
ILLUMINANCE_RANGE_64000LUX =

:nodoc:

0
ILLUMINANCE_RANGE_32000LUX =

:nodoc:

1
ILLUMINANCE_RANGE_16000LUX =

:nodoc:

2
ILLUMINANCE_RANGE_8000LUX =

:nodoc:

3
ILLUMINANCE_RANGE_1300LUX =

:nodoc:

4
ILLUMINANCE_RANGE_600LUX =

:nodoc:

5
INTEGRATION_TIME_50MS =

:nodoc:

0
INTEGRATION_TIME_100MS =

:nodoc:

1
INTEGRATION_TIME_150MS =

:nodoc:

2
INTEGRATION_TIME_200MS =

:nodoc:

3
INTEGRATION_TIME_250MS =

:nodoc:

4
INTEGRATION_TIME_300MS =

:nodoc:

5
INTEGRATION_TIME_350MS =

:nodoc:

6
INTEGRATION_TIME_400MS =

:nodoc:

7

Constants inherited from Device

Device::DEVICE_IDENTIFIER_CHECK_MATCH, Device::DEVICE_IDENTIFIER_CHECK_MISMATCH, Device::DEVICE_IDENTIFIER_CHECK_PENDING, Device::RESPONSE_EXPECTED_ALWAYS_TRUE, Device::RESPONSE_EXPECTED_FALSE, Device::RESPONSE_EXPECTED_INVALID_FUNCTION_ID, Device::RESPONSE_EXPECTED_TRUE

Instance Attribute Summary

Attributes inherited from Device

#callback_formats, #expected_response_function_id, #expected_response_sequence_number, #high_level_callbacks, #registered_callbacks, #replaced, #uid

Instance Method Summary collapse

Methods inherited from Device

#check_validity, #dequeue_response, #enqueue_response, #get_api_version, #get_response_expected, #send_request, #set_response_expected, #set_response_expected_all

Constructor Details

#initialize(uid, ipcon) ⇒ BrickletAmbientLightV2

Creates an object with the unique device ID uid and adds it to the IP Connection ipcon.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/tinkerforge/bricklet_ambient_light_v2.rb', line 70

def initialize(uid, ipcon)
  super uid, ipcon, DEVICE_IDENTIFIER, DEVICE_DISPLAY_NAME

  @api_version = [2, 0, 1]

  @response_expected[FUNCTION_GET_ILLUMINANCE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_ILLUMINANCE_CALLBACK_PERIOD] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_ILLUMINANCE_CALLBACK_PERIOD] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_ILLUMINANCE_CALLBACK_THRESHOLD] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_ILLUMINANCE_CALLBACK_THRESHOLD] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_DEBOUNCE_PERIOD] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_DEBOUNCE_PERIOD] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_CONFIGURATION] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_CONFIGURATION] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE

  @callback_formats[CALLBACK_ILLUMINANCE] = [12, 'L']
  @callback_formats[CALLBACK_ILLUMINANCE_REACHED] = [12, 'L']

  @ipcon.add_device self
end

Instance Method Details

#get_configurationObject

Returns the configuration as set by BrickletAmbientLightV2#set_configuration.



205
206
207
208
209
# File 'lib/tinkerforge/bricklet_ambient_light_v2.rb', line 205

def get_configuration
  check_validity

  send_request FUNCTION_GET_CONFIGURATION, [], '', 10, 'C C'
end

#get_debounce_periodObject

Returns the debounce period as set by BrickletAmbientLightV2#set_debounce_period.



169
170
171
172
173
# File 'lib/tinkerforge/bricklet_ambient_light_v2.rb', line 169

def get_debounce_period
  check_validity

  send_request FUNCTION_GET_DEBOUNCE_PERIOD, [], '', 12, 'L'
end

#get_identityObject

Returns the UID, the UID where the Bricklet is connected to, the position, the hardware and firmware version as well as the device identifier.

The position can be ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’ or ‘h’ (Bricklet Port). A Bricklet connected to an :ref:‘Isolator Bricklet <isolator_bricklet>` is always at position ’z’.

The device identifier numbers can be found :ref:‘here <device_identifier>`. |device_identifier_constant|



221
222
223
# File 'lib/tinkerforge/bricklet_ambient_light_v2.rb', line 221

def get_identity
  send_request FUNCTION_GET_IDENTITY, [], '', 33, 'Z8 Z8 k C3 C3 S'
end

#get_illuminanceObject

Returns the illuminance of the ambient light sensor. The measurement range goes up to about 100000lux, but above 64000lux the precision starts to drop.

.. versionchanged

2.0.2$nbsp;(Plugin)

An illuminance of 0lux indicates an error condition where the sensor cannot
perform a reasonable measurement. This can happen with very dim or very bright
light conditions. In bright light conditions this might indicate that the sensor
is saturated and the configuration should be modified (BrickletAmbientLightV2#set_configuration)
to better match the light conditions.

If you want to get the illuminance periodically, it is recommended to use the CALLBACK_ILLUMINANCE callback and set the period with BrickletAmbientLightV2#set_illuminance_callback_period.



105
106
107
108
109
# File 'lib/tinkerforge/bricklet_ambient_light_v2.rb', line 105

def get_illuminance
  check_validity

  send_request FUNCTION_GET_ILLUMINANCE, [], '', 12, 'L'
end

#get_illuminance_callback_periodObject

Returns the period as set by BrickletAmbientLightV2#set_illuminance_callback_period.



123
124
125
126
127
# File 'lib/tinkerforge/bricklet_ambient_light_v2.rb', line 123

def get_illuminance_callback_period
  check_validity

  send_request FUNCTION_GET_ILLUMINANCE_CALLBACK_PERIOD, [], '', 12, 'L'
end

#get_illuminance_callback_thresholdObject

Returns the threshold as set by BrickletAmbientLightV2#set_illuminance_callback_threshold.



147
148
149
150
151
# File 'lib/tinkerforge/bricklet_ambient_light_v2.rb', line 147

def get_illuminance_callback_threshold
  check_validity

  send_request FUNCTION_GET_ILLUMINANCE_CALLBACK_THRESHOLD, [], '', 17, 'k L L'
end

#register_callback(id, &block) ⇒ Object

Registers a callback with ID id to the block block.



226
227
228
229
# File 'lib/tinkerforge/bricklet_ambient_light_v2.rb', line 226

def register_callback(id, &block)
  callback = block
  @registered_callbacks[id] = callback
end

#set_configuration(illuminance_range, integration_time) ⇒ Object

Sets the configuration. It is possible to configure an illuminance range between 0-600lux and 0-64000lux and an integration time between 50ms and 400ms.

.. versionadded

2.0.2$nbsp;(Plugin)

The unlimited illuminance range allows to measure up to about 100000lux, but
above 64000lux the precision starts to drop.

A smaller illuminance range increases the resolution of the data. A longer integration time will result in less noise on the data.

.. versionchanged

2.0.2$nbsp;(Plugin)

If the actual measure illuminance is out-of-range then the current illuminance
range maximum +0.01lux is reported by BrickletAmbientLightV2#get_illuminance and the
CALLBACK_ILLUMINANCE callback. For example, 800001 for the 0-8000lux range.
.. versionchanged

2.0.2$nbsp;(Plugin)

With a long integration time the sensor might be saturated before the measured
value reaches the maximum of the selected illuminance range. In this case 0lux
is reported by BrickletAmbientLightV2#get_illuminance and the CALLBACK_ILLUMINANCE callback.

If the measurement is out-of-range or the sensor is saturated then you should configure the next higher illuminance range. If the highest range is already in use, then start to reduce the integration time.



198
199
200
201
202
# File 'lib/tinkerforge/bricklet_ambient_light_v2.rb', line 198

def set_configuration(illuminance_range, integration_time)
  check_validity

  send_request FUNCTION_SET_CONFIGURATION, [illuminance_range, integration_time], 'C C', 8, ''
end

#set_debounce_period(debounce) ⇒ Object

Sets the period with which the threshold callbacks

  • CALLBACK_ILLUMINANCE_REACHED,

are triggered, if the thresholds

  • BrickletAmbientLightV2#set_illuminance_callback_threshold,

keep being reached.



162
163
164
165
166
# File 'lib/tinkerforge/bricklet_ambient_light_v2.rb', line 162

def set_debounce_period(debounce)
  check_validity

  send_request FUNCTION_SET_DEBOUNCE_PERIOD, [debounce], 'L', 8, ''
end

#set_illuminance_callback_period(period) ⇒ Object

Sets the period with which the CALLBACK_ILLUMINANCE callback is triggered periodically. A value of 0 turns the callback off.

The CALLBACK_ILLUMINANCE callback is only triggered if the illuminance has changed since the last triggering.



116
117
118
119
120
# File 'lib/tinkerforge/bricklet_ambient_light_v2.rb', line 116

def set_illuminance_callback_period(period)
  check_validity

  send_request FUNCTION_SET_ILLUMINANCE_CALLBACK_PERIOD, [period], 'L', 8, ''
end

#set_illuminance_callback_threshold(option, min, max) ⇒ Object

Sets the thresholds for the CALLBACK_ILLUMINANCE_REACHED callback.

The following options are possible:

"Option", "Description"

"'x'",    "Callback is turned off"
"'o'",    "Callback is triggered when the illuminance is *outside* the min and max values"
"'i'",    "Callback is triggered when the illuminance is *inside* the min and max values"
"'<'",    "Callback is triggered when the illuminance is smaller than the min value (max is ignored)"
"'>'",    "Callback is triggered when the illuminance is greater than the min value (max is ignored)"


140
141
142
143
144
# File 'lib/tinkerforge/bricklet_ambient_light_v2.rb', line 140

def set_illuminance_callback_threshold(option, min, max)
  check_validity

  send_request FUNCTION_SET_ILLUMINANCE_CALLBACK_THRESHOLD, [option, min, max], 'k L L', 8, ''
end