Class: Tinkerforge::BrickletPiezoSpeaker

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

Overview

Creates beep with configurable frequency

Constant Summary collapse

DEVICE_IDENTIFIER =

:nodoc:

242
DEVICE_DISPLAY_NAME =

:nodoc:

'Piezo Speaker Bricklet'
CALLBACK_BEEP_FINISHED =

This callback is triggered if a beep set by BrickletPiezoSpeaker#beep is finished

4
CALLBACK_MORSE_CODE_FINISHED =

This callback is triggered if the playback of the morse code set by BrickletPiezoSpeaker#morse_code is finished.

5
FUNCTION_BEEP =

:nodoc:

1
FUNCTION_MORSE_CODE =

:nodoc:

2
FUNCTION_CALIBRATE =

:nodoc:

3
FUNCTION_GET_IDENTITY =

:nodoc:

255
BEEP_DURATION_OFF =

:nodoc:

0
BEEP_DURATION_INFINITE =

:nodoc:

4294967295

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) ⇒ BrickletPiezoSpeaker

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/tinkerforge/bricklet_piezo_speaker.rb', line 37

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

  @api_version = [2, 0, 0]

  @response_expected[FUNCTION_BEEP] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_MORSE_CODE] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_CALIBRATE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE

  @callback_formats[CALLBACK_BEEP_FINISHED] = [8, '']
  @callback_formats[CALLBACK_MORSE_CODE_FINISHED] = [8, '']

  @ipcon.add_device self
end

Instance Method Details

#beep(duration, frequency) ⇒ Object

Beeps with the given frequency for the given duration.

.. versionchanged

2.0.2$nbsp;(Plugin)

A duration of 0 stops the current beep if any, the frequency parameter is
ignored. A duration of 4294967295 results in an infinite beep.

The Piezo Speaker Bricklet can only approximate the frequency, it will play the best possible match by applying the calibration (see BrickletPiezoSpeaker#calibrate).



61
62
63
64
65
# File 'lib/tinkerforge/bricklet_piezo_speaker.rb', line 61

def beep(duration, frequency)
  check_validity

  send_request FUNCTION_BEEP, [duration, frequency], 'L S', 8, ''
end

#calibrateObject

The Piezo Speaker Bricklet can play 512 different tones. This function plays each tone and measures the exact frequency back. The result is a mapping between setting value and frequency. This mapping is stored in the EEPROM and loaded on startup.

The Bricklet should come calibrated, you only need to call this function (once) every time you reflash the Bricklet plugin.

Returns true after the calibration finishes.



89
90
91
92
93
# File 'lib/tinkerforge/bricklet_piezo_speaker.rb', line 89

def calibrate
  check_validity

  send_request FUNCTION_CALIBRATE, [], '', 9, '?'
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|



105
106
107
# File 'lib/tinkerforge/bricklet_piezo_speaker.rb', line 105

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

#morse_code(morse, frequency) ⇒ Object

Sets morse code that will be played by the piezo buzzer. The morse code is given as a string consisting of “.” (dot), “-” (minus) and “ ” (space) for dits, dahs and pauses. Every other character is ignored.

For example: If you set the string “…—…”, the piezo buzzer will beep nine times with the durations “short short short long long long short short short”.



74
75
76
77
78
# File 'lib/tinkerforge/bricklet_piezo_speaker.rb', line 74

def morse_code(morse, frequency)
  check_validity

  send_request FUNCTION_MORSE_CODE, [morse, frequency], 'Z60 S', 8, ''
end

#register_callback(id, &block) ⇒ Object

Registers a callback with ID id to the block block.



110
111
112
113
# File 'lib/tinkerforge/bricklet_piezo_speaker.rb', line 110

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