#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <SD.h>
#define TFT_CS 15
#define TFT_DC 4
#define SD_CS 13
#define SW_UP 5
#define SW_DOWN 27
#define SW_BTN 14
#define SP_PIN 12
#define LED_PIN LED_BUILTIN
#define INPUT_UP 0
#define INPUT_DOWN 1
#define INPUT_LEFT 2
#define INPUT_RIGHT 3
#define INPUT_ENTER 4
#define INPUT_NULL 100
int width=128;
int height=160;
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC);
int ledStatus=0;
int selectedItem=0;
const int items=4;
char* selections[items] = {
"Beep Test",
"LED",
"Test 3",
"Test 4"
};
void notify(uint16_t color, uint16_t textColor, char* header, char* msg, int lines, int time, int clear)
{
int totalYSpaceUsed=0;
totalYSpaceUsed+=(lines*8)+24; // Size of Dialog Body, So that we can clear it.
tft.fillRect(0, height/2, width, (lines*8)+24, color); // Dialog Body BG
tft.fillRect(0,height/2, width,16, 0xffffff); // Header Bar BG
tft.setTextColor(color); // Set the text color
tft.setCursor(12,4+(height/2)); // position cursor for header title
tft.print(header); // print header
// User input based exit
if(time == 0)
{
tft.setCursor(width-16,4+(height/2)); // set cursor for X
tft.setTextColor(0xffffff); // set text to white
tft.fillRect(width-16-(1),4+(height/2), 8,8, 0); // x BG
tft.print("X"); // print it
}
tft.setTextColor(textColor); // Set text color to User Defined Color
tft.setCursor(0,20+(height/2)); // set cursor for the message.
tft.print(msg); // print the message
// User input based exit
if(time == 0)
{
while(digitalRead(SW_BTN) == 0)
{
delay(10); // wait, speed's up simulators.
}
delay(100); // To prevent click repeat.
}
else { // Delayed exit
delay(time); // delay before return
}
if(clear == 1)
{
tft.fillRect(0, height/2, width, totalYSpaceUsed, 0); // Reset notification, delete it
}
return; // exit
}
void menu()
{
tft.fillRect(0, 0, width, (items*8)+24, 0x0000ff);
tft.fillRect(0,0, width,16, 0xffffff);
tft.setTextColor(0x0000ff);
tft.setCursor(12,4);
tft.print("Hello!");
tft.setTextColor(0xffffff);
tft.setCursor(0,20);
for(int item=0; item<items; item++)
{
if(selectedItem == item)
{
tft.print(">");
}
else {
tft.print(" ");
}
tft.print(selections[item]);
tft.println("");
}
}
int getUserInput()
{
if(digitalRead(SW_UP) == 1)
{
return INPUT_UP; // Return user clicked up.
}
else if(digitalRead(SW_DOWN) == 1)
{
return INPUT_DOWN; // Return user clicked down.
}
else if(digitalRead(SW_BTN) == 1)
{
return INPUT_ENTER; // Return user clicked enter.
}
else {
return INPUT_NULL; // User pressed nothing
}
}
void selectionPrinter(char* menuSelections[], const int menuItems, int menuSelectedItem, char* title, int bgColor, int textColor)
{
tft.fillRect(0, 0, width, (items*8)+24, bgColor);
tft.fillRect(0,0, width,16, 0xffffff);
tft.setTextColor(bgColor);
tft.setCursor(12,4);
tft.print(title);
tft.setTextColor(textColor);
tft.setCursor(0,20);
for(int item=0; item<menuItems; item++)
{
if(menuSelectedItem == item)
{
tft.print(">");
}
else {
tft.print(" ");
}
tft.print(menuSelections[item]);
tft.println("");
}
}
int test1() {
int menuSelectedItem=0;
const int menuItems=2;
int updateUi = 1;
const int testNotes = 8;
int freqs[testNotes] = {
100, 200, 300, 400, 500, 600, 700, 800
}; int delays[testNotes] = {
500, 500, 500, 500, 500, 500, 500, 500
};
char* menuSelections[menuItems] = {
"Test it!",
"back"
};
while(true)
{
if(updateUi==1)
{
selectionPrinter(menuSelections, menuItems, menuSelectedItem, "Beep Test", ST7735_BLUE, 0xffffff);
updateUi=0;
}
int keyPressed=getUserInput();
switch(keyPressed)
{
case INPUT_UP:
tone(SP_PIN, 300, 50);
menuSelectedItem-=1;
updateUi=1;
delay(100);
break;
case INPUT_DOWN:
tone(SP_PIN, 300, 50);
menuSelectedItem+=1;
updateUi=1;
delay(100);
break;
case INPUT_ENTER:
if(menuSelectedItem == 0)
{
for(int i=0; i<testNotes; i++)
{
tone(SP_PIN, freqs[i], delays[i]);
char tmp[1024];
sprintf(tmp, "Testing, F:%d, D:%d.", freqs[i], delays[i]);
notify(ST7735_ORANGE, 0xffffff, "Please Wait", tmp, 1, delays[i], 0);
}
notify(ST7735_GREEN, 0xffffff, "Complete!", "Test Complete, Press YELLOW.", 1, 0, 1);
}
else if(menuSelectedItem == 1)
{
menu();
return 0;
}
break;
}
delay(10);
}
return 0;
}
int led_toggle() {
int menuSelectedItem=0;
const int menuItems=3;
int updateUi = 1;
char* menuSelections[menuItems] = {
"LED On",
"LED Off",
"back"
};
while(true)
{
if(updateUi==1)
{
selectionPrinter(menuSelections, menuItems, menuSelectedItem, "LED Toggle", ST7735_BLUE, 0xffffff);
updateUi=0;
}
int keyPressed=getUserInput();
switch(keyPressed)
{
case INPUT_UP:
tone(SP_PIN, 300, 50);
menuSelectedItem-=1;
updateUi=1;
delay(100);
break;
case INPUT_DOWN:
tone(SP_PIN, 300, 50);
menuSelectedItem+=1;
updateUi=1;
delay(100);
break;
case INPUT_ENTER: // Checks selected item
if(menuSelectedItem == 0)
{
ledStatus=1; // LED On
digitalWrite(LED_PIN, ledStatus);
}
else if(menuSelectedItem == 1)
{
ledStatus=0; // LED Off
digitalWrite(LED_PIN, ledStatus);
}
else if(menuSelectedItem == 2)
{
menu();
return 0;
}
break;
}
delay(10);
}
return 0;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(SW_UP, INPUT);
pinMode(SW_DOWN, INPUT);
pinMode(SW_BTN, INPUT);
pinMode(SP_PIN, OUTPUT);
tft.initR(INITR_GREENTAB);
tft.setCursor(0, 0);
tft.setTextColor(ST7735_WHITE);
tft.setTextSize(1);
if (!SD.begin(SD_CS)) {
tft.println("Card failed, or not present");
// don't do anything more:
while (1);
}
tft.setCursor(0, 0);
tft.print("Dakotath");
delay(5000);
menu();
}
void loop() {
if(digitalRead(SW_UP) == 1)
{
tone(SP_PIN, 300, 50);
selectedItem-=1;
menu();
delay(100);
}
if(digitalRead(SW_DOWN) == 1)
{
tone(SP_PIN, 300, 50);
selectedItem+=1;
menu();
delay(100);
}
if(digitalRead(SW_BTN) == 1)
{
if(selectedItem == 0)
{
test1();
}
else if(selectedItem == 1)
{
led_toggle();
}
else{
char tmp[1024];
sprintf(tmp, "That feature is not supported yet!\nCode %d", selectedItem);
notify(ST7735_RED, 0xffffff, "Warning!!!", tmp, 2, 0, 1);
menu();
}
}
delay(10);
}