Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error moving TFT_Espi code to LGFX_TFT_eSPI.hpp #676

Open
tonbor opened this issue Feb 10, 2025 · 3 comments
Open

Error moving TFT_Espi code to LGFX_TFT_eSPI.hpp #676

tonbor opened this issue Feb 10, 2025 · 3 comments

Comments

@tonbor
Copy link

tonbor commented Feb 10, 2025

Carefully written Issues are more likely to be given priority.
丁寧に記述された報告は優先して対応される可能性が高くなります。

Environment ( 実行環境 )

  • MCU or Board name: [ESP-WROOM-32|ESP32-S3-BOX|M5Stack|WioTerminal|RPi2040|other]
  • Panel Driver IC: [ILI9341|SSD1963|ST7796|other]
  • Bus type: [SPI|I2C|Parallel8|Parallel16|other]
  • LovyanGFX version: [v0.4.17|latest develop|other]
  • FrameWork version: [ArduinoESP32 v2.0.3|ESP-IDF v4.4|other]
  • Build Environment: [ArduinoIDE|PlatformIO|Eclipse|other]
  • Operating System: [Windows|Linux|macOS]

Problem Description ( 問題の内容 )

e:\Arduino files\libraries\LovyanGFX\src/LGFX_TFT_eSPI.hpp:27:18: error: 'LGFX' does not name a type
27 | using TFT_eSPI = LGFX;
| ^~~~
E:\Arduino files\lovyan_open_weather_v01\GfxUi.h:23:17: error: expected ')' before '*' token
23 | GfxUi(TFT_eSPI *tft, OpenFontRender *render);
| ~ ^~
| )
E:\Arduino files\lovyan_open_weather_v01\GfxUi.h:31:3: error: 'TFT_eSPI' does not name a type; did you mean 'TFT_eSprite'?
31 | TFT_eSPI *_tft;
| ^~~~~~~~
| TFT_eSprite
E:\Arduino files\lovyan_open_weather_v01\GfxUi.cpp:10:13: error: expected constructor, destructor, or type conversion before '(' token
10 | GfxUi::GfxUi(TFT_eSPI *tft, OpenFontRender *ofr) {

Expected Behavior ( 期待される動作 )

Actual Behavior ( 実際の動作 )

Steps to reproduce ( 再現のための前提条件 )

try to move TFT_Espi code to lovyan

// If possible, attach a picture of your setup/wiring here.

Code to reproduce this issue ( 再現させるためのコード )

Please submit complete source code that can reproduce your problem.
あなたの問題を再現できる完全なソースコードを提示してください。

// SPDX-FileCopyrightText: 2023 ThingPulse Ltd., https://thingpulse.com
// SPDX-License-Identifier: MIT

// Based on https://github.com/Bodmer/OpenWeather/blob/main/examples/TFT_eSPI_OpenWeather_LittleFS/GfxUi.h

#pragma once

#include <FS.h>
#include <LittleFS.h>
#include <OpenFontRender.h>
//#include <TFT_eSPI.h>
#include <LGFX_TFT_eSPI.hpp>
// JPEG decoder library
#include <TJpg_Decoder.h>

// Maximum of 85 for BUFFPIXEL as 3 x this value is stored in an 8 bit variable!
// 32 is an efficient size for LittleFS due to SPI hardware pipeline buffer size
// A larger value of 80 is better for SD cards
#define BUFFPIXEL 32

class GfxUi {
public:
GfxUi(TFT_eSPI *tft, OpenFontRender *render);
void drawBmp(String filename, uint16_t x, uint16_t y);
void drawLogo();
void drawProgressBar(uint16_t x, uint16_t y, uint16_t w, uint16_t h,
uint8_t percentage, uint16_t frameColor,
uint16_t barColor);

private:
TFT_eSPI *_tft;
OpenFontRender *_ofr;
uint16_t read16(fs::File &f);
uint32_t read32(fs::File &f);
};

@tonbor
Copy link
Author

tonbor commented Feb 16, 2025

anybody?

@tonbor
Copy link
Author

tonbor commented Feb 20, 2025

This is what AI ddeep seek makes of the code, same as me, same problem
/**The MIT License (MIT)
Copyright (c) 2015 by Daniel Eichhorn
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
See more at http://blog.squix.ch
*/

// Adapted by Bodmer to use the TFT_eSPI library:
// https://github.com/Bodmer/TFT_eSPI

#include <LovyanGFX.hpp> // Hardware-specific library

#include <FS.h>
#include <LittleFS.h>

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

#ifndef _GFX_UI_H
#define _GFX_UI_H

// Maximum of 85 for BUFFPIXEL as 3 x this value is stored in an 8 bit variable!
// 32 is an efficient size for LittleFS due to SPI hardware pipeline buffer size
// A larger value of 80 is better for SD cards
#define BUFFPIXEL 32

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);

private:
LGFX * _tft;
uint16_t read16(fs::File &f);
uint32_t read32(fs::File &f);

};

#endif
/**The MIT License (MIT)
Copyright (c) 2015 by Daniel Eichhorn
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
See more at http://blog.squix.ch
*/

// Adapted by Bodmer to use the TFT_eSPI library: https://github.com/Bodmer/TFT_eSPI
// Functions no longer needed weeded out, Jpeg decoder functions added and updated
// drawBMP() updated to buffer input and output pixels and avoid slow seeks

#include "GfxUi.h"

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 ( !LittleFS.exists(filename) )
{
Serial.println(F(" File not found")); // Can comment out if not needed
return;
}

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

uint32_t seekOffset;
uint16_t w, h, row;
uint8_t r, g, b;
bool oldSwap = false;

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;

  oldSwap = _tft->getSwapBytes();
  _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.");

}
_tft->setSwapBytes(oldSwap);
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;
}

//====================================================================================

@tonbor
Copy link
Author

tonbor commented Feb 24, 2025

well after modifing the source a lot, some C++ Class to plain C, I got it working after days

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant