/*
Forum: https://forum.arduino.cc/t/adafruit-320x240-mit-teensy-4-0-flickering/1350338
Wokwi: https://wokwi.com/projects/422068446568927233
ec2021
Draw Bars/Pointers with minimum erasure effort
Wrapper for drawLine and fillRect functions
2025/12/03 For changes see comments in BarPointerClass.h
*/
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "BarPointerClass.h"
constexpr int circleX {120};
constexpr int circleY {170};
constexpr byte TFT_DC {9};
constexpr byte TFT_CS {10};
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
unsigned long lastTime = 0;
int Angle = 180;
int AngleDx = 10;
int value = 1;
int delta = 1;
PointerClass pointerA, pointerB, pointerC, pointerD;
// Function call to draw a line
// from (x0,y0) to (x1, y1) in color col
void drawLine(int x0, int y0, int x1, int y1, uint16_t col) {
tft.drawLine(x0, y0, x1, y1, col);
}
BarClass barA, barB, barC, barD;
// Function call to draw a filled rectangle
// at (x,y) with width w and length l in color col
void fillRect(int x, int y, int w, int l, uint16_t col) {
tft.fillRect(x, y, w, l, col);
}
void setup() {
Serial.begin(115200);
Serial.println("Graphics Example");
tft.begin();
// Create Pointers ("Uhrzeiger") with
// (CenterX, CenterY, Radius, Function Call to draw a line)
pointerA.setXYRF(circleX, circleY, 80, &drawLine);
pointerB.setXYRF(circleX, circleY, 60, &drawLine);
pointerC.setXYRF(circleX, circleY, 50, &drawLine);
pointerD.setXYRF(circleX, circleY, 40, &drawLine);
// Create Bars ("Balken") with
// (StartX, StartY, Width, Orientation, Function Call to paint a Filled Rectangle)
barA.setXYWDF(200, 20, 40, 0, &fillRect);
barB.setXYWDF( 40, 260, 30, 1, &fillRect);
barC.setXYWDF( 20, 120, 20, 2, &fillRect);
barD.setXYWDF(160, 300, 10, 3, &fillRect);
clearScreen();
tft.drawCircle(circleX, circleY, 86, ILI9341_RED);
}
void loop(void) {
if (millis() - lastTime > 100) {
lastTime = millis();
handlePointers();
handleBars();
}
}
void clearScreen() {
tft.fillScreen(ILI9341_BLACK);
}
void handlePointers() {
pointerA.draw(Angle, ILI9341_YELLOW);
pointerB.draw(90 - Angle, ILI9341_GREEN);
pointerC.draw(Angle + 45, ILI9341_YELLOW);
pointerD.draw(180 - Angle, ILI9341_GREEN);
Angle += AngleDx;
if ((Angle > 360)) {
Angle = AngleDx;
}
}
void handleBars() {
barA.draw(value * 10, ILI9341_MAGENTA);
barB.draw(value * 10, ILI9341_RED);
barC.draw(value * 10, ILI9341_BLUE);
barD.draw(value * 10, ILI9341_GREEN);
value += delta;
if (value > 10 || value < 2) {
delta = -delta;
}
}