Git Product home page Git Product logo

oled-js's Introduction

‘npm version’ ‘downloads over month’

oled js

oled-cat

What is this?

This repo is a library compatible with Rick Waldron's johnny-five project. It adds support for I2C/SPI compatible monochrome OLED screens. Works with 128 x 32, 128 x 64 and 96 x 16 sized screens, of the SSD1306 OLED/PLED Controller (read the datasheet here).

Got a MicroView from GeekAmmo/SparkFun? That'll work too.

Interested in the nerdy bits going on behind the scenes? Read my blog post about how OLED screens work!

OLED screens are really cool - now you can control them with JavaScript!

Install

If you haven't already, install NodeJS and the Arduino IDE to your computer.

  1. npm install oled-js
  2. Upload the Standard Firmata sketch (prepackaged with the Arduino IDE) to your Arduino of choice. This can be found under File > Examples > Firmata > StandardFirmata [ further help]

I2C screens

Hook up I2C compatible oled to the Arduino. If using an Arduino Uno, pins are as follows:

  • SDL to pin A4
  • SCL to pin A5
  • RESET to pin 4

Fritzing diagram is here. Look up the correct pins if using a board other than Arduino.

If you'd like to run the demo:

  1. git clone this repo (get latest release instead of master branch)
  2. npm install
  3. Replace width, height, and other values in the options with your own in tests/demoTime.js
  4. node tests/demoTime.js

I2C example

const five = require('johnny-five');
const Oled = require('oled-js');

const board = new five.Board();

board.on('ready', () => {
  console.log('Connected to Arduino, ready.');

  const opts = {
    width: 128,
    height: 64,
    address: 0x3D
  };

  const oled = new Oled(board, five, opts);
  // do cool oled things here
});

Wait, how do I find out the I2C address of my OLED screen?

Yeah this sounds like a nightmare, but it's pretty simple! Before uploading standard firmata to your Arduino, upload the following sketch from the Arduino Playground called 'I2C scanner'. Does what it says on the box. Open up your serial monitor, and you'll see your device address pop up there. Make a note of it, then re-upload standard firmata to your Arduino again.

SPI screens

IMPORTANT NOTE: Using SPI will make your screen update and draw VERY slow. Manual hardware SPI over USB is the only way currently to do this within Johnny-Five, which is not optimized for the normal speed you can expect from SPI in general. Sorry about that. Here is a video I took to show this.

Hook up SPI compatible oled to the Arduino. If using an Arduino Uno, pins are as follows:

  • Data/MOSI to pin D9
  • CLK to pin D10
  • D/C to pin D11
  • RST to pin D13
  • CS/SS to pin 12 (you can change this one if you really want to)

Fritzing diagram coming soon.

SPI example

const five = require('johnny-five');
const Oled = require('oled-js');

const board = new five.Board();

board.on('ready', () => {
  console.log('Connected to Arduino, ready.');

  const opts = {
    width: 128,
    height: 64,
    secondaryPin: 12
  };

  const oled = new Oled(board, five, opts);
  // do cool oled things here
});

MicroView

MicroView uses SPI, so please see the note about drawing speed in the SPI section above.

This one is pretty simple - use the USB programmer that should have come with your MicroView. Insert the MicroView's header pins into the slots on the USB programmer. Plug it in to your nearest USB port, and you're done! No pin mappings, no sweat.

Microview example

const five = require('johnny-five');
const Oled = require('oled-js');

const board = new five.Board();

board.on('ready', () => {
  console.log('Connected to Arduino, ready.');

  const opts = {
    width: 64,
    height: 48,
    microview: true
  };

  const oled = new Oled(board, five, opts);
  // do cool oled things here
});

Available methods

clearDisplay

Fills the buffer with 'off' pixels (0x00). Optional bool argument specifies whether screen updates immediately with result. Default is true.

Usage:

oled.clearDisplay();

dimDisplay

Lowers the contrast on the display. This method takes one argument, a boolean. True for dimming, false to restore normal contrast.

Usage:

oled.dimDisplay(true|false);

invertDisplay

Inverts the pixels on the display. Black becomes white, white becomes black. This method takes one argument, a boolean. True for inverted state, false to restore normal pixel colors.

Usage:

oled.invertDisplay(true|false);

turnOffDisplay

Turns the display off.

Usage:

oled.turnOffDisplay();

turnOnDisplay

Turns the display on.

Usage:

oled.turnOnDisplay();

drawPixel

Draws a pixel at a specified position on the display. This method takes one argument: a multi-dimensional array containing either one or more sets of pixels.

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:

// draws 4 white pixels total
// format: [x, y, color]
oled.drawPixel([
	[128, 1, 1],
	[128, 32, 1],
	[128, 16, 1],
	[64, 16, 1]
]);

drawLine

Draws a one pixel wide line.

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:

// args: (x0, y0, x1, y1, color)
oled.drawLine(1, 1, 128, 32, 1);

fillRect

Draws a filled rectangle.

Arguments:

  • int x0, y0 - top left corner of rectangle
  • int width, height - width and height of rectangle in pixels
  • 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:

// args: (x0, y0, width, height, color)
oled.fillRect(1, 1, 10, 20, 1);

drawRect

Draws an empty rectangle.

Arguments:

  • int x0, y0 - top left corner of rectangle
  • int width, height - width and height of rectangle in pixels
  • 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:

// args: (x0, y0, width, height, color)
oled.drawRect(1, 1, 10, 20, 1);

drawCircle

Draws an empty circle.

Arguments:

  • int x - x of circle's center
  • int y - y of circle's center
  • int r - radius of circle
  • 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:

// args: (x, y, r, color)
oled.drawCircle(30, 10, 5, 1);

drawBitmap

Draws a bitmap using raw pixel data returned from an image parser. The image sourced must be monochrome, and indexed to only 2 colors. Resize the bitmap to your screen dimensions first. Using an image editor or ImageMagick might be required.

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
const pngparse = require('pngparse');

pngparse.parseFile('indexed_file.png', (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
const pngtolcd = require('png-to-lcd');

pngtolcd('nyan-cat.png', true, (err, bitmap) => {
  oled.buffer = bitmap;
  oled.update();
});

startScroll

Scrolls the current display either left or right. Arguments:

  • string direction - direction of scrolling. 'left' or 'right'
  • int start - starting row of scrolling area
  • int stop - end row of scrolling area

Usage:

// args: (direction, start, stop)
oled.startScroll('left', 0, 15); // this will scroll an entire 128 x 32 screen

stopScroll

Stops all current scrolling behaviour.

Usage:

oled.stopScroll();

setCursor

Sets the x and y position of 'cursor', when about to write text. This effectively helps tell the display where to start typing when writeString() method is called.

Call setCursor just before writeString().

Usage:

// sets cursor to x = 1, y = 1
oled.setCursor(1, 1);

writeString

Writes a string of text to the display. Call setCursor() just before, if you need to set starting text position.

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.
  • int linespacing - amount of spacing between lines of text on the screen. Negative numbers are also ok.

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. Want more options? Try rolling your own font module to require instead, using the oled-js font foundry!

Usage:

npm install oled-font-5x7
const 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, 2);

update

Sends the entire buffer in its current state to the oled display, effectively syncing the two. This method generally does not need to be called, unless you're messing around with the framebuffer manually before you're ready to sync with the display. It's also needed if you're choosing not to draw on the screen immediately with the built in methods.

Usage:

oled.update();

Future

  • better documentation
  • 16-bit color oled support

oled-js's People

Contributors

algram avatar eddarmitage avatar greenkeeperio-bot avatar igorklopov avatar lisiadito avatar monteslu avatar noopkat avatar psyrax avatar rudacs avatar thezanke avatar traverso avatar vladiks avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

oled-js's Issues

Cannot read property 'send' of undefined [IO plugins support]

Error:

>> /tmp/remote-script/node_modules/tessel-io/lib/index.js:842
      state.i2c[address].send(buffer);
                        ^

TypeError: Cannot read property 'send' of undefined
    at Board.i2cWrite (/tmp/remote-script/node_modules/tessel-io/lib/index.js:842:25)
    at t._transfer (/tmp/remote-script/node_modules/oled-js/oled.js:1:2777)
    at t._initialise (/tmp/remote-script/node_modules/oled-js/oled.js:1:2133)
    at new t (/tmp/remote-script/node_modules/oled-js/oled.js:1:1652)
    at t.<anonymous> (/tmp/remote-script/index.js:1:226)
    at emitNone (events.js:72:20)
    at t.emit (events.js:166:7)
    at nextTickCallbackWith0Args (node.js:420:9)
    at process._tickDomainCallback (node.js:390:13)

Code:

var five = require('johnny-five'),
    Oled = require('oled-js');
var Tessel = require("tessel-io");
var board = new five.Board({
  io: new Tessel()
});
board.on('ready', function() {
  

  var opts = {
    width: 128,
    height: 32,
    address: 0x06
  };

  var oled = new Oled(board, five, opts);
  var font = require('oled-font-5x7');


oled.drawCircle(30, 10, 5, 1);
oled.update();

});

Hello, please tell me how to solve this problem

SPI resources

Hi @noopkat,

I came across your oled-js project.
I'm trying to draw on a e-ink display module which requires SPI.

I was wondering if you can point me to the best resources you managed to find on how to use SPI as efficiently as possible. For your project, you mentioned it is slow when using SPI. (I'm trying to make a second e-ink display HDMI monitor).

It's also nice to see a fellow Melburnian make it to the big leagues in the US. Keep up the good work! I wouldn't be surprised if I've actually met you at a meetup or two either.

Regards,

PJ

Using the display with a raspberry Pi

I really like your API for the oled displays. Unfortunately I am not able to use it yet as I am using my Johnny Five either with a

  • ESP8266 (with Etherport-client) or
  • preferrer a Raspberry Pi (with rapi-io).

Is there any way to figure out the correct address for the display? Would really help me out.

draw immediately

  • add last arg in drawPixel, fillRect, drawBitmap, drawLine, writeString (default true)
  • draw immediate will take effect with each above method call, unless false is passed in
  • if false is passed in, update method is required when done manipulating buffer
  • when above methods are called, set start col and page, send pixel, then update the buffer to clean state (to match).

text justifying

this is definitely a nice to have, and not required for release.

ssd1322 and othe chip support

hello, great work!!!
would it be possible to use different displays than the ssd1306?
i'm asking because i'm currently trying to connect an ssd1322 to my raspberry via nodejs.
i found a great library, but its just for c... (https://github.com/olikraus/u8glib)
it would be amazing to port this to nodejs to your oled-js module.
best regards

paul

Unable to utilise oled-js

I'm unable to render text on the screen. For some reason, my code appears to not clear the screen properly and finish the rendering.

const five = require('johnny-five');

const board = new five.Board();

const Oled = require('oled-js');

const font = require('oled-font-5x7');

board.on('ready', function () {
  const opts = {
    width: 128,
    height: 64,
    address: 0x3C
  };

  const oled = new Oled(board, five, opts);
  oled.clearDisplay();
  oled.setCursor(1, 1);
  oled.writeString(font, 1, 'Cats and dogs are really cool animals, you know.', 1, true, 2, true);
  oled.update();
})

I used this code and confirmed the LCD works fine but when I run the StandardFirmataPlus on the board and execute the above JS code this is what I end up with – the Hello World doesn't seem to get cleared and the text seems cut off as if the screen dimensions are smaller than 128x64.

image

Can someone say what I'm doing wrong?

OLED SSD1306 - Layouts and mixing text and logo with auto size

Dear @noopkat

I 'd like to use your library to generate 1 or 2 or 3 or 4 lines in a 64x32 or 128x64 pixels picture using ssd1306.

Each line could have different fonts and sizes depending of the number of lines.

This a layout I am looking to do by using javascript or typescript to generate on the fly the png or bitmap to display to a oled display

TEXT264x32_PIXELS_PNG_OR_BITMAP_1_COLOR_DEPTH_AND RGB_8BITS.pdf

Otherwise nice on twitch. Success

Best Regards
Youssef

Color arg for writeString not working

The 'color' argument for the 'writeString' method is not working. Regardless of value, it always outputs as 1, never 0.

It looks like the arg 'color' is not referenced anywhere in the 'writeString' method

Question: How to create new fonts

Hi.

I would like to use a slightly larger font (monospaced would be good enough for me). However, doubling the 5x7 font is too large. So, I am wondering how can I create new fonts files? Do you have a tool to create the json file and what is the source file format for this?

What is the expected refreshrate for I2C OLED screens?

I'm getting the similar effect as the mentioned "SPI screens" slowness on a I2C OLED screen.
I have a small 128x64 OLED screen, but the refresh rate is very very poor. You can even see when the screen refreshes to the newest value.

Also I found a bug with oled.writeString method. Something is not properly implemented, because if I call the method 10-20 times in a second, I get an error:

(node:17852) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 201 I2C-reply-60-0 listeners added to [Firmata]. Use emitter.setMaxListeners() to increase limit

Questions here:

  • what is the expected rate of update we can achieve
  • why do I get the error (is it a bug?)
  • if the update is limited and takes time to execute, wouldn't it be wise to return with a promise, so we can call the next update when the previous screen update finished? There is no way of knowing at the moment when the update has finished

ES6/ES7?

Are you looking to port this library to newer js standards?

Scroll Size

Hi,

I would like to know if it's possible to have a scroll for text that are more longer that the screen size ?

Regards,

How to position a bitmap or png picture on the screen if not taking all the size of the screen

Dear @monteslu , @igorklopov , @noopkat , @psyrax , @rudacs

Nice library but how we could center a bitmap or position it if the size is less than the screen size?

I 'd like to position 6 icons (bmp or png) and position each one in a defined x,y position.

How to do that even to use to drawpixel command ?

And finally, is there a way to manage Layers that means I have a background picture, and some time I have popup like (Yes - No) ? And when selecting one of this option of the popup, Clear the popup without to reload the background picture ?

Best Regards

Season's greetings
Youssef

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.