added multiple fonts support

This commit is contained in:
JP
2014-01-16 17:13:02 +11:00
parent e5989baf36
commit c9b1aa3128
4 changed files with 282 additions and 41 deletions

View File

@@ -1,47 +1,56 @@
#include <avr/pgmspace.h>
#include <MicroView.h>
#include <font5x7.h>
#include <SPI.h>
// Change to add fonts
#include <font5x7.h>
#include <font8x16.h>
#define TOTALFONTS 2
const unsigned char *MICROVIEW::fontsPointer[]={font5x7,font8x16};
// Change to add fonts
/*
Screen memory buffer 64 x 48 divided by 8 = 384 bytes
Screen memory buffer is required because in SPI mode, the host cannot read the SSD1306's GDRAM of the controller. This buffer serves as a scratch RAM for graphical functions.
*/
static uint8_t screenmemory [] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0xC0, 0xC0,
0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80,
0xC0, 0xC0, 0xE0, 0xE0, 0xF0, 0x78, 0xF8, 0xBC, 0xFC, 0xDE, 0xDE, 0xCF, 0xCF, 0xC7, 0xC3, 0xC3,
0xC7, 0x87, 0x8F, 0x0F, 0x1E, 0x1E, 0x3C, 0x78, 0x78, 0xF0, 0xF0, 0xE0, 0xE0, 0xC0, 0xC0, 0x80,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00,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00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F,
0x0F, 0x07, 0x03, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
// LCD Memory organised in 64 horizontal pixel and 6 rows of byte
// B B .............B -----
// y y .............y \
// t t .............t \
// e e .............e \
// 0 1 .............63 \
// \
// D0 D0.............D0 \
// D1 D1.............D1 / ROW 0
// D2 D2.............D2 /
// D3 D3.............D3 /
// D4 D4.............D4 /
// D5 D5.............D5 /
// D6 D6.............D6 /
// D7 D7.............D7 ----
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0xC0, 0xC0, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0xC0, 0xC0, 0xE0, 0xE0, 0xF0, 0x78, 0xF8, 0xBC, 0xFC, 0xDE, 0xDE, 0xCF, 0xCF, 0xC7, 0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0F, 0x1E, 0x3F, 0x3F, 0x3F, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0x7F, 0x1F, 0x3F, 0x3F, 0x1F, 0x03, 0x07, 0x07, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x0F, 0x07, 0x03, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
#define TOTALFONTS 2
const unsigned char *MICROVIEW::fontsPointer[]={font,font};
void MICROVIEW::begin() {
// default 5x7 font
/*
fontType=0;
fontWidth=pgm_read_byte(fontsPointer[fontType]+1);
fontHeight=pgm_read_byte(fontsPointer[fontType]+2);
fontStartChar=pgm_read_byte(fontsPointer[fontType]+3);
fontTotalChar=pgm_read_byte(fontsPointer[fontType]+4);
*/
setFontType(0);
// Setting up SPI pins
pinMode(MOSI, OUTPUT);
@@ -342,11 +351,19 @@ void MICROVIEW::circleFill(uint8_t x0, uint8_t y0, uint8_t r, uint8_t color, uin
}
uint8_t MICROVIEW::getFontWidth(void) {
return pgm_read_byte(fontsPointer[fontType]+1);
return fontWidth;
}
uint8_t MICROVIEW::getFontHeight(void) {
return pgm_read_byte(fontsPointer[fontType]+2);
return fontHeight;
}
uint8_t MICROVIEW::getFontStartChar(void) {
return fontStartChar;
}
uint8_t MICROVIEW::getFontTotalChar(void) {
return fontTotalChar;
}
uint8_t MICROVIEW::getTotalFonts(void) {
@@ -362,15 +379,56 @@ uint8_t MICROVIEW::setFontType(uint8_t type) {
return -1;
fontType=type;
fontWidth=pgm_read_byte(fontsPointer[fontType]+1);
fontHeight=pgm_read_byte(fontsPointer[fontType]+2);
fontStartChar=pgm_read_byte(fontsPointer[fontType]+3);
fontTotalChar=pgm_read_byte(fontsPointer[fontType]+4);
fontMapWidth=(pgm_read_byte(fontsPointer[fontType]+5)*100)+pgm_read_byte(fontsPointer[fontType]+6); // two bytes values into integer 16
}
void MICROVIEW::drawChar(uint8_t x, uint8_t line, uint8_t c, uint8_t mode) {
uint8_t rowsToDraw,row;
uint8_t i,tempX;
uint16_t charPerBitmapRow,charColPositionOnBitmap,charRowPositionOnBitmap,charBitmapStartPosition;
// TODO - char must be able to be drawn anywhere, not limited by line
if ((line >= LCDHEIGHT/8) || (x >= (LCDWIDTH - 6)))
// TODO - char must be able to XOR on background
if ((line >= LCDHEIGHT/fontHeight) || (x > (LCDWIDTH - fontWidth)))
return;
for (uint8_t i =0; i<5; i++ ) {
screenmemory[x + (line*LCDWIDTH) ] = pgm_read_byte(fontsPointer[fontType]+3+(c*5)+i);
x++;
// TODO - check if char is not in memory
tempX=x;
// each row (in datasheet is call page) is 8 bits high, 16 bit high character will have 2 rows to be drawn
rowsToDraw=fontHeight/8; // 8 is LCD's page size, see SSD1306 datasheet
if (rowsToDraw<=1) rowsToDraw=1;
// only 1 row to draw for font with 8 bit height
if (rowsToDraw==1) {
for (i=0; i<fontWidth; i++ ) {
screenmemory[tempX + (line*LCDWIDTH) ] = pgm_read_byte(fontsPointer[fontType]+FONTHEADERSIZE+(c*fontWidth)+i);
tempX++;
}
return;
}
// font height over 8 bit
// take character "0" ASCII 48 as example
charPerBitmapRow=fontMapWidth/fontWidth; // 256/8 =32 char per row
charColPositionOnBitmap=c % charPerBitmapRow; // =16
charRowPositionOnBitmap=int(c/charPerBitmapRow); // =1
charBitmapStartPosition=(fontMapWidth * (fontHeight/8)) + (charColPositionOnBitmap * fontWidth);
tempX=x;
for (row=0; row<rowsToDraw; row++) {
for (i=0; i<fontWidth; i++ ) {
screenmemory[tempX + (( (line*(fontHeight/8)) +row)*LCDWIDTH) ] = pgm_read_byte(fontsPointer[fontType]+FONTHEADERSIZE+(charBitmapStartPosition+i+(row*fontMapWidth)));
tempX++;
}
tempX=x;
}
}