Adafruit Bi-Color 8x8 Matrix

Posted 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):

    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?

    Matt

  • Typical, write a post and find the answer :) Turns out I needed some other methods to be called first to setup the device. The following works:

    // 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;
    
    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;
    
    // Setup I2C
    I2C1.setup({ scl: B6, sda: B7 });
    
    function clearDisplay(){
     for (var i=0; i<8; i++) {
        displaybuffer[i] = 0;
     } 
    }
    
    function setBrightness(lvl) {
      if (lvl > 15) {
        lvl = 15;
      }
      I2C1.writeTo(address, 0xE0 | lvl);
    }
    
    function blinkRate(b) {
      if (b > 3) b = 0; // turn off if not sure
      I2C1.writeTo(address, HT16K33_BLINK_CMD | HT16K33_BLINK_DISPLAYON | (b << 1));
    }
    
    function drawPixel(x,y,color){
      
      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/2] & 0xFF;
        tmp[i+2] = displaybuffer[i/2] >> 8; 
      }
      console.log(tmp);
      I2C1.writeTo(address, tmp);
    }
    
    // Turn on oscillator
    I2C1.writeTo(address, 0x21);
    
    // Set blink rate
    blinkRate(HT16K33_BLINK_OFF);
    
    // Set to full brightness
    setBrightness(15);
    
    // Draw pixels
    drawPixel(0,0,LED_GREEN);
    drawPixel(1,1,LED_RED);
    drawPixel(2,2,LED_YELLOW);
    
    // Update display
    writeDisplay();
    
  • Great! I wonder if this could be a library that used the graphics lib :)

  • @Gordon it already is, see my other post regarding "trying to assign to an un-named type" :) (I always like to get as close to the original to work first, then make better)

    Is there a github repo we can submit new libraries to?

    Matt

  • Yes, sorry - saw that. I got a billion e-mails over the weekend so I'm just working through in order :)

    Repo is EspruinoDocs - but there's a tutorial on modules here I think...

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Adafruit Bi-Color 8x8 Matrix

Posted by Avatar for mattbrailsford @mattbrailsford

Actions