/*
Forum: https://forum.arduino.cc/t/displaying-rgb-bitmap-on-tft-from-c-file/1191496
Wokwi: https://wokwi.com/projects/382087959061470209
*/
#include "hi.h"
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define TFT_DC 9
#define TFT_CS 10
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
constexpr int maxNoOfImgs {10};
int yTop = 0;
int xPos = 0;
int yPos = 0;
int step = 4;
int noOfImgs = 1;
void setup() {
Serial.begin(115200);
Serial.println(image.width);
Serial.println(image.height);
tft.begin();
}
void loop() {
move();
}
void move() {
for (int n = 0; n < noOfImgs; n++) {
decodeImageAt(xPos, yPos + n * 32, ILI9341_BLACK);
}
xPos += step;
if (xPos >= tft.width() - image.width || xPos < 0) {
step = -step;
noOfImgs++;
if (noOfImgs > maxNoOfImgs) {
tft.fillScreen(ILI9341_BLACK);
noOfImgs = 1;
}
}
}
void decodeImageAt(int x, int y, uint16_t bkColor) {
uint16_t a;
uint16_t b;
uint16_t color;
int myX;
deleteTail(x, y, bkColor);
for (int row = yTop; row < image.height; row++) {
for (int col = 0; col < image.width; col++) {
a = pixil_frame[row * image.width * 2 + col * 2];
b = pixil_frame[row * image.width * 2 + col * 2 + 1];
color = b << 8 + a;
if (color == 0) {
color = bkColor;
}
else
{
if (yTop == 0) {
yTop = row;
}; // Forget the rows on top of yTop which are completely "zero"
}
if (step > 0) {
myX = col + x;
} else {
myX = (image.width - col) + x;
}
tft.drawPixel(myX, row + y, color);
}
}
}
void deleteTail(int x, int y, uint16_t bkColor) {
// This routine could be replaced by tft.drawRect() ...
if (x == 0 || x == tft.width() - image.width) {
return;
};
int myX;
for (int row = yTop; row < image.height; row++) {
for (int col = 0; col < abs(step); col++) {
if (step < 0)
{
myX = (image.width + col) + x +1;
}
else
{
myX = col + x -step;
}
tft.drawPixel(myX, row + y, bkColor);
}
}
}