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.

192 lines
6.7 KiB

  1. class Screen{
  2. private:
  3. Menu menu[MAX_MENUS]; //Array of menus to use
  4. Graph graph[MAX_GRAPHS]; //Array of graphs to use
  5. Modifier modifier[MAX_MODIFIERS]; //Array of modifiers to use
  6. int counterM = 0; //Number of menus created
  7. int counterG = 0; //Number of graphs created
  8. int counterMod = 0; //Number of modifiers created
  9. bool redraw = true; //Redraw interface for when there is a change of screen
  10. int currentScreen = 0;
  11. int contentType = 0;
  12. public:
  13. void configure(bool fullsetting, char address){ //This method allows the configuration of the display when the parameter is true. Otherwise only prints a greeting message.
  14. if(fullsetting){
  15. //Adafruit_SSD1306 display(DISP_WIDTH, DISP_HEIGHT, &Wire, -1);
  16. Serial.begin(115200);
  17. if (!display.begin(SSD1306_SWITCHCAPVCC, address)) {
  18. #ifdef __DEBUG__
  19. Serial.println("Display not found!");
  20. #endif
  21. while (true);
  22. }
  23. }
  24. display.clearDisplay();
  25. // Text size
  26. display.setTextSize(2);
  27. // Text color
  28. display.setTextColor(SSD1306_WHITE);
  29. // Text position
  30. display.setCursor(25, 20);
  31. display.println("Welcome");
  32. display.setTextSize(1);
  33. display.display();
  34. delay(5000);
  35. }
  36. void createMenu(int sizex, int sizey){ //This method is used for the creation of a menu
  37. this->menu[counterM].configure(sizex, sizey);
  38. this->counterM++;
  39. }
  40. void createOption(int menuIndex, String content, int destinationType, int destinationIndex){ //this method should be used for creating an option in a menu
  41. this->menu[menuIndex].createOption(content, destinationType, destinationIndex);
  42. }
  43. void createVGraph(String title, double xpos, double ypos, double width, double height,
  44. double yminimum, double ymaximum, double yStepSize, int digit){ //this method calls the configure() of graph for a vertical graph
  45. this->graph[counterG].configure(title, 'a', xpos, ypos, width, height, yminimum, ymaximum, 0, 0, yStepSize, 0, digit, 0);
  46. this->counterG++;
  47. }
  48. void createHGraph(String title, double xpos, double ypos, double width, double height,
  49. double xminimum, double xmaximum, double xStepSize, int digit){ //this method calls the configure() of graph for a horizontal graph
  50. this->graph[counterG].configure(title, 'b', xpos, ypos, width, height, 0, 0, xminimum, xmaximum, 0, xStepSize, digit, 0);
  51. counterG++;
  52. }
  53. void createCGraph(String title, double xpos, double ypos, double width, double height,
  54. double yminimum, double ymaximum, double xminimum, double yStepSize, double xStepSize, int digit, void * maximum){ //this method calls the configure() of graph for a cartesian chart
  55. this->graph[counterG].configure(title, 'c', xpos, ypos, width, height, yminimum, ymaximum, xminimum, 0, yStepSize, xStepSize, digit, maximum);
  56. counterG++;
  57. }
  58. void createModifier(String title, int *value, int max, int min, int step){ //This method is used for the creation of a menu
  59. this->modifier[counterMod].configure(title, value, max, min, step);
  60. this->counterMod++;
  61. }
  62. /*
  63. void redrawFlag(){
  64. this->redraw = true;
  65. }
  66. */
  67. //The following method is used for assingning a value to a graph
  68. //This can be avoided using pointers to the variable to plot in the graph
  69. void graphAssignValue(int graphIndex, double value){
  70. this->graph[graphIndex].assignValue(value);
  71. if(this->currentScreen == graphIndex && this->contentType == 1)
  72. this->redraw = true;
  73. }
  74. //This method controls the whole interface, it needs to be called within a loop
  75. void control(){
  76. if (redraw){
  77. if (contentType == 0){
  78. menu[currentScreen].drawMenu();
  79. }
  80. else if (contentType == 1){
  81. graph[currentScreen].drawGraph();
  82. }
  83. else if (contentType == 2){
  84. modifier[currentScreen].drawModifier();
  85. }
  86. this->redraw = false;
  87. }
  88. }
  89. //The following two methods allow the change in position of the cursor
  90. void increasePos(){
  91. if(this->menu[this->currentScreen].extractPos() < this->menu[this->currentScreen].extractOptNumber() - 1)
  92. this->menu[this->currentScreen].increasePos();
  93. }
  94. void decreasePos(){
  95. if(this->menu[this->currentScreen].extractPos() > 0)
  96. this->menu[this->currentScreen].decreasePos();
  97. }
  98. //This method lets the user go into another screen by selecting an option
  99. void goTo(){
  100. if(this->contentType == 0){
  101. int newScreen = this->menu[this->currentScreen].extractDestinationIndex();
  102. int newContentType = this->menu[this->currentScreen].extractDestinationType();
  103. if (newContentType == 0){
  104. this->menu[newScreen].setPreviousScreen(this->currentScreen);
  105. this->menu[newScreen].setPreviousContentType(this->contentType);
  106. }
  107. else if(newContentType == 1){
  108. this->graph[newScreen].setPreviousScreen(this->currentScreen);
  109. this->graph[newScreen].setPreviousContentType(this->contentType);
  110. this->graph[newScreen].reset();
  111. this->graph[newScreen].redrawFlag();
  112. }
  113. else if(newContentType == 2){
  114. this->modifier[newScreen].setPreviousScreen(this->currentScreen);
  115. this->modifier[newScreen].setPreviousContentType(this->contentType);
  116. }
  117. this->contentType = newContentType;
  118. this->currentScreen = newScreen;
  119. this->redraw = true;
  120. }
  121. }
  122. void goBack(){
  123. if(contentType == 0){
  124. //Gets indexes from previous screen saved in actual screen if it is a menu, and sets them as the current indexes
  125. this->currentScreen = this->menu[this->currentScreen].getPreviousScreen();
  126. this->contentType = this->menu[this->currentScreen].getPreviousContentType();
  127. }
  128. else if(contentType == 1){
  129. //Gets indexes from previous screen saved in actual screen if it is a graph, and sets them as the current indexes
  130. this->currentScreen = this->graph[this->currentScreen].getPreviousScreen();
  131. this->contentType = this->graph[this->currentScreen].getPreviousContentType();
  132. }
  133. else if(contentType == 2){
  134. this->currentScreen = this->modifier[this->currentScreen].getPreviousScreen();
  135. this->contentType = this->modifier[this->currentScreen].getPreviousContentType();
  136. }
  137. this->redraw = true;
  138. }
  139. //These methods control the plus and minus button actions
  140. void plusAction(){
  141. if(contentType == 0){
  142. increasePos();
  143. }
  144. else if(contentType == 2){
  145. this->modifier[currentScreen].increaseValue();
  146. }
  147. this->redraw = true;
  148. }
  149. void minusAction(){
  150. if(contentType == 0){
  151. decreasePos();
  152. }
  153. else if(contentType == 2){
  154. this->modifier[currentScreen].decreaseValue();
  155. }
  156. this->redraw = true;
  157. }
  158. int getCurrentScreen(){
  159. return this->currentScreen;
  160. }
  161. int getContentType(){
  162. return this->contentType;
  163. }
  164. };