Rust - OLED 128x64 Bricklet

This is the description of the Rust API bindings for the OLED 128x64 Bricklet. General information and technical specifications for the OLED 128x64 Bricklet are summarized in its hardware description.

An installation guide for the Rust API bindings is part of their general description. Additional documentation can be found on docs.rs.

Examples

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

Hello World

Download (example_hello_world.rs)

 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
use std::{error::Error, io};

use tinkerforge::{ip_connection::IpConnection, oled_128x64_bricklet::*};

const HOST: &str = "localhost";
const PORT: u16 = 4223;
const UID: &str = "XYZ"; // Change XYZ to the UID of your OLED 128x64 Bricklet.

fn main() -> Result<(), Box<dyn Error>> {
    let ipcon = IpConnection::new(); // Create IP connection.
    let oled = Oled128x64Bricklet::new(UID, &ipcon); // Create device object.

    ipcon.connect((HOST, PORT)).recv()??; // Connect to brickd.
                                          // Don't use device before ipcon is connected.

    // Clear display
    oled.clear_display().recv()?;

    // Write "Hello World" starting from upper left corner of the screen
    oled.write_line(0, 0, "Hello World".to_string()).recv()?;

    println!("Press enter to exit.");
    let mut _input = String::new();
    io::stdin().read_line(&mut _input)?;
    ipcon.disconnect();
    Ok(())
}

Pixel Matrix

Download (example_pixel_matrix.rs)

 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
use std::{error::Error, io};

use tinkerforge::{ip_connection::IpConnection, oled_128x64_bricklet::*};

const HOST: &str = "localhost";
const PORT: u16 = 4223;
const UID: &str = "XYZ"; // Change XYZ to the UID of your OLED 128x64 Bricklet.
const WIDTH: usize = 128;
const HEIGHT: usize = 64;

fn draw_matrix(oled: &Oled128x64Bricklet, pixels: [[bool; WIDTH]; HEIGHT]) {
    let mut pages = [[0u8; WIDTH]; HEIGHT / 8];
    for (col_idx, col) in pages.iter_mut().enumerate() {
        for (row_idx, byte) in col.iter_mut().enumerate() {
            for bit in 0..8 {
                if pixels[col_idx * 8 + bit][row_idx] {
                    *byte |= 1 << bit;
                }
            }
        }
    }

    oled.new_window(0, (WIDTH - 1) as u8, 0, (HEIGHT / 8 - 1) as u8);

    for row in 0..HEIGHT / 8 {
        for col in (0..WIDTH).step_by(64) {
            let mut arr = [0u8; 64];
            arr.copy_from_slice(&pages[row][col..col + 64]);
            oled.write(arr);
        }
    }
}

fn main() -> Result<(), Box<dyn Error>> {
    let ipcon = IpConnection::new(); // Create IP connection.
    let oled = Oled128x64Bricklet::new(UID, &ipcon); // Create device object.

    ipcon.connect((HOST, PORT)).recv()??; // Connect to brickd.
                                          // Don't use device before ipcon is connected.

    // Clear display
    oled.clear_display();

    // Draw checkerboard pattern
    let mut pixels = [[false; WIDTH]; HEIGHT];
    for (row_idx, row) in pixels.iter_mut().enumerate() {
        for (col_idx, pixel) in row.iter_mut().enumerate() {
            *pixel = (row_idx / 8) % 2 == (col_idx / 8) % 2;
        }
    }

    draw_matrix(&oled, pixels);

    println!("Press enter to exit.");
    let mut _input = String::new();
    io::stdin().read_line(&mut _input)?;
    ipcon.disconnect();
    Ok(())
}

API

To allow non-blocking usage, nearly every function of the Rust bindings returns a wrapper around a mpsc::Receiver. To block until the function has finished and get your result, call one of the receiver's recv variants. Those return either the result sent by the device, or any error occurred.

Functions returning a result directly will block until the device has finished processing the request.

All functions listed below are thread-safe, those which return a receiver are lock-free.

Basic Functions

pub fn Oled128x64Bricklet::new(uid: &str, ip_connection: &IpConnection) → Oled128x64Bricklet
Parameters:
  • uid – Type: &str
  • ip_connection – Type: &IPConnection
Returns:
  • oled_128x64 – Type: Oled128x64Bricklet

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

let oled_128x64 = Oled128x64Bricklet::new("YOUR_DEVICE_UID", &ip_connection);

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

pub fn Oled128x64Bricklet::write(&self, data: [u8; 64]) → ConvertingReceiver<()>
Parameters:
  • data – Type: [u8; 64], Range: [0 to 255]

Appends 64 byte of data to the window as set by Oled128x64Bricklet::new_window.

Each row has a height of 8 pixels which corresponds to one byte of data.

Example: if you call Oled128x64Bricklet::new_window with column from 0 to 127 and row from 0 to 7 (the whole display) each call of Oled128x64Bricklet::write (red arrow) will write half of a row.

Display pixel order

The LSB (D0) of each data byte is at the top and the MSB (D7) is at the bottom of the row.

The next call of Oled128x64Bricklet::write will write the second half of the row and the next two the second row and so on. To fill the whole display you need to call Oled128x64Bricklet::write 16 times.

pub fn Oled128x64Bricklet::new_window(&self, column_from: u8, column_to: u8, row_from: u8, row_to: u8) → ConvertingReceiver<()>
Parameters:
  • column_from – Type: u8, Range: [0 to 127]
  • column_to – Type: u8, Range: [0 to 127]
  • row_from – Type: u8, Range: [0 to 7]
  • row_to – Type: u8, Range: [0 to 7]

Sets the window in which you can write with Oled128x64Bricklet::write. One row has a height of 8 pixels.

pub fn Oled128x64Bricklet::clear_display(&self) → ConvertingReceiver<()>

Clears the current content of the window as set by Oled128x64Bricklet::new_window.

pub fn Oled128x64Bricklet::write_line(&self, line: u8, position: u8, text: String) → ConvertingReceiver<()>
Parameters:
  • line – Type: u8, Range: [0 to 7]
  • position – Type: u8, Range: [0 to 25]
  • text – Type: String, Length: up to 26

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

For example: (1, 10, "Hello") will write Hello in the middle of the second line of the display.

You can draw to the display with Oled128x64Bricklet::write and then add text to it afterwards.

The display uses a special 5x7 pixel charset. You can view the characters of the charset in Brick Viewer.

The font conforms to code page 437.

Advanced Functions

pub fn Oled128x64Bricklet::set_display_configuration(&self, contrast: u8, invert: bool) → ConvertingReceiver<()>
Parameters:
  • contrast – Type: u8, Range: [0 to 255], Default: 143
  • invert – Type: bool, Default: false

Sets the configuration of the display.

You can set a contrast value from 0 to 255 and you can invert the color (black/white) of the display.

pub fn Oled128x64Bricklet::get_display_configuration(&self) → ConvertingReceiver<DisplayConfiguration>
Return Object:
  • contrast – Type: u8, Range: [0 to 255], Default: 143
  • invert – Type: bool, Default: false

Returns the configuration as set by Oled128x64Bricklet::set_display_configuration.

pub fn Oled128x64Bricklet::get_identity(&self) → ConvertingReceiver<Identity>
Return Object:
  • uid – Type: String, Length: up to 8
  • connected_uid – Type: String, Length: up to 8
  • position – Type: char, Range: ['a' to 'h', 'z']
  • hardware_version – Type: [u8; 3]
    • 0: major – Type: u8, Range: [0 to 255]
    • 1: minor – Type: u8, Range: [0 to 255]
    • 2: revision – Type: u8, Range: [0 to 255]
  • firmware_version – Type: [u8; 3]
    • 0: major – Type: u8, Range: [0 to 255]
    • 1: minor – Type: u8, Range: [0 to 255]
    • 2: revision – Type: u8, Range: [0 to 255]
  • device_identifier – Type: u16, Range: [0 to 216 - 1]

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.

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.

pub fn Oled128x64Bricklet::get_api_version(&self) → [u8; 3]
Return Object:
  • api_version – Type: [u8; 3]
    • 0: major – Type: u8, Range: [0 to 255]
    • 1: minor – Type: u8, Range: [0 to 255]
    • 2: revision – Type: u8, Range: [0 to 255]

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.

pub fn Oled128x64Bricklet::get_response_expected(&mut self, function_id: u8) → bool
Parameters:
  • function_id – Type: u8, Range: See constants
Returns:
  • response_expected – Type: bool

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 Oled128x64Bricklet::set_response_expected. 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 function_id:

  • OLED_128X64_BRICKLET_FUNCTION_WRITE = 1
  • OLED_128X64_BRICKLET_FUNCTION_NEW_WINDOW = 2
  • OLED_128X64_BRICKLET_FUNCTION_CLEAR_DISPLAY = 3
  • OLED_128X64_BRICKLET_FUNCTION_SET_DISPLAY_CONFIGURATION = 4
  • OLED_128X64_BRICKLET_FUNCTION_WRITE_LINE = 6
pub fn Oled128x64Bricklet::set_response_expected(&mut self, function_id: u8, response_expected: bool) → ()
Parameters:
  • function_id – Type: u8, Range: See constants
  • response_expected – Type: bool

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 function_id:

  • OLED_128X64_BRICKLET_FUNCTION_WRITE = 1
  • OLED_128X64_BRICKLET_FUNCTION_NEW_WINDOW = 2
  • OLED_128X64_BRICKLET_FUNCTION_CLEAR_DISPLAY = 3
  • OLED_128X64_BRICKLET_FUNCTION_SET_DISPLAY_CONFIGURATION = 4
  • OLED_128X64_BRICKLET_FUNCTION_WRITE_LINE = 6
pub fn Oled128x64Bricklet::set_response_expected_all(&mut self, response_expected: bool) → ()
Parameters:
  • response_expected – Type: bool

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

Constants

pub const Oled128x64Bricklet::DEVICE_IDENTIFIER

This constant is used to identify a OLED 128x64 Bricklet.

The Oled128x64Bricklet::get_identity function and the IpConnection::get_enumerate_callback_receiver callback of the IP Connection have a device_identifier parameter to specify the Brick's or Bricklet's type.

pub const Oled128x64Bricklet::DEVICE_DISPLAY_NAME

This constant represents the human readable name of a OLED 128x64 Bricklet.