While working on the Home-Control project, I came up with the idea of creating a library with which you can easily use the TFT Display module and keep the Sketch program free from these routines.
The library manages the display line by line, where the row height is adjustable. The first version contains all the functions to display a list of lines of text and to be able to select using a Rotary encoder. The index and the contents of the selected row can be read out.
The test setup was built with an ESP32 and a 1.8 inch TFT display. Using another controller or display should not cause any problems as long as the display is compatible with the Adafruit-ST7735 library. The pins used must of course be adjusted.
Here the wiring used with the ESP32-Dev board
For hardware, see also "Project HOMEControl Part 2 The Headquarters" in this blog.
The library can be HCScreen Librarydownloaded. In the Arduino IDE, simply import the ZIP file from the download in the Library Manager.
For examples, see HCScreen Example Sketches
To create an instance, you only pass the address of the ST7735 instance.
This function must be called after the orientation of the display (landscape or portrait) has been changed. This function determines the right, left, top, and bottom margins so that the display is centered. It also fills the entire display area with the background color.
This function sets the height for a row in pixels. The characters are seven pixels high. With a pixel space, the minimum value for height = 8. A height=10 increases readability but reduces the number of rows that can be represented. Because a call to this function changes the geometry, the function init() is automatically called and the content is redisplayed.
setBaseColor (font color, background color)
This function can be used to set the color of the font and background for display. The color values are passed in 3-byte format (red, green, blue) as is common on web pages. So e.g. 0xFF0000 = red, 0x00FF00 = gün or 0x0000FF = blue. The colors are then automatically converted to the 16-bit format for the display. Because the background color is changed, this function automatically fills the display area with the new background color and redisplays the content.
setSelectionColor (font color, background color)
This function can be used to set the color of the font and background for the selected line. The format is the same as setBaseColor. The content is displayed again.
setTitleColor (font color, background color)
This function can be used to set the color of the font and background for the title line. The format is the same as setBaseColor. The content is displayed again.
This function represents a title that may be set and as many lines from the list of lines of text as you can find on the display. The display is from a variable start line, so that the display window can be moved over a larger number of rows. When the selection is selected, the selected row is displayed according to the set colors for selected rows.
setTitle (text, font color, background color)
The specified text appears as the title line at the top of the display. Alternatively, the color for font and background can be passed. The contents of the ad are moved down one line.
setMenu(list of entries, number)
The content list for the display is filled with the Items menu and the selection is enabled. The first entry is automatically selected and the display starts with the first entry. Number indicates the number of entries.
When selection is enabled, the next line is selected until the end of the content list is reached. If the selected row is outside the representable range, the start line is incremented so that the displayed portion of the content list is moved up and the selected row remains visible. If the selection is not selected, the start line is incremented by one so that the displayed block of text is moved up. This is done until the last line of the content list is displayed in the last row of the display area.
When the selection is enabled, the previous line is selected until the beginning of the content list is reached. If the selected row is outside the representable range, the start line is reduced so that the displayed portion of the content list is moved down and the selected row remains visible. If the selection is not selected, the start line is reduced by one so that the displayed block of text is moved down. This is done until the first line of the content list is displayed in the first row of the display area.
This function returns the text content of the currently selected line or a blank string if the selection is not enabled.
This function returns the line number (starting with 0) of the currently selected row or -1 if the selection is not selected.
The Sketch ESP32_Control_Center example shows how a simple menu control can be realized with a Rotary encoder. The RotaryEncoder library is used to drive the Rotary Encoder.
A main menu with 15 entries and a submenu with three entries are defined. On the main menu you can test the movement of the display block if you go all the way down with the selection. If you click the Rotary Encoder when the "Submenu" line is selected, the submenu is displayed. If you click the Rotary Encoder when the Back line is selected in the submenu, the main menu is displayed again.