PHP - OLED 64x48 Bricklet

This is the description of the PHP 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 PHP API bindings is part of their general description.

Examples

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

Hello World

Download (ExampleHelloWorld.php)

 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
<?php

require_once('Tinkerforge/IPConnection.php');
require_once('Tinkerforge/BrickletOLED64x48.php');

use Tinkerforge\IPConnection;
use Tinkerforge\BrickletOLED64x48;

const HOST = 'localhost';
const PORT = 4223;
const UID = 'XYZ'; // Change XYZ to the UID of your OLED 64x48 Bricklet

$ipcon = new IPConnection(); // Create IP connection
$oled = new BrickletOLED64x48(UID, $ipcon); // Create device object

$ipcon->connect(HOST, PORT); // Connect to brickd
// Don't use device before ipcon is connected

// Clear display
$oled->clearDisplay();

// Write "Hello World" starting from upper left corner of the screen
$oled->writeLine(0, 0, 'Hello World');

echo "Press key to exit\n";
fgetc(fopen('php://stdin', 'r'));
$ipcon->disconnect();

?>

Pixel Matrix

Download (ExamplePixelMatrix.php)

 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
<?php

require_once('Tinkerforge/IPConnection.php');
require_once('Tinkerforge/BrickletOLED64x48.php');

use Tinkerforge\IPConnection;
use Tinkerforge\BrickletOLED64x48;

const HOST = 'localhost';
const PORT = 4223;
const UID = 'XYZ'; // Change XYZ to the UID of your OLED 64x48 Bricklet
const WIDTH = 64; // Columns (each 1 pixel wide)
const HEIGHT = 6; // Rows (each 8 pixels high)

function drawMatrix($oled, $startColumn, $startRow, $columnCount, $rowCount, $pixels)
{
    $pages = array(array());

    // Split pixel matrix into 8bit pages
    for ($row = 0; $row < $rowCount; $row++)
    {
        for ($column = 0; $column < $columnCount; $column++)
        {
            $pages[$row][$column] = 0;

            for ($bit = 0; $bit < 8; $bit++)
            {
                if ($pixels[($row * 8) + $bit][$column])
                {
                    $pages[$row][$column] |= 1 << $bit;
                }
            }
        }
    }

    // Merge page matrix into single page array
    $data = array();

    for ($i = 0, $row = 0; $row < $rowCount; $row++)
    {
        for ($column = 0; $column < $columnCount; $column++, $i++)
        {
            $data[$i] = $pages[$row][$column];
        }
    }

    // Set new window
    $oled->newWindow($startColumn, $startColumn + $columnCount - 1,
                     $startRow, $startRow + $rowCount - 1);

    // Write page data in 64 byte blocks
    for ($i = 0; $i < count($data); $i += 64)
    {
        $oled->write(array_pad(array_slice($data, $i, 64), 64, 0));
    }
}

$ipcon = new IPConnection(); // Create IP connection
$oled = new BrickletOLED64x48(UID, $ipcon); // Create device object

$ipcon->connect(HOST, PORT); // Connect to brickd
// Don't use device before ipcon is connected

// Clear display
$oled->clearDisplay();

// Draw checkerboard pattern
$pixels = array(array());

for ($y = 0; $y < HEIGHT * 8; $y++)
{
    for ($x = 0; $x < WIDTH; $x++)
    {
        $pixels[$y][$x] = ($y / 8) % 2 == ($x / 8) % 2;
    }
}

drawMatrix($oled, 0, 0, WIDTH, HEIGHT, $pixels);

echo "Press key to exit\n";
fgetc(fopen('php://stdin', 'r'));
$ipcon->disconnect();

?>

Scribble

Download (ExampleScribble.php)

  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
<?php

if (!extension_loaded('gd')) {
    echo "Required gd extension is not available\n";
    exit;
}

require_once('Tinkerforge/IPConnection.php');
require_once('Tinkerforge/BrickletOLED64x48.php');

use Tinkerforge\IPConnection;
use Tinkerforge\BrickletOLED64x48;

const HOST = 'localhost';
const PORT = 4223;
const UID = 'XYZ'; // Change XYZ to the UID of your OLED 64x48 Bricklet
const WIDTH = 64; // Columns (each 1 pixel wide)
const HEIGHT = 6; // Rows (each 8 pixels high)

function drawImage($oled, $startColumn, $startRow, $columnCount, $rowCount, $image)
{
    $pages = array(array());

    // Split image pixels into 8bit pages
    for ($row = 0; $row < $rowCount; $row++)
    {
        for ($column = 0; $column < $columnCount; $column++)
        {
            $pages[$row][$column] = 0;

            for ($bit = 0; $bit < 8; $bit++)
            {
                $index = imagecolorat($image, $column, ($row * 8) + $bit);
                $rgb = imagecolorsforindex($image, $index);

                if ($rgb['red'] > 0) // Assume a black/white image
                {
                    $pages[$row][$column] |= 1 << $bit;
                }
            }
        }
    }

    // Merge page matrix into single page array
    $data = array();

    for ($i = 0, $row = 0; $row < $rowCount; $row++)
    {
        for ($column = 0; $column < $columnCount; $column++, $i++)
        {
            $data[$i] = $pages[$row][$column];
        }
    }

    // Set new window
    $oled->newWindow($startColumn, $startColumn + $columnCount - 1,
                     $startRow, $startRow + $rowCount - 1);

    // Write page data in 64 byte blocks
    for ($i = 0; $i < count($data); $i += 64)
    {
        $oled->write(array_pad(array_slice($data, $i, 64), 64, 0));
    }
}

$ipcon = new IPConnection(); // Create IP connection
$oled = new BrickletOLED64x48(UID, $ipcon); // Create device object

$ipcon->connect(HOST, PORT); // Connect to brickd
// Don't use device before ipcon is connected

// Clear display
$oled->clearDisplay();

// Draw rotating line
$image = imagecreate(WIDTH, HEIGHT * 8);
$black = imagecolorallocate($image, 0, 0, 0);
$white = imagecolorallocate($image, 255, 255, 255);
$originX = WIDTH / 2;
$originY = HEIGHT * 8 / 2;
$length = HEIGHT * 8 / 2 - 2;
$angle = 0;

echo "Press ctrl+c to exit\n";

while (true)
{
    $radians = M_PI * $angle / 180.0;
    $x = (int)($originX + $length * cos($radians));
    $y = (int)($originY + $length * sin($radians));

    imagefilledrectangle($image, 0, 0, WIDTH, HEIGHT * 8, $black);
    imageline($image, $originX, $originY, $x, $y, $white);

    drawImage($oled, 0, 0, WIDTH, HEIGHT, $image);
    usleep(25*1000);

    $angle++;
}

?>

API

Functions that return multiple values return them in an associative array.

Basic Functions

class BrickletOLED64x48(string $uid, IPConnection $ipcon)
Parameters:
  • $uid – Type: string
  • $ipcon – Type: IPConnection
Returns:
  • $oled_64x48 – Type: BrickletOLED64x48

Creates an object with the unique device ID $uid:

<?php   $oled_64x48 = new BrickletOLED64x48('YOUR_DEVICE_UID', $ipcon);   ?>

This object can then be used after the IP Connection is connected.

void BrickletOLED64x48::write(array $data)
Parameters:
  • $data – Type: array(int, ...), Length: 64, Range: [0 to 255]

Appends 64 byte of data to the window as set by newWindow().

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

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

void BrickletOLED64x48::newWindow(int $column_from, int $column_to, int $row_from, int $row_to)
Parameters:
  • $column_from – Type: int, Range: [0 to 63]
  • $column_to – Type: int, Range: [0 to 63]
  • $row_from – Type: int, Range: [0 to 5]
  • $row_to – Type: int, Range: [0 to 5]

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

void BrickletOLED64x48::clearDisplay()

Clears the current content of the window as set by newWindow().

void BrickletOLED64x48::writeLine(int $line, int $position, string $text)
Parameters:
  • $line – Type: int, Range: [0 to 5]
  • $position – Type: int, Range: [0 to 12]
  • $text – Type: string, Length: up to 13

Writes text to a specific line with a specific position. 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 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

void BrickletOLED64x48::setDisplayConfiguration(int $contrast, bool $invert)
Parameters:
  • $contrast – Type: int, 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.

array BrickletOLED64x48::getDisplayConfiguration()
Return Array:
  • 'contrast' – Type: int, Range: [0 to 255], Default: 143
  • 'invert' – Type: bool, Default: FALSE

Returns the configuration as set by setDisplayConfiguration().

array BrickletOLED64x48::getIdentity()
Return Array:
  • 'uid' – Type: string, Length: up to 8
  • 'connected_uid' – Type: string, Length: up to 8
  • 'position' – Type: string, Range: ['a' to 'h', 'z']
  • 'hardware_version' – Type: array(int, ...), Length: 3
    • 0: $major – Type: int, Range: [0 to 255]
    • 1: $minor – Type: int, Range: [0 to 255]
    • 2: $revision – Type: int, Range: [0 to 255]
  • 'firmware_version' – Type: array(int, ...), Length: 3
    • 0: $major – Type: int, Range: [0 to 255]
    • 1: $minor – Type: int, Range: [0 to 255]
    • 2: $revision – Type: int, Range: [0 to 255]
  • 'device_identifier' – Type: int, 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.

array BrickletOLED64x48::getAPIVersion()
Return Array:
  • $api_version – Type: array(int, ...), Length: 3
    • 0: $major – Type: int, Range: [0 to 255]
    • 1: $minor – Type: int, Range: [0 to 255]
    • 2: $revision – Type: int, 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.

bool BrickletOLED64x48::getResponseExpected(int $function_id)
Parameters:
  • $function_id – Type: int, 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 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 $function_id:

  • BrickletOLED64x48::FUNCTION_WRITE = 1
  • BrickletOLED64x48::FUNCTION_NEW_WINDOW = 2
  • BrickletOLED64x48::FUNCTION_CLEAR_DISPLAY = 3
  • BrickletOLED64x48::FUNCTION_SET_DISPLAY_CONFIGURATION = 4
  • BrickletOLED64x48::FUNCTION_WRITE_LINE = 6
void BrickletOLED64x48::setResponseExpected(int $function_id, bool $response_expected)
Parameters:
  • $function_id – Type: int, 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:

  • BrickletOLED64x48::FUNCTION_WRITE = 1
  • BrickletOLED64x48::FUNCTION_NEW_WINDOW = 2
  • BrickletOLED64x48::FUNCTION_CLEAR_DISPLAY = 3
  • BrickletOLED64x48::FUNCTION_SET_DISPLAY_CONFIGURATION = 4
  • BrickletOLED64x48::FUNCTION_WRITE_LINE = 6
void BrickletOLED64x48::setResponseExpectedAll(bool $response_expected)
Parameters:
  • $response_expected – Type: bool

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

Constants

int BrickletOLED64x48::DEVICE_IDENTIFIER

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

The getIdentity() function and the IPConnection::CALLBACK_ENUMERATE callback of the IP Connection have a deviceIdentifier parameter to specify the Brick's or Bricklet's type.

string BrickletOLED64x48::DEVICE_DISPLAY_NAME

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