Skip to content

Instantly share code, notes, and snippets.

@gregslomin
Created October 2, 2015 05:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregslomin/2c29d3e4ee6b65ad744d to your computer and use it in GitHub Desktop.
Save gregslomin/2c29d3e4ee6b65ad744d to your computer and use it in GitHub Desktop.
void drawFontChar(Adafruit_ILI9340 *display, unsigned int c) {
uint32_t bitoffset;
const uint8_t *data;
int oldX = display->cursor_x;
int oldY = display->cursor_y;
//Serial.printf("drawFontChar %d\n", c);
if (c >= font->index1_first && c <= font->index1_last) {
bitoffset = c - font->index1_first;
bitoffset *= font->bits_index;
} else if (c >= font->index2_first && c <= font->index2_last) {
bitoffset = c - font->index2_first + font->index1_last
- font->index1_first + 1;
bitoffset *= font->bits_index;
} else if (font->unicode) {
return; // TODO: implement sparse unicode
} else {
return;
}
//Serial.printf(" index = %d\n", fetchbits_unsigned(font->index, bitoffset, font->bits_index));
data = font->data
+ fetchbits_unsigned(font->index, bitoffset, font->bits_index);
uint32_t encoding = fetchbits_unsigned(data, 0, 3);
if (encoding != 0)
return;
uint32_t width = fetchbits_unsigned(data, 3, font->bits_width);
bitoffset = font->bits_width + 3;
uint32_t height = fetchbits_unsigned(data, bitoffset, font->bits_height);
bitoffset += font->bits_height;
//Serial.printf(" size = %d,%d\n", width, height);
int32_t xoffset = fetchbits_signed(data, bitoffset, font->bits_xoffset);
bitoffset += font->bits_xoffset;
int32_t yoffset = fetchbits_signed(data, bitoffset, font->bits_yoffset);
bitoffset += font->bits_yoffset;
//Serial.printf(" offset = %d,%d\n", xoffset, yoffset);
uint32_t delta = fetchbits_unsigned(data, bitoffset, font->bits_delta);
bitoffset += font->bits_delta;
//Serial.printf(" delta = %d\n", delta);
//Serial.printf(" cursor = %d,%d\n", cursor_x, cursor_y);
fillColor(fontColor, width);
// horizontally, we draw every pixel, or none at all
if (display->cursor_x < 0)
display->cursor_x = 0;
int32_t origin_x = display->cursor_x + xoffset;
if (origin_x < 0) {
display->cursor_x -= xoffset;
origin_x = 0;
}
if (origin_x + (int) width > display->_width) {
//if (!wrap) return;
origin_x = 0;
if (xoffset >= 0) {
display->cursor_x = 0;
} else {
display->cursor_x = -xoffset;
}
display->cursor_y += font->line_space;
}
if (display->cursor_y >= display->_height)
return;
//fillRect(display, display->cursor_x, display->cursor_y, delta, height, 0x00);
display->cursor_x += delta;
memset(buffer, 0x00, 64*64*2);
int l = 0;
// for(l=0; l<xoffset; l++) {
// drawFastVLine(display, origin_x+l-1, display->cursor_y, height, 0x00);
// }
// for(l=display->cursor_x; l>origin_x+xoffset+width; l--) {
// drawFastVLine(display, l, display->cursor_y, height, 0x00);
// }
// vertically, the top and/or bottom can be clipped
int32_t origin_y = display->cursor_y + font->cap_height - height - yoffset;
//Serial.printf(" origin = %d,%d\n", origin_x, origin_y);
// TODO: compute top skip and number of lines
int32_t linecount = height;
//uint32_t loopcount = 0;
uint32_t y = 0;//origin_y;
while (linecount) {
//Serial.printf(" linecount = %d\n", linecount);
uint32_t b = fetchbit(data, bitoffset++);
if (b == 0) {
//Serial.println(" single line");
uint32_t x = 0;
do {
uint32_t xsize = width - x;
if (xsize > 32)
xsize = 32;
uint32_t bits = fetchbits_unsigned(data, bitoffset, xsize);
drawFontBits(display, bits, xsize, x, y, 1, (uint16_t*)buffer, display->cursor_x-oldX+1);
bitoffset += xsize;
x += xsize;
} while (x < width);
y++;
linecount--;
} else {
uint32_t n = fetchbits_unsigned(data, bitoffset, 3) + 2;
bitoffset += 3;
uint32_t x = 0;
do {
uint32_t xsize = width - x;
if (xsize > 32)
xsize = 32;
//Serial.printf(" multi line %d\n", n);
uint32_t bits = fetchbits_unsigned(data, bitoffset, xsize);
drawFontBits(display, bits, xsize, x, y, n, (uint16_t*)buffer, display->cursor_x-oldX+1);
bitoffset += xsize;
x += xsize;
} while (x < width);
y += n;
linecount -= n;
}
setAddrWindow(display, oldX, oldY, display->cursor_x, oldY+48);
SET_BIT(display->dcPort, display->dc);
flush();
sspSendDMA(buffer, delta*50*2);
static long watchdog = 0;
watchdog = 0;
while (!ssp_dma_txf_completed || !ssp_dma_rxf_completed) {
if (++watchdog >= 240000) {
//Chip_GPDMA_Stop(LPC_GPDMA, ssp_dma_rx_ch);
break;
}
}
flush();
CLEAR_BIT(display->dcPort, display->dc);
flush();
//if (++loopcount > 100) {
//Serial.println(" abort draw loop");
//break;
//}
}
}
void fakeDrawFontChar(Adafruit_ILI9340 *display, unsigned int c) {
uint32_t bitoffset;
const uint8_t *data;
//Serial.printf("drawFontChar %d\n", c);
if (c >= font->index1_first && c <= font->index1_last) {
bitoffset = c - font->index1_first;
bitoffset *= font->bits_index;
} else if (c >= font->index2_first && c <= font->index2_last) {
bitoffset = c - font->index2_first + font->index1_last
- font->index1_first + 1;
bitoffset *= font->bits_index;
} else if (font->unicode) {
return; // TODO: implement sparse unicode
} else {
return;
}
//Serial.printf(" index = %d\n", fetchbits_unsigned(font->index, bitoffset, font->bits_index));
data = font->data
+ fetchbits_unsigned(font->index, bitoffset, font->bits_index);
uint32_t encoding = fetchbits_unsigned(data, 0, 3);
if (encoding != 0)
return;
uint32_t width = fetchbits_unsigned(data, 3, font->bits_width);
bitoffset = font->bits_width + 3;
uint32_t height = fetchbits_unsigned(data, bitoffset, font->bits_height);
bitoffset += font->bits_height;
//Serial.printf(" size = %d,%d\n", width, height);
int32_t xoffset = fetchbits_signed(data, bitoffset, font->bits_xoffset);
bitoffset += font->bits_xoffset;
int32_t yoffset = fetchbits_signed(data, bitoffset, font->bits_yoffset);
bitoffset += font->bits_yoffset;
//Serial.printf(" offset = %d,%d\n", xoffset, yoffset);
uint32_t delta = fetchbits_unsigned(data, bitoffset, font->bits_delta);
bitoffset += font->bits_delta;
//Serial.printf(" delta = %d\n", delta);
//Serial.printf(" cursor = %d,%d\n", cursor_x, cursor_y);
fillColor(fontColor, width);
// horizontally, we draw every pixel, or none at all
if (display->cursor_x < 0)
display->cursor_x = 0;
int32_t origin_x = display->cursor_x + xoffset;
if (origin_x < 0) {
display->cursor_x -= xoffset;
origin_x = 0;
}
if (origin_x + (int) width > display->_width) {
//if (!wrap) return;
origin_x = 0;
if (xoffset >= 0) {
display->cursor_x = 0;
} else {
display->cursor_x = -xoffset;
}
display->cursor_y += font->line_space;
}
if (display->cursor_y >= display->_height)
return;
display->cursor_x += delta;
}
void drawFontBits(Adafruit_ILI9340 *display, uint32_t bits, uint32_t numbits,
uint32_t x, uint32_t y, uint32_t repeat, uint16_t *buffer, uint16_t width) {
// TODO: replace this *slow* code with something fast...
//Serial.printf(" %d bits at %d,%d: %X\n", numbits, x, y, bits);
if (bits == 0)
return;
do {
uint32_t x1 = x;
uint32_t n = numbits;
#if 1
do {
n--;
if (bits & (1 << n)) {
buffer[y*width+x1] = fontColor;
//drawPixel(display, x1, y, fontColor);
//Serial.printf(" pixel at %d,%d\n", x1, y);
}
x1++;
}while (n > 0);
#endif
#if 0
int w = 0;
do {
n--;
if (bits & (1 << n)) {
w++;
} else if (w > 0) {
drawFastHLine(display, x1 - w, y, w, fontColor);
w = 0;
}
x1++;
} while (n > 0);
if (w > 0)
drawFastHLine(display, x1 - w, y, w, fontColor);
#endif
y++;
repeat--;
} while (repeat);
}
void updateAndDraw(gfx_label *label, Adafruit_ILI9340 *display, char *str) {
// init is false on first draw
if(!label->init || strcmp(label->str, str)) {
// Save old state
ILI9341_t3_font_t *oldFont = getFontReal();
uint16_t oldFontColor = getFontColor();
// Set font and position
setFontReal(label->font);
display->cursor_x = label->x;
display->cursor_y = label->y;
int drawPos = 0;
// If the old string is not blank
if(label->str != 0) {
int newStrLen = strlen(str);
int temp = 0;
int max = 0;
if(newStrLen >= label->oldStrlen) {
max = label->oldStrlen;
} else {
max = newStrLen;
}
while(temp < max) {
if(str[temp] == label->str[temp])
drawPos++;
else
break;
temp++;
}
label->oldStrlen = newStrLen;
//setFontColor(0x00);
//DrawStringWithFakeChars(display, label->str, label->x, label->y, 1, label->color, drawPos);
} else {
label->oldStrlen = strlen(str);
}
setFontColor(label->color);
DrawStringWithFakeChars(display, str, label->x, label->y, 1, label->color, drawPos);
// Restore state
setFontColor(oldFontColor);
setFontReal(oldFont);
strcpy(label->str, str);
label->init = 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment