/*
Forum: https://forum.arduino.cc/t/displaying-rgb-bitmap-on-tft-from-c-file/1191496
Wokwi: https://wokwi.com/projects/382188352744225793
*/
// If the next line is uncommented it will compile for ILI9341 as used on Wokwi
#define ILI9341DISPLAY
// otherwise for a ST7735 displays
#ifdef ILI9341DISPLAY
constexpr int yOffStatic {200};
constexpr int yOffMoving {32};
#else
constexpr int yOffStatic {90};
constexpr int yOffMoving {32};
#endif
#include "asciiGraphs.h"
// Single sprites created from ASCII arrays
asciiImageStruct img1 {asciiImage1};
asciiImageStruct img2 {asciiImage2};
// An array of sprites
constexpr int noOfSprites {3};
asciiImageStruct sprite[noOfSprites] {
{asciiImage1},
{asciiImage1},
{asciiImage1}
};
uint16_t xColors[noOfSprites] {
MY_YELLOW,
MY_ORANGE,
MY_PINK
};
void setup() {
Serial.begin(115200);
#ifdef ILI9341DISPLAY
tft.begin();
#else
tft.initR(INITR_BLACKTAB); // Init ST7735S chip, black tab
tft.invertDisplay(true);
tft.fillScreen(MY_BLACK);
Serial.println(tft.width());
Serial.println(tft.height());
delay(3000);
#endif
tft.setRotation(1);
img1.init();
img2.init();
// Single sprites prepared for static drawing
int xCenter = (tft.width() - noOfCols) / 2;
img2.setXYandStepAndDelay(xCenter, yOffStatic, 0, 0);
img2.draw();
img2.setXY(xCenter - noOfCols, yOffStatic);
img2.xColor = MY_RED;
img2.draw();
img2.setXY(xCenter + noOfCols, yOffStatic);
img2.xColor = MY_GREEN;
img2.draw();
img2.setXY(xCenter - 2 * noOfCols, yOffStatic);
img2.xColor = MY_CYAN;
img2.draw();
img2.setXY(xCenter + 2 * noOfCols, yOffStatic);
img2.xColor = MY_PURPLE;
img2.draw();
// From here single sprites prepared for moving
// Thissprite starts from left to right at xPos = 0 and yPos = 80 with 4 pixel steps and
// moves earliest every 10 ms
img1.setXYandStepAndDelay(0, 80, 4, 10);
// Array of sprites prepared for moving
// xPos = i * noOfCols -> First sprite starts a zero x, the second one sprite width to the right etc.
// yPos = i * (noOfRows+2)+yOffMoving -> First sprite starts a yOffMoving,
// the second sprite one sprite height plus 2 deeper etc.
// step = 2 + i *2 -> The first sprite uses 2 pixel steps, the second 4, the third 6 steps
// delay = 10 -> all sprites move earliest after 10 ms
for (int i = 0; i < noOfSprites; i++) {
sprite[i].init();
sprite[i].setXYandStepAndDelay(i * noOfCols, i * (noOfRows + 2) + yOffMoving, 2 + i * 2, 10);
sprite[i].xColor = xColors[i];
}
}
void loop() {
img1.move();
for (int i = 0; i < noOfSprites; i++) {
sprite[i].move();
}
checkTurning();
}
// The function checkTurning() checks whether the sprite has changed direction
// If so, the function changeColor() is called. This function toggles the colors of the sprite img1
// It starts from right to left with blue body and a red cross
// At the left border the color switches to red body and a blue cross and vice versa at the right border
// For img1 and all other moving sprites a Serial messages shows that they have turned.
// For the sprite array the xPos is also checked to determine whether the sprite turned at the left or
// at the right border just to demonstrate how to integrate further functionality
void checkTurning() {
if (img1.isTurning) {
Serial.println("img1 has turned");
changeColor();
}
for (int i = 0; i < noOfSprites; i++) {
if (sprite[i].isTurning) {
Serial.print("Sprite ");
Serial.print(i + 1);
Serial.print(" has turned at ");
if (sprite[i].xPos < 10) {
Serial.println("left");
} else {
Serial.println("right");
}
}
}
}
void changeColor() {
if (img1.xColor == MY_BLUE) {
img1.xColor = MY_RED;
img1.oColor = MY_BLUE;
} else {
img1.xColor = MY_BLUE;
img1.oColor = MY_RED;
}
}