I'm trying to write a library for my Adafruit Bi-color 8x8 matrix so that I can release it for others to use. As it is a display and so involves drawing shapes and such, I thought a good idea would be to extend off of the Graphics object and some custom methods to that and return that as my modules API, like so:
// Define global constants
var LED_OFF = 0;
var LED_RED = 1;
var LED_YELLOW = 2;
var LED_GREEN = 3;
var HT16K33_BLINK_CMD = 0x80;
var HT16K33_BLINK_DISPLAYON = 0x01;
var HT16K33_BLINK_OFF = 0;
var HT16K33_BLINK_2HZ = 1;
var HT16K33_BLINK_HALFHZ = 3;
var HT16K33_BRIGHTNESS_CMD = 0xE0;
var HT16K33_ENABLE_OSCILLATOR_CMD = 0x21;
var exports = {};
exports.connect = function(i2c, address, callback){
// Private methods
function drawPixel(x,y,col){
// Handle rotation
switch (rotation) {
case 1:
var t1 = x;
x = 8 - y - 1;
y = t1;
break;
case 2:
x = 8 - x - 1;
y = 8 - y - 1;
break;
case 3:
var t2 = x;
x = y;
y = 8 - t2 - 1;
break;
}
// Handle backwards wiring
if(x < 4){
x += 4;
} else {
x -= 4;
}
if(y < 4){
y += 4;
} else {
y -= 4;
}
// We wamt colours to override
// so turn the current pixel off by default
// then set it's colour again
displaybuffer[y] &= ~(1 << x) & ~(1 << (x+8));
if (col == LED_GREEN) {
displaybuffer[y] |= (1 << x) & ~(1 << (x+8));
} else if (col == LED_RED) {
displaybuffer[y] |= ~(1 << x) & (1 << (x+8));
} else if (col == LED_YELLOW) {
displaybuffer[y] |= (1 << x) | (1 << (x+8));
}
}
function drawRect(x1,y1,x2,y2,col){
for(var x=x1;x<=x2;x++){
for(var y=y1;y<=y2;y++){
drawPixel(x,y,col);
}
}
}
// Create buffer
var display = Graphics.createCallback(8,8,8,{setPixel:drawPixel,fillRect:drawRect});
var displaybuffer = new Uint16Array(8);
var rotation = 0;
display.setRotation = function(rot){
rotation = rot;
};
display.getRotation = function(){
return rotation;
};
display.setBlinkRate = function(b) {
if (b > 3) b = 0; // turn off if not sure
i2c.writeTo(address, HT16K33_BLINK_CMD | HT16K33_BLINK_DISPLAYON | (b << 1));
};
display.setBrightness = function(b) {
if (b > 15) b = 15; // Don't go over max
i2c.writeTo(address, HT16K33_BRIGHTNESS_CMD | b);
};
display.clear = function(){
for (var i=0; i<8; i++) {
displaybuffer[i] = 0;
}
display.write();
};
display.write = function(){
var tmp = new Uint8Array(17);
tmp[0] = 0x00;
for (var i=0; i<16; i+=2) {
tmp[i+1] = displaybuffer[i/2] & 0xFF;
tmp[i+2] = displaybuffer[i/2] >> 8;
}
i2c.writeTo(address, tmp);
};
// Turn on oscillator
i2c.writeTo(address, HT16K33_ENABLE_OSCILLATOR_CMD);
// Set blink rate
display.setBlinkRate(HT16K33_BLINK_OFF);
// Set to full brightness
display.setBrightness(15);
// Trigger callback
if (callback!==undefined){
setTimeout(callback, 100);
}
return display;
};
// Setup I2C
I2C2.setup({ scl: B10, sda: B11 });
var matrix = exports.connect(I2C2, 0x70, function(){
var counter = 1;
setInterval(function(){
matrix.clear();
matrix.setRotation(counter - 1);
matrix.setColor(counter);
matrix.fillRect(0,0,7,1);
matrix.fillRect(0,3,7,4);
matrix.fillRect(0,6,7,7);
matrix.fillRect(0,0,1,7);
matrix.write();
counter = (counter == 3) ? 1 : counter + 1;
}, 2000);
});
The problem is, I keep getting errors when I try to add additional methods to my display object. If I extend the Graphics class via prototype then everything works, however I don't won't my methods available to all Graphics objects, just ones returned from my module. Anyone know what I'm doing wrong (I'm sure I saw @Gordon do this kind of thing in the 123-LED demo video).
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Hey Guys,
I'm trying to write a library for my Adafruit Bi-color 8x8 matrix so that I can release it for others to use. As it is a display and so involves drawing shapes and such, I thought a good idea would be to extend off of the Graphics object and some custom methods to that and return that as my modules API, like so:
The problem is, I keep getting errors when I try to add additional methods to my
display
object. If I extend theGraphics
class viaprototype
then everything works, however I don't won't my methods available to all Graphics objects, just ones returned from my module. Anyone know what I'm doing wrong (I'm sure I saw @Gordon do this kind of thing in the 123-LED demo video).Matt