Go - LCD 16x2 Bricklet

This is the description of the Go API bindings for the LCD 16x2 Bricklet. General information and technical specifications for the LCD 16x2 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)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package main

import (
    "fmt"
    "github.com/Tinkerforge/go-api-bindings/ipconnection"
    "github.com/Tinkerforge/go-api-bindings/lcd_16x2_bricklet"
)

const ADDR string = "localhost:4223"
const UID string = "XYZ" // Change XYZ to the UID of your LCD 16x2 Bricklet.

func main() {
    ipcon := ipconnection.New()
    defer ipcon.Close()
    lcd, _ := lcd_16x2_bricklet.New(UID, &ipcon) // Create device object.

    ipcon.Connect(ADDR) // Connect to brickd.
    defer ipcon.Disconnect()
    // Don't use device before ipcon is connected.

    // Turn backlight on
    lcd.BacklightOn()

    // Write "Hello World"
    lcd.WriteLine(0, 0, "Hello World")

    fmt.Print("Press enter to exit.")
    fmt.Scanln()
}

Button Callback

Download (example_button_callback.go)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package main

import (
    "fmt"
    "github.com/Tinkerforge/go-api-bindings/ipconnection"
    "github.com/Tinkerforge/go-api-bindings/lcd_16x2_bricklet"
)

const ADDR string = "localhost:4223"
const UID string = "XYZ" // Change XYZ to the UID of your LCD 16x2 Bricklet.

func main() {
    ipcon := ipconnection.New()
    defer ipcon.Close()
    lcd, _ := lcd_16x2_bricklet.New(UID, &ipcon) // Create device object.

    ipcon.Connect(ADDR) // Connect to brickd.
    defer ipcon.Disconnect()
    // Don't use device before ipcon is connected.

    lcd.RegisterButtonPressedCallback(func(button uint8) {
        fmt.Printf("Button Pressed: %d\n", button)
    })

    lcd.RegisterButtonReleasedCallback(func(button uint8) {
        fmt.Printf("Button Released: %d\n", button)
    })

    fmt.Print("Press enter to exit.")
    fmt.Scanln()
}

Unicode

Download (example_unicode.go)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main

import (
    "fmt"
    "github.com/Tinkerforge/go-api-bindings/ipconnection"
    "github.com/Tinkerforge/go-api-bindings/lcd_16x2_bricklet"
)

const ADDR string = "localhost:4223"
const UID string = "XYZ" // Change XYZ to the UID of your LCD 16x2 Bricklet.

func UTF8ToKS0066U(utf8 string) string {
    var result []rune
    for _, r := range utf8 {
        var replacement rune
        switch {
        // ASCII subset from JIS X 0201
        // The LCD charset doesn't include '\' and '~', use similar characters instead
        case r == 0x005c:
            replacement = 0xa4 // REVERSE SOLIDUS maps to IDEOGRAPHIC COMMA
        case r == 0x007e:
            replacement = 0x2d // TILDE maps to HYPHEN-MINUS
        case r >= 0x0020 && r <= 0x007e:
            replacement = r

        // Katakana subset from JIS X 0201
        case r >= 0xff61 && r <= 0xff9f:
            replacement = r - 0xfec0

        // Special characters
        case r == 0x00a5:
            replacement = 0x5c // YEN SIGN
        case r == 0x2192:
            replacement = 0x7e // RIGHTWARDS ARROW
        case r == 0x2190:
            replacement = 0x7f // LEFTWARDS ARROW
        case r == 0x00b0:
            replacement = 0xdf // DEGREE SIGN maps to KATAKANA SEMI-VOICED SOUND MARK
        case r == 0x03b1:
            replacement = 0xe0 // GREEK SMALL LETTER ALPHA
        case r == 0x00c4:
            replacement = 0xe1 // LATIN CAPITAL LETTER A WITH DIAERESIS
        case r == 0x00e4:
            replacement = 0xe1 // LATIN SMALL LETTER A WITH DIAERESIS
        case r == 0x00df:
            replacement = 0xe2 // LATIN SMALL LETTER SHARP S
        case r == 0x03b5:
            replacement = 0xe3 // GREEK SMALL LETTER EPSILON
        case r == 0x00b5:
            replacement = 0xe4 // MICRO SIGN
        case r == 0x03bc:
            replacement = 0xe4 // GREEK SMALL LETTER MU
        case r == 0x03c2:
            replacement = 0xe5 // GREEK SMALL LETTER FINAL SIGMA
        case r == 0x03c1:
            replacement = 0xe6 // GREEK SMALL LETTER RHO
        case r == 0x221a:
            replacement = 0xe8 // SQUARE ROOT
        case r == 0x00b9:
            replacement = 0xe9 // SUPERSCRIPT ONE maps to SUPERSCRIPT (minus) ONE
        case r == 0x00a4:
            replacement = 0xeb // CURRENCY SIGN
        case r == 0x00a2:
            replacement = 0xec // CENT SIGN
        case r == 0x2c60:
            replacement = 0xed // LATIN CAPITAL LETTER L WITH DOUBLE BAR
        case r == 0x00f1:
            replacement = 0xee // LATIN SMALL LETTER N WITH TILDE
        case r == 0x00d6:
            replacement = 0xef // LATIN CAPITAL LETTER O WITH DIAERESIS
        case r == 0x00f6:
            replacement = 0xef // LATIN SMALL LETTER O WITH DIAERESIS
        case r == 0x03f4:
            replacement = 0xf2 // GREEK CAPITAL THETA SYMBOL
        case r == 0x221e:
            replacement = 0xf3 // INFINITY
        case r == 0x03a9:
            replacement = 0xf4 // GREEK CAPITAL LETTER OMEGA
        case r == 0x00dc:
            replacement = 0xf5 // LATIN CAPITAL LETTER U WITH DIAERESIS
        case r == 0x00fc:
            replacement = 0xf5 // LATIN SMALL LETTER U WITH DIAERESIS
        case r == 0x03a3:
            replacement = 0xf6 // GREEK CAPITAL LETTER SIGMA
        case r == 0x03c0:
            replacement = 0xf7 // GREEK SMALL LETTER PI
        case r == 0x0304:
            replacement = 0xf8 // COMBINING MACRON
        case r == 0x00f7:
            replacement = 0xfd // DIVISION SIGN

        default:
            replacement = 0xff // BLACK SQUARE
        }

        // Special handling for 'x' followed by COMBINING MACRON
        if replacement == 0xf8 {
            if result[len(result)-1] != 'x' {
                replacement = 0xff // BLACK SQUARE
            }

            if len(result) > 0 {
                result = result[:len(result)-1]
            }
        }
        result = append(result, replacement)
    }
    return string(result)
}

func main() {
    ipcon := ipconnection.New()
    defer ipcon.Close()
    lcd, _ := lcd_16x2_bricklet.New(UID, &ipcon) // Create device object.

    ipcon.Connect(ADDR) // Connect to brickd.
    defer ipcon.Disconnect()
    // Don't use device before ipcon is connected.

    // Turn backlight on
    lcd.BacklightOn()

    // Write a string using the UTF8ToKS0066U function to map to the LCD charset
    lcd.WriteLine(0, 0, UTF8ToKS0066U("Stromstärke: 5µA"))

    // Write a string directly including characters from the LCD charset
    lcd.WriteLine(1, 0, "Drehzahl: 1000s\u00e9")

    fmt.Print("Press enter to exit.")
    fmt.Scanln()
}

API

The LCD 16x2 Bricklet API is defined in the package github.com/Tinkerforge/go-api-bindings/lcd_16x2_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_16x2_bricklet.New(uid string, ipcon *IPConnection) (device LCD16x2Bricklet, err error)
Parameters:
  • uid – Type: string
  • ipcon – Type: *IPConnection
Returns:
  • device – Type: LCD16x2Bricklet
  • err – Type: error

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

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

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

func (*LCD16x2Bricklet) WriteLine(line uint8, position uint8, text string) (err error)
Parameters:
  • line – Type: uint8, Range: [0 to 1]
  • position – Type: uint8, Range: [0 to 15]
  • text – Type: string, Length: up to 16
Returns:
  • err – Type: error

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

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

Deletes all characters from the display.

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

Turns the backlight on.

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

Turns the backlight off.

func (*LCD16x2Bricklet) 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 (*LCD16x2Bricklet) 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 (*LCD16x2Bricklet) 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 (*LCD16x2Bricklet) IsButtonPressed(button uint8) (pressed bool, err error)
Parameters:
  • button – Type: uint8, Range: [0 to 2]
Returns:
  • pressed – Type: bool
  • err – Type: error

Returns true if the button is pressed.

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

func (*LCD16x2Bricklet) 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 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[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 since version 2.0.1.

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

New in version 2.0.1 (Plugin).

func (*LCD16x2Bricklet) 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().

New in version 2.0.1 (Plugin).

func (*LCD16x2Bricklet) 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 (*LCD16x2Bricklet) RegisterButtonPressedCallback(func(button uint8)) (registrationId uint64)
Callback Parameters:
  • button – Type: uint8, Range: [0 to 2]
Returns:
  • registrationId – Type: uint64

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

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

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

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 (*LCD16x2Bricklet) 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 (*LCD16x2Bricklet) 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_16x2_bricklet.FunctionWriteLine = 1
  • lcd_16x2_bricklet.FunctionClearDisplay = 2
  • lcd_16x2_bricklet.FunctionBacklightOn = 3
  • lcd_16x2_bricklet.FunctionBacklightOff = 4
  • lcd_16x2_bricklet.FunctionSetConfig = 6
  • lcd_16x2_bricklet.FunctionSetCustomCharacter = 11
func (*LCD16x2Bricklet) 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_16x2_bricklet.FunctionWriteLine = 1
  • lcd_16x2_bricklet.FunctionClearDisplay = 2
  • lcd_16x2_bricklet.FunctionBacklightOn = 3
  • lcd_16x2_bricklet.FunctionBacklightOff = 4
  • lcd_16x2_bricklet.FunctionSetConfig = 6
  • lcd_16x2_bricklet.FunctionSetCustomCharacter = 11
func (*LCD16x2Bricklet) 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_16x2_bricklet.DeviceIdentifier

This constant is used to identify a LCD 16x2 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_16x2_bricklet.DeviceDisplayName

This constant represents the human readable name of a LCD 16x2 Bricklet.