int ix = 0;
void scrolling() {
  uint32_t start = 598500;
  String text = "000000000000000000000";
  for (int ix = 0; ix < 20 - 5; ix++) {
    uint32_t charpos1 = ((uint32_t)((uint32_t)text.charAt(ix) - (uint32_t)42) * (uint32_t)78UL * 100UL * 3UL);
    uint32_t charpos2 = ((uint32_t)((uint32_t)text.charAt(ix + 5) - (uint32_t)42) * (uint32_t)78UL * 100UL * 3UL);
    String myScroll = text.substring(ix, ix + 5);
    for (int stringx = 0; stringx < 78; stringx++) {
      transparentTileImage( start + charpos1  , bgstart, 6 * 78 - stringx , 165, 78, 100, 77 - stringx, 0xFFFFFF);
      DrawString( 598500  , 78 - stringx, 165, 78, 100, myScroll, false, true);
      transparentTileImageRight( start + charpos2 , bgstart, 0 , 165, 78, 100, stringx, 0xFFFFFF);
      delay(10);
    }
  }
}


void DrawString( uint32_t check, int x, int y, int width, int height, String text, bool compare, bool transp) {
  int lengthl = text.length();

  for (int l = 0; l < lengthl; l++)
  {
    uint32_t charpos = ((uint32_t)((uint32_t)text.charAt(l) - (uint32_t)48) * (uint32_t)width * height * 3);
    //  uint32_t charpos = ((uint32_t)((uint32_t)text.charAt(l) - (uint32_t)48) * (uint32_t)width * height * 3);

    if (text.charAt(l) == '.')
      charpos = ((uint32_t)((uint32_t)11) * (uint32_t)width * height * 3);
    else  if (text.charAt(l) == ' ')
      charpos = ((uint32_t)((uint32_t)12) * (uint32_t)width * height * 3);
    if (transp)
      transparentImage( check + charpos, bgstart, x + ( l * width), y, width, height,  0xFFFFFF);
    else
      notransparentImage( check + charpos, bgstart, x + ( l * width), y, width, height, 0xFFFFFF);
    lasttext = text;
  }
}

uint32_t startdata = 0x151000;
void transparentImage(uint32_t startpic, uint32_t startbackground, int x, int y, int width, int height, uint32_t tcolor) {
  tft.setWindow(x, y, x + width - 1, y + height - 1);
  //read background clipping
  // File f = SPIFFS.open( "/test.bin", "r");
  char *buf = (char*)malloc(width * height * 3);
  char *temp = (char*)malloc(width * 3);
  unsigned long offset = 0;
  for (int i = 0; i < height; i++) {
    unsigned long offsetbg = startbackground + ((y + i) * 480 * 3 ) + (x * 3) ;
    memcpy(buf + (i * width * 3), data + offsetbg, (width * 3) );
  }

  for (int i = 0; i < height; i++) {
    unsigned long offset = startpic + i * width * 3;
    //spi_flash_read(startdata+offset, (char*)temp, width*3);
    memcpy(temp, data + offset, (width * 3) );
    for (int h = 0; h < width * 3; h = h + 3)
    {
      uint32_t bp = temp[ h];
      bp = (bp << 8) | temp[h + 1];
      bp = (bp << 16) | temp[ h + 2];
      if (bp >= 0x111111 || y + i > 272) {
        buf[width * i * 3 + h] = temp[h];
        buf[width * i * 3 + h + 1] = temp[h + 1];
        buf[width * i * 3 + h + 2] = temp[h + 2];
      }
    }
  }

  vspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
  digitalWrite(5, LOW); //pull SS slow to prep other end for transfer
  vspi->writeBytes((uint8_t*)buf, width * height  * 3);
  digitalWrite(5, HIGH); //pull ss high to signify end of data transfer
  vspi->endTransaction();
  //  f.close();
  //Serial.println("test");
  free(buf);
  free(temp);
}


void transparentTileImage(uint32_t startpic, uint32_t startbackground, int x, int y, int width, int height, int clipping , uint32_t tcolor) {
  tft.setWindow(x, y, x + (width - clipping) - 1, y + height - 1);
  //read background clipping
  // File f = SPIFFS.open( "/test.bin", "r");
  char *buf = (char*)malloc((width - clipping) * height * 3);
  char *temp = (char*)malloc((width - clipping) * 3);
  unsigned long offset = 0;
  for (int i = 0; i < height; i++) {
    unsigned long offsetbg = startbackground + ((y + i) * 480 * 3 ) + (x * 3) ;
    memcpy(buf + (i * (width - clipping) * 3), data + offsetbg, ((width - clipping) * 3) );
  }

  for (int i = 0; i < height; i++) {
    unsigned long offset = startpic + i * width * 3;
    //spi_flash_read(startdata+offset, (char*)temp, width*3);
    memcpy(temp, data + offset, ((width - clipping) * 3) );
    for (int h = 0; h < (width - clipping) * 3; h = h + 3)
    {
      uint32_t bp = temp[h];
      bp = (bp << 8) | temp[h + 1];
      bp = (bp << 16) | temp[ h + 2];
      if (bp >= 0x111111 || y + i > 272) {
        buf[(width - clipping) * i * 3 + h] = temp[h];
        buf[(width - clipping) * i * 3 + h + 1] = temp[h + 1];
        buf[(width - clipping) * i * 3 + h + 2] = temp[h + 2];
      }
    }
  }

  vspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
  digitalWrite(5, LOW); //pull SS slow to prep other end for transfer
  vspi->writeBytes((uint8_t*)buf, (width - clipping) * height  * 3);
  digitalWrite(5, HIGH); //pull ss high to signify end of data transfer
  vspi->endTransaction();
  //  f.close();

  free(buf);
  free(temp);
}

void transparentTileImageRight(uint32_t startpic, uint32_t startbackground, int x, int y, int width, int height, int clipping , uint32_t tcolor) {
  tft.setWindow(x, y, x + (width - clipping) - 1, y + height - 1);
  char *buf = (char*)malloc( (width - clipping) * height * 3);
  char *temp = (char*)malloc( (width - clipping) * 3);
  unsigned long offset = 0;
  for (int i = 0; i < height; i++) {
    unsigned long offsetbg = startbackground + ((y + i) * 480 * 3 ) + (x * 3) ;
    memcpy(buf + (i * (width - clipping) * 3), data + offsetbg, ( width - clipping) * 3 );
  }

  for (int i = 0; i < height; i++) {
    unsigned long offset = startpic + ((i * width) + (clipping)) * 3;
    //spi_flash_read(startdata+offset, (char*)temp, width*3);
    memcpy(temp, data + offset, (width - clipping) * 3 );
    for (int h = 0; h < (width - clipping) * 3; h = h + 3)
    {
      uint32_t bp = temp[h];
      bp = (bp << 8) | temp[h + 1];
      bp = (bp << 16) | temp[ h + 2];
      if (bp >= 0x111111 || y + i > 272) {
        buf[(width - clipping)* i * 3 + h] = temp[h];
        buf[(width - clipping)* i * 3 + h + 1] = temp[h + 1];
        buf[(width - clipping)* i * 3 + h + 2] = temp[h + 2];
      }
    }
  }

  vspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
  digitalWrite(5, LOW); //pull SS slow to prep other end for transfer
  vspi->writeBytes((uint8_t*)buf, (width - clipping) * height  * 3);
  digitalWrite(5, HIGH); //pull ss high to signify end of data transfer
  vspi->endTransaction();

  free(buf);
  free(temp);
}