#include "SPI.h"
#include <Adafruit_ILI9341.h>
#include <Adafruit_GFX.h>
// TFT LCD Display
#define PIN_TFT_CS 17
#define PIN_TFT_DC 7
#define PIN_TFT_RST 6
Adafruit_ILI9341 tft = Adafruit_ILI9341(PIN_TFT_CS, PIN_TFT_DC, PIN_TFT_RST);
// Push button
#define PIN_SW1 28
#define PIN_SW2 27
#define PIN_SW3 26
// Framebuffer dimensions
#define SCREEN_WIDTH 240
#define SCREEN_HEIGHT 320
uint16_t framebuffer[SCREEN_WIDTH * SCREEN_HEIGHT];
void setup() {
Serial1.begin(115200);
Serial1.println("Hello, Raspberry Pi Pico!");
pinMode(PIN_SW1, INPUT_PULLUP);
pinMode(PIN_SW2, INPUT_PULLUP);
pinMode(PIN_SW3, INPUT_PULLUP);
tft.begin();
Serial1.println(tft.readcommand8(ILI9341_RDSELFDIAG));
// Clear the framebuffer
memset(framebuffer, 0, sizeof(framebuffer));
// Draw on framebuffer
drawTextOnFramebuffer();
// Push framebuffer to display
updateDisplay();
}
void loop() {
static unsigned long lastSwitchCheck = 0;
if (millis() > lastSwitchCheck + 1000) {
Serial1.println(String(digitalRead(PIN_SW1)) + " " +
String(digitalRead(PIN_SW2)) + " " +
String(digitalRead(PIN_SW3)));
lastSwitchCheck = millis();
}
delay(1); // this speeds up the simulation
}
void drawTextOnFramebuffer() {
// Set background color
for (int y = 0; y < SCREEN_HEIGHT; y++) {
for (int x = 0; x < SCREEN_WIDTH; x++) {
framebuffer[y * SCREEN_WIDTH + x] = ILI9341_BLACK; // Background
}
}
// Draw text manually into framebuffer (this is a simplified example)
// You can extend this to draw shapes, images, etc.
drawText(0, 0, "Hello, TFT!", ILI9341_RED);
drawText(20, 160, "I can has colors?", ILI9341_GREEN);
}
void drawText(int x, int y, const char* text, uint16_t color) {
int textSize = 2; // Set text size
for (int i = 0; text[i] != '\0'; i++) {
// Draw each character into framebuffer (simple bitmap font approach)
// This should ideally use a proper font rendering library
// Here we just simulate by coloring a small rectangle
for (int dx = 0; dx < 6 * textSize; dx++) { // Character width
for (int dy = 0; dy < 8 * textSize; dy++) { // Character height
if (dx < 6 * textSize && dy < 8 * textSize) {
framebuffer[(y + dy) * SCREEN_WIDTH + (x + dx)] = color;
}
}
}
x += 6 * textSize; // Move x position for next character
}
}
void updateDisplay() {
// Push the framebuffer to the display
tft.setAddrWindow(0, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1);
for (int y = 0; y < SCREEN_HEIGHT; y++) {
for (int x = 0; x < SCREEN_WIDTH; x++) {
tft.pushColor(framebuffer[y * SCREEN_WIDTH + x]);
}
}
}