kublet / kgfx

0 stars 2 forks source link

Can't draw anything over 180px High #1

Open davidgs opened 4 months ago

davidgs commented 4 months ago

It appears that there is a major bug in the KGFX library.

TFT_eSprite Spr = ui.createSprite(240, 190);

Doesn't actually work. In fact, it causes the Sprite to be set to 240x240, and then nothing is drawn to the screen. Anything over 180px in height causes this bug.

This means that you can't draw to the whole screen, as the last part is inaccessible. IMG_2075

davidgs commented 4 months ago

Complete code to reproduce this:


#include <Arduino.h>
#include <otaserver.h>
#include <kgfx.h>

OTAServer otaserver;
KGFX ui;

TFT_eSprite Spr = ui.createSprite(240, 185);
void setup() {
  Serial.begin(460800);
  Serial.println("Starting app");

  otaserver.connectWiFi(); // DO NOT EDIT.
  otaserver.run(); // DO NOT EDIT

  ui.init();
  ui.clear();
  int h = Spr.getViewportHeight();
  int w = Spr.getViewportWidth();
  Serial.printf("Sprite height: %d, width: %d\n", h, w);
  Spr.fillSprite(TFT_RED);
  Spr.pushSprite(0, 0);
}

void loop() {
  if((WiFi.status() == WL_CONNECTED)) {
    otaserver.handle(); // DO NOT EDIT
  }
  delay(1);
}

You will then see on the console output: Sprite height: 185, width: 240

If you then set the height to 188 you will see nothing on the screen, and the following on the console: Sprite height: 240, width: 240

audreylim commented 4 months ago

Thanks for alerting us to this. A sprite is kept in the processors RAM, and since space is limited, a 16-bit color Sprite is limited to about 200x200 pixels (~80Kbytes), an 8-bit sprite to 320x240 pixels (~76kbytes).

If you want a 16-bit color sprite, you can go up to a max size of a 240x80 sprite, or 200x200, in those ranges. For large sprites of 240x240 we would create an 8 bit sprite. I've created a release 0.0.17 with the following new/modified APIs:

TFT_eSprite createSpriteLarge(int width, int height); // can go to 240x240
void createChartSpriteLarge(int x, int y); // can go to 240x240

void drawChart(std::vector<float> arr, int color, int y, int spacing=7, int height=80);
void drawChartWide(std::vector<float> arr, int color, int y); // using spacing 8 which stretches the graph horizontally
void drawChartLarge(std::vector<float> arr, int color, int y, int height=120); // leave height param blank for default 120px height

Example usage:

  ui.createChartSpriteLarge(240, 240);

  float a[30] = {1, 3, 4, 12, 2, 1, 5, 4, 3, 12, 4, 5, 3, 2, 4, 4, 5, 6, 3, 2, 2, 4, 5, 2, 3, 4, 5, 6, 3, 4};
  std::vector<float> arr;
  for (int i=0; i<30; i++) {
    arr.push_back(a[i]);
  }

  ui.drawChartLarge(arr, K_GREEN, 0);

  // OR  ui.drawChartLarge(arr, K_GREEN, 0, 160);

For sprites that are smaller you can use the previously existing APIs and have access to the 16 bit color range.

Update platformio.ini:

kublet/kgfx@^0.0.17

Might need to run pio system prune if the latest version isn't found.

Let us know if this works.

EDIT: use latest v0.0.18

davidgs commented 4 months ago

IMG_2078

That works. Thanks! It's still a little off on the right margin, but it's so much better than it was!

audreylim commented 4 months ago

That's great! If you want to fill the entire width we can possibly allow for an array size of 32 or 33 instead of 30. But we thought 30 was a nice cutoff and the right margin allows room for chart markets. Let me know if you need it.