Project HOME Control part 3 menu

In the third part we create a menu that is displayed on the TFT display. With the Rotary Encoder from the 35in1 sensor kit we can select an entry in the menu. If there are more menu entries than display lines, the display scrolls up or down automatically so that the selected entry is always visible.

Wiring:

Program:

 

'35; include <adafruit'u gfx.h> // Core graphics library
'35; includes <adafruu st7735.h> // Hardware-specific library
//Integration of Font libraries
'35; include <Fonts/FreeSans9pt7b.h> //Font for Logo
'35; include <Fonts/FreeSansBold9pt7b.h> //Font for Logo

'35; include <spi.h>
'35; include <sd.h>

//Definition of the pin used
"35; define TFT CS 5// Chip select line for TFT display
"35; define RST TFT 22// Reset line for TFT
"35; define TFT DC 21// Data/command line for TFT

';define SD CS 16// Chip select line for SD card


//tft instance
Adafruit ST7735 tft = Adafruit ST7735(TFT CS, TFT DC, TFT RST);

char *menu[] = {"entry 1", "entry 2", "entry 3", "entry 4",
"entry 5", "entry 6", "entry 7", "entry 8", "entry 9",
"entry 10", "entry 11", "entry 12", "entry 13", "entry 14"};
               
uint8 u t menu of lines = 14; //Number of menu entries

const byte button = 15;
const byte pulse = 14;
const byte pulseDirection = 4;

portMUX TYPE mux = portMUX INITIALIZER UNLOCKED;
volatile int menu = 0;
volatile int menu
volatile int menu = 0;

//global variables for the menu
You can't use the line = 127; //Number of pixels per line
uint8 t start menu = 32; //Line where the menu starts
uint8 t menu size = 12; //Number of rows to 10 pixels
WHITE, WHITE, WHITE, WHITE, WHITE, WHITE, WHITE //Font
OUT16u t bg u color = ST7735 BLACK; //Menu
The colour of the ST7735 BLACK //Font color for selection
The colour of the ST7735 GREEN //Background for selection
uint8 t line selected = 0; //Selected menu line
uint8 t top line = 0; //first displayed menu line

//function for writing a selected line
void menuSelLine(uint8 u t line, char *txt){
uint8 to y1 = line * 10 + start menu; //Top of the line
tft.setCursor(4,y1);
tft.setTextColor(sel fnt u color, sel u bg)
tft.print(txt);
}

//function for writing a line
void menuLine(uint8 u t line, char *txt){
uint8 to y1 = line * 10 + start menu; //Top of the line
tft.setCursor(4,y1);
tft.setTextColor(fnt color,bg color);
tft.print(txt);
}

//function to display the menu
void showMenu()
   //first delete the background
tft.fillRect(0,(start menu),line linen,(menu size *10),bg color);
uint8 t mill = top line;
while ((mlin < menu u lines) && ((mlin &top < menu u size){
if (mln == line/ u selected)
menuSelLine(mlin-top line,menu[mlin]);
} else {
menuLine(mlin-top line,menu[mlin]);
       }
mlin+;
   }
   
}

//Function to move the selection down
void selectionDown()
line selected+;
if (line/ u selected >= menu/ lines) line/ u selected = menu/ lines-1;
if ((line selected -top >= menu size) top line+;
showMenu();
}


//Function to move the selection up
void selectionUp()
if (line/ u selected > 0) line/ u selected--;
if (line selected < top line) top line--;
showMenu();
}

//Function to display a text on the display in a certain color
void displayText(char *text, uint16 u t color)
tft.setTextColor(color);
tft.setTextWrap(true); //automatic line break is enabled

tft.print(text);
}

//Function to display a text at an y point (x,y)
//the display. The color can be selected
void displayText(uint16 to x, uint16 to y, char *text *uint16 to t color)
tft.setCursor(x, y);
displayText(text,color);
}

//function for displaying the logo at the top of the display
void displayLogo()
   //Fill Background
tft.fillRect(0,0127,30,ST7735-u YELLOW);
   //Draw frame
tft.drawRect(1,1125,28,ST7735u BLUE);
   //Font for the word HOME bold
tft.setFont(&FreeSansBold9pt7b);
    //Position cursor
tft.setCursor(7,20);
    //Print text in black
tft.setTextColor(ST7735 BLACK);
tft.print("HOME");
    //Font for the word control not fat
tft.setFont(&FreeSans9pt7b);
    //Print text in red
tft.setTextColor(ST7735)
tft.print("Control");
    //Reset Font to Default
tft.setFont(NULL);
}

//interrupt service for switch
//change current color
void IRAM ATTR btnClick() ()
ISR CRITICAL(&mux);
menuBtn = 1;
portEXIT CRITICAL ISR(&mux);
}

//interrupt for rotary pulse
void IRAM ATTR rotaryPulse()
byte dir = digitalRead(pulseDirection);
if ((menuUp == 0) && (menuDown === 0)) {
ISR CRITICAL(&mux);
if (dir=0)
MenuUp = 1;
} else {
MenuDown = 1;
       }
portEXIT CRITICAL ISR(&mux);
   }
}

   
void setup()
Serial.start(115200);
   //tft initialize and black background
tft.initR(INITR BLACKTAB);
tft.fillScreen(ST7735 BLACK);
   //Show logo
displayLogo();
   //Initialize SD card and show result
If (! SD.start(SD CS){
displayText(25,40,"No SD-Card",ST7735 YELLOW);
} else {
displayText(34,40,"SD-Card OK",ST7735 GREEN);
   }
delay(1000);
showMenu();
   //define input pins
pinMode(button, INPUT)
pinMode(pulse, INPUT)
pinMode(pulseDirection, INPUT)
   //define interrupts
attachInterrupt(digitalPinToInterrupt(pulses),rotaryPulse, FALLING);
attachInterrupt(digitalPinToInterrupt(button),btnClick, FALLING);
}

void loop()
if (menu up)
delay(100); //To unload
portENTER-u CRITICAL(&mux);
MenuUp = 0;
menuDown=0;
portEXIT CRITICAL(&mux);
selectionUp();
Serial.println("Up");
   }
if (menu)
delay(100); //To unload
portENTER-u CRITICAL(&mux);
menuUp=0;
MenuDown = 0;
portEXIT CRITICAL(&mux);
selectionDown();
Serial.println("Down!") What?
   }


}

 

Description of the parameters:

  • line = 127 Width of a display line in pixels
  • start menu = 32 Position of the top edge of the first row of the manoeuvre
  • menu size = 12 Number of rows used for the menu
  • fnt color = ST7735 WHITE Colour of the font for an entry
  • bg color = ST7735 BLACK Background color for an entry
  • color = ST7735 BLACK Colour of the font for a selected entry
  • color = ST7735 GREEN Background color for a selected entry
  • line selected = 0 Number of line in the menu that has just been selected
  • top line = 0 Number of the line in the menu that appears at the top of the display. A value greater 0 causes the menu to scroll

 

The program can also be loaded as a file from GIT-Hub. Central.ino file in ZIP package

https://github.com/GerLech/HomeControl/archive/master.zip

DisplaysEsp-32Projekte für fortgeschritteneSmart home

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. Install ESP32 now from the board manager
  2. Lüftersteuerung Raspberry Pi
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1
  4. ESP32 - das Multitalent
  5. OTA - Over the Air - ESP programming via WLAN