ssd1351
v2.0.0
Published
Node.js module for controlling [OLED SSD1351](https://www.newhavendisplay.com/app_notes/SSD1351.pdf) devices.
Downloads
8
Readme
ssd1351
Node.js module for controlling OLED SSD1351 devices.
Prerequisites
Wiring
| SSD1351 pin |Raspberry Pi pin|Raspberry Pi GPIO| |----------------|:---------------|:----------------| | GND | 20 | | | VCC (3,3 V) | 17 | | | SCL (SPI_CLK) | 23 | 11 | | SDA (SPI_MOSI) | 19 | 10 | | RES | 22 | 25 | | DC | 18 | 24 | | CS (SPI_CEO_N) | 24 | 8 |
Installation
npm install ssd1351
Available Properties
RawData
Sets or gets the array of bits to be displayed on the OLED screen.
Remark : This method can be used to display an image directly on the oled screen. In the below example, the library jimp - MIT is used to resize the picture.
Usage:
const Jimp = require("jimp");
const height = 128, width = 128;
let i = 0;
const pixelsBuffer = Array.from({ length: height * width * 2 });
const myImage = await Jimp.read(path.join(__dirname, "testPicture.png"));
await myImage.rgba(false);
myImage.resize(height, width)
.scan(0, 0, height, width, function (x, y, idx) {
const bytes = Ssd1351.convertRgbColourToRgb565(this.bitmap.data[idx + 0], this.bitmap.data[idx + 1], this.bitmap.data[idx + 2], this.bitmap.data[idx + 3]);
pixelsBuffer[idx / 2] = bytes[0];
pixelsBuffer[idx / 2 + 1] = bytes[1];
if (0 === idx) {
console.info('convert rgb colour to rgb 565 bit colour');
}
else if (height * width === idx / 4) {
resolve(pixelsBuffer);
}
});
console.info('draw image');
ssd1351.RawData = pixelsBuffer;
const pixelsBuffer = ssd1351.RawData;
await ssd1351.setCursor(0, 0);
await ssd1351.updateScreen();
Available Methods
convertHexColourToRgb
Converts hexadecimal colour code to a rgb colour code.
Usage:
Ssd1351.convertHexColourToRgb('#FF530D'); // Returns { r: 255, g: 83, b: 13 }
convertRgbColourToRgb565
Converts rgb colour code to the a 16 bits colour code compatible with the oled screen.
Usage:
Ssd1351.convertRgbColourToRgb565(128, 128, 128); // Returns [ 132, 16 ]
drawCanvas
Converts a canvas generated by Cairo to a bytes array and saves it in the application memory buffer.
Remark : This method only writes the string in the application buffer. Use updateScreen to update the oled display content.
Usage:
//Creates a canvas context using 'canvas'
const { createCanvas } = require('canvas');
const ssd1351 = new Ssd1351();
ssd1351.clearDisplay();
const canvas = createCanvas(128, 128);
const ctx = canvas.getContext('2d', { pixelFormat: 'RGB16_565' });
const textToDisplay = "myDisplayedText";
const textMeasures = ctx.measureText(textToDisplay);
ctx.fillText(textToDisplay, 0, textMeasures.actualBoundingBoxAscent);
//Copy the canvas image to the application memory buffer
ssd1351.drawCanvas(ctx)
drawLine
Draws a line with the specified colour and saves it in the application memory buffer.
Remark : This method only writes the string in the application buffer. Use updateScreen to update the oled display content.
Usage:
await ssd1351.drawLine(0, 0, 127, 127); // Draws a white line from the top left corner of the screen to the bottom right.
await ssd1351.drawLine(0, 0, 127, 127,Ssd1351.convertHexColourToRgb('#FF530D')); // Draws a red line from the top left corner of the screen to the bottom right.
drawCircle
Draws a circle with the specified colour and saves it in the application memory buffer.
Remark : This method only writes the string in the application buffer. Use updateScreen to update the oled display content.
Usage:
ssd1351.drawCircle(63, 63, 63); // Draws a white circle.
ssd1351.drawCircle(63, 63, 63, Ssd1351.convertHexColourToRgb('#FF530D')); // Draws a red circle.
fillCircle
Fills the interior of a circle with the specified colour and saves it in the application memory buffer.
Remark : This method only writes the string in the application buffer. Use updateScreen to update the oled display content.
Usage:
ssd1351.fillCircle(63, 63, 32); // Colours in white the interior of the circle.
ssd1351.fillCircle(63, 63, 32, Ssd1351.convertHexColourToRgb('#FF530D')); // Colours in red the interior of the circle.
drawRectangle
Draws a rectangle with the specified colour and saves it in the application memory buffer.
Remark : This method only writes the string in the application buffer. Use updateScreen to update the oled display content.
Usage:
ssd1351.drawRectangle(32, 32, 64, 64); // Draws the white borders of the rectangle.
ssd1351.drawRectangle(32, 32, 64, 64, Ssd1351.convertHexColourToRgb('#FF530D')); // Draws the red borders of the rectangle.
fillRectangle
Fills the interior of a rectangle with the specified colour and saves it in the application memory buffer.
Remark : This method only writes the string in the application buffer. Use updateScreen to update the oled display content.
Usage:
ssd1351.fillRectangle(48, 48, 32, 32); // Colours in white the interior of the rectangle.
ssd1351.fillRectangle(48, 48, 32, 32,Ssd1351.convertHexColourToRgb('#FF530D')); // Colours in red the interior of the rectangle.
getCursor
Gets current cursor position as an object {x,y}.
Usage:
ssd1351.getCursor(); // Returns for example{ x:64, y: 40}
setCursor
Sets the current cursor position.
Usage:
ssd1351.setCursor(0, 0); // Sets the cursor position : 1 row, 1 column
setVerticalScroll
Sets the row to be displayed at the top of the screen. This method can be used to scroll vertically the content of the screen.
Usage:
await ssd1351.setVerticalScroll(10);
turnOffDisplay
Turn off the display.
Usage:
await ssd1351.turnOffDisplay();
turnOnDisplay
Turns on the display. Per default the contrast is set to the maximum.
Usage:
const Ssd1351 = require('ssd1351').Ssd1351;
const ssd1351 = new Ssd1351();
await ssd1351.turnOnDisplay(); // Maximum brightness
await ssd1351.turnOnDisplay(0x10); // Reduced brightness
updateScreen
Writes the application buffer to the oled GDDRAM (Graphic Display Data RAM). Call this method to update the oled display content.
Usage:
await ssd1351.updateScreen(); // The display is refreshed
writeString
Writes the specified string given in parameter at the current cursor position. The parameters colour,wrap, padding and background colour are optional. Remark : This method only writes the string in the application buffer. Use updateScreen to update the oled display content.
Usage:
await ssd1351.writeString(oledFont5x7, 4, '12:12', { r: 255, g: 255, b: 255 }); // Writes in white the string 12:12 with the pixel font 'oledFont5x7', the character size '4'
await ssd1351.writeString(oledFont5x7, 4, '12:12', { r: 255, g: 255, b: 255 },undefined,undefined,{ r: 128, g: 128 , b: 128 }); // Writes in white the string 12:12 with the pixel font 'oledFont5x7', the character size '4'.
Credits
- canvas to convert the canvas to a bytes array - MIT
- Roboto font to display the default outline font - Apache v2
- spi-device to send spi messages - MIT
- oled-font-5x7 to display string using a 5 x 7 system font - MIT
- oled-i2c-bus for the idea and display algorithms - MIT
- onoff to control the spi device - MIT
- chai for unit testing - MIT
- jimp for creating samples with bitmap images - MIT
- mocha for unit testing - MIT
- nodemon for testing - MIT
- nyc for unit testing - ISC
- rewire for unit testing - MIT
- weather-icons for creating sample with fonts - code MIT and icons SIL OFL 1.1