/*****************************************************
https://chatgpt.com/share/978dbdeb-d379-4332-9f89-4b2d1fc280d1
#py code
import tkinter as tk
from tkinter import Canvas
import colorsys
import random
# TFT parameters
TFT_WIDTH = 320
TFT_HEIGHT = 240
SQUARE_SIZE = 20
# Define colors
ILI9341_BLACK = "black"
ILI9341_RED = "red"
ILI9341_WHITE = "white"
class VirtualTFT:
def __init__(self, master):
self.master = master
self.canvas = Canvas(master, width=TFT_WIDTH, height=TFT_HEIGHT, bg=ILI9341_BLACK)
self.canvas.pack()
# Main loop
self.draw_squares()
self.draw_attractive_design()
def draw_squares(self):
for side_length in range(200, 0, -20):
# Change background to rainbow colors
self.draw_rainbow_background()
# Set the TFT color to red
color = ILI9341_RED
# Draw the square
x = TFT_WIDTH / 2 - side_length / 2
y = TFT_HEIGHT / 2 - side_length / 2
self.canvas.create_rectangle(x, y, x + side_length, y + side_length, fill=color)
self.master.update()
self.master.after(500) # Adjust delay if needed
# Clear the square by filling it with the background color
self.canvas.create_rectangle(x, y, x + side_length, y + side_length, fill=ILI9341_BLACK)
self.master.update()
def draw_attractive_design(self):
for _ in range(35):
random_color = "#{:02x}{:02x}{:02x}".format(
random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)
)
x, y = TFT_WIDTH / 2, TFT_HEIGHT / 2
self.canvas.create_oval(x - 100, y - 100, x + 100, y + 100, outline=random_color)
self.master.update()
self.master.after(100) # Adjust the delay to control the drawing speed
self.canvas.delete("all") # Clear the screen for the next circle
self.canvas.create_rectangle(0, 0, TFT_WIDTH, TFT_HEIGHT, fill=ILI9341_BLACK)
self.master.update()
# Display text "by Arvind" in white color
self.canvas.create_text(TFT_WIDTH / 2, TFT_HEIGHT - 20, text="by Arvind", fill=ILI9341_WHITE, font=("Arial", 12, "bold"))
self.master.update()
def draw_rainbow_background(self):
for i in range(TFT_WIDTH):
hue = int((i / TFT_WIDTH) * 255)
color = "#{:02x}{:02x}{:02x}".format(
*tuple(int(c * 255) for c in colorsys.hsv_to_rgb(hue / 255, 1, 1))
)
self.canvas.create_line(i, 0, i, TFT_HEIGHT, fill=color)
self.master.update()
self.master.after(500) # Adjust delay if needed
root = tk.Tk()
root.title("Virtual TFT Display")
virtual_tft = VirtualTFT(root)
root.mainloop()
at chat gpt promt "write convert python code to c plus so that it can be played on
tftIli9341 connected to arduino nano via spi keep the backgroung
ith rainbow color"
and get converted code
paste the code to wokwi simulator with arduino nano
by arvind patil 28/07/24
*******************************************************/
// converted code from python
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <SPI.h>
// TFT parameters
#define TFT_CS 10
#define TFT_RST 9
#define TFT_DC 8
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
#define TFT_WIDTH 320
#define TFT_HEIGHT 240
#define SQUARE_SIZE 20
void setup() {
tft.begin();
tft.setRotation(3); // Adjust if necessary
tft.fillScreen(ILI9341_BLACK);
}
void loop() {
drawRainbowBackground();
drawSquares();
drawAttractiveDesign();
}
void drawSquares() {
for (int sideLength = 200; sideLength > 0; sideLength -= 20) {
// Draw the square in red
int x = TFT_WIDTH / 2 - sideLength / 2;
int y = TFT_HEIGHT / 2 - sideLength / 2;
tft.fillRect(x, y, sideLength, sideLength, ILI9341_RED);
delay(500);
// Clear the square by filling it with the rainbow background
drawRainbowBackground();
}
}
void drawAttractiveDesign() {
for (int i = 0; i < 35; i++) {
uint16_t randomColor = tft.color565(random(255), random(255), random(255));
int x = TFT_WIDTH / 2;
int y = TFT_HEIGHT / 2;
tft.drawCircle(x, y, 100, randomColor);
delay(100);
tft.fillScreen(ILI9341_BLACK);
drawRainbowBackground();
}
// Display text "by Arvind" in white color
tft.setCursor(TFT_WIDTH / 2 - 30, TFT_HEIGHT - 20);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print("by Arvind");
}
void drawRainbowBackground() {
for (int x = 0; x < TFT_WIDTH; x++) {
float hue = (float)x / TFT_WIDTH;
uint16_t color = HSVtoRGB(hue, 1.0, 1.0);
tft.drawLine(x, 0, x, TFT_HEIGHT, color);
}
}
uint16_t HSVtoRGB(float h, float s, float v) {
int i = int(h * 6);
float f = h * 6 - i;
float p = v * (1 - s);
float q = v * (1 - f * s);
float t = v * (1 - (1 - f) * s);
float r, g, b;
switch (i % 6) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
uint8_t red = r * 255;
uint8_t green = g * 255;
uint8_t blue = b * 255;
return tft.color565(red, green, blue);
}