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.

185 lines
3.9 KiB

  1. #define MAX_OPTIONS 10 //Maximum number of options for each menu
  2. #define MAX_MENUS 10
  3. #define MAX_GRAPH 10
  4. #define __DEBUG__
  5. #include <Arduino.h>
  6. #include <SPI.h>
  7. #include <Wire.h>
  8. #include <Adafruit_GFX.h>
  9. #include <Adafruit_SSD1306.h>
  10. #include <Fonts/FreeSans12pt7b.h>
  11. #define DISP_WIDTH 128 // OLED display width
  12. #define DISP_HEIGHT 64 // OLED display height
  13. Adafruit_SSD1306 display(DISP_WIDTH, DISP_HEIGHT, &Wire, -1);
  14. void setDisp(bool fullsetting){
  15. if(fullsetting){
  16. //Adafruit_SSD1306 display(DISP_WIDTH, DISP_HEIGHT, &Wire, -1);
  17. Serial.begin(115200);
  18. if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
  19. #ifdef __DEBUG__
  20. Serial.println("No se encuentra la pantalla OLED");
  21. #endif
  22. while (true);
  23. }
  24. }
  25. display.clearDisplay();
  26. // Tamaño del texto
  27. display.setTextSize(2);
  28. // Color del texto
  29. display.setTextColor(SSD1306_WHITE);
  30. // Posición del texto
  31. display.setCursor(0, 16);
  32. // Escribir texto
  33. display.println("Hola");
  34. display.display();
  35. display.setCursor(0, 32);
  36. display.println("mundo!!");
  37. display.display();
  38. }
  39. class Option{
  40. private:
  41. int sizex;
  42. int sizey;
  43. String content;
  44. int pos;
  45. bool fill = false;
  46. bool disp = false;
  47. void (*f)(void);
  48. public:
  49. //Option(){}
  50. void configure(String content, int sizex, int sizey, int pos){
  51. this->sizex = sizex;
  52. this->sizey = sizey;
  53. this->content = content;
  54. this->pos = pos;
  55. this->disp = true;
  56. }
  57. void drawopt(int page, int pos){
  58. if(this->disp){
  59. if(this->pos == pos){
  60. display.fillRect(0, (this->sizey)*(this->pos) - (page*DISP_WIDTH), this->sizex, this->sizey, WHITE);
  61. display.setTextColor(SSD1306_BLACK);
  62. display.setCursor((this->sizex) + 3, (this->sizey) - 3);
  63. display.println(this->content);
  64. display.setTextColor(SSD1306_WHITE);
  65. }
  66. else{
  67. display.drawRect(0, (this->sizey)*(this->pos), this->sizex, this->sizey, WHITE);
  68. display.setCursor((this->sizex) + 3, (this->sizey) - 3 - (DISP_WIDTH*128));
  69. display.println(this->content);
  70. }
  71. }
  72. }
  73. void task(void (*f)(void)){
  74. this->f = f;
  75. }
  76. };
  77. class Menu{
  78. private:
  79. int id;
  80. int sizex;
  81. int sizey;
  82. int options = 0; //This indicates the number of options created
  83. int pos = 0; //This indicates the position of the cursor
  84. int page = 0; //If the menu is too long, this indicates the page that is being displayed
  85. Option opt[MAX_OPTIONS];
  86. public:
  87. void configure(int id, int sizex, int sizey){
  88. this->id = id;
  89. this->sizex = sizex;
  90. this->sizey = sizey;
  91. }
  92. void createOption(String content){
  93. this->opt[this->options].configure(content, this->sizex, this->sizey, this->options++);
  94. }
  95. void function(int optionindex, void (*f)(void)){ //Defines functions for each option
  96. this->opt[optionindex].task(f);
  97. }
  98. void drawMenu(){
  99. int dir;
  100. this->page = pos/3;
  101. for(int i = 0; i < options; i++){
  102. this->opt[i].drawopt(this->page, this->pos);
  103. }
  104. }
  105. int getid(){
  106. int id = this->id;
  107. return id;
  108. }
  109. int getoptions(){
  110. int options = this->options;
  111. return options;
  112. }
  113. void increasePos(){
  114. this->pos++;
  115. }
  116. void decreasePos(){
  117. this->pos--;
  118. }
  119. };
  120. class Screen{
  121. private:
  122. Menu menu[MAX_MENUS];
  123. int ids[MAX_MENUS];
  124. int counter = 0;
  125. int actualScreen;
  126. public:
  127. void createMenu(int id, int sizex, int sizey){
  128. this->menu[counter].configure(id, sizex, sizey);
  129. counter++;
  130. }
  131. void createOption(int idMenu, String content){
  132. int mem = 0;
  133. for(int i = 0; i < counter; i++){
  134. if(this->ids[i] == idMenu)
  135. mem = i;
  136. break;
  137. }
  138. this->menu[mem].createOption(content);
  139. }
  140. void control(){
  141. }
  142. };
  143. void setup(){
  144. }
  145. void loop(){
  146. }