This is the description of the JavaScript 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 JavaScript API bindings is part of their general description.
The example code below is Public Domain (CC0 1.0).
Download (ExampleHelloWorld.js)
1var Tinkerforge = require('tinkerforge');
2
3var HOST = 'localhost';
4var PORT = 4223;
5var UID = 'XYZ'; // Change XYZ to the UID of your OLED 64x48 Bricklet
6
7var ipcon = new Tinkerforge.IPConnection(); // Create IP connection
8var oled = new Tinkerforge.BrickletOLED64x48(UID, ipcon); // Create device object
9
10ipcon.connect(HOST, PORT,
11 function (error) {
12 console.log('Error: ' + error);
13 }
14); // Connect to brickd
15// Don't use device before ipcon is connected
16
17ipcon.on(Tinkerforge.IPConnection.CALLBACK_CONNECTED,
18 function (connectReason) {
19 // Clear display
20 oled.clearDisplay();
21
22 // Write "Hello World" starting from upper left corner of the screen
23 oled.writeLine(0, 0, 'Hello World');
24 }
25);
26
27console.log('Press key to exit');
28process.stdin.on('data',
29 function (data) {
30 ipcon.disconnect();
31 process.exit(0);
32 }
33);
Download (ExamplePixelMatrix.js)
1var Tinkerforge = require('tinkerforge');
2
3var HOST = 'localhost';
4var PORT = 4223;
5var UID = 'XYZ'; // Change XYZ to the UID of your OLED 64x48 Bricklet
6var SCREEN_WIDTH = 64;
7var SCREEN_HEIGHT = 48;
8
9var ipcon = new Tinkerforge.IPConnection(); // Create IP connection
10var oled = new Tinkerforge.BrickletOLED64x48(UID, ipcon); // Create device object
11
12function drawMatrix(oled, pixels) {
13 column = [];
14
15 for (var i = 0; i < SCREEN_HEIGHT/8; i++) {
16 column[i] = [];
17 }
18
19 for (var i = 0; i < SCREEN_HEIGHT/8; i++) {
20 for (var j = 0; j < SCREEN_WIDTH; j++) {
21 page = 0;
22
23 for (var k = 0; k < 8; k++) {
24 if (pixels[i*8 + k][j]) {
25 page |= 1 << k;
26 }
27 }
28
29 column[i][j] = page;
30 }
31 }
32
33 oled.newWindow(0, SCREEN_WIDTH-1, 0, 5)
34
35 for (var i = 0; i < SCREEN_HEIGHT/8; i++) {
36 oled.write(column[i])
37 }
38}
39
40ipcon.connect(HOST, PORT,
41 function (error) {
42 console.log('Error: ' + error);
43 }
44); // Connect to brickd
45// Don't use device before ipcon is connected
46
47ipcon.on(Tinkerforge.IPConnection.CALLBACK_CONNECTED,
48 function (connectReason) {
49 // Clear display
50 oled.clearDisplay();
51
52 // Draw checkerboard pattern
53 var pixelMatrix = [];
54 for (var h = 0; h < SCREEN_HEIGHT; h++) {
55 pixelMatrix[h] = [];
56 for (var w = 0; w < SCREEN_WIDTH; w++) {
57 pixelMatrix[h][w] = Math.floor(h / 8) % 2 == Math.floor(w / 8) % 2;
58 }
59 }
60
61 drawMatrix(oled, pixelMatrix);
62 }
63);
64
65console.log('Press key to exit');
66process.stdin.on('data',
67 function (data) {
68 ipcon.disconnect();
69 process.exit(0);
70 }
71);
1var Tinkerforge = require('tinkerforge');
2var GM = require('gm'); // FIXME: maybe use node-gd instead
3var getPixels = require('get-pixels');
4
5var HOST = 'localhost';
6var PORT = 4223;
7var UID = 'XYZ'; // Change XYZ to the UID of your OLED 64x48 Bricklet
8var WIDTH = 64;
9var HEIGHT = 48;
10
11var ipcon = new Tinkerforge.IPConnection(); // Create IP connection
12var oled = new Tinkerforge.BrickletOLED64x48(UID, ipcon); // Create device object
13var originX = WIDTH / 2;
14var originY = HEIGHT / 2;
15var length = HEIGHT / 2 - 2;
16var angle = 0;
17
18function drawImage(oled, image) {
19 // FIXME: GraphicsMagick doesn't seem to have a way to access the individual
20 // pixels. Convert to PNG and then used get-pixels to read the pixels back.
21 // This is far from ideal, but better than nothing.
22 image.toBuffer('PNG', function (err, buffer) {
23 if (err) {
24 console.log('toBuffer: ' + err);
25 return;
26 }
27
28 getPixels(buffer, 'image/png', function(err, pixels) {
29 if (err) {
30 console.log('getPixels: ' + err);
31 return;
32 }
33
34 column = [];
35
36 for (var i = 0; i < HEIGHT/8; i++) {
37 column[i] = [];
38 }
39
40 for (var i = 0; i < HEIGHT/8; i++) {
41 for (var j = 0; j < WIDTH; j++) {
42 page = 0;
43
44 for (var k = 0; k < 8; k++) {
45 if (pixels.get(j, i*8 + k, 0) > 0) {
46 page |= 1 << k;
47 }
48 }
49
50 column[i][j] = page;
51 }
52 }
53
54 oled.newWindow(0, WIDTH-1, 0, 5)
55
56 for (var i = 0; i < HEIGHT/8; i++) {
57 oled.write(column[i])
58 }
59 });
60 });
61}
62
63ipcon.connect(HOST, PORT,
64 function (error) {
65 console.log('Error: ' + error);
66 }
67); // Connect to brickd
68// Don't use device before ipcon is connected
69
70ipcon.on(Tinkerforge.IPConnection.CALLBACK_CONNECTED,
71 function (connectReason) {
72 // Clear display
73 oled.clearDisplay();
74
75 // Draw rotating line
76 function render() {
77 // FIXME: Creating the image once and reusing it would be better, but
78 // on the second call toBuffer() fails with "Stream yields empty buffer"
79 var image = GM(WIDTH, HEIGHT, '#000');
80 var radians = angle * Math.PI / 180;
81 var x = Math.floor(originX + length * Math.cos(radians));
82 var y = Math.floor(originY + length * Math.sin(radians));
83
84 image.fill('#FFF').drawLine(originX, originY, x, y);
85
86 drawImage(oled, image);
87
88 angle++;
89 }
90
91 setInterval(render, 25);
92 }
93);
94
95console.log('Press key to exit');
96process.stdin.on('data',
97 function (data) {
98 ipcon.disconnect();
99 process.exit(0);
100 }
101);
Download (ExampleHelloWorld.html), Test (ExampleHelloWorld.html)
1<!DOCTYPE html>
2<html>
3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4 <head>
5 <title>Tinkerforge | JavaScript Example</title>
6 </head>
7 <body>
8 <div style="text-align:center;">
9 <h1>OLED 64x48 Bricklet Hello World Example</h1>
10 <p>
11 <input value="localhost" id="host" type="text" size="20">:
12 <input value="4280" id="port" type="text" size="5">,
13 <input value="uid" id="uid" type="text" size="5">
14 <input value="Start Example" id="start" type="button" onclick="startExample();">
15 </p>
16 <p>
17 <textarea readonly id="text" cols="80" rows="24" style="resize:none;"
18 >Press "Start Example" to begin ...</textarea>
19 </p>
20 </div>
21 <script src="./Tinkerforge.js" type='text/javascript'></script>
22 <script type='text/javascript'>
23 var ipcon;
24 var textArea = document.getElementById("text");
25 function startExample() {
26 textArea.value = "";
27 var HOST = document.getElementById("host").value;
28 var PORT = parseInt(document.getElementById("port").value);
29 var UID = document.getElementById("uid").value;
30 if(ipcon !== undefined) {
31 ipcon.disconnect();
32 }
33 ipcon = new Tinkerforge.IPConnection(); // Create IP connection
34 var oled = new Tinkerforge.BrickletOLED64x48(UID, ipcon); // Create device object
35 ipcon.connect(HOST, PORT,
36 function(error) {
37 textArea.value += 'Error: ' + error + '\n';
38 }
39 ); // Connect to brickd
40 // Don't use device before ipcon is connected
41
42 ipcon.on(Tinkerforge.IPConnection.CALLBACK_CONNECTED,
43 function (connectReason) {
44 // Clear display
45 oled.clearDisplay();
46
47 // Write "Hello World" starting from upper left corner of the screen
48 oled.writeLine(0, 0, 'Hello World');
49 }
50 );
51 }
52 </script>
53 </body>
54</html>
Download (ExamplePixelMatrix.html), Test (ExamplePixelMatrix.html)
1<!DOCTYPE html>
2<html>
3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4 <head>
5 <title>Tinkerforge | JavaScript Example</title>
6 </head>
7 <body>
8 <div style="text-align:center;">
9 <h1>OLED 64x48 Bricklet Pixel Matrix Example</h1>
10 <p>
11 <input value="localhost" id="host" type="text" size="20">:
12 <input value="4280" id="port" type="text" size="5">,
13 <input value="uid" id="uid" type="text" size="5">
14 <input value="Start Example" id="start" type="button" onclick="startExample();">
15 </p>
16 <p>
17 <textarea readonly id="text" cols="80" rows="24" style="resize:none;"
18 >Press "Start Example" to begin ...</textarea>
19 </p>
20 </div>
21 <script src="./Tinkerforge.js" type='text/javascript'></script>
22 <script type='text/javascript'>
23 var ipcon;
24 var textArea = document.getElementById("text");
25 var SCREEN_WIDTH = 64;
26 var SCREEN_HEIGHT = 48;
27
28 function drawMatrix(oled, pixels) {
29 column = [];
30 for (var i = 0; i < SCREEN_HEIGHT/8; i++) {
31 column[i] = [];
32 }
33 for (var i = 0; i < SCREEN_HEIGHT/8; i++) {
34 for (var j = 0; j < SCREEN_WIDTH; j++) {
35 page = 0;
36
37 for (var k = 0; k < 8; k++) {
38 if (pixels[i*8 + k][j]) {
39 page |= 1 << k;
40 }
41 }
42 column[i][j] = page;
43 }
44 }
45 oled.newWindow(0, SCREEN_WIDTH-1, 0, 5)
46 for (var i = 0; i < 6; i++) {
47 oled.write(column[i])
48 }
49 }
50 function startExample() {
51 textArea.value = "";
52 var HOST = document.getElementById("host").value;
53 var PORT = parseInt(document.getElementById("port").value);
54 var UID = document.getElementById("uid").value;
55 if(ipcon !== undefined) {
56 ipcon.disconnect();
57 }
58 ipcon = new Tinkerforge.IPConnection(); // Create IP connection
59 var oled = new Tinkerforge.BrickletOLED64x48(UID, ipcon); // Create device object
60 ipcon.connect(HOST, PORT,
61 function(error) {
62 textArea.value += 'Error: ' + error + '\n';
63 }
64 ); // Connect to brickd
65 // Don't use device before ipcon is connected
66
67 ipcon.on(Tinkerforge.IPConnection.CALLBACK_CONNECTED,
68 function (connectReason) {
69 // Clear display
70 oled.clearDisplay();
71
72 // Draw checkerboard pattern
73 var pixelMatrix = [];
74 for (var h = 0; h < SCREEN_HEIGHT; h++) {
75 pixelMatrix[h] = [];
76 for (var w = 0; w < SCREEN_WIDTH; w++) {
77 pixelMatrix[h][w] = Math.floor(h / 8) % 2 == Math.floor(w / 8) % 2;
78 }
79 }
80
81 drawMatrix(oled, pixelMatrix);
82 }
83 );
84 }
85 </script>
86 </body>
87</html>
Generally, every function of the JavaScript bindings can take two optional
parameters, returnCallback and errorCallback. These are two user
defined callback functions. The returnCallback function is called with the
results as arguments, if the function returns its results asynchronously. The
errorCallback is called with an error code in case of an error. The error
code can be one of the following values:
IPConnection.ERROR_ALREADY_CONNECTED = 11
IPConnection.ERROR_NOT_CONNECTED = 12
IPConnection.ERROR_CONNECT_FAILED = 13
IPConnection.ERROR_INVALID_FUNCTION_ID = 21
IPConnection.ERROR_TIMEOUT = 31
IPConnection.ERROR_INVALID_PARAMETER = 41
IPConnection.ERROR_FUNCTION_NOT_SUPPORTED = 42
IPConnection.ERROR_UNKNOWN_ERROR = 43
IPConnection.ERROR_STREAM_OUT_OF_SYNC = 51
IPConnection.ERROR_NON_ASCII_CHAR_IN_SECRET = 71
IPConnection.ERROR_WRONG_DEVICE_TYPE = 81
IPConnection.ERROR_DEVICE_REPLACED = 82
IPConnection.ERROR_WRONG_RESPONSE_LENGTH = 83
IPConnection.ERROR_INT64_NOT_SUPPORTED = 91
The namespace for the JavaScript bindings is Tinkerforge.*.
| Parameters: |
|
|---|---|
| Returns: |
|
Creates an object with the unique device ID uid:
var oled64x48 = new BrickletOLED64x48("YOUR_DEVICE_UID", ipcon);
This object can then be used after the IP Connection is connected.
| Parameters: |
|
|---|---|
| Callback Parameters: |
|
| Returns: |
|
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.
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.
| Parameters: |
|
|---|---|
| Callback Parameters: |
|
| Returns: |
|
Sets the window in which you can write with write(). One row
has a height of 8 pixels.
| Callback Parameters: |
|
|---|---|
| Returns: |
|
Clears the current content of the window as set by newWindow().
| Parameters: |
|
|---|---|
| Callback 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 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: |
|
|---|---|
| Callback 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.
| Callback Parameters: |
|
|---|---|
| Returns: |
|
Returns the configuration as set by setDisplayConfiguration().
| Callback 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.
| 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: |
|
|---|---|
| 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
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
| 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:
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
| 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 getIdentity() function and the
IPConnection.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.