/*
Simple "Hello World" for ILI9341 LCD
https://wokwi.com/arduino/projects/308024602434470466
*/
#include "SPI.h"
//#include "TFT.h"
// Adafruit GFX Graphics Library
// https://learn.adafruit.com/adafruit-gfx-graphics-library
#include <Adafruit_GFX.h>
// displayio driver for ST7735B TFT-LCD display
// Examples in https://github.com/adafruit/Adafruit-ST7735-Library
#include <Adafruit_ST7735.h>
// pin definition for monitor
#define cs 10
#define dc 9
#define rst 8 //reset
// create an instance of the library
//TFT tft = TFT(cs, dc, rst);
Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, rst);
// joy stick
#define VRX_PIN A0 // Arduino pin connected to VRX pin
#define VRY_PIN A1 // Arduino pin connected to VRY pin
#define JOY_BUT_PIN 2 // Arduino pin connected to SW pin
// Comparison thresholds to determine joystick state
#define JOY_LEFT_THRESHOLD 350
#define JOY_RIGHT_THRESHOLD 650
#define JOY_UP_THRESHOLD 350
#define JOY_DOWN_THRESHOLD 650
// Commands from the joystick
#define JOY_NULL 0x00
#define JOY_LEFT 0x01
#define JOY_RIGHT 0x02
#define JOY_UP 0x04
#define JOY_DOWN 0x08
/**
* See "joy_points_*" functions.
*/
uint8_t joy_command = JOY_NULL;
/**
* Update the global variable "joy_command".
*
* Compare the state with the "joy_points_..." functions.
*/
void update_joy() {
int joy_x_position = analogRead(VRX_PIN);
int joy_y_position = analogRead(VRY_PIN);
joy_command = JOY_NULL;
// Check left/right commands
if (joy_x_position < JOY_LEFT_THRESHOLD)
joy_command = joy_command | JOY_LEFT;
else if (joy_x_position > JOY_RIGHT_THRESHOLD)
joy_command = joy_command | JOY_RIGHT;
// check up/down commands
if (joy_y_position < JOY_UP_THRESHOLD)
joy_command = joy_command | JOY_UP;
else if (joy_y_position > JOY_DOWN_THRESHOLD)
joy_command = joy_command | JOY_DOWN;
}
/**
* Check if joystick points left.
*/
bool inline joy_points_left() {
return joy_command & JOY_LEFT;
}
/**
* Check if joystick points right.
*/
bool inline joy_points_right() {
return joy_command & JOY_RIGHT;
}
/**
* Check if joystick points up.
*/
bool inline joy_points_up() {
return joy_command & JOY_UP;
}
/**
* Check if joystick points down.
*/
bool inline joy_points_down() {
return joy_command & JOY_DOWN;
}
void setup() {
Serial.begin(9600);
tft.initR(INITR_BLACKTAB);
tft.setRotation(3);
tft.setCursor(0, 0);
tft.setTextColor(ST7735_WHITE);
tft.setTextWrap(true);
tft.fillScreen(ST7735_BLACK);
}
/**
* Present dialog options to the player and return the chosen one.
*/
uint8_t prompt_dialog_option(
const __FlashStringHelper *prompt,
const __FlashStringHelper *option_0,
const __FlashStringHelper *option_1 = nullptr,
const __FlashStringHelper *option_2 = nullptr,
const __FlashStringHelper *option_3 = nullptr
) {
uint8_t previous_option = 1;
uint8_t selected_option = 0;
uint8_t option_count = 4;
uint16_t fg_color[4] = {
ST7735_WHITE, ST7735_WHITE, ST7735_WHITE, ST7735_WHITE
};
uint16_t bg_color[4] = {
ST7735_BLACK, ST7735_BLACK, ST7735_BLACK, ST7735_BLACK
};
// Make sure that empty options come last
if (option_3 == nullptr) {
option_count = 3;
}
if (option_2 == nullptr) {
option_3 = nullptr;
option_count = 2;
}
if (option_1 == nullptr) {
option_3 = nullptr;
option_2 = nullptr;
option_count = 1;
}
tft.fillScreen(ST7735_BLACK);
tft.setTextSize(1);
tft.setTextWrap(true);
// Show selection dialog until user presses the joystick's button
while (digitalRead(JOY_BUT_PIN)) {
// Update selected option
update_joy();
if (joy_points_up()) {
selected_option -= 1;
// Handle underflow
if (selected_option >= option_count) {
selected_option = option_count - 1;
}
} else if (joy_points_down()) {
// Modulo handles overflow
selected_option = (selected_option + 1) % option_count;
}
if (previous_option != selected_option) {
// Update colors
fg_color[previous_option] = ST7735_WHITE;
bg_color[previous_option] = ST7735_BLACK;
fg_color[selected_option] = ST7735_BLACK;
bg_color[selected_option] = ST7735_WHITE;
// Prepare screen and print prompt
tft.setCursor(0, 0);
tft.setTextColor(ST7735_WHITE, ST7735_BLACK);
tft.print(prompt);
tft.print(F("\n\n"));
// Print options, colors of selected one are inverted
tft.setTextColor(fg_color[0], bg_color[0]);
tft.print(option_0);
if (option_1 != nullptr) {
tft.print(F("\n\n"));
tft.setTextColor(fg_color[1], bg_color[1]);
tft.print(option_1);
}
if (option_2 != nullptr) {
tft.setTextColor(fg_color[2], bg_color[2]);
tft.print(F("\n\n"));
tft.print(option_2);
}
if (option_3 != nullptr) {
tft.setTextColor(fg_color[3], bg_color[3]);
tft.print(F("\n\n"));
tft.print(option_3);
}
// Finished redrawing, update previous option
previous_option = selected_option;
}
delay(100);
}
return selected_option;
}
void loop() {
//prompt_dialog_option(
// F("Nachdem du dich mit Maultaschen gestaerkt hast, kannst du dich endlich deiner Arbeit zuwenden."),
// F("[Weiter]")
//);
tft.fillScreen(ST77XX_BLACK);
tft.setTextSize(1);
tft.setCursor(0,0);
tft.print(F("Congratulations Dr. Sun, you have done great research!"));
delay(80000);
while(1) {
prompt_dialog_option(
F("Congratulations Dr. Sun, you have done great research!"),
F("[Continue]")
);
delay(1000);
}
}