Advertisement
TheArcticGentoo

Serial Grapher 0.8 (Serial NYI)

Sep 23rd, 2018
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.97 KB | None | 0 0
  1. //Copyright (C) Andrew Spangler 2018
  2. //Feel free to use this, just credit me.
  3. #define VERSION .8              //version .9 is serial input enabled, 1 is graph number, size, location instructed over serial, 2 is library
  4. #include <Adafruit_GFX.h>
  5. #include <Adafruit_SSD1306.h>
  6. #include <Wire.h>
  7.  
  8.  
  9. /*-----Serial Stuff-----*/
  10. String inputString = "";            // a String to hold incoming data
  11. bool stringComplete = false;        // whether the string is complete
  12. #define enableDebugSpew false        // Set to true to enable debug spew into the serial console
  13. String screenString = "";
  14.  
  15.  
  16. /*-----OLED Stuff-----*/
  17. #define OLED_RESET LED_BUILTIN            //My LCD doesn't support a reset pin (wish it did for esp8266, this code needs a full power cycle to work properly otherwise the screen gets stuck on reset)
  18. Adafruit_SSD1306 display(OLED_RESET);     //this makes the led flash when the display would normally reset, most I2C SSD1306's don't have a reset pin but it must be defined to set up the display.
  19.  
  20. #define TEXT_SMALL 1                      //Personal Ease-of-reading definitions
  21. #define TEXT_BIG   2
  22. #define TEXT_HUGE  3
  23.  
  24.  
  25. /*-----Graph Stuff-----*/
  26. #define graphBufferSize 22                //Set buffer size, SET TO (WIDTH OF YOUR WIDEST GRAPH + 1) OR YOU WILL EXCEED THE ARRAY.
  27. #define overflowProtect true              // ||NYI|| If set to true will prevent graph function from running if the passed graph width is grater than the buffer ||NYI||
  28.  
  29.  
  30. int graphBuffer[graphBufferSize];
  31. int graphBuffer2[graphBufferSize];
  32. int graphBuffer3[graphBufferSize];
  33. int graphBuffer4[graphBufferSize];
  34. int graphMax = 1023;                      //max value to pass to the graph, exceeding this value will display at the top of the graph
  35.  
  36. /*-------FPS/LPS Counter-------*/
  37. unsigned long timer;          /////DO NOT CALL VARIABLE "time" on ESP8266, CAUSES PROBLEMS W/ COMPILER, works on the trinket 3v. nano also.
  38. int loops[2];  //counts loops, 0 for current number of loops this second, 1 to store the last value to reprint on the I2C LCD
  39.  
  40.  
  41. void setup() {
  42.   //Serial.begin(9600);                        // Start Serial
  43.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
  44.   display.display();                          
  45.   delay(1000);
  46.   display.clearDisplay();                     //To reduce program size you can edit the header to remove the adafruit logo but then you may not redistribute it
  47.   display.setCursor(0,0);
  48.   display.setTextSize(1);
  49.   display.setTextColor(WHITE);
  50.   display.println("Lyfe's Serial Grapher");
  51.   display.setCursor(0,9);
  52.   display.println("Serial NYI");
  53.   display.setCursor(0,17);
  54.   String versionString = "Version: " + VERSION;
  55.   display.println(versionString);
  56.   display.display();
  57.   delay(5000);
  58. }
  59.  
  60. void loop() {
  61.  display.clearDisplay();                    
  62.  grapher(0, 0, 31, 32, random(1023));
  63.  grapher2(32, 0, 31, 32, random(1023));
  64.  grapher3(64, 0, 31, 32, random(1023));
  65.  grapher4(96, 0, 31, 32, random(1023));
  66.  fps();
  67.  display.display();
  68. }
  69.  
  70.  
  71. /*---Display Print Functions, structured for different data types, also prints t/f for booleans---*/
  72. //Pass String
  73. void displayPrint(String instring, int line, int textSize, bool clear) {
  74.  if (clear) {
  75.    display.clearDisplay();  //only clear if specified
  76.  }
  77.  display.setTextSize(textSize);                            //set text size
  78.  display.setTextColor(WHITE);                              //one-color displays are always "white" even if the oled is a different color
  79.  display.setCursor(0, line * 8 * textSize);                //sets text line, left aligned,
  80.  display.println(instring);                                //print data
  81. }
  82.  
  83. //Pass Int
  84. void displayPrint(int instring, int line, int textSize, bool clear) {
  85.  if (clear) {
  86.    display.clearDisplay();
  87.  }
  88.  display.setTextSize(textSize);
  89.  display.setTextColor(WHITE);
  90.  display.setCursor(0, line * 8 * textSize);
  91.  display.println(instring);
  92. }
  93.  
  94. //Pass Bool
  95. void displayPrint(bool instring, int line, int textSize, bool clear) {
  96.  if (clear) {
  97.    display.clearDisplay();
  98.  }
  99.  display.setTextSize(textSize);
  100.  display.setTextColor(WHITE);
  101.  display.setCursor(0, line * 8 * textSize);
  102.  if ( instring ) {
  103.    display.println("True");
  104.  } else {
  105.    display.println("False");
  106.  }
  107. }
  108.  
  109. //Pass Float
  110. void displayPrint(float instring, int line, int textSize, bool clear) {
  111.  if (clear) {
  112.    display.clearDisplay();
  113.  }
  114.  display.setTextSize(textSize);
  115.  display.setTextColor(WHITE);
  116.  display.setCursor(0, line * 8 * textSize);   //character line is 7 pixels high
  117.  display.println(instring);
  118. }
  119.  
  120.  
  121.  
  122.  
  123. void grapher(int gx, int gy, int gw, int gh, int inVal) {               //Pass the leftmost pixel (gx), topmost pixel (gy), graph width (gw), graph height (gh), and new value to add (inVal)
  124.  graphBuffer[0] = ((inVal * gh) / graphMax);                           //Set the first value of the graphBuffer array to the input value scaled to the graph height and max input value
  125.  display.drawRect(gx, gy, gw, gh, WHITE);                              //Draw a rectangle aroung the graph
  126.  for (int i = gw - 2; i >= 0; i--) {                                   //Draw the graph, shifting the array down one notch so the last value is lost and the first value is duplicated between the first and second location in the array, making room for new data on next call and shifting it down
  127.    display.drawLine((gx + i + 1), (gy + (gh) - graphBuffer[i + 1] + 1), (gx + i), (gy + (gh) - graphBuffer[i] + 1), WHITE);
  128.    graphBuffer[i + 1] = graphBuffer[i];
  129.  }
  130. }
  131.  
  132.  
  133. void grapher2(int gx, int gy, int gw, int gh, int inVal) {
  134.  graphBuffer2[0] = ((inVal * gh) / graphMax);
  135.  display.drawRect(gx, gy, gw, gh, WHITE);
  136.  for (int i = gw - 2; i >= 0; i--) {
  137.    display.drawLine((gx + i + 1), (gy + (gh) - graphBuffer2[i + 1] + 1), (gx + i), (gy + (gh) - graphBuffer2[i] + 1), WHITE);
  138.    graphBuffer2[i + 1] = graphBuffer2[i];
  139.  }
  140. }
  141.  
  142.  
  143. void grapher3(int gx, int gy, int gw, int gh, int inVal) {
  144.  graphBuffer3[0] = ((inVal * gh) / graphMax);
  145.  display.drawRect(gx, gy, gw, gh, WHITE);
  146.  for (int i = gw - 2; i >= 0; i--) {
  147.    display.drawLine((gx + i + 1), (gy + (gh) - graphBuffer3[i + 1] + 1), (gx + i), (gy + (gh) - graphBuffer3[i] + 1), WHITE);
  148.    graphBuffer3[i + 1] = graphBuffer3[i];
  149.  }
  150. }
  151.  
  152.  
  153. void grapher4(int gx, int gy, int gw, int gh, int inVal) {
  154.  graphBuffer4[0] = ((inVal * gh) / graphMax);
  155.  display.drawRect(gx, gy, gw, gh, WHITE);
  156.  for (int i = gw - 2; i >= 0; i--) {
  157.    display.drawLine((gx + i + 1), (gy + (gh) - graphBuffer4[i + 1] + 1), (gx + i), (gy + (gh) - graphBuffer4[i] + 1), WHITE);
  158.    graphBuffer4[i + 1] = graphBuffer4[i];
  159.  }
  160. }
  161.  
  162.  
  163. //FASTER GRAPH, ORIGINAL, NO LINE DRAW, GOOD FOR SLOW-CHANGING DATA
  164. void fastGrapher(int gx, int gy, int gw, int gh, int inVal) {
  165.  graphBuffer[0] = ((inVal * gh) / graphMax);
  166.  display.drawRect(gx, gy, gw, gh, WHITE);
  167.  for (int i = gw - 2; i >= 0; i--) {
  168.    display.drawPixel((gx + i + 1), (gy + (gh) - graphBuffer[i + 1] + 1), WHITE);
  169.    graphBuffer[i + 1] = graphBuffer[i];
  170.  }
  171. }
  172.  
  173.  
  174. //FPS Counter, basically updates once a second and displays how many times the program has looped since then
  175. void fps() {
  176.  if (timer + 1000 <= (millis() )) {
  177.    loops[1] = loops[0];
  178.    timer = millis();
  179.    loops[0] = 0;
  180.  } else {
  181.    loops[0]++;
  182.  }
  183.  displayPrint(loops[1], 0, TEXT_SMALL, false);
  184. }
  185.  
  186.  
  187. /*
  188. * Comment out to prevent running on every loop if not needed
  189. void serialEvent() {
  190.  while (Serial.available()) {
  191.    // get the new byte:
  192.    char inChar = (char)Serial.read();
  193.    if (inChar == '\n') {
  194.      stringComplete = true;
  195.      if (enableDebugSpew) {
  196.        Serial.println("\n\n_______________________________");
  197.        Serial.print("Received via Serial: ");
  198.        Serial.println(inputString);
  199.      }
  200.    } else {
  201.      inputString += inChar;
  202.    }
  203.  }
  204. }
  205. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement