Skip to content

Commit

Permalink
Fix ssd1306 font in ssd1306_i2c example (#404)
Browse files Browse the repository at this point in the history
* Fix ssd1306 font in ssd1306_i2c example

* Remove comment
  • Loading branch information
kuleshmaria authored Jul 6, 2023
1 parent eca13ac commit 085d5ac
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 59 deletions.
74 changes: 37 additions & 37 deletions i2c/ssd1306_i2c/ssd1306_font.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,41 @@
// Theses are defined vertically to make them quick to copy to FB

static uint8_t font[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Nothing
0x1e, 0x28, 0x48, 0x88, 0x48, 0x28, 0x1e, 0x00, //A
0xfe, 0x92, 0x92, 0x92, 0x92, 0x92, 0xfe, 0x00, //B
0x7e, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x00, //C
0xfe, 0x82, 0x82, 0x82, 0x82, 0x82, 0x7e, 0x00, //D
0xfe, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x00, //E
0xfe, 0x90, 0x90, 0x90, 0x90, 0x80, 0x80, 0x00, //F
0xfe, 0x82, 0x82, 0x82, 0x8a, 0x8a, 0xce, 0x00, //G
0xfe, 0x10, 0x10, 0x10, 0x10, 0x10, 0xfe, 0x00, //H
0x00, 0x00, 0x00, 0xfe, 0x00, 0x00, 0x00, 0x00, //I
0x84, 0x82, 0x82, 0xfc, 0x80, 0x80, 0x80, 0x00, //J
0x00, 0xfe, 0x10, 0x10, 0x28, 0x44, 0x82, 0x00, //K
0xfe, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, //L
0xfe, 0x40, 0x20, 0x10, 0x20, 0x40, 0xfe, 0x00, //M
0xfe, 0x40, 0x20, 0x10, 0x08, 0x04, 0xfe, 0x00, //N
0x7c, 0x82, 0x82, 0x82, 0x82, 0x82, 0x7c, 0x00, //O
0xfe, 0x88, 0x88, 0x88, 0x88, 0x88, 0x70, 0x00, //P
0x7c, 0x82, 0x82, 0x92, 0x8a, 0x86, 0x7e, 0x00, //Q
0xfe, 0x88, 0x88, 0x88, 0x8c, 0x8a, 0x70, 0x00, //R
0x62, 0x92, 0x92, 0x92, 0x92, 0x0c, 0x00, 0x00, //S
0x80, 0x80, 0x80, 0xfe, 0x80, 0x80, 0x80, 0x00, //T
0xfc, 0x02, 0x02, 0x02, 0x02, 0x02, 0xfc, 0x00, //U
0xf0, 0x08, 0x04, 0x02, 0x04, 0x08, 0xf0, 0x00, //V
0xfe, 0x04, 0x08, 0x10, 0x08, 0x04, 0xfe, 0x00, //W
0x00, 0x82, 0x44, 0x28, 0x28, 0x44, 0x82, 0x00, //X
0x80, 0x40, 0x20, 0x1e, 0x20, 0x40, 0x80, 0x00, //Y
0x82, 0x86, 0x9a, 0xa2, 0xc2, 0x82, 0x00, 0x00, //Z
0x7c, 0x82, 0x82, 0x92, 0x82, 0x82, 0x7c, 0x00, //0
0x00, 0x00, 0x42, 0xfe, 0x02, 0x00, 0x00, 0x00, //1
0x0c, 0x92, 0x92, 0x92, 0x92, 0x62, 0x00, 0x00, //2
0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x6c, 0x00, //3
0xfc, 0x04, 0x04, 0x1e, 0x04, 0x04, 0x00, 0x00, //4
0xf2, 0x92, 0x92, 0x92, 0x92, 0x0c, 0x00, 0x00, //5
0xfc, 0x12, 0x12, 0x12, 0x12, 0x12, 0x0c, 0x00, //6
0x80, 0x80, 0x80, 0x86, 0x8c, 0xb0, 0xc0, 0x00, //7
0x6c, 0x92, 0x92, 0x92, 0x92, 0x92, 0x6c, 0x00, //8
0x60, 0x90, 0x90, 0x90, 0x90, 0x90, 0xfe, 0x00, //9
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Nothing
0x78, 0x14, 0x12, 0x11, 0x12, 0x14, 0x78, 0x00, //A
0x7f, 0x49, 0x49, 0x49, 0x49, 0x49, 0x7f, 0x00, //B
0x7e, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x00, //C
0x7f, 0x41, 0x41, 0x41, 0x41, 0x41, 0x7e, 0x00, //D
0x7f, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x00, //E
0x7f, 0x09, 0x09, 0x09, 0x09, 0x01, 0x01, 0x00, //F
0x7f, 0x41, 0x41, 0x41, 0x51, 0x51, 0x73, 0x00, //G
0x7f, 0x08, 0x08, 0x08, 0x08, 0x08, 0x7f, 0x00, //H
0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00, 0x00, //I
0x21, 0x41, 0x41, 0x3f, 0x01, 0x01, 0x01, 0x00, //J
0x00, 0x7f, 0x08, 0x08, 0x14, 0x22, 0x41, 0x00, //K
0x7f, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00, //L
0x7f, 0x02, 0x04, 0x08, 0x04, 0x02, 0x7f, 0x00, //M
0x7f, 0x02, 0x04, 0x08, 0x10, 0x20, 0x7f, 0x00, //N
0x3e, 0x41, 0x41, 0x41, 0x41, 0x41, 0x3e, 0x00, //O
0x7f, 0x11, 0x11, 0x11, 0x11, 0x11, 0x0e, 0x00, //P
0x3e, 0x41, 0x41, 0x49, 0x51, 0x61, 0x7e, 0x00, //Q
0x7f, 0x11, 0x11, 0x11, 0x31, 0x51, 0x0e, 0x00, //R
0x46, 0x49, 0x49, 0x49, 0x49, 0x30, 0x00, 0x00, //S
0x01, 0x01, 0x01, 0x7f, 0x01, 0x01, 0x01, 0x00, //T
0x3f, 0x40, 0x40, 0x40, 0x40, 0x40, 0x3f, 0x00, //U
0x0f, 0x10, 0x20, 0x40, 0x20, 0x10, 0x0f, 0x00, //V
0x7f, 0x20, 0x10, 0x08, 0x10, 0x20, 0x7f, 0x00, //W
0x00, 0x41, 0x22, 0x14, 0x14, 0x22, 0x41, 0x00, //X
0x01, 0x02, 0x04, 0x78, 0x04, 0x02, 0x01, 0x00, //Y
0x41, 0x61, 0x59, 0x45, 0x43, 0x41, 0x00, 0x00, //Z
0x3e, 0x41, 0x41, 0x49, 0x41, 0x41, 0x3e, 0x00, //0
0x00, 0x00, 0x42, 0x7f, 0x40, 0x00, 0x00, 0x00, //1
0x30, 0x49, 0x49, 0x49, 0x49, 0x46, 0x00, 0x00, //2
0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x36, 0x00, //3
0x3f, 0x20, 0x20, 0x78, 0x20, 0x20, 0x00, 0x00, //4
0x4f, 0x49, 0x49, 0x49, 0x49, 0x30, 0x00, 0x00, //5
0x3f, 0x48, 0x48, 0x48, 0x48, 0x48, 0x30, 0x00, //6
0x01, 0x01, 0x01, 0x61, 0x31, 0x0d, 0x03, 0x00, //7
0x36, 0x49, 0x49, 0x49, 0x49, 0x49, 0x36, 0x00, //8
0x06, 0x09, 0x09, 0x09, 0x09, 0x09, 0x7f, 0x00, //9
};
27 changes: 5 additions & 22 deletions i2c/ssd1306_i2c/ssd1306_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ void SSD1306_init() {
SSD1306_SET_COM_OUT_DIR | 0x08, // set COM (common) output scan direction. Scan from bottom up, COM[N-1] to COM0
SSD1306_SET_DISP_OFFSET, // set display offset
0x00, // no offset
SSD1306_SET_COM_PIN_CFG, // set COM (common) pins hardware configuration. Board specific magic number.
SSD1306_SET_COM_PIN_CFG, // set COM (common) pins hardware configuration. Board specific magic number.
// 0x02 Works for 128x32, 0x12 Possibly works for 128x64. Other options 0x22, 0x32
#if ((SSD1306_WIDTH == 128) && (SSD1306_HEIGHT == 32))
0x02,
0x02,
#elif ((SSD1306_WIDTH == 128) && (SSD1306_HEIGHT == 64))
0x12,
#else
Expand Down Expand Up @@ -207,7 +207,7 @@ void render(uint8_t *buf, struct render_area *area) {
area->start_page,
area->end_page
};

SSD1306_send_cmd_list(cmds, count_of(cmds));
SSD1306_send_buf(buf, area->buflen);
}
Expand Down Expand Up @@ -274,24 +274,7 @@ static inline int GetFontIndex(uint8_t ch) {
else return 0; // Not got that char so space.
}

static uint8_t reversed[sizeof(font)] = {0};

static uint8_t reverse(uint8_t b) {
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
return b;
}
static void FillReversedCache() {
// calculate and cache a reversed version of fhe font, because I defined it upside down...doh!
for (int i=0;i<sizeof(font);i++)
reversed[i] = reverse(font[i]);
}

static void WriteChar(uint8_t *buf, int16_t x, int16_t y, uint8_t ch) {
if (reversed[0] == 0)
FillReversedCache();

if (x > SSD1306_WIDTH - 8 || y > SSD1306_HEIGHT - 8)
return;

Expand All @@ -303,7 +286,7 @@ static void WriteChar(uint8_t *buf, int16_t x, int16_t y, uint8_t ch) {
int fb_idx = y * 128 + x;

for (int i=0;i<8;i++) {
buf[fb_idx++] = reversed[idx * 8 + i];
buf[fb_idx++] = font[idx * 8 + i];
}
}

Expand Down Expand Up @@ -381,7 +364,7 @@ int main() {
area.end_col = IMG_WIDTH - 1;

calc_render_area_buflen(&area);

uint8_t offset = 5 + IMG_WIDTH; // 5px padding

for (int i = 0; i < 3; i++) {
Expand Down

0 comments on commit 085d5ac

Please sign in to comment.