Browse Source

New explanatory commentaries added.

master
parent
commit
0b4359033a
1 changed files with 42 additions and 39 deletions
  1. +42
    -39
      DisplayESP32.ino

+ 42
- 39
DisplayESP32.ino View File

@ -13,13 +13,13 @@
#define MAX_OPTIONS 10 //Maximum number of options for each menu #define MAX_OPTIONS 10 //Maximum number of options for each menu
#define MAX_MENUS 3 #define MAX_MENUS 3
#define MAX_GRAPH 3
#define MAX_GRAPHS 3
#define DISP_WIDTH 128 // OLED display width #define DISP_WIDTH 128 // OLED display width
#define DISP_HEIGHT 64 // OLED display height #define DISP_HEIGHT 64 // OLED display height
#define REFRESH 10 //Refresh time in ms #define REFRESH 10 //Refresh time in ms
#define ADDRESS 0x3C //I2C address of the display
Adafruit_SSD1306 display(DISP_WIDTH, DISP_HEIGHT, &Wire, -1); Adafruit_SSD1306 display(DISP_WIDTH, DISP_HEIGHT, &Wire, -1);
int i = 0;
class Option{ class Option{
private: private:
@ -80,7 +80,7 @@ class Option{
} }
}; };
class Menu{ //ContentTypeMenu true, it is a menu
class Menu{ //ContentType (1)
private: private:
int sizex; //X size for each option in the menu int sizex; //X size for each option in the menu
@ -118,31 +118,34 @@ class Menu{ //ContentTypeMenu true, it is a menu
return destinationIndex; return destinationIndex;
} }
//The following method draws the whole menu by drawing every option configured within it
void drawMenu(){ void drawMenu(){
display.clearDisplay(); display.clearDisplay();
this->page = pos/this->optPPage;
this->page = pos/this->optPPage; //The current page is obtained by dividing the position by the number of options per page (only integer)
for(int i = 0; i < options; i++){ for(int i = 0; i < options; i++){
this->opt[i].drawopt(this->page, this->pos, this->optPPage); this->opt[i].drawopt(this->page, this->pos, this->optPPage);
} }
display.display(); display.display();
} }
int extractPos(){
//Methods used by Screen
int extractPos(){ //Gets the current position of the cursor
return(this->pos); return(this->pos);
} }
int extractOptNumber(){
int extractOptNumber(){ //Gets the number of options in the menu
return(this->options); return(this->options);
} }
void increasePos(){
void increasePos(){ //Increases the position of the cursor
this->pos++; this->pos++;
} }
void decreasePos(){
void decreasePos(){ //Decreases the position of the cursor
this->pos--; this->pos--;
} }
//Both of the following methods store the values of the previous screen passed as parameters by Screen
void setPreviousScreen(int prev){ void setPreviousScreen(int prev){
this->previousScreen = prev; this->previousScreen = prev;
} }
@ -151,6 +154,7 @@ class Menu{ //ContentTypeMenu true, it is a menu
this->previousContentType = prev; this->previousContentType = prev;
} }
//Both of the following methods retrieve the values of the screen previous to the menu containing these data.
int getPreviousScreen(){ int getPreviousScreen(){
int prev = this->previousScreen; int prev = this->previousScreen;
return prev; return prev;
@ -162,7 +166,7 @@ class Menu{ //ContentTypeMenu true, it is a menu
} }
}; };
class Graph{ //ContentTypeMenu false, it is not a menu
class Graph{
private: private:
String title; String title;
@ -181,7 +185,7 @@ class Graph{ //ContentTypeMenu false, it is not a menu
double xmaximum; //For: Horizontal Bar Cartesian double xmaximum; //For: Horizontal Bar Cartesian
double yStepSize; //For: Vertical Bar Cartesian double yStepSize; //For: Vertical Bar Cartesian
double xStepSize; //For: Horizontal Bar Cartesian double xStepSize; //For: Horizontal Bar Cartesian
double digit; //For: Vertical Bar Horizontal Bar Cartesian
int digit; //For: Vertical Bar Horizontal Bar Cartesian
double x; double x;
double yrange; double yrange;
double xrange; double xrange;
@ -196,8 +200,9 @@ class Graph{ //ContentTypeMenu false, it is not a menu
public: public:
//This method configures the graph created, defines its parameters according the type of graph selected.
void configure(String title, char graphType, double xpos, double ypos, double width, double height, void configure(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){
double yminimum, double ymaximum, double xminimum, double xmaximum, double yStepSize, double xStepSize, int digit){
this->title = title; this->title = title;
this->graphType = graphType; this->graphType = graphType;
this->yminimum = yminimum; this->yminimum = yminimum;
@ -236,7 +241,7 @@ class Graph{ //ContentTypeMenu false, it is not a menu
case 'a': case 'a':
double my; double my;
if (this->redraw) {
if (this->redraw) { //Prints the labels
display.clearDisplay(); display.clearDisplay();
this->redraw = false; this->redraw = false;
display.fillRect(0, 0, 127 , 14, SSD1306_WHITE); display.fillRect(0, 0, 127 , 14, SSD1306_WHITE);
@ -374,7 +379,7 @@ class Graph{ //ContentTypeMenu false, it is not a menu
} }
} }
void redrawFlag(){
void redrawFlag(){ // Activates the redraw bool to get the graph printed correctly
this->redraw = true; this->redraw = true;
} }
@ -410,21 +415,22 @@ class Graph{ //ContentTypeMenu false, it is not a menu
class Screen{ class Screen{
private: private:
Menu menu[MAX_MENUS];
Graph graph[MAX_GRAPH];
int counterM = 0;
int counterG = 0;
bool redraw = true;
Menu menu[MAX_MENUS]; //Array of menus to use
Graph graph[MAX_GRAPHS]; //Array of graphs to use
int counterM = 0; //Number of menus created
int counterG = 0; //Number of graphs created
bool redraw = true; //Redraw interface for when there is a change of screen
int currentScreen = 0; int currentScreen = 0;
int contentType = 0; int contentType = 0;
public: public:
void configure(bool fullsetting){
void configure(bool fullsetting){ //This method allows the configuration of the display when the parameter is true. Otherwise only prints a greeting message.
if(fullsetting){ if(fullsetting){
//Adafruit_SSD1306 display(DISP_WIDTH, DISP_HEIGHT, &Wire, -1); //Adafruit_SSD1306 display(DISP_WIDTH, DISP_HEIGHT, &Wire, -1);
Serial.begin(115200); Serial.begin(115200);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
if (!display.begin(SSD1306_SWITCHCAPVCC, ADDRESS)) {
#ifdef __DEBUG__ #ifdef __DEBUG__
Serial.println("Display not found!"); Serial.println("Display not found!");
#endif #endif
@ -449,29 +455,29 @@ class Screen{
delay(5000); delay(5000);
} }
void createMenu(int sizex, int sizey){
void createMenu(int sizex, int sizey){ //This method is used for the creation of a menu
this->menu[counterM].configure(sizex, sizey); this->menu[counterM].configure(sizex, sizey);
this->counterM++; this->counterM++;
} }
void createOption(int menuIndex, String content, bool destinationTypeMenu, int destinationIndex){
void createOption(int menuIndex, String content, bool destinationTypeMenu, int destinationIndex){ //this method should be used for creating an option in a menu
this->menu[menuIndex].createOption(content, destinationTypeMenu, destinationIndex); this->menu[menuIndex].createOption(content, destinationTypeMenu, destinationIndex);
} }
void createVGraph(String title, double xpos, double ypos, double width, double height, void createVGraph(String title, double xpos, double ypos, double width, double height,
double yminimum, double ymaximum, double yStepSize, double digit){
double yminimum, double ymaximum, double yStepSize, int digit){ //this method calls the configure() of graph for a vertical graph
this->graph[counterG].configure(title, 'a', xpos, ypos, width, height, yminimum, ymaximum, 0, 0, yStepSize, 0, digit); this->graph[counterG].configure(title, 'a', xpos, ypos, width, height, yminimum, ymaximum, 0, 0, yStepSize, 0, digit);
this->counterG++; this->counterG++;
} }
void createHGraph(String title, double xpos, double ypos, double width, double height, void createHGraph(String title, double xpos, double ypos, double width, double height,
double xminimum, double xmaximum, double xStepSize, double digit){
double xminimum, double xmaximum, double xStepSize, int digit){ //this method calls the configure() of graph for a horizontal graph
this->graph[counterG].configure(title, 'b', xpos, ypos, width, height, 0, 0, xminimum, xmaximum, 0, xStepSize, digit); this->graph[counterG].configure(title, 'b', xpos, ypos, width, height, 0, 0, xminimum, xmaximum, 0, xStepSize, digit);
counterG++; counterG++;
} }
void createCGraph(String title, double xpos, double ypos, double width, double height, void createCGraph(String title, double xpos, double ypos, double width, double height,
double yminimum, double ymaximum, double xminimum, double xmaximum, double yStepSize, double xStepSize, double digit){
double yminimum, double ymaximum, double xminimum, double xmaximum, double yStepSize, double xStepSize, int digit){ //this method calls the configure() of graph for a cartesian chart
this->graph[counterG].configure(title, 'c', xpos, ypos, width, height, yminimum, ymaximum, xminimum, xmaximum, yStepSize, xStepSize, digit); this->graph[counterG].configure(title, 'c', xpos, ypos, width, height, yminimum, ymaximum, xminimum, xmaximum, yStepSize, xStepSize, digit);
counterG++; counterG++;
} }
@ -482,11 +488,13 @@ class Screen{
} }
*/ */
//The following method is used for assingning a value to a graph
void graphAssignValue(int graphIndex, double value){ void graphAssignValue(int graphIndex, double value){
this->graph[graphIndex].assignValue(value); this->graph[graphIndex].assignValue(value);
this->redraw = true; this->redraw = true;
} }
//This method controls the whole interface, it needs to be called within a loop
void control(){ void control(){
if (redraw){ if (redraw){
if (contentType == 0){ if (contentType == 0){
@ -499,6 +507,7 @@ class Screen{
} }
} }
//The following two methods allow the change in position of the cursor
void increasePos(){ void increasePos(){
if(this->menu[this->currentScreen].extractPos() < this->menu[this->currentScreen].extractOptNumber() - 1) if(this->menu[this->currentScreen].extractPos() < this->menu[this->currentScreen].extractOptNumber() - 1)
this->menu[this->currentScreen].increasePos(); this->menu[this->currentScreen].increasePos();
@ -509,6 +518,7 @@ class Screen{
this->menu[this->currentScreen].decreasePos(); this->menu[this->currentScreen].decreasePos();
} }
//This method lets the user go into another screen by selecting an option
void goTo(){ void goTo(){
if(this->contentType == 0){ if(this->contentType == 0){
int newScreen = this->menu[this->currentScreen].extractDestinationIndex(); int newScreen = this->menu[this->currentScreen].extractDestinationIndex();
@ -545,6 +555,7 @@ class Screen{
} }
} }
//These methods control the plus and minus button actions
void plusAction(){ void plusAction(){
if(contentType == 0){ if(contentType == 0){
increasePos(); increasePos();
@ -572,6 +583,7 @@ class Keyboard{
public: public:
//Keyboard constructor
Keyboard(byte goTo, byte goBack, byte plus, byte minus, byte debounceTime, Screen * screen){ Keyboard(byte goTo, byte goBack, byte plus, byte minus, byte debounceTime, Screen * screen){
this->goTo = goTo; this->goTo = goTo;
this->goBack = goBack; this->goBack = goBack;
@ -586,7 +598,7 @@ class Keyboard{
pinMode(plus, INPUT_PULLUP); pinMode(plus, INPUT_PULLUP);
pinMode(minus, INPUT_PULLUP); pinMode(minus, INPUT_PULLUP);
} }
//Debouncing functions
void checkGoTo(){ void checkGoTo(){
static char cont; static char cont;
if(digitalRead(this->goTo) == LOW) if(digitalRead(this->goTo) == LOW)
@ -594,10 +606,7 @@ class Keyboard{
else else
cont = 0; cont = 0;
if(cont == debounceTime/REFRESH){ if(cont == debounceTime/REFRESH){
cont = 0;
this->screen->goTo(); this->screen->goTo();
while(digitalRead(this->goTo) == LOW){
}
} }
} }
@ -609,10 +618,7 @@ class Keyboard{
else else
cont = 0; cont = 0;
if(cont == debounceTime/REFRESH){ if(cont == debounceTime/REFRESH){
cont = 0;
this->screen->goBack(); this->screen->goBack();
while(digitalRead(this->goBack) == LOW){
}
} }
} }
@ -623,10 +629,7 @@ class Keyboard{
else else
cont = 0; cont = 0;
if(cont == debounceTime/REFRESH){ if(cont == debounceTime/REFRESH){
cont = 0;
this->screen->plusAction(); this->screen->plusAction();
while(digitalRead(this->plus) == LOW){
}
} }
} }
@ -637,13 +640,11 @@ class Keyboard{
else else
cont = 0; cont = 0;
if(cont == debounceTime/REFRESH){ if(cont == debounceTime/REFRESH){
cont = 0;
this->screen->minusAction(); this->screen->minusAction();
while(digitalRead(this->minus) == LOW){
}
} }
} }
// All buttons are checked with this method
void control(){ void control(){
this->checkGoTo(); this->checkGoTo();
this->checkGoBack(); this->checkGoBack();
@ -652,6 +653,8 @@ class Keyboard{
} }
}; };
int i = 0;
Screen screen; Screen screen;
Keyboard keyboard(13, 12, 14, 27, 30, &screen); Keyboard keyboard(13, 12, 14, 27, 30, &screen);


Loading…
Cancel
Save