I've been spending the last couple of hours trying to convert some Adafruit code to work with the Espruino, but am struggling to get it to work. Can anyone offer any advice?
The code from adafruit is as follows (just the methods I'm bothered about):
void Adafruit_BicolorMatrix::drawPixel(int16_t x, int16_t y, uint16_t color) {
if ((y < 0) || (y >= 8)) return;
if ((x < 0) || (x >= 8)) return;
switch (getRotation()) {
case 1:
swap(x, y);
x = 8 - x - 1;
break;
case 2:
x = 8 - x - 1;
y = 8 - y - 1;
break;
case 3:
swap(x, y);
y = 8 - y - 1;
break;
}
if(x < 4){
x += 4;
} else {
x -= 4;
}
if(y < 4){
y += 4;
} else {
y -= 4;
}
if (color == LED_GREEN) {
displaybuffer[y] |= 1 << x;
} else if (color == LED_RED) {
displaybuffer[y] |= 1 << (x+8);
} else if (color == LED_YELLOW) {
displaybuffer[y] |= (1 << (x+8)) | (1 << x);
} else if (color == LED_OFF) {
displaybuffer[y] &= ~(1 << x) & ~(1 << (x+8));
}
}
void Adafruit_LEDBackpack::writeDisplay(void) {
Wire.beginTransmission(i2c_addr);
Wire.write((uint8_t)0x00); // start at address $00
for (uint8_t i=0; i<8; i++) {
Wire.write((displaybuffer[i]) & 0xFF);
Wire.write((displaybuffer[i]) >> 8);
}
Wire.endTransmission();
}
And I have currently got the following javascript for the Espruino:
// Variables
var address = 0x70;
var displaybuffer = new Uint16Array(8);
var LED_OFF = 0;
var LED_RED = 1;
var LED_YELLOW = 2;
var LED_GREEN = 3;
// Setup I2C
I2C1.setup({ scl: B6, sda: B7 });
function clearDisplay(){
for (var i=0; i<8; i++) {
displaybuffer[i] = 0;
}
}
function drawPixel(x,y,color){
// Not bothered about rotation yet
if(x < 4){
x += 4;
} else {
x -= 4;
}
if(y < 4){
y += 4;
} else {
y -= 4;
}
if (color == LED_GREEN) {
displaybuffer[y] |= 1 << x;
} else if (color == LED_RED) {
displaybuffer[y] |= 1 << (x+8);
} else if (color == LED_YELLOW) {
displaybuffer[y] |= (1 << (x+8)) | (1 << x);
} else if (color == LED_OFF) {
displaybuffer[y] &= ~(1 << x) & ~(1 << (x+8));
}
}
function writeDisplay(){
var tmp = new Uint8Array(17);
tmp[0] = 0x00;
for (var i=0; i<16; i+=2) {
tmp[i+1] = displaybuffer[i] & 0xFF;
tmp[i+2] = displaybuffer[i] >> 8;
}
console.log(tmp);
I2C1.writeTo(address, tmp);
}
drawPixel(0,0,LED_GREEN);
drawPixel(1,1,LED_RED);
drawPixel(2,2,LED_YELLOW);
writeDisplay();
The problem is, I just don't get any lights lighting up. Have I converted the code incorrectly? Or am I miss-understanding how you work with I2C on the Espruino?
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.
Hi Guys,
I've been spending the last couple of hours trying to convert some Adafruit code to work with the Espruino, but am struggling to get it to work. Can anyone offer any advice?
The code from adafruit is as follows (just the methods I'm bothered about):
And I have currently got the following javascript for the Espruino:
The problem is, I just don't get any lights lighting up. Have I converted the code incorrectly? Or am I miss-understanding how you work with I2C on the Espruino?
Matt