NodeJS module for controlling oled devices on the Raspbery Pi (including the SSD1306 and SH1106 OLED screens)
npm install oled-rpi-i2c-bus!‘npm version’ !‘downloads over month’
OLED JS Pi over i2c-bus
========================
This is fork of package oled-js-pi that works thru i2c-bus package and not use package i2c.
A NodeJS driver for I2C/SPI compatible monochrome OLED screens; to be used on the Raspberry Pi! Works with 128 x 32, 128 x 64 and 96 x 16 sized screens, of the SSD1306/SH1106 OLED/PLED Controller (read the datasheet here).
This based on the Blog Post and code by Suz Hinton - Read her blog post about how OLED screens work!
OLED screens are really cool - now you can control them with JavaScript!
Raspberry Pi allows for software I2C. To enable software I2C, add dtoverlay=i2c-gpio,bus=3 to /boot.config.txt. The software I2C would be available on bus no 3
where the SDA is on pin GPIO23/BCM 16 and SCK is on pun GPIO24/BCM 18.
If you haven't already, install NodeJS.
npm install oled-i2c-bus
For SH1106, if you get an error:
```
"Error: , Remote I/O error"
You might have to lower the baudrate by adding the following line to /boot/config.txt and rebooting the Pi``
dtparam=i2c_baudrate=10000
This is a known issue with Raspberry Pi as noted in Raspberry Pi I2C hardware bug. Alternatively, use software I2C.
`javascript
var i2c = require('i2c-bus');
var oled = require('oled-i2c-bus');
var opts = {
width: 128,
height: 64,
address: 0x3D,
bus: 1,
driver:"SSD1306"
};
var i2cbus = i2c.openSync(opts.bus)
var oled = new oled(i2cBus, opts);
// do cool oled things here
`
Usage:
`javascript`
oled.clearDisplay();
Usage:
`javascript`
oled.dimDisplay(true|false);
Usage:
`javascript`
oled.invertDisplay(true|false);
Usage:
`javascript`
oled.turnOffDisplay();
Usage:
`javascript`
oled.turnOnDisplay();
Each pixel needs an x position, a y position, and a color. Colors can be specified as either 0 for 'off' or black, and 1 or 255 for 'on' or white.
Optional bool as last argument specifies whether screen updates immediately with result. Default is true.
Usage:
`javascript`
// draws 4 white pixels total
// format: [x, y, color]
oled.drawPixel([
[128, 1, 1],
[128, 32, 1],
[128, 16, 1],
[64, 16, 1]
]);
Arguments:
+ int x0, y0 - start location of line
+ int x1, y1 - end location of line
+ int color - can be specified as either 0 for 'off' or black, and 1 or 255 for 'on' or white.
Optional bool as last argument specifies whether screen updates immediately with result. Default is true.
Usage:
`javascript`
// args: (x0, y0, x1, y1, color)
oled.drawLine(1, 1, 128, 32, 1);
Arguments:
+ int x0, y0 - top left corner of rectangle
+ int w, h - width and height of rectangle
+ int color - can be specified as either 0 for 'off' or black, and 1 or 255 for 'on' or white.
Optional bool as last argument specifies whether screen updates immediately with result. Default is true.
Usage:
`javascript`
// args: (x0, y0, x1, y1, color)
oled.fillRect(1, 1, 10, 20, 1);
Optional bool as last argument specifies whether screen updates immediately with result. Default is true.
Tip: use a NodeJS image parser to get the pixel data, such as pngparse. A demonstration of using this is below.
Example usage:
``
npm install pngparse
`javascript
var pngparse = require('pngparse');
pngparse.parseFile('indexed_file.png', function(err, image) {
oled.drawBitmap(image.data);
});
`
This method is provided as a primitive convenience. A better way to display images is to use NodeJS package png-to-lcd instead. It's just as easy to use as drawBitmap, but is compatible with all image depths (lazy is good!). It will also auto-dither if you choose. You should still resize your image to your screen dimensions. This alternative method is covered below:
``
npm install png-to-lcd
`javascript
var pngtolcd = require('png-to-lcd');
pngtolcd('nyan-cat.png', true, function(err, bitmap) {
oled.buffer = bitmap;
oled.update();
});
`
Use a library such as pngjs to read a png
file into the required rgba data structure.
Example:
`JavaScript
const fs = require('fs');
const PNG = require('pngjs').PNG;
const i2c = require('i2c-bus');
const oled = require('oled-i2c-bus');
var i2cBus = i2c.openSync(0);
var opts = {
width: 128,
height: 64,
address: 0x3C
};
var display = new oled(i2cBus, opts);
display.clearDisplay();
display.turnOnDisplay();
fs.createReadStream('./test.png')
.pipe(new PNG({ filterType: 4 }))
.on('parsed', function () {
setInterval(() => { drawImage(this) }, 1000);
});
function drawImage(image) {
let x = Math.floor(Math.random() * (display.WIDTH) - image.width / 2);
let y = Math.floor(Math.random() * (display.HEIGHT) - image.height / 2);
display.drawRGBAImage(image, x, y);
}
`
Usage:
`javascript`
// args: (direction, start, stop)
oled.startscroll('left', 0, 15); // this will scroll an entire 128 x 32 screen
Usage:
`javascript`
oled.stopscroll();
Call setCursor just before writeString().
Usage:
`javascript`
// sets cursor to x = 1, y = 1
oled.setCursor(1, 1);
Arguments:
+ obj font - font object in JSON format (see note below on sourcing a font)
+ int size - font size, as multiplier. Eg. 2 would double size, 3 would triple etc.
+ string text - the actual text you want to show on the display.
+ int color - color of text. Can be specified as either 0 for 'off' or black, and 1 or 255 for 'on' or white.
+ bool wrapping - true applies word wrapping at the screen limit, false for no wrapping. If a long string without spaces is supplied as the text, just letter wrapping will apply instead.
Optional bool as last argument specifies whether screen updates immediately with result. Default is true.
Before all of this text can happen, you need to load a font buffer for use. A good font to start with is NodeJS package oled-font-5x7.
Usage:
``
npm install oled-font-5x7
`javascript
var font = require('oled-font-5x7');
// sets cursor to x = 1, y = 1
oled.setCursor(1, 1);
oled.writeString(font, 1, 'Cats and dogs are really cool animals, you know.', 1, true);
`
Checkout https://www.npmjs.com/package/oled-font-pack for all-in-one font package.
Usage:
`javascript`
oled.update();
usage:
`javascript`
// args: (x,y,percentage)
oled.battery(1,1,20);
javascript
//args: (x,y)
oled.bluetooth(1,1);
`
$3
Draw a WiFi signal strength in percentage indicator. This method allows for up to 4 different signal strength of the WiFi signal:
- 0 bar : signal < 10%
- 1 bar : 10% >= signal < 40%
- 2 bar : 40% >= signal < 70%
- 3 bar : signal >= 70%
Arguments:
* int x - start column
* int y - start row
* int percentage - signal strength in percentage usage:
`javascript
// args: (x,y,percentage)
oled.wifi(1,1,20);
` $3
A wrapper for drawRGBAImage that supports a fix animation. The animation always start from x=1 and y=1. Arguments:
* int x - start column (ignored on
animation = true)
* int y - start row (ignored on animation=true)
* string image - full path to the image or the filename of the image in the resources folder
* object font - font to draw "error" message
* boolean clear - clear the display before the draw
* boolean reset - stop all animations
* boolean animated - enable/disable animation
* boolean wrapping - enable/disable of the error message wrapping usage:
`javascript
var font = require('oled-font-pack')
oled.image(1,1,'rpi-frambuesa.png',font.oled_5x7,true,false,false,true);
``