#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define TFT_DC 9
#define TFT_CS 10
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
//debounce timer
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 10;
//home button
const int PB_2 = 2;
int PB_2_STATE = 0;
int last_PB_2_STATE = LOW;
//increase
const int PB_3 = 3;
int PB_3_STATE = 0;
int last_PB_3_STATE = LOW;
//decrease
const int PB_4 = 4;
int PB_4_STATE = 0;
int last_PB_4_STATE = LOW;
//select button
const int PB_5 = 5;
int PB_5_STATE = 0;
int last_PB_5_STATE = LOW;
//back button
const int PB_6 = 6;
int PB_6_STATE = 0;
int last_PB_6_STATE = LOW;
int currentPage;
int currentSubPage;
int count=0;
int sub_count = 0;
int select_status = 0;
void setup() {
Serial.begin(115200);
Serial.println("Start");
tft.begin();
pinMode(PB_2, INPUT);
pinMode(PB_3, INPUT);
pinMode(PB_4, INPUT);
tft.setCursor(26, 50);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(2);
tft.println("Home Screen Test");
tft.setCursor(0, 160);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println("PB_2 HOME");
tft.println("PB_3 UP");
tft.println("PB_4 DOWN");
delay(1000);
home_screen();
delay(1000);
}
void home_screen(){
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(20, 10);
tft.setTextColor(ILI9341_BLUE);
tft.setTextSize(4);
tft.println("HOME");
currentPage = 0;
count = 0; // reset count
sub_count = 0; //reset subcategory
}
void menu_1(){
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(20, 10);
tft.setTextColor(ILI9341_BLUE);
tft.setTextSize(4);
tft.println("MENU #1");
tft.fillCircle(120, 160, 50, ILI9341_BLUE);
currentPage = 1;
sub_count = 1;
}
void menu_2(){
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(20, 10);
tft.setTextColor(ILI9341_BLUE);
tft.setTextSize(4);
tft.println("MENU #2");
currentPage = 2;
sub_count = 2;
}
void menu_3(){
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(20, 10);
tft.setTextColor(ILI9341_BLUE);
tft.setTextSize(4);
tft.println("MENU #3");
currentPage = 3;
}
void menu_4(){
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(20, 10);
tft.setTextColor(ILI9341_BLUE);
tft.setTextSize(4);
tft.println("MENU #4");
currentPage = 4;
}
void menu_5(){
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(20, 10);
tft.setTextColor(ILI9341_BLUE);
tft.setTextSize(4);
tft.println("MENU #5");
tft.setTextSize(6);
tft.setTextColor(ILI9341_ORANGE);
tft.println("HI");
tft.println("GREGG");
currentPage = 5;
}
void loop() {
int PB_2_STATE = digitalRead(PB_2); //home
int PB_3_STATE_read = digitalRead(PB_3); //up button
int PB_4_STATE_read = digitalRead(PB_4); // down button
int PB_5_STATE_read = digitalRead(PB_5); // select button
int PB_6_STATE_read = digitalRead(PB_6); // back button
//increase count
if(PB_3_STATE_read != last_PB_3_STATE){
lastDebounceTime = millis();
}
if((millis() - lastDebounceTime) > debounceDelay) {
if(PB_3_STATE_read != PB_3_STATE) {
PB_3_STATE = PB_3_STATE_read;
if(PB_3_STATE == HIGH){
count++;
sub_count++; // sub category counting increase
Serial.print(count);
Serial.print(" ");
Serial.println(sub_count);
}
}
}
// decrease count
if(PB_4_STATE_read != last_PB_4_STATE){
lastDebounceTime = millis();
}
if((millis() - lastDebounceTime) > debounceDelay) {
if(PB_4_STATE_read != PB_4_STATE) {
PB_4_STATE = PB_4_STATE_read;
if(PB_4_STATE == HIGH){
count--;
sub_count--; //sub category counting decrease
Serial.print(count);
Serial.print(" ");
Serial.println(sub_count);
}
}
}
last_PB_3_STATE = PB_3_STATE_read;
last_PB_4_STATE = PB_4_STATE_read;
//PB_5 debounce
if(PB_5_STATE_read != last_PB_5_STATE){
lastDebounceTime = millis();
}
if((millis() - lastDebounceTime) > debounceDelay) {
if(PB_5_STATE_read != PB_5_STATE) {
PB_5_STATE = PB_5_STATE_read;
if(PB_5_STATE == HIGH){
select_status = 1;
Serial.print("select pressed ");
Serial.println(select_status);
}
}
}
last_PB_5_STATE = PB_5_STATE_read;
//back button maybe delete later or add a debounce
if(PB_6_STATE_read == HIGH){
select_status = 0;
Serial.print("select status = ");
Serial.println(select_status);
}
//max count
if(count == 6){
count--;
Serial.println(count);
Serial.println("MAX SCREEN");
}
//min count
if(count == -1){
count++;
Serial.println(count);
Serial.println("MIN SCREEN");
}
//count checker
switch (count) {
//go to home screen
case 0:
if(currentPage != 0){
home_screen();
}
break;
//go to menu 1
case 1:
if(currentPage != 1){
menu_1();
}
break;
//go to menu 2
case 2:
if(currentPage != 2){
menu_2();
}
break;
//go to menu 3
case 3:
if(currentPage != 3){
menu_3();
}
break;
//go to menu 4
case 4:
if(currentPage != 4){
menu_4();
}
break;
//go to menu 5
case 5:
if(currentPage != 5){
menu_5();
}
break;
} //end of got to page switch
//go to home screen via home button
if((currentPage != 0) && (PB_2_STATE == HIGH)){
home_screen();
Serial.println("HOME SCREEN (page 0)");
Serial.println(count);
}else{}
//current page
switch (currentPage){
//home page function
case 0:
if(PB_5_STATE == HIGH){
Serial.println("home page selected");
}
break;
//menu 1 function
case 1:
if(PB_5_STATE == HIGH){
Serial.println("menu 1 select");
}
break;
}// end switch (current page)
}// end main loop