diff -r 49e2960d4642 -r 3fd5e0fc075f components/tft/tft.c --- a/components/tft/tft.c Fri Oct 26 21:23:46 2018 +0200 +++ b/components/tft/tft.c Fri Oct 26 22:19:48 2018 +0200 @@ -123,7 +123,6 @@ static uint8_t *userfont = NULL; static int TFT_OFFSET = 0; static propFont fontChar; -static float _arcAngleMax = DEFAULT_ARC_ANGLE_MAX; // ========================================================================= @@ -134,17 +133,6 @@ // ========================================================================= -// Compare two colors; return 0 if equal -//============================================ -int TFT_compare_colors(color_t c1, color_t c2) -{ - if ((c1.r & 0xFC) != (c2.r & 0xFC)) return 1; - if ((c1.g & 0xFC) != (c2.g & 0xFC)) return 1; - if ((c1.b & 0xFC) != (c2.b & 0xFC)) return 1; - - return 0; -} - // draw color pixel on screen //------------------------------------------------------------------------ static void _drawPixel(int16_t x, int16_t y, color_t color, uint8_t sel) { @@ -447,35 +435,6 @@ //----------------------------------------------------------------------------------------------- -static void _drawLineByAngle(int16_t x, int16_t y, int16_t angle, uint16_t length, color_t color) -{ - _drawLine( - x, - y, - x + length * cos((angle + _angleOffset) * DEG_TO_RAD), - y + length * sin((angle + _angleOffset) * DEG_TO_RAD), color); -} - -//--------------------------------------------------------------------------------------------------------------- -static void _DrawLineByAngle(int16_t x, int16_t y, int16_t angle, uint16_t start, uint16_t length, color_t color) -{ - _drawLine( - x + start * cos((angle + _angleOffset) * DEG_TO_RAD), - y + start * sin((angle + _angleOffset) * DEG_TO_RAD), - x + (start + length) * cos((angle + _angleOffset) * DEG_TO_RAD), - y + (start + length) * sin((angle + _angleOffset) * DEG_TO_RAD), color); -} - -//=========================================================================================================== -void TFT_drawLineByAngle(uint16_t x, uint16_t y, uint16_t start, uint16_t len, uint16_t angle, color_t color) -{ - x += dispWin.x1; - y += dispWin.y1; - - if (start == 0) _drawLineByAngle(x, y, angle, len, color); - else _DrawLineByAngle(x, y, angle, start, len, color); -} - // Draw a triangle //-------------------------------------------------------------------------------------------------------------------- @@ -486,21 +445,6 @@ _drawLine(x2, y2, x0, y0, color); } -//================================================================================================================ -void TFT_drawTriangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, color_t color) -{ - x0 += dispWin.x1; - y0 += dispWin.y1; - x1 += dispWin.x1; - y1 += dispWin.y1; - x2 += dispWin.x1; - y2 += dispWin.y1; - - _drawLine(x0, y0, x1, y1, color); - _drawLine(x1, y1, x2, y2, color); - _drawLine(x2, y2, x0, y0, color); -} - // Fill a triangle //-------------------------------------------------------------------------------------------------------------------- static void _fillTriangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, color_t color) @@ -579,16 +523,6 @@ } } -//================================================================================================================ -void TFT_fillTriangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, color_t color) -{ - _fillTriangle( - x0 + dispWin.x1, y0 + dispWin.y1, - x1 + dispWin.x1, y1 + dispWin.y1, - x2 + dispWin.x1, y2 + dispWin.y1, - color); -} - //==================================================================== void TFT_drawCircle(int16_t x, int16_t y, int radius, color_t color) { x += dispWin.x1; @@ -634,395 +568,6 @@ fillCircleHelper(x, y, radius, 3, 0, color); } -//---------------------------------------------------------------------------------------------------------------- -static void _draw_ellipse_section(uint16_t x, uint16_t y, uint16_t x0, uint16_t y0, color_t color, uint8_t option) -{ - disp_select(); - // upper right - if ( option & TFT_ELLIPSE_UPPER_RIGHT ) _drawPixel(x0 + x, y0 - y, color, 0); - // upper left - if ( option & TFT_ELLIPSE_UPPER_LEFT ) _drawPixel(x0 - x, y0 - y, color, 0); - // lower right - if ( option & TFT_ELLIPSE_LOWER_RIGHT ) _drawPixel(x0 + x, y0 + y, color, 0); - // lower left - if ( option & TFT_ELLIPSE_LOWER_LEFT ) _drawPixel(x0 - x, y0 + y, color, 0); - disp_deselect(); -} - -//===================================================================================================== -void TFT_drawEllipse(uint16_t x0, uint16_t y0, uint16_t rx, uint16_t ry, color_t color, uint8_t option) -{ - x0 += dispWin.x1; - y0 += dispWin.y1; - - uint16_t x, y; - int32_t xchg, ychg; - int32_t err; - int32_t rxrx2; - int32_t ryry2; - int32_t stopx, stopy; - - rxrx2 = rx; - rxrx2 *= rx; - rxrx2 *= 2; - - ryry2 = ry; - ryry2 *= ry; - ryry2 *= 2; - - x = rx; - y = 0; - - xchg = 1; - xchg -= rx; - xchg -= rx; - xchg *= ry; - xchg *= ry; - - ychg = rx; - ychg *= rx; - - err = 0; - - stopx = ryry2; - stopx *= rx; - stopy = 0; - - while( stopx >= stopy ) { - _draw_ellipse_section(x, y, x0, y0, color, option); - y++; - stopy += rxrx2; - err += ychg; - ychg += rxrx2; - if ( 2*err+xchg > 0 ) { - x--; - stopx -= ryry2; - err += xchg; - xchg += ryry2; - } - } - - x = 0; - y = ry; - - xchg = ry; - xchg *= ry; - - ychg = 1; - ychg -= ry; - ychg -= ry; - ychg *= rx; - ychg *= rx; - - err = 0; - - stopx = 0; - - stopy = rxrx2; - stopy *= ry; - - while( stopx <= stopy ) { - _draw_ellipse_section(x, y, x0, y0, color, option); - x++; - stopx += ryry2; - err += xchg; - xchg += ryry2; - if ( 2*err+ychg > 0 ) { - y--; - stopy -= rxrx2; - err += ychg; - ychg += rxrx2; - } - } -} - -//----------------------------------------------------------------------------------------------------------------------- -static void _draw_filled_ellipse_section(uint16_t x, uint16_t y, uint16_t x0, uint16_t y0, color_t color, uint8_t option) -{ - // upper right - if ( option & TFT_ELLIPSE_UPPER_RIGHT ) _drawFastVLine(x0+x, y0-y, y+1, color); - // upper left - if ( option & TFT_ELLIPSE_UPPER_LEFT ) _drawFastVLine(x0-x, y0-y, y+1, color); - // lower right - if ( option & TFT_ELLIPSE_LOWER_RIGHT ) _drawFastVLine(x0+x, y0, y+1, color); - // lower left - if ( option & TFT_ELLIPSE_LOWER_LEFT ) _drawFastVLine(x0-x, y0, y+1, color); -} - -//===================================================================================================== -void TFT_fillEllipse(uint16_t x0, uint16_t y0, uint16_t rx, uint16_t ry, color_t color, uint8_t option) -{ - x0 += dispWin.x1; - y0 += dispWin.y1; - - uint16_t x, y; - int32_t xchg, ychg; - int32_t err; - int32_t rxrx2; - int32_t ryry2; - int32_t stopx, stopy; - - rxrx2 = rx; - rxrx2 *= rx; - rxrx2 *= 2; - - ryry2 = ry; - ryry2 *= ry; - ryry2 *= 2; - - x = rx; - y = 0; - - xchg = 1; - xchg -= rx; - xchg -= rx; - xchg *= ry; - xchg *= ry; - - ychg = rx; - ychg *= rx; - - err = 0; - - stopx = ryry2; - stopx *= rx; - stopy = 0; - - while( stopx >= stopy ) { - _draw_filled_ellipse_section(x, y, x0, y0, color, option); - y++; - stopy += rxrx2; - err += ychg; - ychg += rxrx2; - if ( 2*err+xchg > 0 ) { - x--; - stopx -= ryry2; - err += xchg; - xchg += ryry2; - } - } - - x = 0; - y = ry; - - xchg = ry; - xchg *= ry; - - ychg = 1; - ychg -= ry; - ychg -= ry; - ychg *= rx; - ychg *= rx; - - err = 0; - - stopx = 0; - - stopy = rxrx2; - stopy *= ry; - - while( stopx <= stopy ) { - _draw_filled_ellipse_section(x, y, x0, y0, color, option); - x++; - stopx += ryry2; - err += xchg; - xchg += ryry2; - if ( 2*err+ychg > 0 ) { - y--; - stopy -= rxrx2; - err += ychg; - ychg += rxrx2; - } - } -} - - -// ==== ARC DRAWING =================================================================== - -//--------------------------------------------------------------------------------------------------------------------------------- -static void _fillArcOffsetted(uint16_t cx, uint16_t cy, uint16_t radius, uint16_t thickness, float start, float end, color_t color) -{ - //float sslope = (float)cos_lookup(start) / (float)sin_lookup(start); - //float eslope = (float)cos_lookup(end) / (float)sin_lookup(end); - float sslope = (cos(start/_arcAngleMax * 2 * PI) * _arcAngleMax) / (sin(start/_arcAngleMax * 2 * PI) * _arcAngleMax) ; - float eslope = (cos(end/_arcAngleMax * 2 * PI) * _arcAngleMax) / (sin(end/_arcAngleMax * 2 * PI) * _arcAngleMax); - - if (end == 360) eslope = -1000000; - - int ir2 = (radius - thickness) * (radius - thickness); - int or2 = radius * radius; - - disp_select(); - for (int x = -radius; x <= radius; x++) { - for (int y = -radius; y <= radius; y++) { - int x2 = x * x; - int y2 = y * y; - - if ( - (x2 + y2 < or2 && x2 + y2 >= ir2) && - ( - (y > 0 && start < 180 && x <= y * sslope) || - (y < 0 && start > 180 && x >= y * sslope) || - (y < 0 && start <= 180) || - (y == 0 && start <= 180 && x < 0) || - (y == 0 && start == 0 && x > 0) - ) && - ( - (y > 0 && end < 180 && x >= y * eslope) || - (y < 0 && end > 180 && x <= y * eslope) || - (y > 0 && end >= 180) || - (y == 0 && end >= 180 && x < 0) || - (y == 0 && start == 0 && x > 0) - ) - ) - _drawPixel(cx+x, cy+y, color, 0); - } - } - disp_deselect(); -} - - -//=========================================================================================================================== -void TFT_drawArc(uint16_t cx, uint16_t cy, uint16_t r, uint16_t th, float start, float end, color_t color, color_t fillcolor) -{ - cx += dispWin.x1; - cy += dispWin.y1; - - if (th < 1) th = 1; - if (th > r) th = r; - - int f = TFT_compare_colors(fillcolor, color); - - float astart = fmodf(start, _arcAngleMax); - float aend = fmodf(end, _arcAngleMax); - - astart += _angleOffset; - aend += _angleOffset; - - if (astart < 0) astart += (float)360; - if (aend < 0) aend += (float)360; - - if (aend == 0) aend = (float)360; - - if (astart > aend) { - _fillArcOffsetted(cx, cy, r, th, astart, _arcAngleMax, fillcolor); - _fillArcOffsetted(cx, cy, r, th, 0, aend, fillcolor); - if (f) { - _fillArcOffsetted(cx, cy, r, 1, astart, _arcAngleMax, color); - _fillArcOffsetted(cx, cy, r, 1, 0, aend, color); - _fillArcOffsetted(cx, cy, r-th, 1, astart, _arcAngleMax, color); - _fillArcOffsetted(cx, cy, r-th, 1, 0, aend, color); - } - } - else { - _fillArcOffsetted(cx, cy, r, th, astart, aend, fillcolor); - if (f) { - _fillArcOffsetted(cx, cy, r, 1, astart, aend, color); - _fillArcOffsetted(cx, cy, r-th, 1, astart, aend, color); - } - } - if (f) { - _drawLine(cx + (r-th) * cos(astart * DEG_TO_RAD), cy + (r-th) * sin(astart * DEG_TO_RAD), - cx + (r-1) * cos(astart * DEG_TO_RAD), cy + (r-1) * sin(astart * DEG_TO_RAD), color); - _drawLine(cx + (r-th) * cos(aend * DEG_TO_RAD), cy + (r-th) * sin(aend * DEG_TO_RAD), - cx + (r-1) * cos(aend * DEG_TO_RAD), cy + (r-1) * sin(aend * DEG_TO_RAD), color); - } -} - -//============================================================================================================= -void TFT_drawPolygon(int cx, int cy, int sides, int diameter, color_t color, color_t fill, int rot, uint8_t th) -{ - cx += dispWin.x1; - cy += dispWin.y1; - - int deg = rot - _angleOffset; - int f = TFT_compare_colors(fill, color); - - if (sides < MIN_POLIGON_SIDES) sides = MIN_POLIGON_SIDES; // This ensures the minimum side number - if (sides > MAX_POLIGON_SIDES) sides = MAX_POLIGON_SIDES; // This ensures the maximum side number - - int Xpoints[sides], Ypoints[sides]; // Set the arrays based on the number of sides entered - int rads = 360 / sides; // This equally spaces the points. - - for (int idx = 0; idx < sides; idx++) { - Xpoints[idx] = cx + sin((float)(idx*rads + deg) * deg_to_rad) * diameter; - Ypoints[idx] = cy + cos((float)(idx*rads + deg) * deg_to_rad) * diameter; - } - - // Draw the polygon on the screen. - if (f) { - for(int idx = 0; idx < sides; idx++) { - if((idx+1) < sides) _fillTriangle(cx,cy,Xpoints[idx],Ypoints[idx],Xpoints[idx+1],Ypoints[idx+1], fill); - else _fillTriangle(cx,cy,Xpoints[idx],Ypoints[idx],Xpoints[0],Ypoints[0], fill); - } - } - - if (th) { - for (int n=0; n