This is the description of the C/C++ 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 C/C++ API bindings is part of their general description.
The example code below is Public Domain (CC0 1.0).
Download (example_hello_world.c)
1#include <stdio.h>
2
3#include "ip_connection.h"
4#include "bricklet_oled_64x48.h"
5
6#define HOST "localhost"
7#define PORT 4223
8#define UID "XYZ" // Change XYZ to the UID of your OLED 64x48 Bricklet
9
10int main(void) {
11 // Create IP connection
12 IPConnection ipcon;
13 ipcon_create(&ipcon);
14
15 // Create device object
16 OLED64x48 oled;
17 oled_64x48_create(&oled, UID, &ipcon);
18
19 // Connect to brickd
20 if(ipcon_connect(&ipcon, HOST, PORT) < 0) {
21 fprintf(stderr, "Could not connect\n");
22 return 1;
23 }
24 // Don't use device before ipcon is connected
25
26 // Clear display
27 oled_64x48_clear_display(&oled);
28
29 // Write "Hello World" starting from upper left corner of the screen
30 oled_64x48_write_line(&oled, 0, 0, "Hello World");
31
32 printf("Press key to exit\n");
33 getchar();
34 oled_64x48_destroy(&oled);
35 ipcon_destroy(&ipcon); // Calls ipcon_disconnect internally
36 return 0;
37}
Download (example_pixel_matrix.c)
1#include <stdio.h>
2
3#include "ip_connection.h"
4#include "bricklet_oled_64x48.h"
5
6#define HOST "localhost"
7#define PORT 4223
8#define UID "XYZ" // Change XYZ to the UID of your OLED 64x48 Bricklet
9#define WIDTH 64
10#define HEIGHT 48
11
12void draw_matrix(OLED64x48 *oled, bool pixels[HEIGHT][WIDTH]) {
13 uint8_t pages[HEIGHT / 8][WIDTH];
14 int row, column, bit;
15
16 // Convert pixels into pages
17 for (row = 0; row < HEIGHT / 8; row++) {
18 for (column = 0; column < WIDTH; column++) {
19 pages[row][column] = 0;
20
21 for (bit = 0; bit < 8; bit++) {
22 if (pixels[(row * 8) + bit][column]) {
23 pages[row][column] |= 1 << bit;
24 }
25 }
26 }
27 }
28
29 // Write all pages
30 oled_64x48_new_window(oled, 0, WIDTH - 1, 0, HEIGHT / 8 - 1);
31
32 for (row = 0; row < HEIGHT / 8; row++) {
33 oled_64x48_write(oled, pages[row]);
34 }
35}
36
37int main(void) {
38 // Create IP connection
39 IPConnection ipcon;
40 ipcon_create(&ipcon);
41
42 // Create device object
43 OLED64x48 oled;
44 oled_64x48_create(&oled, UID, &ipcon);
45
46 // Connect to brickd
47 if(ipcon_connect(&ipcon, HOST, PORT) < 0) {
48 fprintf(stderr, "Could not connect\n");
49 return 1;
50 }
51 // Don't use device before ipcon is connected
52
53 // Clear display
54 oled_64x48_clear_display(&oled);
55
56 // Draw checkerboard pattern
57 int row, column;
58 bool pixels[HEIGHT][WIDTH];
59
60 for (row = 0; row < HEIGHT; row++) {
61 for (column = 0; column < WIDTH; column++) {
62 pixels[row][column] = (row / 8) % 2 == (column / 8) % 2;
63 }
64 }
65
66 draw_matrix(&oled, pixels);
67
68 printf("Press key to exit\n");
69 getchar();
70 oled_64x48_destroy(&oled);
71 ipcon_destroy(&ipcon); // Calls ipcon_disconnect internally
72 return 0;
73}
1#include <stdio.h>
2#include <math.h>
3#include <gd.h> // https://libgd.github.io/
4
5#define IPCON_EXPOSE_MILLISLEEP
6
7#include "ip_connection.h"
8#include "bricklet_oled_64x48.h"
9
10#define HOST "localhost"
11#define PORT 4223
12#define UID "XYZ" // Change XYZ to the UID of your OLED 64x48 Bricklet
13#define WIDTH 64
14#define HEIGHT 48
15
16void draw_image(OLED64x48 *oled, gdImagePtr image) {
17 uint8_t pages[HEIGHT / 8][WIDTH];
18 int row, column, bit, index;
19
20 // Convert pixels into pages
21 for (row = 0; row < HEIGHT / 8; row++) {
22 for (column = 0; column < WIDTH; column++) {
23 pages[row][column] = 0;
24
25 for (bit = 0; bit < 8; bit++) {
26 index = gdImageGetPixel(image, column, (row * 8) + bit);
27
28 if (gdImageRed(image, index) > 0) {
29 pages[row][column] |= 1 << bit;
30 }
31 }
32 }
33 }
34
35 // Write all pages
36 oled_64x48_new_window(oled, 0, WIDTH - 1, 0, HEIGHT / 8 - 1);
37
38 for (row = 0; row < HEIGHT / 8; row++) {
39 oled_64x48_write(oled, pages[row]);
40 }
41}
42
43int main(void) {
44 // Create IP connection
45 IPConnection ipcon;
46 ipcon_create(&ipcon);
47
48 // Create device object
49 OLED64x48 oled;
50 oled_64x48_create(&oled, UID, &ipcon);
51
52 // Connect to brickd
53 if(ipcon_connect(&ipcon, HOST, PORT) < 0) {
54 fprintf(stderr, "Could not connect\n");
55 return 1;
56 }
57 // Don't use device before ipcon is connected
58
59 // Clear display
60 oled_64x48_clear_display(&oled);
61
62 // Draw rotating line
63 gdImagePtr image = gdImageCreate(WIDTH, HEIGHT);
64 int black = gdImageColorAllocate(image, 0, 0, 0);
65 int white = gdImageColorAllocate(image, 255, 255, 255);
66 int origin_x = WIDTH / 2;
67 int origin_y = HEIGHT / 2;
68 int length = HEIGHT / 2 - 2;
69 int angle = 0;
70
71 printf("Press ctrl+c exit\n");
72
73 while (true) {
74 double radians = M_PI * angle / 180.0;
75 int x = (int)(origin_x + length * cos(radians));
76 int y = (int)(origin_y + length * sin(radians));
77
78 gdImageFilledRectangle(image, 0, 0, WIDTH, HEIGHT, black);
79 gdImageLine(image, origin_x, origin_y, x, y, white);
80
81 draw_image(&oled, image);
82 millisleep(25);
83
84 angle++;
85 }
86
87 gdImageDestroy(image);
88 ipcon_destroy(&ipcon); // Calls ipcon_disconnect internally
89 return 0;
90}
Most functions of the C/C++ bindings return an error code (e_code).
Data returned from the device, when a getter is called,
is handled via output parameters. These parameters are labeled with the
ret_ prefix.
Possible error codes are:
E_OK = 0
E_TIMEOUT = -1
E_NO_STREAM_SOCKET = -2
E_HOSTNAME_INVALID = -3
E_NO_CONNECT = -4
E_NO_THREAD = -5
E_NOT_ADDED = -6 (unused since C/C++ bindings version 2.0.0)
E_ALREADY_CONNECTED = -7
E_NOT_CONNECTED = -8
E_INVALID_PARAMETER = -9
E_NOT_SUPPORTED = -10
E_UNKNOWN_ERROR_CODE = -11
E_STREAM_OUT_OF_SYNC = -12
E_INVALID_UID = -13
E_NON_ASCII_CHAR_IN_SECRET = -14
E_WRONG_DEVICE_TYPE = -15
E_DEVICE_REPLACED = -16
E_WRONG_RESPONSE_LENGTH = -17
as defined in ip_connection.h.
All functions listed below are thread-safe.
| Parameters: |
|
|---|
Creates the device object oled_64x48 with the unique device ID uid and adds
it to the IPConnection ipcon:
OLED64x48 oled_64x48;
oled_64x48_create(&oled_64x48, "YOUR_DEVICE_UID", &ipcon);
This device object can be used after the IP connection has been connected.
| Parameters: |
|
|---|
Removes the device object oled_64x48 from its IPConnection and destroys it.
The device object cannot be used anymore afterwards.
| Parameters: |
|
|---|---|
| Returns: |
|
Appends 64 byte of data to the window as set by oled_64x48_new_window().
Each row has a height of 8 pixels which corresponds to one byte of data.
Example: if you call oled_64x48_new_window() with column from 0 to 63 and row
from 0 to 5 (the whole display) each call of oled_64x48_write() (red arrow) will
write one row.
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 oled_64x48_write() will write the second row and so on. To
fill the whole display you need to call oled_64x48_write() 6 times.
| Parameters: |
|
|---|---|
| Returns: |
|
Sets the window in which you can write with oled_64x48_write(). One row
has a height of 8 pixels.
| Parameters: |
|
|---|---|
| Returns: |
|
Clears the current content of the window as set by oled_64x48_new_window().
| Parameters: |
|
|---|---|
| Returns: |
|
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 oled_64x48_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.
| Parameters: |
|
|---|---|
| Returns: |
|
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.
| Parameters: |
|
|---|---|
| Output Parameters: |
|
| Returns: |
|
Returns the configuration as set by oled_64x48_set_display_configuration().
| Parameters: |
|
|---|---|
| Output Parameters: |
|
| Returns: |
|
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 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.
| Parameters: |
|
|---|---|
| Output Parameters: |
|
| Returns: |
|
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.
| Parameters: |
|
|---|---|
| Output Parameters: |
|
| Returns: |
|
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
oled_64x48_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_64X48_FUNCTION_WRITE = 1
OLED_64X48_FUNCTION_NEW_WINDOW = 2
OLED_64X48_FUNCTION_CLEAR_DISPLAY = 3
OLED_64X48_FUNCTION_SET_DISPLAY_CONFIGURATION = 4
OLED_64X48_FUNCTION_WRITE_LINE = 6
| Parameters: |
|
|---|---|
| Returns: |
|
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_64X48_FUNCTION_WRITE = 1
OLED_64X48_FUNCTION_NEW_WINDOW = 2
OLED_64X48_FUNCTION_CLEAR_DISPLAY = 3
OLED_64X48_FUNCTION_SET_DISPLAY_CONFIGURATION = 4
OLED_64X48_FUNCTION_WRITE_LINE = 6
| Parameters: |
|
|---|---|
| Returns: |
|
Changes the response expected flag for all setter and callback configuration functions of this device at once.
This constant is used to identify a OLED 64x48 Bricklet.
The oled_64x48_get_identity() function and the IPCON_CALLBACK_ENUMERATE
callback of the IP Connection have a device_identifier parameter to specify
the Brick's or Bricklet's type.
This constant represents the human readable name of a OLED 64x48 Bricklet.