Go Down

Topic: "Convert" 5x7 font & library to scroll 5x5 font on LED Matrix? (Read 5290 times) previous topic - next topic

1ChicagoDave

Bits & bytes givin' me a headache.

I have 5x7 bicolor LED matrix (see attached) which I'm using with a Holtek H16K33 backpack from Adafruit http://www.adafruit.com/products/1427 to run matrix in horizontal position (see attached) with the top left connected to be (0,0).

Datasheet for the chip on backpack - http://www.adafruit.com/datasheets/ht16K33v110.pdf
Adafruit library for backpack -https://github.com/adafruit/Adafruit-LED-Backpack-Library

I've combined the cathodes on matrix to make it 'common cathode', have one color connected to A0-A6, and other color connected to A8-A14.

The library I'm using https://github.com/adafruit/Adafruit-GFX-Library/blob/master/Adafruit_GFX.cpp is designed to handle the standard 5x7 fonts like this --https://github.com/adafruit/Adafruit-GFX-Library/blob/master/glcdfont.c on an 8x8 bicolor matrix.

Trouble is, my display is now only 5 pixels tall. Everything works as intended. I just lose the bottom few pixels of every letter. Not pretty.

So, I want/need to figure out how to run this 5x5 font on my setup.
Code: [Select]

byte a[8]={0x00,0x7c,0x44,0x44,0x7c,0x44,0x00,0x00};   
  byte b[8]={0x00,0x7c,0x44,0x78,0x44,0x7c,0x00,0x00}; 
  byte c[8]={0x00,0x7c,0x40,0x40,0x40,0x7c,0x00,0x00}; 
  byte d[8]={0x00,0x78,0x44,0x44,0x44,0x78,0x00,0x00}; 
  byte e[8]={0x00,0x7c,0x40,0x78,0x40,0x7c,0x00,0x00};
  byte f[8]={0x00,0x7c,0x40,0x70,0x40,0x40,0x00,0x00};
  byte g[8]={0x00,0x7c,0x40,0x4c,0x44,0x7c,0x00,0x00};
  byte h[8]={0x00,0x44,0x44,0x7c,0x44,0x44,0x00,0x00};
  byte i[8]={0x00,0x7c,0x10,0x10,0x10,0x7c,0x00,0x00};
  byte j[8]={0x00,0x0c,0x04,0x04,0x44,0x7c,0x00,0x00};
  byte k[8]={0x00,0x44,0x48,0x70,0x48,0x44,0x00,0x00};
  byte l[8]={0x00,0x40,0x40,0x40,0x40,0x7c,0x00,0x00};
  byte m[8]={0x00,0x44,0x6c,0x54,0x44,0x44,0x00,0x00};
  byte n[8]={0x00,0x44,0x64,0x54,0x4c,0x44,0x00,0x00};
  byte o[8]={0x00,0x38,0x44,0x44,0x44,0x38,0x00,0x00}; 
  byte p[8]={0x00,0x78,0x44,0x78,0x40,0x40,0x00,0x00};             
  byte q[8]={0x00,0x7c,0x44,0x44,0x7c,0x10,0x00,0x00};           
  byte r[8]={0x00,0x78,0x44,0x78,0x44,0x44,0x00,0x00};           
  byte s[8]={0x00,0x7c,0x40,0x7c,0x04,0x7c,0x00,0x00};           
  byte t[8]={0x00,0x7c,0x10,0x10,0x10,0x10,0x00,0x00};             
  byte u[8]={0x00,0x44,0x44,0x44,0x44,0x7c,0x00,0x00};           
  byte v[8]={0x00,0x44,0x44,0x28,0x28,0x10,0x00,0x00};           
  byte w[8]={0x00,0x44,0x44,0x54,0x54,0x28,0x00,0x00};           
  byte x[8]={0x00,0x44,0x28,0x10,0x28,0x44,0x00,0x00};           
  byte y[8]={0x00,0x44,0x44,0x28,0x10,0x10,0x00,0x00};           
  byte z[8]={0x00,0x7c,0x08,0x10,0x20,0x7c,0x00,0x00};           
 
  byte ze[8]={0x00,0x7c,0x4c,0x54,0x64,0x7c,0x00,0x00};           
  byte on[8]={0x00,0x10,0x30,0x10,0x10,0x38,0x00,0x00};           
  byte tw[8]={0x00,0x78,0x04,0x38,0x40,0x7c,0x00,0x00};           
  byte th[8]={0x00,0x7c,0x04,0x38,0x04,0x7c,0x00,0x00};           
  byte fo[8]={0x00,0x40,0x40,0x50,0x7c,0x10,0x00,0x00};           
  byte fi[8]={0x00,0x7c,0x40,0x78,0x04,0x78,0x00,0x00};           
  byte si[8]={0x00,0x7c,0x40,0x7c,0x44,0x7c,0x00,0x00};           
  byte se[8]={0x00,0x7c,0x04,0x08,0x10,0x10,0x00,0x00};           
  byte ei[8]={0x00,0x7c,0x44,0x7c,0x44,0x7c,0x00,0x00};           
  byte ni[8]={0x00,0x7c,0x44,0x7c,0x04,0x7c,0x00,0x00};



The full library and code is visible at the links above.

Here are (I believe) the relevant pieces, for your convenience -
Code: [Select]


Adafruit_BicolorMatrix::Adafruit_BicolorMatrix(void) : Adafruit_GFX(8, 8) {
}

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 (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));
  }
}



Then this in sketch to scroll text

Code: [Select]

  matrix.setTextWrap(false);  // we dont want text to wrap so it scrolls nicely
  matrix.setTextSize(1);
  matrix.setTextColor(LED_GREEN);
  for (int8_t x=7; x>=-36; x--) {
    matrix.clear();
    matrix.setCursor(x,0);
    matrix.print("Hello");
    matrix.writeDisplay();
    delay(100);
  }



Calling (I think) this from library...

Code: [Select]

size_t Adafruit_GFX::write(uint8_t c) {
#else
void Adafruit_GFX::write(uint8_t c) {
#endif
  if (c == '\n') {
    cursor_y += textsize*8;
    cursor_x  = 0;
  } else if (c == '\r') {
    // skip em
  } else {
    drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
    cursor_x += textsize*6;
    if (wrap && (cursor_x > (_width - textsize*6))) {
      cursor_y += textsize*8;
      cursor_x = 0;
    }
  }
#if ARDUINO >= 100
  return 1;
#endif
}

// Draw a character
void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c,
    uint16_t color, uint16_t bg, uint8_t size) {

  if((x >= _width)            || // Clip right
     (y >= _height)           || // Clip bottom
     ((x + 6 * size - 1) < 0) || // Clip left
     ((y + 8 * size - 1) < 0))   // Clip top
    return;

  for (int8_t i=0; i<6; i++ ) {
    uint8_t line;
    if (i == 5)
      line = 0x0;
    else
      line = pgm_read_byte(font+(c*5)+i);
    for (int8_t j = 0; j<8; j++) {
      if (line & 0x1) {
        if (size == 1) // default size
          drawPixel(x+i, y+j, color);
        else {  // big size
          fillRect(x+(i*size), y+(j*size), size, size, color);
        }
      } else if (bg != color) {
        if (size == 1) // default size
          drawPixel(x+i, y+j, bg);
        else {  // big size
          fillRect(x+i*size, y+j*size, size, size, bg);
        }
      }
      line >>= 1;
      }
   }
}


So far, I figure...create a new font header file with new font, and have the library include it instead of the current 5x7 font header. And that's about it.

The existing library is written for an 8x8 bicolor matrix.

Is there a practical way to 'adjust' the existing library code to read/translate the new font code into something that will display properly?
If so, what is it?
I'm sure the order of bytes in font header matter. Are they already in the correct order?
I think I can lose the extra "0x00"s in front and back of new font. Correct or no?

Thanks!


Go Up