Rust - OLED 64x48 Bricklet

This is the description of the Rust API bindings for the OLED 64x48 Bricklet. General information and technical specifications for the OLED 64x48 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_64x48_bricklet::*};

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

fn main() -> Result<(), Box<dyn Error>> {
    let ipcon = IpConnection::new(); // Create IP connection.
    let oled = Oled64x48Bricklet::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();

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

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

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

const HOST: &str = "127.0.0.1";
const PORT: u16 = 4223;
const UID: &str = "XYZ"; // Change XYZ to the UID of your OLED 64x48 Bricklet
const WIDTH: usize = 64;
const HEIGHT: usize = 48;

fn draw_matrix(oled: &Oled64x48Bricklet, 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 {
        oled.write(pages[row]);
    }
}

fn main() -> Result<(), Box<dyn Error>> {
    let ipcon = IpConnection::new(); // Create IP connection
    let oled = Oled64x48Bricklet::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 occured.

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 Oled64x48Bricklet::new(uid: &str, ip_connection: &IpConnection) → Oled64x48Bricklet

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

This device object can be used after the IP connection has been connected (see examples above).

pub fn Oled64x48Bricklet::write(&self, data: [u8; 64]) → ConvertingReceiver<()>

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

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

Example: if you call Oled64x48Bricklet::new_window with column from 0 to 63 and row from 0 to 5 (the whole display) each call of Oled64x48Bricklet::write (red arrow) will write one 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 Oled64x48Bricklet::write will write the second row and so on. To fill the whole display you need to call Oled64x48Bricklet::write 6 times.

pub fn Oled64x48Bricklet::new_window(&self, column_from: u8, column_to: u8, row_from: u8, row_to: u8) → ConvertingReceiver<()>

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

The columns have a range of 0 to 63 and the rows have a range of 0 to 5.

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

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

pub fn Oled64x48Bricklet::write_line(&self, line: u8, position: u8, text: String) → ConvertingReceiver<()>

Writes text to a specific line (0 to 5) with a specific position (0 to 12). The text can have a maximum of 13 characters.

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

You can draw to the display with Oled64x48Bricklet::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.

Advanced Functions

pub fn Oled64x48Bricklet::set_display_configuration(&self, contrast: u8, invert: bool) → ConvertingReceiver<()>

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.

The default values are contrast 143 and inverting off.

pub fn Oled64x48Bricklet::get_display_configuration(&self) → ConvertingReceiver<DisplayConfiguration>

Returns the configuration as set by Oled64x48Bricklet::set_display_configuration.

pub fn Oled64x48Bricklet::get_api_version(&self) → [u8; 3]

Returns the version of the API definition (major, minor, revision) 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 Oled64x48Bricklet::get_response_expected(&mut self, function_id: u8) → 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 Oled64x48Bricklet::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 send and errors are silently ignored, because they cannot be detected.

See Oled64x48Bricklet::set_response_expected for the list of function ID constants available for this function.

pub fn Oled64x48Bricklet::set_response_expected(&mut self, function_id: u8, response_expected: 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 send and errors are silently ignored, because they cannot be detected.

The following function ID constants are available for this function:

  • OLED_64X48_BRICKLET_FUNCTION_WRITE = 1
  • OLED_64X48_BRICKLET_FUNCTION_NEW_WINDOW = 2
  • OLED_64X48_BRICKLET_FUNCTION_CLEAR_DISPLAY = 3
  • OLED_64X48_BRICKLET_FUNCTION_SET_DISPLAY_CONFIGURATION = 4
  • OLED_64X48_BRICKLET_FUNCTION_WRITE_LINE = 6
pub fn Oled64x48Bricklet::set_response_expected_all(&mut self, response_expected: bool) → ()

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

pub fn Oled64x48Bricklet::get_identity(&self) → ConvertingReceiver<Identity>

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' or 'd'.

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

Constants

Oled64x48Bricklet::DEVICE_IDENTIFIER

This constant is used to identify a OLED 64x48 Bricklet.

The Oled64x48Bricklet::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.

Oled64x48Bricklet::DEVICE_DISPLAY_NAME

This constant represents the human readable name of a OLED 64x48 Bricklet.