Here's the updated code with button control for mode changes. I've added support for three physical buttons connected to Arduino pins:
cpp
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
// Pin definitions for Arduino Nano
#define TFT_CS 9 // Chip select
#define TFT_DC 7 // Data/Command
#define TFT_RST 8 // Reset
// Button pin definitions
#define BTN_SET 2 // Button for SET mode
#define BTN_PWR 3 // Button for PWR mode
#define BTN_OCS 4 // Button for OCS mode
// Custom vibrant colors
#define NEON_PINK 0xF81F // Hot pink
#define ELECTRIC_BLUE 0x041F // Bright blue
#define LIME_GREEN 0x87E0 // Bright green
#define ORANGE 0xFC00 // Bright orange
#define PURPLE 0x801F // Purple
#define AQUA 0x07FF // Bright cyan
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
// ─────────────────────────────────────────────────────────────────────
// Global variables for screen state and dynamic values
// ─────────────────────────────────────────────────────────────────────
enum ScreenMode {
SET_MODE,
PWR_MODE,
OCS_MODE
};
ScreenMode currentMode; // Variable to track the active screen mode
float currentFreq = 25.00;
float currentVolt = 12.01;
float currentAmp = 3.76;
// Timers to control update frequency for dynamic values
unsigned long lastFreqUpdate = 0;
unsigned long lastVoltUpdate = 0;
unsigned long lastAmpUpdate = 0;
// Button debounce variables
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
int lastButtonState[3] = {HIGH, HIGH, HIGH};
int buttonState[3] = {HIGH, HIGH, HIGH};
// ─────────────────────────────────────────────────────────────────────
// Function to draw the stylised power plug icon
// ─────────────────────────────────────────────────────────────────────
void drawPlug(int x, int y) {
// 1. Prongs (Rounded + Layered Metal Look)
tft.fillRoundRect(x + 10, y, 8, 20, 3, 0xC618); // Light gray
tft.fillRoundRect(x + 32, y, 8, 20, 3, 0xC618);
tft.fillRect(x + 12, y + 20, 4, 5, 0x8410); // Dark gray
tft.fillRect(x + 34, y + 20, 4, 5, 0x8410);
// 2. Plug Body (Layered Gradient + Bold Outline)
tft.fillRoundRect(x, y + 25, 50, 35, 10, 0x47E0); // Dark green
tft.fillRoundRect(x + 5, y + 22, 40, 30, 8, LIME_GREEN); // Bright green
tft.drawRoundRect(x, y + 25, 50, 35, 10, ST77XX_WHITE);
tft.drawRoundRect(x + 2, y + 23, 46, 31, 8, ST77XX_WHITE);
// 3. Cable (Rounded + Extended End)
tft.fillRoundRect(x + 18, y + 60, 14, 35, 5, ST77XX_WHITE); // Main cable
// 4. Lightning Bolt (Filled with Rectangles + Accent Lines)
tft.fillRect(x + 22, y + 35, 6, 10, ST77XX_YELLOW); // Vertical base
tft.fillRect(x + 18, y + 45, 14, 6, ST77XX_YELLOW); // Horizontal middle
tft.fillRect(x + 22, y + 51, 6, 10, ST77XX_YELLOW); // Vertical tip
tft.drawLine(x + 24, y + 34, x + 19, y + 44, ST77XX_BLACK);
tft.drawLine(x + 19, y + 44, x + 29, y + 44, ST77XX_BLACK);
tft.drawLine(x + 29, y + 44, x + 24, y + 54, ST77XX_BLACK);
}
// ─────────────────────────────────────────────────────────────────────
// Function to draw the complete splash screen
// ─────────────────────────────────────────────────────────────────────
void drawSplashScreen() {
tft.fillScreen(ST77XX_BLACK);
// Animated background effect
for (int i = 0; i < 240; i += 40) {
uint16_t color = (i % 80 == 0) ? 0x1082 : 0x0841;
tft.drawCircle(120, 120, i, color);
}
// Draw plug icon
drawPlug(95, 40);
// "POWER" text with glow effect
tft.setTextSize(5);
// Shadow/glow
tft.setCursor(51, 141);
tft.setTextColor(0x8410);
tft.print("POWER");
// Main text
tft.setCursor(50, 140);
tft.setTextColor(LIME_GREEN);
tft.print("POWER");
// Loading bar frame
tft.drawRoundRect(20, 200, 200, 20, 5, ST77XX_WHITE);
}
// ─────────────────────────────────────────────────────────────────────
// Function to update the loading bar progress
// ─────────────────────────────────────────────────────────────────────
void updateLoadingBar(int progress) {
// Use a static variable to avoid redrawing if progress hasn't changed
static int lastLoadProgress = -1;
if (progress != lastLoadProgress) {
// Clear previous bar
tft.fillRoundRect(22, 202, 196, 16, 3, ST77XX_BLACK);
// Draw new bar
tft.fillRoundRect(22, 202, (196 * progress) / 100, 16, 3, LIME_GREEN);
lastLoadProgress = progress;
}
}
// ─────────────────────────────────────────────────────────────────────
// Function to draw a single top button
// ─────────────────────────────────────────────────────────────────────
void drawTopButton(int x, int y, int w, int h, const char* label, bool highlighted) {
if (highlighted) {
tft.fillRoundRect(x, y, w, h, 8, LIME_GREEN);
tft.drawRoundRect(x, y, w, h, 8, ST77XX_WHITE);
tft.setTextColor(ST77XX_BLACK);
} else {
tft.drawRoundRect(x, y, w, h, 8, ELECTRIC_BLUE);
tft.setTextColor(ELECTRIC_BLUE);
}
tft.setTextSize(3);
int16_t x1, y1;
uint16_t w1, h1;
tft.getTextBounds(label, x, y, &x1, &y1, &w1, &h1);
tft.setCursor(x + (w - w1) / 2, y + (h - h1) / 2);
tft.print(label);
}
// ─────────────────────────────────────────────────────────────────────
// Function to draw the common background and top buttons
// ─────────────────────────────────────────────────────────────────────
void drawButtonsAndBackground(ScreenMode mode) {
tft.fillScreen(0x0000); // Clear the whole screen
// Background pattern
for (int i = 0; i < 240; i += 20) {
tft.drawFastHLine(0, i, 240, 0x1082);
}
// Three top buttons
int buttonWidth = 70;
int buttonHeight = 40;
int spacing = (240 - (3 * buttonWidth)) / 4;
drawTopButton(spacing, 10, buttonWidth, buttonHeight, "SET", mode == SET_MODE);
drawTopButton(spacing + buttonWidth + spacing, 10, buttonWidth, buttonHeight, "PWR", mode == PWR_MODE);
drawTopButton(spacing + 2*(buttonWidth + spacing), 10, buttonWidth, buttonHeight, "OCS", mode == OCS_MODE);
}
// ─────────────────────────────────────────────────────────────────────
// Functions to draw content for each screen mode
// ─────────────────────────────────────────────────────────────────────
void drawPwrScreenContent() {
// Clear the area below the buttons for the new content
tft.fillRect(0, 60, 240, 240 - 60, 0x0000);
// Draw static voltage and current labels
tft.setTextSize(5);
tft.setTextColor(NEON_PINK);
tft.setCursor(10, 70);
tft.print("12.01 V"); // This will be updated immediately
tft.setTextColor(ORANGE);
tft.setCursor(10, 130);
tft.print("03.76 A"); // This will be updated immediately
// Draw frequency background
tft.fillRoundRect(10, 195, 220, 40, 8, 0x0841);
tft.setTextSize(3);
tft.setTextColor(AQUA);
tft.setCursor(50, 205);
tft.print("F: 25.00 KHz"); // This will be updated immediately
}
void drawSetScreenContent() {
// Clear the area below the buttons for the new content
tft.fillRect(0, 60, 240, 240 - 60, 0x0000);
// Placeholder content for SET screen
tft.setTextSize(3);
tft.setCursor(20, 120);
tft.setTextColor(PURPLE);
tft.print("SETTINGS MODE");
}
void drawOcsScreenContent() {
// Clear the area below the buttons for the new content
tft.fillRect(0, 60, 240, 240 - 60, 0x0000);
// Placeholder content for OCS screen
tft.setTextSize(3);
tft.setCursor(20, 120);
tft.setTextColor(AQUA);
tft.print("OSCILLOSCOPE MODE");
}
// ─────────────────────────────────────────────────────────────────────
// Functions to update dynamic values on the PWR screen
// ─────────────────────────────────────────────────────────────────────
void updateVoltage(float voltage) {
// Clear the area where the voltage text is displayed
tft.fillRect(10, 70, 220, 50, 0x0000);
char voltStr[16];
sprintf(voltStr, "%.2f V", voltage);
tft.setTextSize(5);
tft.setCursor(10, 70);
tft.setTextColor(NEON_PINK);
tft.print(voltStr);
}
void updateCurrent(float current) {
// Clear the area where the current text is displayed
tft.fillRect(10, 130, 220, 50, 0x0000);
char ampStr[16];
sprintf(ampStr, "%.2f A", current);
tft.setTextSize(5);
tft.setCursor(10, 130);
tft.setTextColor(ORANGE);
tft.print(ampStr);
}
void updateFrequency(float freq) {
char freqStr[16];
sprintf(freqStr, "F: %.2f KHz", freq);
// Clear frequency area only and redraw the background
tft.fillRoundRect(12, 197, 216, 36, 6, 0x0841);
tft.setTextSize(3);
tft.setTextColor(AQUA);
int16_t x1, y1;
uint16_t w1, h1;
tft.getTextBounds(freqStr, 0, 0, &x1, &y1, &w1, &h1);
tft.setCursor((240 - w1) / 2, 205);
tft.print(freqStr);
}
// ─────────────────────────────────────────────────────────────────────
// Function to handle screen mode changes
// ─────────────────────────────────────────────────────────────────────
void setScreenMode(ScreenMode newMode) {
// Only redraw if the mode is actually changing
if (newMode == currentMode) return;
currentMode = newMode;
// 1. Redraw the background and buttons with the new highlight
drawButtonsAndBackground(currentMode);
// 2. Draw the content specific to the new mode
switch (currentMode) {
case SET_MODE:
drawSetScreenContent();
break;
case PWR_MODE:
drawPwrScreenContent();
// Immediately update with the current values after drawing the static content
updateVoltage(currentVolt);
updateCurrent(currentAmp);
updateFrequency(currentFreq);
break;
case OCS_MODE:
drawOcsScreenContent();
break;
}
}
// ─────────────────────────────────────────────────────────────────────
// Function to read and debounce buttons
// ─────────────────────────────────────────────────────────────────────
void readButtons() {
int reading[3];
reading[0] = digitalRead(BTN_SET);
reading[1] = digitalRead(BTN_PWR);
reading[2] = digitalRead(BTN_OCS);
// Check if any button state has changed
bool stateChanged = false;
for (int i = 0; i < 3; i++) {
if (reading[i] != lastButtonState[i]) {
stateChanged = true;
lastButtonState[i] = reading[i];
}
}
if (stateChanged) {
lastDebounceTime = millis();
}
// If enough time has passed, check the button states
if ((millis() - lastDebounceTime) > debounceDelay) {
// Check each button
for (int i = 0; i < 3; i++) {
if (reading[i] != buttonState[i]) {
buttonState[i] = reading[i];
// Only act on button press (LOW state when pressed)
if (buttonState[i] == LOW) {
switch (i) {
case 0:
setScreenMode(SET_MODE);
break;
case 1:
setScreenMode(PWR_MODE);
break;
case 2:
setScreenMode(OCS_MODE);
break;
}
}
}
}
}
}
// ─────────────────────────────────────────────────────────────────────
// Arduino setup