Go - LCD 20x4 Bricklet

This is the description of the Go API bindings for the LCD 20x4 Bricklet. General information and technical specifications for the LCD 20x4 Bricklet are summarized in its hardware description.

An installation guide for the Go API bindings is part of their general description. Additional documentation can be found on godoc.org.

Examples

The example code below is Public Domain (CC0 1.0).

Hello World

Download (example_hello_world.go)

 1package main
 2
 3import (
 4    "fmt"
 5    "github.com/Tinkerforge/go-api-bindings/ipconnection"
 6    "github.com/Tinkerforge/go-api-bindings/lcd_20x4_bricklet"
 7)
 8
 9const ADDR string = "localhost:4223"
10const UID string = "XYZ" // Change XYZ to the UID of your LCD 20x4 Bricklet.
11
12func main() {
13    ipcon := ipconnection.New()
14    defer ipcon.Close()
15    lcd, _ := lcd_20x4_bricklet.New(UID, &ipcon) // Create device object.
16
17    ipcon.Connect(ADDR) // Connect to brickd.
18    defer ipcon.Disconnect()
19    // Don't use device before ipcon is connected.
20
21    // Turn backlight on
22    lcd.BacklightOn()
23
24    // Write "Hello World"
25    lcd.WriteLine(0, 0, "Hello World")
26
27    fmt.Print("Press enter to exit.")
28    fmt.Scanln()
29}

Button Callback

Download (example_button_callback.go)

 1package main
 2
 3import (
 4    "fmt"
 5    "github.com/Tinkerforge/go-api-bindings/ipconnection"
 6    "github.com/Tinkerforge/go-api-bindings/lcd_20x4_bricklet"
 7)
 8
 9const ADDR string = "localhost:4223"
10const UID string = "XYZ" // Change XYZ to the UID of your LCD 20x4 Bricklet.
11
12func main() {
13    ipcon := ipconnection.New()
14    defer ipcon.Close()
15    lcd, _ := lcd_20x4_bricklet.New(UID, &ipcon) // Create device object.
16
17    ipcon.Connect(ADDR) // Connect to brickd.
18    defer ipcon.Disconnect()
19    // Don't use device before ipcon is connected.
20
21    lcd.RegisterButtonPressedCallback(func(button uint8) {
22        fmt.Printf("Button Pressed: %d\n", button)
23    })
24
25    lcd.RegisterButtonReleasedCallback(func(button uint8) {
26        fmt.Printf("Button Released: %d\n", button)
27    })
28
29    fmt.Print("Press enter to exit.")
30    fmt.Scanln()
31}

Unicode

Download (example_unicode.go)

  1package main
  2
  3import (
  4    "fmt"
  5    "github.com/Tinkerforge/go-api-bindings/ipconnection"
  6    "github.com/Tinkerforge/go-api-bindings/lcd_20x4_bricklet"
  7)
  8
  9const ADDR string = "localhost:4223"
 10const UID string = "XYZ" // Change XYZ to the UID of your LCD 20x4 Bricklet.
 11
 12func UTF8ToKS0066U(utf8 string) string {
 13    var result []rune
 14    for _, r := range utf8 {
 15        var replacement rune
 16        switch {
 17        // ASCII subset from JIS X 0201
 18        // The LCD charset doesn't include '\' and '~', use similar characters instead
 19        case r == 0x005c:
 20            replacement = 0xa4 // REVERSE SOLIDUS maps to IDEOGRAPHIC COMMA
 21        case r == 0x007e:
 22            replacement = 0x2d // TILDE maps to HYPHEN-MINUS
 23        case r >= 0x0020 && r <= 0x007e:
 24            replacement = r
 25
 26        // Katakana subset from JIS X 0201
 27        case r >= 0xff61 && r <= 0xff9f:
 28            replacement = r - 0xfec0
 29
 30        // Special characters
 31        case r == 0x00a5:
 32            replacement = 0x5c // YEN SIGN
 33        case r == 0x2192:
 34            replacement = 0x7e // RIGHTWARDS ARROW
 35        case r == 0x2190:
 36            replacement = 0x7f // LEFTWARDS ARROW
 37        case r == 0x00b0:
 38            replacement = 0xdf // DEGREE SIGN maps to KATAKANA SEMI-VOICED SOUND MARK
 39        case r == 0x03b1:
 40            replacement = 0xe0 // GREEK SMALL LETTER ALPHA
 41        case r == 0x00c4:
 42            replacement = 0xe1 // LATIN CAPITAL LETTER A WITH DIAERESIS
 43        case r == 0x00e4:
 44            replacement = 0xe1 // LATIN SMALL LETTER A WITH DIAERESIS
 45        case r == 0x00df:
 46            replacement = 0xe2 // LATIN SMALL LETTER SHARP S
 47        case r == 0x03b5:
 48            replacement = 0xe3 // GREEK SMALL LETTER EPSILON
 49        case r == 0x00b5:
 50            replacement = 0xe4 // MICRO SIGN
 51        case r == 0x03bc:
 52            replacement = 0xe4 // GREEK SMALL LETTER MU
 53        case r == 0x03c2:
 54            replacement = 0xe5 // GREEK SMALL LETTER FINAL SIGMA
 55        case r == 0x03c1:
 56            replacement = 0xe6 // GREEK SMALL LETTER RHO
 57        case r == 0x221a:
 58            replacement = 0xe8 // SQUARE ROOT
 59        case r == 0x00b9:
 60            replacement = 0xe9 // SUPERSCRIPT ONE maps to SUPERSCRIPT (minus) ONE
 61        case r == 0x00a4:
 62            replacement = 0xeb // CURRENCY SIGN
 63        case r == 0x00a2:
 64            replacement = 0xec // CENT SIGN
 65        case r == 0x2c60:
 66            replacement = 0xed // LATIN CAPITAL LETTER L WITH DOUBLE BAR
 67        case r == 0x00f1:
 68            replacement = 0xee // LATIN SMALL LETTER N WITH TILDE
 69        case r == 0x00d6:
 70            replacement = 0xef // LATIN CAPITAL LETTER O WITH DIAERESIS
 71        case r == 0x00f6:
 72            replacement = 0xef // LATIN SMALL LETTER O WITH DIAERESIS
 73        case r == 0x03f4:
 74            replacement = 0xf2 // GREEK CAPITAL THETA SYMBOL
 75        case r == 0x221e:
 76            replacement = 0xf3 // INFINITY
 77        case r == 0x03a9:
 78            replacement = 0xf4 // GREEK CAPITAL LETTER OMEGA
 79        case r == 0x00dc:
 80            replacement = 0xf5 // LATIN CAPITAL LETTER U WITH DIAERESIS
 81        case r == 0x00fc:
 82            replacement = 0xf5 // LATIN SMALL LETTER U WITH DIAERESIS
 83        case r == 0x03a3:
 84            replacement = 0xf6 // GREEK CAPITAL LETTER SIGMA
 85        case r == 0x03c0:
 86            replacement = 0xf7 // GREEK SMALL LETTER PI
 87        case r == 0x0304:
 88            replacement = 0xf8 // COMBINING MACRON
 89        case r == 0x00f7:
 90            replacement = 0xfd // DIVISION SIGN
 91
 92        default:
 93            replacement = 0xff // BLACK SQUARE
 94        }
 95
 96        // Special handling for 'x' followed by COMBINING MACRON
 97        if replacement == 0xf8 {
 98            if result[len(result)-1] != 'x' {
 99                replacement = 0xff // BLACK SQUARE
100            }
101
102            if len(result) > 0 {
103                result = result[:len(result)-1]
104            }
105        }
106        result = append(result, replacement)
107    }
108    return string(result)
109}
110
111func main() {
112    ipcon := ipconnection.New()
113    defer ipcon.Close()
114    lcd, _ := lcd_20x4_bricklet.New(UID, &ipcon) // Create device object.
115
116    ipcon.Connect(ADDR) // Connect to brickd.
117    defer ipcon.Disconnect()
118    // Don't use device before ipcon is connected.
119
120    // Turn backlight on
121    lcd.BacklightOn()
122
123    // Write some strings using the UTF8ToKS0066U function to map to the LCD charset
124    lcd.WriteLine(0, 0, UTF8ToKS0066U("Stromstärke: 17µA"))
125    lcd.WriteLine(1, 0, UTF8ToKS0066U("Temperatur:  23°C"))
126
127    // Write a string directly including characters from the LCD charset
128    lcd.WriteLine(2, 0, "Drehzahl: 750min\u00e9")
129
130    fmt.Print("Press enter to exit.")
131    fmt.Scanln()
132}

API

The LCD 20x4 Bricklet API is defined in the package github.com/Tinkerforge/go-api-bindings/lcd_20x4_bricklet

Nearly every function of the Go bindings can return an ipconnection.DeviceError, implementing the error interface. The error can have one of the following values:

  • ipconnection.DeviceErrorSuccess = 0

  • ipconnection.DeviceErrorInvalidParameter = 1

  • ipconnection.DeviceErrorFunctionNotSupported = 2

  • ipconnection.DeviceErrorUnknownError = 3

which correspond to the values returned from Bricks and Bricklets.

All functions listed below are thread-safe.

Basic Functions

func lcd_20x4_bricklet.New(uid string, ipcon *IPConnection) (device LCD20x4Bricklet, err error)
Parameters:
  • uid – Type: string
  • ipcon – Type: *IPConnection
Returns:
  • device – Type: LCD20x4Bricklet
  • err – Type: error

Creates a new LCD20x4Bricklet object with the unique device ID uid and adds it to the IPConnection ipcon:

device, err := lcd_20x4_bricklet.New("YOUR_DEVICE_UID", &ipcon)

This device object can be used after the IPConnection has been connected.

func (*LCD20x4Bricklet) WriteLine(line uint8, position uint8, text string) (err error)
Parameters:
  • line – Type: uint8, Range: [0 to 3]
  • position – Type: uint8, Range: [0 to 19]
  • text – Type: string, Length: up to 20
Returns:
  • err – Type: error

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

For example: (0, 7, "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 for details. The Unicode example above shows how to specify non-ASCII characters and how to translate from Unicode to the LCD charset.

func (*LCD20x4Bricklet) ClearDisplay() (err error)
Returns:
  • err – Type: error

Deletes all characters from the display.

func (*LCD20x4Bricklet) BacklightOn() (err error)
Returns:
  • err – Type: error

Turns the backlight on.

func (*LCD20x4Bricklet) BacklightOff() (err error)
Returns:
  • err – Type: error

Turns the backlight off.

func (*LCD20x4Bricklet) IsBacklightOn() (backlight bool, err error)
Returns:
  • backlight – Type: bool
  • err – Type: error

Returns true if the backlight is on and false otherwise.

Advanced Functions

func (*LCD20x4Bricklet) SetConfig(cursor bool, blinking bool) (err error)
Parameters:
  • cursor – Type: bool, Default: false
  • blinking – Type: bool, Default: false
Returns:
  • err – Type: error

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

func (*LCD20x4Bricklet) GetConfig() (cursor bool, blinking bool, err error)
Returns:
  • cursor – Type: bool, Default: false
  • blinking – Type: bool, Default: false
  • err – Type: error

Returns the configuration as set by SetConfig().

func (*LCD20x4Bricklet) IsButtonPressed(button uint8) (pressed bool, err error)
Parameters:
  • button – Type: uint8, Range: [0 to 3]
Returns:
  • pressed – Type: bool
  • err – Type: error

Returns true if the button (0 to 2 or 0 to 3 since hardware version 1.2) is pressed.

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

func (*LCD20x4Bricklet) SetCustomCharacter(index uint8, character [8]uint8) (err error)
Parameters:
  • index – Type: uint8, Range: [0 to 7]
  • character – Type: [8]uint8, Range: [0 to 31]
Returns:
  • err – Type: error

The LCD 20x4 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[0] = 0b00010001 (decimal value 17)

  • character[1] = 0b00010001 (decimal value 17)

  • character[2] = 0b00010001 (decimal value 17)

  • character[3] = 0b00011111 (decimal value 31)

  • character[4] = 0b00010001 (decimal value 17)

  • character[5] = 0b00010001 (decimal value 17)

  • character[6] = 0b00010001 (decimal value 17)

  • character[7] = 0b00000000 (decimal value 0)

The characters can later be written with WriteLine() 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 version since 2.0.1.

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

Added in version 2.0.1$nbsp;(Plugin).

func (*LCD20x4Bricklet) GetCustomCharacter(index uint8) (character [8]uint8, err error)
Parameters:
  • index – Type: uint8, Range: [0 to 7]
Returns:
  • character – Type: [8]uint8, Range: [0 to 31]
  • err – Type: error

Returns the custom character for a given index, as set with SetCustomCharacter().

Added in version 2.0.1$nbsp;(Plugin).

func (*LCD20x4Bricklet) SetDefaultText(line uint8, text string) (err error)
Parameters:
  • line – Type: uint8, Range: [0 to 3]
  • text – Type: string, Length: up to 20
Returns:
  • err – Type: error

Sets the default text for lines 0-3. The max number of characters per line is 20.

The default text is shown on the LCD, if the default text counter expires, see SetDefaultTextCounter().

Added in version 2.0.2$nbsp;(Plugin).

func (*LCD20x4Bricklet) GetDefaultText(line uint8) (text string, err error)
Parameters:
  • line – Type: uint8, Range: [0 to 3]
Returns:
  • text – Type: string, Length: up to 20
  • err – Type: error

Returns the default text for a given line (0-3) as set by SetDefaultText().

Added in version 2.0.2$nbsp;(Plugin).

func (*LCD20x4Bricklet) SetDefaultTextCounter(counter int32) (err error)
Parameters:
  • counter – Type: int32, Unit: 1 ms, Range: [-231 to 231 - 1], Default: -1
Returns:
  • err – Type: error

Sets the default text counter. This counter is decremented each ms by the LCD firmware. If the counter reaches 0, the default text (see SetDefaultText()) is shown on the LCD.

This functionality can be used to show a default text if the controlling program crashes or the connection is interrupted.

A possible approach is to call SetDefaultTextCounter() every minute with the parameter 1000*60*2 (2 minutes). In this case the default text will be shown no later than 2 minutes after the controlling program crashes.

A negative counter turns the default text functionality off.

Added in version 2.0.2$nbsp;(Plugin).

func (*LCD20x4Bricklet) GetDefaultTextCounter() (counter int32, err error)
Returns:
  • counter – Type: int32, Unit: 1 ms, Range: [-231 to 231 - 1], Default: -1
  • err – Type: error

Returns the current value of the default text counter.

Added in version 2.0.2$nbsp;(Plugin).

func (*LCD20x4Bricklet) GetIdentity() (uid string, connectedUid string, position rune, hardwareVersion [3]uint8, firmwareVersion [3]uint8, deviceIdentifier uint16, err error)
Returns:
  • uid – Type: string, Length: up to 8
  • connectedUid – Type: string, Length: up to 8
  • position – Type: rune, Range: ['a' to 'h', 'z']
  • hardwareVersion – Type: [3]uint8
    • 0: major – Type: uint8, Range: [0 to 255]
    • 1: minor – Type: uint8, Range: [0 to 255]
    • 2: revision – Type: uint8, Range: [0 to 255]
  • firmwareVersion – Type: [3]uint8
    • 0: major – Type: uint8, Range: [0 to 255]
    • 1: minor – Type: uint8, Range: [0 to 255]
    • 2: revision – Type: uint8, Range: [0 to 255]
  • deviceIdentifier – Type: uint16, Range: [0 to 216 - 1]
  • err – Type: error

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 Isolator Bricklet is always at position 'z'.

The device identifier numbers can be found here. There is also a constant for the device identifier of this Bricklet.

Callbacks

Callbacks can be registered to receive time critical or recurring data from the device. The registration is done with the corresponding Register*Callback function, which returns a unique callback ID. This ID can be used to deregister the callback later with the corresponding Deregister*Callback function.

Note

Using callbacks for recurring events is always preferred compared to using getters. It will use less USB bandwidth and the latency will be a lot better, since there is no round trip time.

func (*LCD20x4Bricklet) RegisterButtonPressedCallback(func(button uint8)) (registrationId uint64)
Callback Parameters:
  • button – Type: uint8, Range: [0 to 3]
Returns:
  • registrationId – Type: uint64

This callback is triggered when a button is pressed. The callback parameter is the number of the button (0 to 2 or 0 to 3 since hardware version 1.2).

func (*LCD20x4Bricklet) RegisterButtonReleasedCallback(func(button uint8)) (registrationId uint64)
Callback Parameters:
  • button – Type: uint8, Range: [0 to 3]
Returns:
  • registrationId – Type: uint64

This callback is triggered when a button is released. The callback parameter is the number of the button (0 to 2 or 0 to 3 since hardware version 1.2).

Virtual Functions

Virtual functions don't communicate with the device itself, but operate only on the API bindings device object. They can be called without the corresponding IP Connection object being connected.

func (*LCD20x4Bricklet) GetAPIVersion() (apiVersion [3]uint8, err error)
Returns:
  • apiVersion – Type: [3]uint8
    • 0: major – Type: uint8, Range: [0 to 255]
    • 1: minor – Type: uint8, Range: [0 to 255]
    • 2: revision – Type: uint8, Range: [0 to 255]
  • err – Type: error

Returns the version of the API definition implemented by this API bindings. This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.

func (*LCD20x4Bricklet) GetResponseExpected(functionId uint8) (responseExpected bool, err error)
Parameters:
  • functionId – Type: uint8, Range: See constants
Returns:
  • responseExpected – Type: bool
  • err – Type: error

Returns the response expected flag for the function specified by the function ID parameter. It is true if the function is expected to send a response, false otherwise.

For getter functions this is enabled by default and cannot be disabled, because those functions will always send a response. For callback configuration functions it is enabled by default too, but can be disabled by SetResponseExpected(). For setter functions it is disabled by default and can be enabled.

Enabling the response expected flag for a setter function allows to detect timeouts and other error conditions calls of this setter as well. The device will then send a response for this purpose. If this flag is disabled for a setter function then no response is sent and errors are silently ignored, because they cannot be detected.

The following constants are available for this function:

For functionId:

  • lcd_20x4_bricklet.FunctionWriteLine = 1

  • lcd_20x4_bricklet.FunctionClearDisplay = 2

  • lcd_20x4_bricklet.FunctionBacklightOn = 3

  • lcd_20x4_bricklet.FunctionBacklightOff = 4

  • lcd_20x4_bricklet.FunctionSetConfig = 6

  • lcd_20x4_bricklet.FunctionSetCustomCharacter = 11

  • lcd_20x4_bricklet.FunctionSetDefaultText = 13

  • lcd_20x4_bricklet.FunctionSetDefaultTextCounter = 15

func (*LCD20x4Bricklet) SetResponseExpected(functionId uint8, responseExpected bool) (err error)
Parameters:
  • functionId – Type: uint8, Range: See constants
  • responseExpected – Type: bool
Returns:
  • err – Type: error

Changes the response expected flag of the function specified by the function ID parameter. This flag can only be changed for setter (default value: false) and callback configuration functions (default value: true). For getter functions it is always enabled.

Enabling the response expected flag for a setter function allows to detect timeouts and other error conditions calls of this setter as well. The device will then send a response for this purpose. If this flag is disabled for a setter function then no response is sent and errors are silently ignored, because they cannot be detected.

The following constants are available for this function:

For functionId:

  • lcd_20x4_bricklet.FunctionWriteLine = 1

  • lcd_20x4_bricklet.FunctionClearDisplay = 2

  • lcd_20x4_bricklet.FunctionBacklightOn = 3

  • lcd_20x4_bricklet.FunctionBacklightOff = 4

  • lcd_20x4_bricklet.FunctionSetConfig = 6

  • lcd_20x4_bricklet.FunctionSetCustomCharacter = 11

  • lcd_20x4_bricklet.FunctionSetDefaultText = 13

  • lcd_20x4_bricklet.FunctionSetDefaultTextCounter = 15

func (*LCD20x4Bricklet) SetResponseExpectedAll(responseExpected bool) (err error)
Parameters:
  • responseExpected – Type: bool
Returns:
  • err – Type: error

Changes the response expected flag for all setter and callback configuration functions of this device at once.

Constants

lcd_20x4_bricklet.DeviceIdentifier

This constant is used to identify a LCD 20x4 Bricklet.

The GetIdentity() function and the (*IPConnection) RegisterEnumerateCallback() callback of the IPConnection have a deviceIdentifier parameter to specify the Brick's or Bricklet's type.

lcd_20x4_bricklet.DeviceDisplayName

This constant represents the human readable name of a LCD 20x4 Bricklet.