Class: Tinkerforge::BrickletLCD16x2

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

Overview

16x2 character alphanumeric display with blue backlight

Constant Summary collapse

DEVICE_IDENTIFIER =

:nodoc:

211
DEVICE_DISPLAY_NAME =

:nodoc:

'LCD 16x2 Bricklet'
CALLBACK_BUTTON_PRESSED =

This callback is triggered when a button is pressed. The parameter is the number of the button.

9
CALLBACK_BUTTON_RELEASED =

This callback is triggered when a button is released. The parameter is the number of the button.

10
FUNCTION_WRITE_LINE =

:nodoc:

1
FUNCTION_CLEAR_DISPLAY =

:nodoc:

2
FUNCTION_BACKLIGHT_ON =

:nodoc:

3
FUNCTION_BACKLIGHT_OFF =

:nodoc:

4
FUNCTION_IS_BACKLIGHT_ON =

:nodoc:

5
FUNCTION_SET_CONFIG =

:nodoc:

6
FUNCTION_GET_CONFIG =

:nodoc:

7
FUNCTION_IS_BUTTON_PRESSED =

:nodoc:

8
FUNCTION_SET_CUSTOM_CHARACTER =

:nodoc:

11
FUNCTION_GET_CUSTOM_CHARACTER =

:nodoc:

12
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) ⇒ BrickletLCD16x2

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tinkerforge/bricklet_lcd_16x2.rb', line 43

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

  @api_version = [2, 0, 1]

  @response_expected[FUNCTION_WRITE_LINE] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_CLEAR_DISPLAY] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_BACKLIGHT_ON] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_BACKLIGHT_OFF] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_IS_BACKLIGHT_ON] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_CONFIG] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_CONFIG] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_IS_BUTTON_PRESSED] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_CUSTOM_CHARACTER] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_CUSTOM_CHARACTER] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE

  @callback_formats[CALLBACK_BUTTON_PRESSED] = [9, 'C']
  @callback_formats[CALLBACK_BUTTON_RELEASED] = [9, 'C']

  @ipcon.add_device self
end

Instance Method Details

#backlight_offObject

Turns the backlight off.



98
99
100
101
102
# File 'lib/tinkerforge/bricklet_lcd_16x2.rb', line 98

def backlight_off
  check_validity

  send_request FUNCTION_BACKLIGHT_OFF, [], '', 8, ''
end

#backlight_onObject

Turns the backlight on.



91
92
93
94
95
# File 'lib/tinkerforge/bricklet_lcd_16x2.rb', line 91

def backlight_on
  check_validity

  send_request FUNCTION_BACKLIGHT_ON, [], '', 8, ''
end

#clear_displayObject

Deletes all characters from the display.



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

def clear_display
  check_validity

  send_request FUNCTION_CLEAR_DISPLAY, [], '', 8, ''
end

#get_configObject

Returns the configuration as set by BrickletLCD16x2#set_config.



122
123
124
125
126
# File 'lib/tinkerforge/bricklet_lcd_16x2.rb', line 122

def get_config
  check_validity

  send_request FUNCTION_GET_CONFIG, [], '', 10, '? ?'
end

#get_custom_character(index) ⇒ Object

Returns the custom character for a given index, as set with BrickletLCD16x2#set_custom_character.

.. versionadded

2.0.1$nbsp;(Plugin)



173
174
175
176
177
# File 'lib/tinkerforge/bricklet_lcd_16x2.rb', line 173

def get_custom_character(index)
  check_validity

  send_request FUNCTION_GET_CUSTOM_CHARACTER, [index], 'C', 16, 'C8'
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|



189
190
191
# File 'lib/tinkerforge/bricklet_lcd_16x2.rb', line 189

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

#is_backlight_onObject

Returns true if the backlight is on and false otherwise.



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

def is_backlight_on
  check_validity

  send_request FUNCTION_IS_BACKLIGHT_ON, [], '', 9, '?'
end

#is_button_pressed(button) ⇒ Object

Returns true if the button is pressed.

If you want to react on button presses and releases it is recommended to use the CALLBACK_BUTTON_PRESSED and CALLBACK_BUTTON_RELEASED callbacks.



132
133
134
135
136
# File 'lib/tinkerforge/bricklet_lcd_16x2.rb', line 132

def is_button_pressed(button)
  check_validity

  send_request FUNCTION_IS_BUTTON_PRESSED, [button], 'C', 9, '?'
end

#register_callback(id, &block) ⇒ Object

Registers a callback with ID id to the block block.



194
195
196
197
# File 'lib/tinkerforge/bricklet_lcd_16x2.rb', line 194

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

#set_config(cursor, blinking) ⇒ Object

Configures if the cursor (shown as “_”) should be visible and if it should be blinking (shown as a blinking block). The cursor position is one character behind the the last text written with BrickletLCD16x2#write_line.



115
116
117
118
119
# File 'lib/tinkerforge/bricklet_lcd_16x2.rb', line 115

def set_config(cursor, blinking)
  check_validity

  send_request FUNCTION_SET_CONFIG, [cursor, blinking], '? ?', 8, ''
end

#set_custom_character(index, character) ⇒ Object

The LCD 16x2 Bricklet can store up to 8 custom characters. The characters consist of 5x8 pixels and can be addressed with the index 0-7. To describe the pixels, the first 5 bits of 8 bytes are used. For example, to make a custom character “H”, you should transfer the following:

  • character = 0b00010001“ (decimal value 17)

  • character = 0b00010001“ (decimal value 17)

  • character = 0b00010001“ (decimal value 17)

  • character = 0b00011111“ (decimal value 31)

  • character = 0b00010001“ (decimal value 17)

  • character = 0b00010001“ (decimal value 17)

  • character = 0b00010001“ (decimal value 17)

  • character = 0b00000000“ (decimal value 0)

The characters can later be written with BrickletLCD16x2#write_line by using the characters with the byte representation 8 (“\x08” or “\u0008”) to 15 (“\x0F” or “\u000F”).

You can play around with the custom characters in Brick Viewer since version 2.0.1.

Custom characters are stored by the LCD in RAM, so they have to be set after each startup.

.. versionadded

2.0.1$nbsp;(Plugin)



163
164
165
166
167
# File 'lib/tinkerforge/bricklet_lcd_16x2.rb', line 163

def set_custom_character(index, character)
  check_validity

  send_request FUNCTION_SET_CUSTOM_CHARACTER, [index, character], 'C C8', 8, ''
end

#write_line(line, position, text) ⇒ Object

Writes text to a specific line with a specific position. The text can have a maximum of 16 characters.

For example: (0, 5, “Hello”) will write Hello in the middle of the first line of the display.

The display uses a special charset that includes all ASCII characters except backslash and tilde. The LCD charset also includes several other non-ASCII characters, see the ‘charset specification <github.com/Tinkerforge/lcd-16x2-bricklet/raw/master/datasheets/standard_charset.pdf>`__ for details. The Unicode example above shows how to specify non-ASCII characters and how to translate from Unicode to the LCD charset.



77
78
79
80
81
# File 'lib/tinkerforge/bricklet_lcd_16x2.rb', line 77

def write_line(line, position, text)
  check_validity

  send_request FUNCTION_WRITE_LINE, [line, position, text], 'C C Z16', 8, ''
end