class Tinkerforge::BrickletPiezoSpeaker

Creates beep with configurable frequency

Constants

CALLBACK_BEEP_FINISHED

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

CALLBACK_MORSE_CODE_FINISHED

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

Public Class Methods

new(uid, ipcon) click to toggle source

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

Calls superclass method
# File lib/tinkerforge/bricklet_piezo_speaker.rb, line 36
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

Public Instance Methods

beep(duration, frequency) click to toggle source

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).

# File lib/tinkerforge/bricklet_piezo_speaker.rb, line 60
def beep(duration, frequency)
  check_validity

  send_request FUNCTION_BEEP, [duration, frequency], 'L S', 8, ''
end
calibrate() click to toggle source

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.

# File lib/tinkerforge/bricklet_piezo_speaker.rb, line 88
def calibrate
  check_validity

  send_request FUNCTION_CALIBRATE, [], '', 9, '?'
end
get_identity() click to toggle source

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|

# File lib/tinkerforge/bricklet_piezo_speaker.rb, line 104
def get_identity
  send_request FUNCTION_GET_IDENTITY, [], '', 33, 'Z8 Z8 k C3 C3 S'
end
morse_code(morse, frequency) click to toggle source

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”.

# File lib/tinkerforge/bricklet_piezo_speaker.rb, line 73
def morse_code(morse, frequency)
  check_validity

  send_request FUNCTION_MORSE_CODE, [morse, frequency], 'Z60 S', 8, ''
end
register_callback(id, &block) click to toggle source

Registers a callback with ID id to the block block.

# File lib/tinkerforge/bricklet_piezo_speaker.rb, line 109
def register_callback(id, &block)
  callback = block
  @registered_callbacks[id] = callback
end