#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>
// TFT Display Pins
#define TFT_CS 10
#define TFT_RST 4
#define TFT_DC 9
// Create TFT and Touchscreen objects
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
Adafruit_FT6206 ts = Adafruit_FT6206();
// Sensor values
int co2_in = 1200; // Example sensor 1 value
int co2_out = 900; // Example sensor 2 value
// Screen state: 0 = Main CO2 Page, 1 = Graph Page
int screenState = 0;
void setup() {
Serial.begin(115200);
tft.begin();
tft.setRotation(3);
if (!ts.begin()) {
Serial.println("Touchscreen not found!");
} else {
Serial.println("Touchscreen initialized.");
}
drawCO2Page();
}
void loop() {
//
}
// Draw top header with date and battery icon
void drawTopBar() {
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(1);
tft.setCursor(10, 10);
tft.print("25/03/2025 Mon");
// Draw Battery Icon
int batteryX = 300, batteryY = 10;
tft.drawRect(batteryX, batteryY, 18, 9, ILI9341_WHITE); // Battery border
tft.fillRect(batteryX + 2, batteryY + 2, 12, 5, ILI9341_WHITE); // Battery fill
tft.fillRect(batteryX + 18, batteryY + 3, 3, 3, ILI9341_WHITE); // Battery tip
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(1);
tft.setCursor(280,10);
tft.print("85%");
drawWifiIcon(220,10);
drawBluetoothIcon(240,10);
drawGpsIcon(260,10);
}
// Draw the Main CO2 Page
void drawCO2Page() {
tft.fillScreen(ILI9341_BLACK);
drawTopBar();
// Draw overlapping circles (CO2 In and CO2 Out)
int circleX1 = 120, circleY1 = 120; // Position for CO2 In
int circleX2 = 240, circleY2 = 120; // Position for CO2 Out
int radius = 75;
tft.fillCircle(circleX1, circleY1, radius, ILI9341_BLUE); // In (Blue)
tft.fillCircle(circleX2, circleY2, radius, ILI9341_GREEN); // Out (Red)
// Labels for circles
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(circleX1 - 85, circleY1 - 70);
tft.print("CO2");
tft.setCursor(circleX2 + 40, circleY2 + 70);
tft.print("CO");
// Display CO2 values inside circles
tft.setTextSize(3);
tft.setCursor(circleX1 - 45, circleY1 - 10);
tft.print(co2_in);
tft.setTextSize(1);
tft.setCursor(circleX1 + 25, circleY1 + 10);
tft.print("ppm");
tft.setTextSize(3);
tft.setCursor(circleX2 - 20, circleY2 - 10);
tft.print(co2_out);
tft.setTextSize(1);
tft.setCursor(circleX2 + 50, circleY2 + 10);
tft.print("ppm");
}
// ------------------- STATUS ICON DRAW FUNCTIONS -------------------
void drawWifiIcon(int x, int y) {
int centerX = x + 4;
int centerY = y + 4;
tft.drawLine(centerX - 3, y + 2, centerX + 3, y + 2, ILI9341_WHITE);
tft.drawLine(centerX - 2, y + 4, centerX + 2, y + 4, ILI9341_WHITE);
tft.drawLine(centerX - 1, y + 6, centerX + 1, y + 6, ILI9341_WHITE);
tft.drawPixel(centerX, y + 8, ILI9341_WHITE);
}
void drawBluetoothIcon(int x, int y) {
int centerX = x + 4;
int centerY = y + 4;
tft.drawLine(centerX, y, centerX, y + 8, ILI9341_WHITE);
tft.drawLine(centerX, y, centerX + 2, y + 2, ILI9341_WHITE);
tft.drawLine(centerX + 2, y + 2, centerX - 2, centerY + 2, ILI9341_WHITE);
tft.drawLine(centerX - 2, centerY - 2, centerX + 2, centerY + 2, ILI9341_WHITE);
tft.drawLine(centerX + 2, centerY + 2, centerX, centerY + 4, ILI9341_WHITE);
}
void drawGpsIcon(int x, int y) {
int centerX = x + 4;
int centerY = y + 4;
tft.drawCircle(centerX, centerY, 2, ILI9341_WHITE);
tft.drawPixel(centerX, centerY, ILI9341_WHITE);
tft.drawLine(centerX - 4, centerY, centerX - 2, centerY, ILI9341_WHITE);
tft.drawLine(centerX + 2, centerY, centerX + 4, centerY, ILI9341_WHITE);
tft.drawLine(centerX, centerY - 4, centerX, centerY - 2, ILI9341_WHITE);
tft.drawLine(centerX, centerY + 2, centerX, centerY + 4, ILI9341_WHITE);
}