Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grove LCD and NodeJS #144

Closed
k33g opened this issue Sep 14, 2015 · 7 comments
Closed

Grove LCD and NodeJS #144

k33g opened this issue Sep 14, 2015 · 7 comments

Comments

@k33g
Copy link
Contributor

k33g commented Sep 14, 2015

Hello,

I’m a newbie with GrovePi and I’ve understood how to extend AnalogicSensor to use sound sensor. But it becomes more complicated (for me) with Grove LCD and Leds. It would be fine to have some samples about LCD & leds.

Thank you

Regards
Philippe

@marcellobarile
Copy link
Contributor

Hello @k33g,
I'll try to provide you something useful for your needs. Let's keep in touch, okay?

@k33g
Copy link
Contributor Author

k33g commented Sep 17, 2015

oh nice 😃 👍 thank you very much 😄

@chewbecky
Copy link

Hey, i'm also curious :) Is there any example code on how to use the 96x96 OLED Display with node?

@jlorek
Copy link

jlorek commented Dec 20, 2016

Im also looking for an example or at least an direction on how to use the display in NodeJS.
Any help is appreciated!
Thanks :)

@marcellobarile
Copy link
Contributor

Sorry for being late guys, I don't have that display but I'll do my best to give you some reasonable answer :)

@danibram
Copy link

danibram commented Dec 22, 2016

Hey guys,
I wrote this class for a workshop with my groove pi

const GrovePi = require('node-grovepi').GrovePi;
const fs = require('fs');
const sleep = require('sleep');
const extend = require('extend');

const I2cSensor = GrovePi.sensors.base.I2C;
const i2cBus = require('i2c-bus');

const DISPLAY_RGB_ADDR = 0x62;
const DISPLAY_TEXT_ADDR = 0x3e;

const i2c0Path  = '/dev/i2c-0'
const i2c1Path  = '/dev/i2c-1'
const i2c2Path  = '/dev/i2c-2'

class LCD_I2C {
    constructor(){
        this.data = {}

        this.initialized = false;
        this.bus = null;
        // Bus Address from i2cdetect command
        this.address = 0x68;
        // Default delay for streaming
        this.watchDelay = 100;

        let busNumber

        if (fs.existsSync(i2c0Path)) {
          busNumber = 0
        } else if (fs.existsSync(i2c1Path)) {
          busNumber = 1
        } else if (fs.existsSync(i2c2Path)) {
          busNumber = 2
        } else {
          var err = console.log('ERR: Could not determine your i2c device')
        }

        this.i2c1 = i2cBus.openSync(1);
        console.log(this.i2c1)

        this.setText('Ready!');
        this.setRGB(55, 55, 255);
    }

    //data
    setData(data) {
        this.data = Object.assign({}, this.data, data)
        this.update(this.data)
    }

    update(data) {
        var text = ""
        Object.keys(data).map((k) => {
            text = text + ` ${k}: ${data[k]}`
        })
        this.setText(text);
    }

    //Hardware
    setRGB(r, g, b) {
      this.i2c1.writeByteSync(DISPLAY_RGB_ADDR,0,0)
      this.i2c1.writeByteSync(DISPLAY_RGB_ADDR,1,0)
      this.i2c1.writeByteSync(DISPLAY_RGB_ADDR,0x08,0xaa)
      this.i2c1.writeByteSync(DISPLAY_RGB_ADDR,4,r)
      this.i2c1.writeByteSync(DISPLAY_RGB_ADDR,3,g)
      this.i2c1.writeByteSync(DISPLAY_RGB_ADDR,2,b)
    }

    textCommand(cmd) {
      this.i2c1.writeByteSync(DISPLAY_TEXT_ADDR, 0x80, cmd);
    }

    setText(text) {
      this.textCommand(0x01) // clear display
      sleep.usleep(50000);
      this.textCommand(0x08 | 0x04) // display on, no cursor
      this.textCommand(0x28) // 2 lines
      sleep.usleep(50000);

      var count = 0;
      var row = 0;
      for(var i = 0, len = text.length; i < len; i++) {
        if(text[i] === '\n' || count === 16) {
          count = 0;
          row ++;
            if(row === 2)
              break;
          this.textCommand(0xc0)
          if(text[i] === '\n')
            continue;
        }
        count++;
        this.i2c1.writeByteSync(DISPLAY_TEXT_ADDR, 0x40, text[i].charCodeAt(0));
      }
    }
}

export default LCD_I2C

I hope this help you, its written in es6 but you can easy extract the basic idea

@marcellobarile
Copy link
Contributor

hello @danibram,
that's awesome, thank you for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants