Display SSD1306 for ESP32
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
9.2 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. # Readme
  2. # Basic Instructions of Repository DisplayESP32
  3. **This repository aims to provide some functions for creating a user interface for the display
  4. Adafruit SSD1306 on microcontroller ESP32.**
  5. For such, new classes are implemented, which are included in the *.ino* code included in this repo.
  6. ## To begin with
  7. To be able to use this repository, it's important to first connect the display to the
  8. microcontroller correctly. The following diagram shows how it should be done when using an ESP32-WROOM board.
  9. ![Connection diagram used for DisplayESP32 repository](http://gmarxcc.com:8088/MSP430/DisplayESP32/raw/commit/b68980865195c06e96173dc9e7beeacc0f3e3a5f/Connection%20Diagram_UI.png "Connection diagram")
  10. It is possible to see that the display used in this project uses I2C protocol, thus only two pins are needed to allow
  11. communication with the microcontroller.
  12. On the other hand, the keyboard uses one pin for each button (configured as Pull up), and a connection to Ground.
  13. ## Macros
  14. Macros are defined to help use the library easily, making possible to modify the display specs in an easy way and also, the behaviour of the interface, by just changing the values defined.
  15. #define MAX_OPTIONS 10 //Maximum number of options for each menu
  16. #define MAX_MENUS 3
  17. #define MAX_GRAPH 3
  18. #define DISP_WIDTH 128 // OLED display width
  19. #define DISP_HEIGHT 64 // OLED display height
  20. #define REFRESH 10 //Refresh time in ms
  21. * **MAX_OPTIONS** defines the maximum number of options a menu can hold, in this case, there can only be three menus in total, as there is only enough memory reserved for them.
  22. * **MAX_MENUS** declares the maximum number of menu screens in the interface, it's not possible to create more menus than this number.
  23. * **MAX_GRAPHS** is the maximum number of graphs to create.
  24. * **DISP_WIDTH** and **DISP_HEIGHT** are hardware specific (SSD1306), it's possible to modify this values in the case that it's required to employ a different kind of display.
  25. * **REFRESH** is the time in milliseconds the interface will take in refreshing (this time affects the loop, keep that in mind)
  26. ## Classes
  27. In this section, the created classes are discussed, to better understand how each of them works.
  28. Before getting into the next classes, it's necessary to note that most methods in them aren't supposed to be accessed directly, there are methods in the classes Screen and Keyboard that use them to control the interface through their own methods, which end up calling those in each class but also control the behaviour of the display at the same time, which allows for less code lines when using the library to program an interface.
  29. Despite the above, the methods in each class will be presented to better know how they work.
  30. ### Option class
  31. This class is implemented to create options that will be later used to conform a menu.
  32. #### Attributes
  33. The attributes of this class are:
  34. private:
  35. int sizex;
  36. int sizey;
  37. String content;
  38. int pos;
  39. int textSpacing; //According to the height, defines the space for the text, so that it's vertically centered
  40. bool fill = false; //In case an option is not selected this should be false
  41. bool disp = false; //In case an option is not configured, it should be false and, thus hidden
  42. int destinationType; //Defines what the option leads to (another menu, graph, something else)
  43. int destinationIndex; //Defines where the option leads to (index of the destination)
  44. * **sizex**: Defines the size the option will occupy in the x axis (width), this value is gotten from the menu. Menu class has an attribute that defines the size it will have in the x axis.
  45. * **sizey**: Defines the height of the option (this value is gotten from the menu it belongs to).
  46. * **content**: This string corresponds to the caption the option will display once created.
  47. * **pos**: Integer that defines the position it has in the menu.
  48. * **destinationType**: Used to know what type of screen the option leads to (menu, graph, etc...).
  49. * **destinationIndex**: Used to know what index the destination screen has, leading only to that screen.
  50. **It is important to note that indexes are assigned according to the order the screen was created, if it is the first screen created, then its index is *0*.**
  51. #### Methods
  52. The following are the methods used to interact with each option created.
  53. ##### configure()
  54. This method assigns the values to the option created, according to the way it's containing menu is defined and the parameters we want for such option as is its destination.
  55. This method shouldn't be called on its own, because class Screen already has one method to create options and assign them to a specific menu. The prototype for this method is:
  56. void configure(String content, int sizex, int sizey, int pos, int destinationType, int destinationIndex)
  57. ##### getDestinationType() and getDestinationIndex()
  58. Methods accessed by others from Screen class. These return and integer corresponding to the type and index of the screen they lead to. It's not needed to call this methods directly, there are functions that do that and update the screen at the same time.
  59. ##### drawOpt()
  60. With this method, an option is drawn. This is another method that shouldn't be called directly, as there are other methods in the next classes that call it for all the options in a menu.
  61. This function requires the parameters shown in its prototype:
  62. void drawopt(int page, int pos, int optPPage)
  63. These parameters are obtained automatically from the method drawMenu in Menu that draws all the options within it. In this method, if the option is selected, then it will be printed in a filled rectangle, otherwise, it will appear as a black rectangle.
  64. ### Menu class
  65. This class is used to create menus and keep their attributes stored throughout the use of the interface.
  66. #### Attributes
  67. The following attributes belong to this class:
  68. private:
  69. int sizex;
  70. int sizey; //Y size of each option in the menu
  71. int options = 0; //This indicates the number of options created
  72. int pos = 0; //This indicates the position of the cursor
  73. int page = 0; //If the menu is too long, this indicates the page that is being displayed
  74. Option opt[MAX_OPTIONS];
  75. int optPPage;
  76. int previousScreen = 0;
  77. int previousContentType = 0;
  78. Where:
  79. * **sizex**: Defines the width of the options, it's preferable to leave this option as DISP_WIDTH or 128, since it will cover the whole display width.
  80. * **sizey**: Defines the height of each option in this menu, a value of 13 grants a good visualization of each option, greater values improve spacing, reducing the number of options per page though.
  81. * **options**: This acts as a counter that stores the number of options created in the menu.
  82. * **pos**: This variable stores the position of the menu in which the user is at a certain time.
  83. * **page**: According to the number of options per page (optPPage), this variable holds the page to be printed in larger menus.
  84. * **Option opt[MAX_OPTIONS]**: This is an array that holds every option created in the menu (with a maximum of MAX_OPTIONS).
  85. * **previousScreen** and **previousContentType**: These integers keep the values of the screen that led to this menu, to make it possible to return to that screen. This values are assigned from a method in Menu, called from another one in Screen, which is used to control the interaction with the interface. This will be discussed later in this document, see Screen class.
  86. #### Methods
  87. Now, we'll talk about the methods included in this class.
  88. ##### configure()
  89. To create a menu, it's important to configure it first. That's where this method is applied, though not directly, since there is another method in Screen that calls for this method and creates a menu for the interface. The prototype for this method is as follows:
  90. void configure(int sizex, int sizey)
  91. From the height of each option, it is possible to determine the number of options by each page of the menu. As a result, in this method, optPPage is assigned too.
  92. ##### createOption
  93. This method is in charge of adding options to the menu. It recurs to the method configure in Option and assigns the values for the size, position and destination. The prototype is the shown next:
  94. void createOption(String content, bool destinationTypeMenu, int destinationIndex)
  95. The parameters are received from the method in Screen to create an option for a menu.
  96. As we can see in the next line, the option receives a position according to the number of options currently in the menu, then the variable is increased because there is one more option.
  97. this->opt[this->options].configure(content, this->sizex, this->sizey, this->options++, destinationTypeMenu, destinationIndex)
  98. Furthermore, the option is assinged to a place in the array of options.
  99. ##### extractDestinationType() and extractDestinationIndex()
  100. Both methods are used to get the integers corresponding to the destination of the option selected, which serve to interact with the interface, allowing the user to enter an option of the menu and change from screen to screen.
  101. ##### drawMenu()
  102. **drawMenu()** is used to draw all the options in the menu.
  103. ### Graph class
  104. ### Screen class
  105. ### Keyboard class
  106. ### Implementation