Skip to content

Commit

Permalink
implement createChar (with helper options) and fix write(int)
Browse files Browse the repository at this point in the history
  • Loading branch information
natevw committed Jan 4, 2013
1 parent 3c5df29 commit e4f05ca
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions lib/lcd.js
Expand Up @@ -179,19 +179,31 @@ LCD.prototype.autoscroll = function (on) {

LCD.prototype.noAutoscroll = function () { this.autoscroll(false); }

// TODO: finish this up (make .write handle buffer instead of string?)
LCD.prototype.createCharTODO = function (location, charmap) {
LCD.prototype.createChar = function (location, charmap) {
location &= 0x7;
this.command(LCD_SETCGRAMADDR | (location << 3));
for (var i = 0; i < 8; i++) {
this.write(charmap[i]);
}

var buffer = new Buffer(8);
if (Array.isArray(charmap)) for (var i = 0; i < 8; i++) {
buffer[i] = parseInt(charmap[i], 2);
} else if (typeof charmap === 'string') for (var i = 0; i < 8; i++) {
var byte = 0;
if (charmap[5*i + 0] !== ' ') byte |= 0x10;
if (charmap[5*i + 1] !== ' ') byte |= 0x08;
if (charmap[5*i + 2] !== ' ') byte |= 0x04;
if (charmap[5*i + 3] !== ' ') byte |= 0x02;
if (charmap[5*i + 4] !== ' ') byte |= 0x01;
buffer[i] = byte;
} else buffer = charmap;
this.write(buffer);
}

LCD.prototype.write = LCD.prototype.print = function (str) {
// TODO: map misc Unicode chars to typical LCD charset?
for (var i = 0, len = str.length; i < len; i++)
this.send(str.charCodeAt(i), this.board.HIGH);
// TODO: map misc Unicode chars to typical LCD extended charset?
var bytes = (typeof str === 'string') ? new Buffer(str, 'ascii') :
(typeof str === 'object') ? str : new Buffer([str]);
for (var i = 0, len = bytes.length; i < len; i++)
this.send(bytes[i], this.board.HIGH);
}


Expand Down

0 comments on commit e4f05ca

Please sign in to comment.