Skip to content

Instantly share code, notes, and snippets.

@mamuesp
Created July 28, 2018 17:09
Show Gist options
  • Save mamuesp/338963396ef9e6816d7e9a3bf2035e7b to your computer and use it in GitHub Desktop.
Save mamuesp/338963396ef9e6816d7e9a3bf2035e7b to your computer and use it in GitHub Desktop.
Extend the Mongoose-OS lib ili9341-spi and allows to draw DIFs driectly from memory
bool mgos_draw_dif_zipped(uint16_t x, uint16_t y, const char * difName) {
struct mbuf *picRead = NULL;
struct mbuf *picCache = NULL;
char *bmpBuf = NULL;
char *dif_hdr = NULL;
char *dif_start = NULL;
size_t bufLen = 0;
size_t picLen = 0;
size_t difhLen = 16;
size_t w, h;
bool doCache = true;
doCache = doCache && (strstr(difName, "Heydo-Starting-0") == NULL);
picRead = doCache ? (struct mbuf *) find_in_cache((char *) difName) : NULL;
if (picRead != NULL) {
LOG(LL_INFO, ("Found cache entry: pic size is <%ld>", (unsigned long) picRead->len));
// just point to the start ...
bmpBuf = picRead->buf;
dif_hdr = bmpBuf;
dif_start = (bmpBuf + difhLen);
bufLen = picRead->len;
picLen = bufLen - difhLen;
} else {
const char *archFile = mgos_sys_config_get_heydo_bmpArchFile();
bufLen = mgos_get_zipped_buffer_size((char *) archFile, (char *) difName);
if (doCache) {
picCache = heap_caps_malloc(sizeof(struct mbuf), MALLOC_CAP_8BIT);
picCache->buf = heap_caps_malloc (bufLen + 1, MALLOC_CAP_8BIT);
picCache->len = bufLen;
picCache->size = bufLen + 1;
add_to_cache((char *) difName, (char *) picCache);
//mbuf_init(picCache, bufLen);
//mbuf_append(picCache, bmpBuf, bufLen);
//mbuf_trim(picCache);
bmpBuf = (char *) mgos_get_zipped_data(archFile, difName, picCache->buf, picCache->size);
} else {
bmpBuf = (char *) mgos_get_zipped_data(archFile, difName, NULL, 0);
}
if (!bmpBuf) {
LOG(LL_ERROR, ("%s: Could not get zipped data from <%s>", difName, archFile));
doCache = false;
free(picCache->buf);
free(picCache);
picCache = NULL;
return false;
}
if (doCache) {
memcpy(picCache->buf, bmpBuf, bufLen);
}
// just point to the start ...
dif_hdr = bmpBuf;
dif_start = (bmpBuf + difhLen);
picLen = bufLen - difhLen;
if (dif_hdr[0] != 'D' || dif_hdr[1] != 'I' || dif_hdr[2] != 'F' || dif_hdr[3] != 1) {
LOG(LL_ERROR, ("%s: Invalid DIF header", difName));
mz_free(bmpBuf);
return false;
}
}
w = dif_hdr[7] + (dif_hdr[6] << 8) + (dif_hdr[5] << 16) + (dif_hdr[4] << 24);
h = dif_hdr[11] + (dif_hdr[10] << 8) + (dif_hdr[9] << 16) + (dif_hdr[8] << 24);
LOG(LL_INFO, ("%s: width=%d height=%d", difName, w, h));
mgos_ili9341_sendPixels(x, y, w, h, (uint8_t *) dif_start, picLen);
if (!doCache) {
mz_free(bmpBuf);
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment