Skip to content

text_on_image.tc

text_on_image.tc — Flicker-free text overlay on JPEG background

Source on GitHub

// text_on_image.tc — Flicker-free text overlay on JPEG background
//
// Demonstrates dspImgText() which composites text onto an image
// sub-rect in RAM and pushes the result in one SPI transaction.
// The image pixels serve as background — no erase/redraw flicker.
//
// dspImgText(slot, x, y, color, fieldWidth, align, text)
//   slot:       image slot from dspLoadImage()
//   x, y:       pixel position on image (and screen)
//   color:      RGB565 text color
//   fieldWidth: field width in chars (0 = auto fit to text length)
//   align:      0=left, 1=right, 2=center
//
// Requires: /Aerial.jpg on device filesystem

#define IMAGE   "/Aerial.jpg"
#define TEXT_X  10
#define BLACK   0
#define WHITE   65535

int img;
int img_w;
int img_h;

void main() {
  char buf[80];
  char tmp[32];

  img = dspLoadImage(IMAGE);
  if (img < 0) {
    dspText("[x10y20Ci7]Image load failed!");
    return;
  }

  img_w = dspImageWidth(img);
  img_h = dspImageHeight(img);

  // draw full image as background
  dspPushImageRect(img, 0, 0, 0, 0, img_w, img_h);

  int count = 0;
  int y;

  while (1) {
    y = 10;

    // small font — left-aligned status line
    dspText("[f1s1]");
    int h1 = dspTextHeight();
    strcpy(buf, "Count=");
    sprintf(tmp, "%d", count);
    strcat(buf, tmp);
    strcat(buf, "  Heap=");
    sprintf(tmp, "%d", tasm_heap);
    strcat(buf, tmp);
    dspImgText(img, TEXT_X, y, YELLOW, 40, 0, buf);
    y = y + h1 + 4;

    // medium font — left-aligned uptime
    dspText("[f2s1]");
    int h2 = dspTextHeight();
    sprintf(buf, "Uptime: %ds", tasm_uptime);
    dspImgText(img, TEXT_X, y, YELLOW, 24, 0, buf);
    y = y + h2 + 4;

    // large font — right-aligned value
    dspText("[f3s1]");
    sprintf(buf, "%d kB", tasm_heap / 1024);
    dspImgText(img, TEXT_X, y, YELLOW, 16, 1, buf);

    count = count + 1;
    delay(1000);
  }
}