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.

246 lines
9.6 KiB

  1. class Graph{ //ContentType (1)
  2. private:
  3. String title;
  4. char graphType; //'a' Vertical Bar, 'b' Horizontal Bar, 'c' Cartesian Graph
  5. //Assign whatever value in "configure(..." if a parameter is not required for the specified graphType
  6. double value; //For: Vertical Bar Horizontal Bar Cartesian
  7. double xpos; //For: Vertical Bar Horizontal Bar Cartesian
  8. double ypos; //For: Vertical Bar Horizontal Bar Cartesian
  9. double height; //For: Vertical Bar Horizontal Bar Cartesian
  10. double width; //For: Vertical Bar Horizontal Bar Cartesian
  11. double yminimum; //For: Vertical Bar Cartesian
  12. double ymaximum; //For: Vertical Bar Cartesian
  13. double xminimum; //For: Horizontal Bar Cartesian
  14. double xmaximum; //For: Horizontal Bar Cartesian
  15. double yStepSize; //For: Vertical Bar Cartesian
  16. double xStepSize; //For: Horizontal Bar Cartesian
  17. int digit; //For: Vertical Bar Horizontal Bar Cartesian
  18. int * maximum; //For: Cartesian
  19. double x;
  20. double yrange;
  21. double xrange;
  22. double ox;
  23. double oy;
  24. double count;
  25. double graphScale;
  26. bool redraw = true;
  27. int previousScreen = 0;
  28. int previousContentType = 0;
  29. public:
  30. //This method configures the graph created, defines its parameters according the type of graph selected.
  31. void configure(String title, char graphType, double xpos, double ypos, double width, double height,
  32. double yminimum, double ymaximum, double xminimum, double xmaximum, double yStepSize, double xStepSize, int digit, void *maximum){
  33. this->title = title;
  34. this->graphType = graphType;
  35. this->yminimum = yminimum;
  36. this->ymaximum = ymaximum;
  37. this->xminimum = xminimum;
  38. this->count = xminimum;
  39. this->xmaximum = xmaximum;
  40. this->height = height;
  41. this->width = width;
  42. this->yStepSize = yStepSize;
  43. this->xStepSize = xStepSize;
  44. this->digit = digit;
  45. this->xpos = xpos;
  46. this->ypos = ypos;
  47. this->maximum = (int *) maximum;
  48. switch(graphType){
  49. case 'a':
  50. this->yrange = ymaximum - yminimum;
  51. this->graphScale = (yStepSize) * (height / this->yrange) - .001; //Adjusts the scale of the graph, according to the range and the size of the step
  52. break;
  53. case 'b':
  54. this->xrange = xmaximum - xminimum;
  55. this->graphScale = (xStepSize) * (width / this->xrange) - .001; //Adjusts the scale of the graph, according to the range and the size of the step
  56. break;
  57. case 'c':
  58. this->yrange = ymaximum - yminimum;
  59. break;
  60. }
  61. }
  62. void drawGraph(){
  63. double level, data, i;
  64. switch(graphType){
  65. case 'a':
  66. double my;
  67. if (this->redraw) { //Prints the labels
  68. display.clearDisplay();
  69. this->redraw = false;
  70. display.fillRect(0, 0, 127 , 14, SSD1306_WHITE);
  71. display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
  72. display.setTextSize(1);
  73. display.setCursor(2, 4);
  74. display.println(this->title);
  75. for (i = 0; i <= this->height; i += this->graphScale) {
  76. my = this->ypos - this->height + i;
  77. display.drawFastHLine(this->xpos + this->width + 1, my, 5, SSD1306_WHITE);
  78. // draw lables
  79. display.setTextSize(1);
  80. display.setTextColor(SSD1306_WHITE, SSD1306_BLACK);
  81. display.setCursor(this->xpos + this->width + 12, my - 3 );
  82. data = this->ymaximum - ( i * (this->yStepSize / this->graphScale));
  83. display.print(data, this->digit);
  84. }
  85. }
  86. // compute level of bar graph that is scaled to the height and the hi and low vals
  87. // this is needed to accompdate for +/- range
  88. level = (this->height * (((this->value - this->yminimum) / (this->yrange))));
  89. // draw the bar graph
  90. // write a upper and lower bar to minimize flicker cause by blanking out bar and redraw on update
  91. display.drawRect(this->xpos, this->ypos - this->height, this->width, this->height, SSD1306_WHITE);
  92. display.fillRect(this->xpos, this->ypos - this->height, this->width, this->height - level, SSD1306_BLACK);
  93. display.drawRect(this->xpos, this->ypos - this->height, this->width, this->height, SSD1306_WHITE);
  94. display.fillRect(this->xpos, this->ypos - level, this->width, level, SSD1306_WHITE);
  95. // up until now print sends data to a video buffer NOT the screen
  96. // this call sends the data to the screen
  97. display.display();
  98. break;
  99. case 'b':
  100. if (this->redraw) {
  101. display.clearDisplay();
  102. this->redraw = false;
  103. display.fillRect(0, 0, 127 , 16, SSD1306_WHITE);
  104. display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
  105. display.setTextSize(1);
  106. display.setCursor(2, 4);
  107. display.println(this->title);
  108. // draw the text
  109. for (i = 0; i <= this->width; i += this->graphScale) {
  110. display.drawFastVLine(i + this->xpos , this->ypos , 5, SSD1306_WHITE);
  111. // draw lables
  112. display.setTextSize(1);
  113. display.setTextColor(SSD1306_WHITE, SSD1306_BLACK);
  114. display.setCursor(i + this->xpos , this->ypos + 10);
  115. // addling a small value to eliminate round off errors
  116. // this val may need to be adjusted
  117. data = ( i * (this->xStepSize / this->graphScale)) + this->xminimum + 0.00001;
  118. display.print(data, this->digit);
  119. }
  120. }
  121. // compute level of bar graph that is scaled to the width and the hi and low vals
  122. // this is needed to accompdate for +/- range capability
  123. // draw the bar graph
  124. // write a upper and lower bar to minimize flicker cause by blanking out bar and redraw on update
  125. level = (this->width * (((this->value - this->xminimum) / (this->xmaximum - this->xminimum))));
  126. display.fillRect(this->xpos + level, this->ypos - this->height, this->width - level, this->height, SSD1306_BLACK);
  127. display.drawRect(this->xpos, this->ypos - this->height, this->width, this->height, SSD1306_WHITE);
  128. display.fillRect(this->xpos, this->ypos - this->height, level, this->height, SSD1306_WHITE);
  129. // up until now print sends data to a video buffer NOT the screen
  130. // this call sends the data to the screen
  131. display.display();
  132. break;
  133. case 'c':
  134. double temp;
  135. if (this->redraw == true) {
  136. this->redraw = false;
  137. this->xrange = *this->maximum - xminimum;
  138. display.clearDisplay();
  139. display.fillRect(0, 0, 127 , 16, SSD1306_WHITE);
  140. display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
  141. display.setTextSize(1);
  142. display.setCursor(2, 4);
  143. display.println(title);
  144. this->ox = (this->count - this->xminimum) * (this->width) / (this->xrange) + this->xpos;
  145. this->oy = (this->value - this->yminimum) * (- this->height) / (this->yrange) + this->ypos;
  146. // draw y scale
  147. display.setTextSize(1);
  148. display.setTextColor(SSD1306_WHITE, SSD1306_BLACK);
  149. for ( i = this->yminimum; i <= this->ymaximum; i += this->yStepSize) {
  150. // compute the transform
  151. temp = (i - this->yminimum) * (- this->height) / (this->ymaximum - this->yminimum) + this->ypos;
  152. if (i == 0) {
  153. display.drawFastHLine(this->xpos - 3, temp, this->width + 3, SSD1306_WHITE);
  154. }
  155. else {
  156. display.drawFastHLine(this->xpos - 3, temp, 3, SSD1306_WHITE);
  157. }
  158. display.setCursor(this->xpos - 27, temp - 3);
  159. display.println(i, this->digit);
  160. }
  161. // draw x scale
  162. for (i = this->xminimum; i <= *this->maximum; i += this->xStepSize) {
  163. // compute the transform
  164. display.setTextSize(1);
  165. display.setTextColor(SSD1306_WHITE, SSD1306_BLACK);
  166. temp = (i - this->xminimum) * (this->width) / (this->xrange) + this->xpos;
  167. if (i == 0) {
  168. display.drawFastVLine(temp, this->ypos - this->height, this->height + 3, SSD1306_WHITE);
  169. }
  170. else {
  171. display.drawFastVLine(temp, this->ypos, 3, SSD1306_WHITE);
  172. }
  173. display.setCursor(temp, this->ypos + 6);
  174. display.println(i, this->digit);
  175. }
  176. }
  177. // graph drawn now plot the data
  178. // the entire plotting code are these few lines...
  179. this->x = (this->count - this->xminimum) * (this->width) / (this->xrange) + this->xpos;
  180. this->value = (this->value - this->yminimum) * (- this->height) / (this->yrange) + this->ypos;
  181. display.drawLine(this->ox, this->oy, this->x, this->value, SSD1306_WHITE);
  182. display.drawLine(this->ox, this->oy - 1, this->x, this->value - 1, SSD1306_WHITE);
  183. this->ox = this->x;
  184. this->oy = this->value;
  185. // up until now print sends data to a video buffer NOT the screen
  186. // this call sends the data to the screen
  187. display.display();
  188. this->count += 1;
  189. if(this->ox >= (this->xpos + this->width)){
  190. this->redraw = true;
  191. this->count = xminimum;
  192. }
  193. }
  194. }
  195. void redrawFlag(){ // Activates the redraw bool to get the graph printed correctly
  196. this->redraw = true;
  197. this->count = xminimum;
  198. }
  199. void setPreviousScreen(int prev){
  200. this->previousScreen = prev;
  201. }
  202. void setPreviousContentType(int prev){
  203. this->previousContentType = prev;
  204. }
  205. int getPreviousScreen(){
  206. int prev = this->previousScreen;
  207. return prev;
  208. }
  209. int getPreviousContentType(){
  210. int prev = this->previousContentType;
  211. return prev;
  212. }
  213. void assignValue(double value){
  214. this->value = value;
  215. }
  216. void reset(){
  217. this->x = 0;
  218. }
  219. };