Electronics > Microcontrollers

Bodmer/TFT_eSPI to library GfxUi to LGFX in C++

(1/1)

ats3788:
Hello
I need someone with C++ knowledge. My knowledge in C++ is limited.
I rewrote the GfxUi in the main.cpp file and it works, but I don't get it compiled successfully, when I try to make a Header and cpp File.

The Original File is right here https://github.com/Bodmer/DarkSkyWeather/blob/master/examples/TFT_eSPI_weather/GfxUi.h
My new 1024 x 600 display does not work with the Super tolle work from  https://github.com/Bodmer/TFT_eSPI
Mr. Bodmer did a great Job with graphics and sprites in the MCU world


--- Code: ---
#define LGFX_USE_V1
#include <LovyanGFX.hpp>
 #define SCREEN_HD
//#define SCREEN_NORMAL

#ifdef SCREEN_HD
#define SCREEN_W 1024
#define SCREEN_H 600
#endif

#define FS_NO_GLOBALS // Avoid conflict with SD library File type definition
#include <FS.h>

// JPEG decoder library
#include <JPEGDecoder.h>

class GfxUi {
  public:
    GfxUi(LGFX * tft);
   void drawBmp(String filename, uint16_t x, uint16_t y);
    void drawProgressBar(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t percentage, uint16_t frameColor, uint16_t barColor);
    void jpegInfo();
    void drawJpeg(String filename, int xpos, int ypos);
    void jpegRender(int xpos, int ypos);
   
  private:
    LGFX * _tft;
    uint16_t read16(fs::File &f);
    uint32_t read32(fs::File &f);

};


GfxUi::GfxUi(LGFX *tft) {
  _tft = tft;
}



void GfxUi::drawProgressBar(uint16_t x0, uint16_t y0, uint16_t w, uint16_t h, uint8_t percentage, uint16_t frameColor, uint16_t barColor) {
  if (percentage == 0) {
    _tft->fillRoundRect(x0, y0, w, h, 3, TFT_BLACK);
  }
  uint8_t margin = 2;
  uint16_t barHeight = h - 2 * margin;
  uint16_t barWidth = w - 2 * margin;
  _tft->drawRoundRect(x0, y0, w, h, 3, frameColor);
  _tft->fillRect(x0 + margin, y0 + margin, barWidth * percentage / 100.0, barHeight, barColor);
}

// Bodmer's streamlined x2 faster "no seek" version
void GfxUi::drawBmp(String filename, uint16_t x, uint16_t y)
{

  if ((x >= _tft->width()) || (y >= _tft->height())) return;

  fs::File bmpFS;

  // Check file exists and open it
  // Serial.println(filename);

  // Note: ESP32 passes "open" test even if file does not exist, whereas ESP8266 returns NULL
  if ( !SPIFFS.exists(filename) )
  {
    Serial.println(F(" File not found")); // Can comment out if not needed
    return;
  }

  // Open requested file
  bmpFS = SPIFFS.open(filename, "r");

  uint32_t seekOffset;
  uint16_t w, h, row, col;
  uint8_t  r, g, b;

  if (read16(bmpFS) == 0x4D42)
  {
    read32(bmpFS);
    read32(bmpFS);
    seekOffset = read32(bmpFS);
    read32(bmpFS);
    w = read32(bmpFS);
    h = read32(bmpFS);

    if ((read16(bmpFS) == 1) && (read16(bmpFS) == 24) && (read32(bmpFS) == 0))
    {
      y += h - 1;

      _tft->setSwapBytes(true);
      bmpFS.seek(seekOffset);

      // Calculate padding to avoid seek
      uint16_t padding = (4 - ((w * 3) & 3)) & 3;
      uint8_t lineBuffer[w * 3 + padding];

      for (row = 0; row < h; row++) {
       
        bmpFS.read(lineBuffer, sizeof(lineBuffer));
        uint8_t*  bptr = lineBuffer;
        uint16_t* tptr = (uint16_t*)lineBuffer;
        // Convert 24 to 16 bit colours using the same line buffer for results
        for (uint16_t col = 0; col < w; col++)
        {
          b = *bptr++;
          g = *bptr++;
          r = *bptr++;
          *tptr++ = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
        }

        // Push the pixel row to screen, pushImage will crop the line if needed
        // y is decremented as the BMP image is drawn bottom up
        _tft->pushImage(x, y--, w, 1, (uint16_t*)lineBuffer);
      }
    }
    else Serial.println("BMP format not recognized.");
  }
  bmpFS.close();
}

// These read 16- and 32-bit types from the SD card file.
// BMP data is stored little-endian, Arduino is little-endian too.
// May need to reverse subscript order if porting elsewhere.

uint16_t GfxUi::read16(fs::File &f) {
  uint16_t result;
  ((uint8_t *)&result)[0] = f.read(); // LSB
  ((uint8_t *)&result)[1] = f.read(); // MSB
  return result;
}

uint32_t GfxUi::read32(fs::File &f) {
  uint32_t result;
  ((uint8_t *)&result)[0] = f.read(); // LSB
  ((uint8_t *)&result)[1] = f.read();
  ((uint8_t *)&result)[2] = f.read();
  ((uint8_t *)&result)[3] = f.read(); // MSB
  return result;
}







--- End code ---

Navigation

[0] Message Index

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod