Skip to content

Instantly share code, notes, and snippets.

@chrisnew
Last active August 12, 2016 13:23
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save chrisnew/6725633 to your computer and use it in GitHub Desktop.
Save chrisnew/6725633 to your computer and use it in GitHub Desktop.
HD44780 LCD on a Raspberry Pi controlled by node.js with onoff and sleep.
var Gpio = require('onoff').Gpio;
var sleep = require('sleep');
var displayPorts = {
RS: 7,
E: 8,
D4: 25,
D5: 24,
D6: 23,
D7: 18,
CHR: 1,
CMD: 0
};
function LCD(displayConfig) {
displayConfig = displayConfig || {};
this._displayConfig = {
width: displayConfig.width || 16,
line1: 0x80,
line2: 0xc0,
pulse: displayConfig.pulse || 0.0001,
delay: displayConfig.delay || 0.0001
};
this._ports = {
rs: null,
e: null,
d4: null,
d5: null,
d6: null,
d7: null
};
}
LCD.prototype._sleep = function (seconds) {
sleep.usleep(seconds * 1000000);
};
LCD.prototype._initDisplay = function () {
this.writeByte(0x33, displayPorts.CMD);
this.writeByte(0x32, displayPorts.CMD);
this.writeByte(0x28, displayPorts.CMD);
this.writeByte(0x0C, displayPorts.CMD);
this.writeByte(0x06, displayPorts.CMD);
this.writeByte(0x01, displayPorts.CMD);
};
LCD.prototype.init = function (callback) {
this._ports.d4 = new Gpio(displayPorts.D4, 'out');
this._ports.d5 = new Gpio(displayPorts.D5, 'out');
this._ports.d6 = new Gpio(displayPorts.D6, 'out');
this._ports.d7 = new Gpio(displayPorts.D7, 'out');
this._ports.rs = new Gpio(displayPorts.RS, 'out');
this._ports.e = new Gpio(displayPorts.E, 'out');
this._initDisplay();
callback.call(this);
};
LCD.prototype._clean = function () {
for (var key in this._ports) {
this._ports[key].unexport();
}
};
LCD.prototype.shutdown = function () {
// this.writeString("\n");
this._clean();
};
LCD.prototype.writeString = function (string) {
var parts = string.split("\n");
var lines = [this._displayConfig.line1, this._displayConfig.line2];
for (var key in parts) {
this.writeByte(lines[key], displayPorts.CMD);
for (var i = 0; i != this._displayConfig.width; i++) {
var c = parts[key].charCodeAt(i) || 0x20;
this.writeByte(c, displayPorts.CHR);
}
}
};
LCD.prototype.writeByte = function (bits, mode) {
this._ports.rs.writeSync(mode);
this._ports.d4.writeSync(0);
this._ports.d5.writeSync(0);
this._ports.d6.writeSync(0);
this._ports.d7.writeSync(0);
if ((bits & 0x10) == 0x10) {
this._ports.d4.writeSync(1);
}
if ((bits & 0x20) == 0x20) {
this._ports.d5.writeSync(1);
}
if ((bits & 0x40) == 0x40) {
this._ports.d6.writeSync(1);
}
if ((bits & 0x80) == 0x80) {
this._ports.d7.writeSync(1);
}
this._sleep(this._displayConfig.delay);
this._ports.e.writeSync(1);
this._sleep(this._displayConfig.pulse);
this._ports.e.writeSync(0);
this._sleep(this._displayConfig.delay);
this._ports.d4.writeSync(0);
this._ports.d5.writeSync(0);
this._ports.d6.writeSync(0);
this._ports.d7.writeSync(0);
if ((bits & 0x1) == 0x1) {
this._ports.d4.writeSync(1);
}
if ((bits & 0x2) == 0x2) {
this._ports.d5.writeSync(1);
}
if ((bits & 0x4) == 0x4) {
this._ports.d6.writeSync(1);
}
if ((bits & 0x8) == 0x8) {
this._ports.d7.writeSync(1);
}
this._sleep(this._displayConfig.delay);
this._ports.e.writeSync(1);
this._sleep(this._displayConfig.pulse);
this._ports.e.writeSync(0);
this._sleep(this._displayConfig.delay);
};
var lcd = new LCD();
lcd.init(function () {
lcd.writeString("Hello World!\nFeelin' fine.");
lcd.shutdown();
});
@chrisnew
Copy link
Author

Check out my implementation here: http://myimages.eu/photos/single/313yta.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment