Class: Tinkerforge::BrickletPiezoBuzzer

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

Overview

Creates 1kHz beep

Constant Summary collapse

DEVICE_IDENTIFIER =

:nodoc:

214
DEVICE_DISPLAY_NAME =

:nodoc:

'Piezo Buzzer Bricklet'
CALLBACK_BEEP_FINISHED =

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

3
CALLBACK_MORSE_CODE_FINISHED =

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

4
FUNCTION_BEEP =

:nodoc:

1
FUNCTION_MORSE_CODE =

:nodoc:

2
FUNCTION_GET_IDENTITY =

:nodoc:

255

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

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tinkerforge/bricklet_piezo_buzzer.rb', line 34

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_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) ⇒ Object

Beeps for the given duration.



50
51
52
53
54
# File 'lib/tinkerforge/bricklet_piezo_buzzer.rb', line 50

def beep(duration)
  check_validity

  send_request FUNCTION_BEEP, [duration], 'L', 8, ''
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|



79
80
81
# File 'lib/tinkerforge/bricklet_piezo_buzzer.rb', line 79

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

#morse_code(morse) ⇒ 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”.



63
64
65
66
67
# File 'lib/tinkerforge/bricklet_piezo_buzzer.rb', line 63

def morse_code(morse)
  check_validity

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

#register_callback(id, &block) ⇒ Object

Registers a callback with ID id to the block block.



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

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