2.8" TFT LCD Library Error fillTriangle

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

2.8" TFT LCD Library Error fillTriangle

Postby owendelong » Mon Feb 13, 2012 1:41 pm

The fillTriangle function is buggy and will (for some values of angle (i) in the following) fill nearly the whole screen instead of just the area intended.

Specifically, 8 seconds (48º) to 12 seconds (72º) will reliably break.

Less critical, but, also annoying, the two halves sometimes have some pixels in between them which are not drawn.
I have not tested whether a drawLine up the middle would work around the missing pixel issue or not.

Code to reproduce problem:


#include <TFTLCD.h>

// Modify as needed for your configuration
#define LCD_RESET A4
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_BL 8 // To Backlight Control Transistor

#define FACECENTRX 63
#define FACECENTRY 63

TFTLCD lcd(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

// This should initialize the display and draw a diamond shaped hand rotating around the dial ala a second hand on a clock.
// This example is reduced from an actual clock application to the minimum necessary to replicate the library bug
void setup()
{
lcd.reset();
lcd.initDisplay();
lcd.fillScreen(lcd.Color565(0,0,0));
lcd.setRotation(2);
pinMode(LCD_BL, OUTPUT);
digitalWrite(LCD_BL, HIGH);
int i;
while (1)
{
for(i=0; i<360; i+=6)
{
drawHand(lcd, i, 46, 255,255,255, 10);
delay(1000);
drawHand(lcd, i, 46, 0, 0, 0, 10);
}
}
}

void drawHand(TFTLCD D2, int a, int d, byte r, byte g, byte b, int width)
{
float c,w, z;
int dh_x2, dh_y2;
int dh_x1, dh_y1;
unsigned int co;
float rad;
co=D2.Color565(r,g,b);
c = ((a+270)%360); // orient correctly
z = c = c * PI/180;
// Draw hands as polygons (fast)
dh_x1 = cos(c)*d+FACECENTRX; dh_y1 = sin(c)*d+FACECENTRY; // tip of hand
w=width*PI/180;
c -= w/2; // Widen (go counterclockwise w/2 radians (== width/2 degrees)
rad = d*(d<40 ? 0.6 : 0.8);
dh_x2 = cos(c)*rad+FACECENTRX; dh_y2 = sin(c)*rad+FACECENTRY; // Counterclockwise wide point
D2.fillTriangle(FACECENTRX, FACECENTRY, dh_x1, dh_y1, dh_x2, dh_y2, D2.Color565(r, g, b)); // Draw counterclockwise half of hand
c += w; // Widen other direction (clockwise w radians == width degrees from counterclockwise edge)
dh_x2 = cos(c)*rad+FACECENTRX; dh_y2 = sin(c)*rad+FACECENTRY; // Colckwise wide point
D2.fillTriangle(FACECENTRX, FACECENTRY, dh_x1, dh_y1, dh_x2, dh_y2, D2.Color565(r, g, b)); // Draw clockwise half of hand
}

void loop()
{
}
owendelong
 
Posts: 51
Joined: Fri Feb 10, 2012 3:25 am

Re: 2.8" TFT LCD Library Error fillTriangle

Postby adafruit » Mon Feb 13, 2012 1:55 pm

Can you post an issue to the github repo? that would be most helpful!
User avatar
adafruit
 
Posts: 10489
Joined: Thu Apr 06, 2006 3:21 pm
Location: nyc

Re: 2.8" TFT LCD Library Error fillTriangle

Postby owendelong » Mon Feb 13, 2012 2:37 pm

OK... Issue created. It's currently the only open issue on the library.
owendelong
 
Posts: 51
Joined: Fri Feb 10, 2012 3:25 am

Re: 2.8" TFT LCD Library Error fillTriangle

Postby stimps » Thu Feb 16, 2012 8:49 am

Has anyone worked out what is going on with this?
I have had the same problem, with it filling the whole screen sometimes, getting away with it so i can finish the project, by keeping all three points of the triangle on a different x axis.
No fix for it yet?
stimps
 
Posts: 26
Joined: Wed Nov 09, 2011 7:24 pm

Re: 2.8" TFT LCD Library Error fillTriangle

Postby owendelong » Fri Feb 17, 2012 2:16 pm

Nothing from adafruit here and nothing on the issue at github so far.

I could really use a fix as well.

I'll have to investigate the x1/x2/x3 equivalence you describe. I hadn't put that much information together about the problem, but, if that's the case, that's a strong clue and I might end up trying to debug this myself. Unfortunately, my knowledge of the math involved in efficiently filling a triangle is less than minimal, so debugging someone else's algorithm for doing so will likely be a learning experience beyond what I was hoping for.
owendelong
 
Posts: 51
Joined: Fri Feb 10, 2012 3:25 am

Re: 2.8" TFT LCD Library Error fillTriangle

Postby adafruit » Sat Feb 18, 2012 5:05 pm

we have a long list of fixes to go thru, we'll get to this as soon as we can, but there is no ETA on when the fix will be pushed
User avatar
adafruit
 
Posts: 10489
Joined: Thu Apr 06, 2006 3:21 pm
Location: nyc

Re: 2.8" TFT LCD Library Error fillTriangle

Postby owendelong » Sun Feb 19, 2012 4:10 pm

Understood. I'll continue to look at it. I found a little bit more detail:


I've narrowed this down a little bit more.

The problem seems to occur when (after sorting the coordinate pairs such that y0<=y1<=y2, you are left with a case where x0<x2<x1.

It does not seem to matter whether or not any of the x values are equal or whether or not any of the y values are equal.

I'm continuing to work on figuring out a fix.

(also posted to github).

If I get a fix before you guys get to it, I'll post diffs here and on github.
owendelong
 
Posts: 51
Joined: Fri Feb 10, 2012 3:25 am

Re: 2.8" TFT LCD Library Error fillTriangle

Postby owendelong » Sun Feb 19, 2012 5:43 pm

Resolved...

Here's the deal... Sometimes the algorithm passes to drawHorizontalLine a value where x1>x2. This results in a line width being passed to drawHorizontalLine which is a negative signed long int. drawHorizontalLine expects an unsigned 16 bit integer.

Solution: reorder the arguments to drawHorizontalLine and reverse the computation for the width of the line when necessary.

Essentially replace occurrences of:

Code: Select all
      drawHorizontalLine(sx1/1000, sy, (sx2-sx1)/1000, color);


With:

Code: Select all
      if (sx2 < sx1)
      {
        drawHorizontalLine(sx2/1000, sy, (sx1-sx2)/1000, color);
      }
      else
      {
        drawHorizontalLine(sx1/1000, sy, (sx2-sx1)/1000, color);
      }

Hopefully when you guys merge this into the library, you can also merge in my improvements to drawChar.
owendelong
 
Posts: 51
Joined: Fri Feb 10, 2012 3:25 am


Re: 2.8" TFT LCD Library Error fillTriangle

Postby adafruit » Mon Feb 20, 2012 4:18 pm

owendelong, please email support@adafruit.com if/when you see this - we have a special gift for you :)

cheers,
adafruit support, phil
User avatar
adafruit
 
Posts: 10489
Joined: Thu Apr 06, 2006 3:21 pm
Location: nyc

Re: 2.8" TFT LCD Library Error fillTriangle

Postby owendelong » Sat Feb 25, 2012 11:23 pm

There are still some funky results from some triangles in my further testing, but, at least it doesn't run off and cover the whole screen.

It's working well enough that I probably won't get back to messing with it.
owendelong
 
Posts: 51
Joined: Fri Feb 10, 2012 3:25 am

Re: 2.8" TFT LCD Library Error fillTriangle

Postby owendelong » Mon Mar 05, 2012 11:52 pm

Interestingly, while I didn't change the code in the fillTriangle routine in the Max32 port I just did, the strange graphical artifacts disappeared on the Max32.

As such, I suspect it may be a math problem or rounding error on the AVR that is responsible.The PIC is definitely a WHOLE lot faster than the AVR ATMega2560.
owendelong
 
Posts: 51
Joined: Fri Feb 10, 2012 3:25 am


Return to Glowy things (LCD, LED, TFT, EL) purchased at Adafruit

Who is online

Users browsing this forum: No registered users and 1 guest

Stuff to buy from the Adafruit store and links to product documentation!


New Products [102]

Raspberry Pi[80]
 
FLORA[23]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[11]
Arduino[60]
 
NETduino[14]
 
BeagleBone[24]
 
Android[6]
 
XBee[10]
More Dev Boards[30]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[33]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[8]


 
Breakout Boards[33]
LCDs & Displays[48]
Components & Parts[69]
Batteries & Power[49]
EL Wire/Tape/Panel[52]
LEDs[109]
 
Wireless[14]
Cables[60]
 
Lasers[6]
Sensors/Parts[145]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[70]
 
iDevices[13]
Tools[71]
 
Wearables[39]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[24]


 
Stickers[41]
 
Skill badges[55]
 
Books[25]
 
Circuit Playground[7]
 
Gift Certificates[4]