diff --git a/DisplayESP32.ino b/DisplayESP32.ino index 94033c1..7785828 100644 --- a/DisplayESP32.ino +++ b/DisplayESP32.ino @@ -13,36 +13,7 @@ #define DISP_HEIGHT 64 // OLED display height Adafruit_SSD1306 display(DISP_WIDTH, DISP_HEIGHT, &Wire, -1); - - -void setDisp(bool fullsetting){ - if(fullsetting){ - //Adafruit_SSD1306 display(DISP_WIDTH, DISP_HEIGHT, &Wire, -1); - Serial.begin(115200); - if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { -#ifdef __DEBUG__ - Serial.println("Display not found!"); -#endif - while (true); - } - } - display.clearDisplay(); - - // Text size - display.setTextSize(2); - // Text color - display.setTextColor(SSD1306_WHITE); - // Text position - - display.setCursor(0, 20); - display.println("Bienvenido"); - - - display.setTextSize(1); - - display.display(); - delay(5000); -} +int i = 0; class Option{ private: @@ -85,14 +56,14 @@ class Option{ void drawopt(int page, int pos, int optPPage){ if(this->disp){ if(this->pos == pos){ - display.fillRect(0, (this->sizey)*(this->pos) + pos - (page*optPPage*this->sizey), this->sizex, this->sizey, WHITE); + display.fillRect(0, (this->sizey)*(this->pos) + 1 - (page*optPPage*this->sizey), this->sizex, this->sizey, WHITE); display.setTextColor(SSD1306_BLACK); display.setCursor(5, (this->sizey)*(this->pos + 1) - (page*optPPage*this->sizey) - this->textSpacing); display.print(this->content); display.setTextColor(SSD1306_WHITE); } else{ - display.drawRect(0, (this->sizey)*(this->pos) + pos - (page*optPPage*this->sizey), this->sizex, this->sizey, WHITE); + display.drawRect(0, (this->sizey)*(this->pos) + 1 - (page*optPPage*this->sizey), this->sizex, this->sizey, WHITE); display.setCursor(5, (this->sizey)*(this->pos + 1) - (page*optPPage*this->sizey) - this->textSpacing); display.print(this->content); } @@ -194,8 +165,9 @@ class Graph{ //ContentTypeMenu false, it is not a menu double x; double yrange; double xrange; - double xorigin; - double yorigin; + double ox; + double oy; + double count; double graphScale; bool redraw = true; @@ -211,6 +183,7 @@ class Graph{ //ContentTypeMenu false, it is not a menu this->yminimum = yminimum; this->ymaximum = ymaximum; this->xminimum = xminimum; + this->count = xminimum; this->xmaximum = xmaximum; this->height = height; this->width = width; @@ -278,7 +251,6 @@ class Graph{ //ContentTypeMenu false, it is not a menu break; case 'b': - double mx; if (this->redraw) { display.clearDisplay(); @@ -316,8 +288,7 @@ class Graph{ //ContentTypeMenu false, it is not a menu break; case 'c': - double temp, ox, oy; - int rot, newrot; + double temp; if (this->redraw == true) { this->redraw = false; @@ -327,15 +298,15 @@ class Graph{ //ContentTypeMenu false, it is not a menu display.setTextSize(1); display.setCursor(2, 4); display.println(title); - ox = (this->x - this->xminimum) * (this->width) / (this->xrange) + this->xpos; - oy = (this->value - this->yminimum) * (this->ypos - this->height - this->ypos) / (this->yrange) + this->ypos; + this->ox = (this->count - this->xminimum) * (this->width) / (this->xrange) + this->xpos; + this->oy = (this->value - this->yminimum) * (- this->height) / (this->yrange) + this->ypos; // draw y scale display.setTextSize(1); display.setTextColor(SSD1306_WHITE, SSD1306_BLACK); for ( i = this->yminimum; i <= this->ymaximum; i += this->yStepSize) { // compute the transform // note my transform funcition is the same as the map function, except the map uses long and we need doubles - temp = (i - this->yminimum) * (this->ypos - this->height - this->ypos) / (this->ymaximum - this->yminimum) + this->ypos; + temp = (i - this->yminimum) * (- this->height) / (this->ymaximum - this->yminimum) + this->ypos; if (i == 0) { display.drawFastHLine(this->xpos - 3, temp, this->width + 3, SSD1306_WHITE); } @@ -365,21 +336,21 @@ class Graph{ //ContentTypeMenu false, it is not a menu // graph drawn now plot the data // the entire plotting code are these few lines... - this->x = (this->x - this->xminimum) * (this->width) / (this->xrange) + this->xpos; - this->value = (this->value - this->yminimum) * (this->ypos - this->height - this->ypos) / (this->yrange) + this->ypos; - display.drawLine(ox, oy, this->x, this->value, SSD1306_WHITE); - display.drawLine(ox, oy - 1, this->x, this->value - 1, SSD1306_WHITE); - ox = this->x; - oy = this->value; - - if(ox == this->xpos + this->width) - this->redraw = true; - this->x = 0; + this->x = (this->count - this->xminimum) * (this->width) / (this->xrange) + this->xpos; + this->value = (this->value - this->yminimum) * (- this->height) / (this->yrange) + this->ypos; + display.drawLine(this->ox, this->oy, this->x, this->value, SSD1306_WHITE); + display.drawLine(this->ox, this->oy - 1, this->x, this->value - 1, SSD1306_WHITE); + this->ox = this->x; + this->oy = this->value; // up until now print sends data to a video buffer NOT the screen // this call sends the data to the screen display.display(); - this->x++; + this->count += 1; + if(this->ox >= (this->xpos + this->width)){ + this->redraw = true; + this->count = xminimum; + } } } @@ -408,6 +379,10 @@ class Graph{ //ContentTypeMenu false, it is not a menu void assignValue(double value){ this->value = value; } + + void reset(){ + this->x = 0; + } }; @@ -425,9 +400,38 @@ class Screen{ public: + void configure(bool fullsetting){ + if(fullsetting){ + //Adafruit_SSD1306 display(DISP_WIDTH, DISP_HEIGHT, &Wire, -1); + Serial.begin(115200); + if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { + #ifdef __DEBUG__ + Serial.println("Display not found!"); + #endif + while (true); + } + } + display.clearDisplay(); + + // Text size + display.setTextSize(2); + // Text color + display.setTextColor(SSD1306_WHITE); + // Text position + + display.setCursor(0, 20); + display.println("Bienvenido"); + + + display.setTextSize(1); + + display.display(); + delay(5000); + } + void createMenu(int sizex, int sizey){ this->menu[counterM].configure(sizex, sizey); - counterM++; + this->counterM++; } void createOption(int menuIndex, String content, bool destinationTypeMenu, int destinationIndex){ @@ -437,7 +441,7 @@ class Screen{ void createVGraph(String title, double xpos, double ypos, double width, double height, double yminimum, double ymaximum, double yStepSize, double digit){ this->graph[counterG].configure(title, 'a', xpos, ypos, width, height, yminimum, ymaximum, 0, 0, yStepSize, 0, digit); - counterG++; + this->counterG++; } void createHGraph(String title, double xpos, double ypos, double width, double height, @@ -451,10 +455,12 @@ class Screen{ this->graph[counterG].configure(title, 'c', xpos, ypos, width, height, yminimum, ymaximum, xminimum, xmaximum, yStepSize, xStepSize, digit); counterG++; } - + +/* void redrawFlag(){ this->redraw = true; } +*/ void graphAssignValue(int graphIndex, double value){ this->graph[graphIndex].assignValue(value); @@ -492,6 +498,8 @@ class Screen{ else{ this->graph[newScreen].setPreviousScreen(this->currentScreen); this->graph[newScreen].setPreviousContentTypeMenu(this->contentTypeMenu); + this->graph[newScreen].reset(); + this->graph[newScreen].redrawFlag(); } this->contentTypeMenu = newContentTypeMenu; this->currentScreen = newScreen; @@ -501,11 +509,12 @@ class Screen{ void goBack(){ if (contentTypeMenu){ + //Gets indexes from previous screen saved in actual screen if it is a menu, and sets them as the current indexes this->currentScreen = this->menu[this->currentScreen].getPreviousScreen(); this->contentTypeMenu = this->menu[this->currentScreen].getPreviousContentTypeMenu(); } else{ - this->graph[this->currentScreen].redrawFlag(); + //Gets indexes from previous screen saved in actual screen if it is a graph, and sets them as the current indexes this->currentScreen = this->graph[this->currentScreen].getPreviousScreen(); this->contentTypeMenu = this->graph[this->currentScreen].getPreviousContentTypeMenu(); } @@ -516,23 +525,47 @@ class Screen{ Screen screen; void setup(){ - setDisp(true); + screen.configure(true); - screen.createMenu(128, 13); + screen.createMenu(128, 13); //Menu 0 + screen.createMenu(128, 13); //Menu 1 /*String title, char graphType, double xpos, double ypos, double width, double height, double yminimum, double ymaximum, double xminimum, double xmaximum, double yStepSize, double xStepSize, double digit*/ - screen.createVGraph("Grafica 1", 25, 60, 40, 40, 0, 100, 10, 0); - screen.createHGraph("Grafica 2", 10, 60, 100, 20, 0, 100, 10, 0); - screen.createCGraph("Grafica 3", 30, 50, 75, 30, 0, 1024, 0, 10, 100, 10, 0); + screen.createVGraph("Grafica 1", 25, 60, 40, 40, 0, 100, 10, 0); //Graph 0 + screen.createHGraph("Grafica 2", 10, 40, 100, 20, 0, 100, 10, 0); //Graph 1 + screen.createCGraph("Grafica 3", 30, 50, 75, 30, 0, 100, 0, 100, 25, 25, 0); //Graph 2 - screen.createOption(0, "Grafica vertical", false, 0); - screen.createOption(0, "Grafica horizontal", false, 1); - screen.createOption(0, "Grafica cartesiana", false, 2); + screen.createOption(0, "Vertical graph", false, 0); + //Creates the first option in Menu 0, directing to a graph (Menu Content = false), 0 (Graph 0) + screen.createOption(0, "Horizontal graph", false, 1); + screen.createOption(0, "Cartesian graph", false, 2); + screen.createOption(0, "Extra option", true, 1); + + screen.createOption(1, "Test", false, 3); + screen.createOption(1, "Working?", false, 4); + +// screen.increasePos(); +// screen.increasePos(); +// screen.goTo(); +// screen.graphAssignValue(2, 50); +// screen.goBack(); +// screen.increasePos(); +// screen.goTo(); +// screen.goBack(); +// screen.decreasePos(); } void loop(){ - screen.control(); + screen.control(); //Controls the screen and redraws if needed delay(10); -} + + if(i <= 100){ + screen.graphAssignValue(1, i); //Assigning a demo value to Graph 1 + screen.graphAssignValue(2, i); //Assigning a demo value to Graph 2 + i++; + } + else + i = 0; +} \ No newline at end of file