Class: Tinkerforge::BrickletTemperature

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

Overview

Measures ambient temperature with 0.5°C accuracy

Constant Summary collapse

DEVICE_IDENTIFIER =

:nodoc:

216
DEVICE_DISPLAY_NAME =

:nodoc:

'Temperature Bricklet'
CALLBACK_TEMPERATURE =

This callback is triggered periodically with the period that is set by BrickletTemperature#set_temperature_callback_period. The parameter is the temperature of the sensor.

CALLBACK_TEMPERATURE is only triggered if the temperature has changed since the last triggering.

8
CALLBACK_TEMPERATURE_REACHED =

This callback is triggered when the threshold as set by BrickletTemperature#set_temperature_callback_threshold is reached. The parameter is the temperature of the sensor.

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

9
FUNCTION_GET_TEMPERATURE =

:nodoc:

1
FUNCTION_SET_TEMPERATURE_CALLBACK_PERIOD =

:nodoc:

2
FUNCTION_GET_TEMPERATURE_CALLBACK_PERIOD =

:nodoc:

3
FUNCTION_SET_TEMPERATURE_CALLBACK_THRESHOLD =

:nodoc:

4
FUNCTION_GET_TEMPERATURE_CALLBACK_THRESHOLD =

:nodoc:

5
FUNCTION_SET_DEBOUNCE_PERIOD =

:nodoc:

6
FUNCTION_GET_DEBOUNCE_PERIOD =

:nodoc:

7
FUNCTION_SET_I2C_MODE =

:nodoc:

10
FUNCTION_GET_I2C_MODE =

:nodoc:

11
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:

'>'
I2C_MODE_FAST =

:nodoc:

0
I2C_MODE_SLOW =

:nodoc:

1

Constants inherited from Device

Device::RESPONSE_EXPECTED_ALWAYS_FALSE, 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, #registered_callbacks, #uid

Instance Method Summary collapse

Methods inherited from Device

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

Constructor Details

#initialize(uid, ipcon) ⇒ BrickletTemperature

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/tinkerforge/bricklet_temperature.rb', line 55

def initialize(uid, ipcon)
  super uid, ipcon

  @api_version = [2, 0, 0]

  @response_expected[FUNCTION_GET_TEMPERATURE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_TEMPERATURE_CALLBACK_PERIOD] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_TEMPERATURE_CALLBACK_PERIOD] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_TEMPERATURE_CALLBACK_THRESHOLD] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_TEMPERATURE_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[CALLBACK_TEMPERATURE] = RESPONSE_EXPECTED_ALWAYS_FALSE
  @response_expected[CALLBACK_TEMPERATURE_REACHED] = RESPONSE_EXPECTED_ALWAYS_FALSE
  @response_expected[FUNCTION_SET_I2C_MODE] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_I2C_MODE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE

  @callback_formats[CALLBACK_TEMPERATURE] = 's'
  @callback_formats[CALLBACK_TEMPERATURE_REACHED] = 's'
end

Instance Method Details

#get_debounce_periodObject

Returns the debounce period as set by BrickletTemperature#set_debounce_period.



142
143
144
# File 'lib/tinkerforge/bricklet_temperature.rb', line 142

def get_debounce_period
  send_request(FUNCTION_GET_DEBOUNCE_PERIOD, [], '', 4, 'L')
end

#get_i2c_modeObject

Returns the I2C mode as set by BrickletTemperature#set_i2c_mode.

.. versionadded

2.0.1$nbsp;(Plugin)



166
167
168
# File 'lib/tinkerforge/bricklet_temperature.rb', line 166

def get_i2c_mode
  send_request(FUNCTION_GET_I2C_MODE, [], '', 1, 'C')
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’ or ‘d’.

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



178
179
180
# File 'lib/tinkerforge/bricklet_temperature.rb', line 178

def get_identity
  send_request(FUNCTION_GET_IDENTITY, [], '', 25, 'Z8 Z8 k C3 C3 S')
end

#get_temperatureObject

Returns the temperature of the sensor. The value has a range of -2500 to 8500 and is given in °C/100, e.g. a value of 4223 means that a temperature of 42.23 °C is measured.

If you want to get the temperature periodically, it is recommended to use the callback CALLBACK_TEMPERATURE and set the period with BrickletTemperature#set_temperature_callback_period.



84
85
86
# File 'lib/tinkerforge/bricklet_temperature.rb', line 84

def get_temperature
  send_request(FUNCTION_GET_TEMPERATURE, [], '', 2, 's')
end

#get_temperature_callback_periodObject

Returns the period as set by BrickletTemperature#set_temperature_callback_period.



100
101
102
# File 'lib/tinkerforge/bricklet_temperature.rb', line 100

def get_temperature_callback_period
  send_request(FUNCTION_GET_TEMPERATURE_CALLBACK_PERIOD, [], '', 4, 'L')
end

#get_temperature_callback_thresholdObject

Returns the threshold as set by BrickletTemperature#set_temperature_callback_threshold.



122
123
124
# File 'lib/tinkerforge/bricklet_temperature.rb', line 122

def get_temperature_callback_threshold
  send_request(FUNCTION_GET_TEMPERATURE_CALLBACK_THRESHOLD, [], '', 5, 'k s s')
end

#register_callback(id, &block) ⇒ Object

Registers a callback with ID id to the block block.



183
184
185
186
# File 'lib/tinkerforge/bricklet_temperature.rb', line 183

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

#set_debounce_period(debounce) ⇒ Object

Sets the period in ms with which the threshold callback

  • CALLBACK_TEMPERATURE_REACHED

is triggered, if the threshold

  • BrickletTemperature#set_temperature_callback_threshold

keeps being reached.

The default value is 100.



137
138
139
# File 'lib/tinkerforge/bricklet_temperature.rb', line 137

def set_debounce_period(debounce)
  send_request(FUNCTION_SET_DEBOUNCE_PERIOD, [debounce], 'L', 0, '')
end

#set_i2c_mode(mode) ⇒ Object

Sets the I2C mode. Possible modes are:

  • 0: Fast (400kHz, default)

  • 1: Slow (100kHz)

If you have problems with obvious outliers in the Temperature Bricklet measurements, they may be caused by EMI issues. In this case it may be helpful to lower the I2C speed.

It is however not recommended to lower the I2C speed in applications where a high throughput needs to be achieved.

.. versionadded

2.0.1$nbsp;(Plugin)



159
160
161
# File 'lib/tinkerforge/bricklet_temperature.rb', line 159

def set_i2c_mode(mode)
  send_request(FUNCTION_SET_I2C_MODE, [mode], 'C', 0, '')
end

#set_temperature_callback_period(period) ⇒ Object

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

CALLBACK_TEMPERATURE is only triggered if the temperature has changed since the last triggering.

The default value is 0.



95
96
97
# File 'lib/tinkerforge/bricklet_temperature.rb', line 95

def set_temperature_callback_period(period)
  send_request(FUNCTION_SET_TEMPERATURE_CALLBACK_PERIOD, [period], 'L', 0, '')
end

#set_temperature_callback_threshold(option, min, max) ⇒ Object

Sets the thresholds for the CALLBACK_TEMPERATURE_REACHED callback.

The following options are possible:

"Option", "Description"

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

The default value is (‘x’, 0, 0).



117
118
119
# File 'lib/tinkerforge/bricklet_temperature.rb', line 117

def set_temperature_callback_threshold(option, min, max)
  send_request(FUNCTION_SET_TEMPERATURE_CALLBACK_THRESHOLD, [option, min, max], 'k s s', 0, '')
end