Update drawArc, add new example

This commit is contained in:
Bodmer committed 2023-01-14 22:40:17 +00:00
1 parent 19eadee669
commit 890f6ff2b9
4 files changed
+65 -52

No files matched your search

@@ -0,0 +1,47 @@
// Arc drawing example - draw a colour wheel
#include <TFT_eSPI.h> // Include the graphics library
TFT_eSPI tft = TFT_eSPI(); // Create object "tft"
uint16_t colors[12];
// -------------------------------------------------------------------------
// Setup
// -------------------------------------------------------------------------
void setup(void) {
Serial.begin(115200);
tft.init();
tft.fillScreen(TFT_BLACK);
// Create the outer ring colours
for (uint8_t c = 0; c < 2; c++) {
colors[c + 10] = tft.alphaBlend(128 + c * 127, TFT_RED, TFT_MAGENTA);
colors[c + 8] = tft.alphaBlend(128 + c * 127, TFT_MAGENTA, TFT_BLUE);
colors[c + 6] = tft.alphaBlend(128 + c * 127, TFT_BLUE, TFT_GREEN);
colors[c + 4] = tft.alphaBlend(128 + c * 127, TFT_GREEN, TFT_YELLOW);
colors[c + 2] = tft.alphaBlend(128 + c * 127, TFT_YELLOW, TFT_ORANGE);
colors[c + 0] = tft.alphaBlend(128 + c * 127, TFT_ORANGE, TFT_RED);
}
}
// -------------------------------------------------------------------------
// Main loop
// -------------------------------------------------------------------------
void loop() {
uint16_t rDelta = (tft.width() - 1) / 10;
uint16_t x = tft.width() / 2;
uint16_t y = tft.height() / 2;
bool smooth = true;
// Draw rings as a series of arcs, increasingly blend colour with white towards middle
for (uint16_t i = 5; i > 0; i--) {
for (uint16_t angle = 0; angle <= 330; angle += 30) {
uint16_t radius = i * rDelta;
uint16_t wheelColor = tft.alphaBlend((i * 255.0)/5.0, colors[angle / 30], TFT_WHITE);
tft.drawArc(x, y, radius, radius - rDelta, angle, angle + 30, wheelColor, TFT_BLACK, smooth);
}
smooth = false; // Only outer ring is smooth
}
while (1) delay(100);
}
+1 -32
View File
@@ -7,8 +7,6 @@
// The sides of the arc can optionally be smooth or not. Smooth arcs have
// a much better appearance, especially at small sizes.
// Start angle for drawArc must be smaller than end angle
#include <TFT_eSPI.h> // Include the graphics library
TFT_eSPI tft = TFT_eSPI(); // Create object "tft"
@@ -42,7 +40,7 @@ void loop()
// 0 degrees is at 6 o'clock position
// Arcs are drawn clockwise from start_angle to end_angle
// Start angle for drawArc must be smaller than end angle (function will swap them otherwise)
// Start angle can be greater than end angle, the arc will then be drawn through 0 degrees
uint16_t start_angle = random(361); // Start angle must be in range 0 to 360
uint16_t end_angle = random(361); // End angle must be in range 0 to 360
@@ -50,35 +48,6 @@ void loop()
tft.drawArc(x, y, radius, inner_radius, start_angle, end_angle, fg_color, bg_color, smooth);
//tft.drawArc(x, y, radius, inner_radius, start_angle, end_angle, fg_color, bg_color); // always smooth sides if parameter is missing
// The following function allows arcs to be drawn through the 6 o'clock position by drawing in 2 segments if
// the start angle is greater than the end angle
//drawAnyArc(x, y, radius, inner_radius, start_angle, end_angle, fg_color, bg_color, smooth); // smooth sides if parameter is missing
count++;
if (count < 30) delay(500); // After 15s draw as fast as possible!
}
// The following function allows arcs to be drawn through the 0 degree position by drawing in 2 segments
// Function prototype with default smooth setting
void drawAnyArc(int32_t x, int32_t y, int32_t r, int32_t ir, int32_t startAngle, int32_t endAngle,
uint32_t fg_color, uint32_t bg_color, bool smooth = true);
void drawAnyArc(int32_t x, int32_t y, int32_t r, int32_t ir, int32_t startAngle, int32_t endAngle,
uint32_t fg_color, uint32_t bg_color, bool smooth)
{
if (endAngle > startAngle)
{
// Draw arc in single sweep
tft.drawArc(x, y, r, ir, startAngle, endAngle, fg_color, bg_color);
}
else
{
// Arc sweeps through 6 o'clock so draw in two parts
tft.drawArc(x, y, r, ir, startAngle, 360, fg_color, bg_color);
tft.drawArc(x, y, r, ir, 0, endAngle, fg_color, bg_color);
}
}