Skip to content

Instantly share code, notes, and snippets.

@shikarunochi
Created April 15, 2020 09:03
Show Gist options
  • Save shikarunochi/c00cce986990458e5b695b5887686950 to your computer and use it in GitHub Desktop.
Save shikarunochi/c00cce986990458e5b695b5887686950 to your computer and use it in GitHub Desktop.
Adafruit SSD1306 用 efont 表示
#ifndef __EFONT_SSD1306_H__
#define __EFONT_SSD1306_H__
#include "efont.h"
//#define EFONT_DEBUG
void printEfont(Adafruit_SSD1306 *display, char *str) {
int posX = display->getCursorX();
int posY = display->getCursorY();
uint8_t textsize = 1;//display->textsize;
uint32_t textcolor = 1;//display->textcolor;
uint32_t textbgcolor = 0;//display->textbgcolor;
byte font[32];
while( *str != 0x00 ){
// 改行処理
if( *str == '\n' ){
// 改行
posY += 16 * textsize;
posX = display->getCursorX();
str++;
continue;
}
// フォント取得
uint16_t strUTF16;
str = efontUFT8toUTF16( &strUTF16, str );
getefontData( font, strUTF16 );
// 文字横幅
int width = 16 * textsize;
if( strUTF16 < 0x0100 ){
// 半角
width = 8 * textsize;
}
#ifdef EFONT_DEBUG
Serial.printf( "str : U+%04X\n", strUTF16 );
#endif
// 背景塗りつぶし
display->fillRect(posX, posY, width, 16 * textsize, textbgcolor);
// 取得フォントの確認
for (uint8_t row = 0; row < 16; row++) {
word fontdata = font[row*2] * 256 + font[row*2+1];
for (uint8_t col = 0; col < 16; col++) {
#ifdef EFONT_DEBUG
Serial.write( ( (0x8000 >> col) & fontdata ) ? "#" : " " );
#endif
if( (0x8000 >> col) & fontdata ){
int drawX = posX + col * textsize;
int drawY = posY + row * textsize;
if( textsize == 1 ){
display->drawPixel(drawX, drawY, textcolor);
} else {
display->fillRect(drawX, drawY, textsize, textsize, textcolor);
}
}
}
#ifdef EFONT_DEBUG
Serial.write( "\n" );
#endif
}
// 描画カーソルを進める
posX += width;
// 折返し処理
if( display->width() <= posX ){
posX = 0;
posY += 16 * textsize;
}
}
// カーソルを更新
display->setCursor(posX, posY);
}
void printEfont(Adafruit_SSD1306 *display, char *str, int x, int y) {
display->setCursor(x, y);
printEfont(display, str);
}
void printEfont(Adafruit_SSD1306 *display, char *str, int x, int y, int textsize) {
display->setCursor(x, y);
//display->textsize = textsize;
printEfont(display, str);
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment