Advertisement
Guest User

E-paper UI

a guest
Feb 17th, 2023
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 7.38 KB | Source Code | 0 0
  1. #define ENABLE_GxEPD2_GFX 1
  2.  
  3. #include <GxEPD2_3C.h>
  4. #include <U8g2_for_Adafruit_GFX.h>
  5.  
  6. /*  ---E-PAPER CONNECTIONS FOR ESP32---
  7.  * BUSY - 4
  8.  * RST  - 15
  9.  * DC   - 27
  10.  * CS   - 5
  11.  * CLK  - 18
  12.  * DIN  - 23
  13.  * GND  - GND
  14.  * VCC  - 3.3V
  15.  */
  16.  
  17. #define GxEPD2_DRIVER_CLASS GxEPD2_420c
  18. #define colorBlack GxEPD_BLACK
  19. #define colorWhite GxEPD_WHITE
  20. #define colorRed GxEPD_RED
  21.  
  22. #define fontBig u8g2_font_inr33_mr            //33px
  23. #define fontMid u8g2_font_lubR24_te           //25px
  24. #define fontSml u8g2_font_fur20_tr            //20px
  25. #define fontSpecial u8g2_font_t0_22_mf        //13px
  26. #define fontxSml u8g2_font_helvB08_tf
  27.  
  28. #define batteryFont u8g2_font_battery19_tn
  29.  
  30. GxEPD2_3C<GxEPD2_420c, GxEPD2_420c::HEIGHT> display(GxEPD2_420c(/*CS=5*/ SS, /*DC=*/27, /*RST=*/15, /*BUSY=*/4));
  31. U8G2_FOR_ADAFRUIT_GFX u8g2Fonts;
  32.  
  33. const int graphSize = 30;
  34. const int scaleMargin = 0;
  35.  
  36. const int numYMarkers = 4;
  37. const int numLines = 8;
  38.  
  39. float gDataT[graphSize];
  40. float gDataH[graphSize];
  41. float gDataP[graphSize];
  42.  
  43. void setup() {
  44.   Serial.begin(115200);
  45.   display.init(0, true, 2, false);
  46.   u8g2Fonts.begin(display);
  47.  
  48.   display.setRotation(2);
  49.   u8g2Fonts.setForegroundColor(colorBlack); // apply Adafruit GFX color
  50.   u8g2Fonts.setBackgroundColor(colorWhite);
  51.  
  52.   //random graph data for now
  53.   randomSeed(analogRead(A0));
  54. }
  55.  
  56. void loop() {
  57.   drawUI();
  58.   delay(300000);
  59. }
  60.  
  61. void drawUI(){
  62.   int h = 0;
  63.   int l = 100000;
  64.  
  65.   //fill arrays with random data for testing
  66.   for(int i = 0; i < graphSize; i++){
  67.     gDataT[i] = random(25);
  68.     gDataH[i] = random(100);
  69.     gDataP[i] = random(1000, 1023);
  70.  
  71.     if(gDataT[i] < l) { l = gDataT[i]; }
  72.     if(gDataT[i] > h) { h = gDataT[i]; }  
  73.   }
  74.  
  75.   display.fillScreen(GxEPD_WHITE);
  76.  
  77.   //draw graph section
  78.   drawGraph(260, 20, 120, 75, true, 0, 100, gDataT, "Temperature");   //xPos, yPos, width, height, enable autoscaling, yMin, yMax, data array, title
  79.   drawGraph(260, 110, 120, 75, false, 0.0, 100.0, gDataH, "Humidity");
  80.   drawGraph(260, 200, 120, 75, true, 0, 100, gDataP, "Air pressure");
  81.  
  82.   drawBattery(5, 2, 2.4, true, false);
  83.  
  84.   //draw temp section
  85.   drawMainDisplay(5, 20, 250, 165, 2, "22.3c", "24.2", "19.8", "TEMP");
  86.  
  87.   //draw humidity/pressure section
  88.   drawSecDisplay(5, 190, 122, 105, 2, "50.4", "46", "55", "RH");
  89.   drawSecDisplay(132, 190, 122, 105, 2, "1023", "", "", "hPa");
  90.  
  91.   display.display();
  92.   display.powerOff();
  93. }
  94. // UI STUFF //
  95. //xPos    = xPosition of upper left corner of the box
  96. //yPos    = yPosition of upper left corner of the box
  97. //w       = width of box
  98. //h       = height of box, going downward!
  99. //border  = thickness of box border, in pixels
  100. //mainNum = String that gets displayed at the top
  101. //subNum = Strings that get displayed under mainNum
  102. //title  = text to display in the left upper corner
  103.  
  104. void drawSecDisplay(int xPos, int yPos, int w, int h, int border, String mainNum, String subNumOne, String subNumTwo, String title){
  105.  
  106.   drawBox(xPos, yPos, w, h, border);
  107.  
  108.   u8g2Fonts.setFont(fontMid);
  109.   //u8g2Fonts.setCursor(xPos, yPos);
  110.  
  111.   u8g2Fonts.setCursor((w/2) - (u8g2Fonts.getUTF8Width(mainNum.c_str())/2) + xPos, yPos+50);
  112.   u8g2Fonts.print(mainNum);
  113.  
  114.   u8g2Fonts.setFont(fontSpecial);
  115.   u8g2Fonts.setCursor(xPos+5, yPos+15);
  116.   u8g2Fonts.print(title);
  117.  
  118.   u8g2Fonts.setFont(fontSml);
  119.  
  120.   String subNum = subNumOne + "|" + subNumTwo;
  121.   u8g2Fonts.setCursor((w/2) - (u8g2Fonts.getUTF8Width(subNum.c_str())/2) + xPos, yPos+80);   //25, 270
  122.   u8g2Fonts.print(subNum);
  123. }
  124.  
  125. void drawMainDisplay(int xPos, int yPos, int w, int h, int border, String mainNum, String subNumOne, String subNumTwo, String title){
  126.  
  127.   drawBox(xPos, yPos, w, h, border);
  128.  
  129.   u8g2Fonts.setFont(fontBig);
  130.   u8g2Fonts.setCursor((w/2) - (u8g2Fonts.getUTF8Width(mainNum.c_str())/2) + xPos, yPos+70);
  131.   u8g2Fonts.print(mainNum);
  132.  
  133.   u8g2Fonts.setFont(fontMid);
  134.   String subNum = subNumOne + "|" + subNumTwo;
  135.   u8g2Fonts.setCursor((w/2) - (u8g2Fonts.getUTF8Width(subNum.c_str())/2) + xPos, yPos+105);
  136.   u8g2Fonts.print(subNum);
  137.  
  138.   u8g2Fonts.setFont(fontSpecial);
  139.   u8g2Fonts.setCursor(xPos+5, yPos+15);
  140.   u8g2Fonts.print("TEMP");
  141.  
  142. }
  143.  
  144. void drawGraph(int xPos, int yPos, int width, int height, bool autoScale, float yMinStatic, float yMaxStatic, float data[graphSize], String title) {
  145.   u8g2Fonts.setFont(fontxSml);
  146.  
  147.   u8g2Fonts.setCursor(xPos, yPos);
  148.   u8g2Fonts.print(title);
  149.  
  150.   float yCoords[graphSize];
  151.   float yMin = 10000;
  152.   float yMax = -10000;
  153.  
  154.   //find min/max if autoscaling is enabled
  155.   if(autoScale){
  156.     for(int i = 0; i < graphSize; i++){
  157.       if(data[i] > yMax) { yMax = data[i]; }
  158.       if(data[i] < yMin) { yMin = data[i]; }
  159.     }
  160.   }
  161.   else{
  162.     yMin = yMinStatic;
  163.     yMax = yMaxStatic;
  164.   }
  165.   //Serial.println("H: " + String(yMax) + " L: " + String(yMin));
  166.   //draw graph outline
  167.   display.drawRect(xPos, yPos, width, height, GxEPD_BLACK);
  168.  
  169.  
  170.   //draw graph data
  171.   for (int i = 0; i < graphSize; i++) { yCoords[i] = mapf(data[i], yMin, yMax, yPos+height, yPos); }
  172.  
  173.  
  174.   for(int gx = 0; gx < graphSize-1; gx++){
  175.     int xOne = 0;
  176.     int xTwo = 0;
  177.  
  178.     //xOne = map(gx, 0, graphSize, xPos, xPos+width);
  179.     xOne = xPos + (width / graphSize) * gx;
  180.     xTwo = xOne + (width / graphSize);
  181.  
  182.     display.drawLine(xOne, yCoords[gx], xTwo, yCoords[gx+1], GxEPD_RED);
  183.   }
  184.  
  185.   //draw y markers
  186.  
  187.   for(int i = 0; i < numYMarkers+1; i++){
  188.     if(i > 0 && i < numYMarkers) {
  189.       int markYPosLine = (height / numYMarkers * i) + yPos;
  190.       for(int j = 0; j < numLines; j++){
  191.         int lineX = width / numLines * j;
  192.         display.drawFastHLine(lineX+(5/2)+xPos, markYPosLine, 10, GxEPD_BLACK);
  193.       }
  194.     }
  195.  
  196.     int markYPosText = (height / numYMarkers * i) + yPos;
  197.     u8g2Fonts.setCursor(xPos+width+1, markYPosText+4);
  198.     //Serial.println(yMax - (float)(yMax - yMin) / numYMarkers * i);
  199.     if(yMax < yMin+10) { u8g2Fonts.print(String(yMax - (float)(yMax - yMin) / numYMarkers * i + 0.01, 1));}  //show decimals if range is smaller than 10
  200.     else { u8g2Fonts.print(String(yMax - (float)(yMax - yMin) / numYMarkers * i, 0)); }
  201.   }
  202.   //display.display();
  203.   //display.powerOff();
  204.   //Serial.println("-----------------------");
  205. }
  206.  
  207. void drawBattery(int xPos, int yPos, float level, bool showLvl, bool useFont){
  208.  
  209.   if(useFont){
  210.       u8g2Fonts.setFont(batteryFont);
  211.       u8g2Fonts.setCursor(xPos, yPos-5);
  212.       if(level > 3.5){ u8g2Fonts.print(7); }
  213.       else if(level > 2.8 && level < 3.5) { u8g2Fonts.print(6); }
  214.       else if(level > 2.1 && level < 2.8) { u8g2Fonts.print(5); }
  215.       else if(level > 1.4 && level < 2.1) { u8g2Fonts.print(4); }
  216.       else if(level < 0.7) { u8g2Fonts.print(3); }
  217.       else { u8g2Fonts.print(0); }
  218.   }
  219.   else{
  220.     int fill = mapf(level, 0, 4.2, 0, 34);
  221.     display.drawRect(xPos, yPos, 35, 15, colorBlack);
  222.     display.drawRect(xPos+34, yPos+3, 6, 10, colorBlack);
  223.     display.fillRect(xPos+1, yPos, fill, 14, colorBlack);
  224.   }
  225.  
  226.   if(showLvl) {
  227.     display.setCursor(xPos+50, yPos+5);
  228.     display.print(String(level) + "v");
  229.   }
  230. }
  231.  
  232. //erases whatever is inside the box area!
  233. void drawBox(int xPos, int yPos, int w, int h, int borderSize){
  234.   display.fillRect(xPos, yPos, w, h, colorBlack);
  235.   display.fillRect(xPos+borderSize, yPos+borderSize, w-(borderSize*2), h-(borderSize*2), colorWhite);
  236. }
  237.  
  238. float mapf(float x, float in_min, float in_max, float out_min, float out_max){
  239.   return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement