Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Copyright (C) Andrew Spangler 2018
- //Feel free to use this, just credit me.
- #define VERSION .8 //version .9 is serial input enabled, 1 is graph number, size, location instructed over serial, 2 is library
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #include <Wire.h>
- /*-----Serial Stuff-----*/
- String inputString = ""; // a String to hold incoming data
- bool stringComplete = false; // whether the string is complete
- #define enableDebugSpew false // Set to true to enable debug spew into the serial console
- String screenString = "";
- /*-----OLED Stuff-----*/
- #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)
- 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.
- #define TEXT_SMALL 1 //Personal Ease-of-reading definitions
- #define TEXT_BIG 2
- #define TEXT_HUGE 3
- /*-----Graph Stuff-----*/
- #define graphBufferSize 22 //Set buffer size, SET TO (WIDTH OF YOUR WIDEST GRAPH + 1) OR YOU WILL EXCEED THE ARRAY.
- #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||
- int graphBuffer[graphBufferSize];
- int graphBuffer2[graphBufferSize];
- int graphBuffer3[graphBufferSize];
- int graphBuffer4[graphBufferSize];
- int graphMax = 1023; //max value to pass to the graph, exceeding this value will display at the top of the graph
- /*-------FPS/LPS Counter-------*/
- unsigned long timer; /////DO NOT CALL VARIABLE "time" on ESP8266, CAUSES PROBLEMS W/ COMPILER, works on the trinket 3v. nano also.
- 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
- void setup() {
- //Serial.begin(9600); // Start Serial
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32)
- display.display();
- delay(1000);
- display.clearDisplay(); //To reduce program size you can edit the header to remove the adafruit logo but then you may not redistribute it
- display.setCursor(0,0);
- display.setTextSize(1);
- display.setTextColor(WHITE);
- display.println("Lyfe's Serial Grapher");
- display.setCursor(0,9);
- display.println("Serial NYI");
- display.setCursor(0,17);
- String versionString = "Version: " + VERSION;
- display.println(versionString);
- display.display();
- delay(5000);
- }
- void loop() {
- display.clearDisplay();
- grapher(0, 0, 31, 32, random(1023));
- grapher2(32, 0, 31, 32, random(1023));
- grapher3(64, 0, 31, 32, random(1023));
- grapher4(96, 0, 31, 32, random(1023));
- fps();
- display.display();
- }
- /*---Display Print Functions, structured for different data types, also prints t/f for booleans---*/
- //Pass String
- void displayPrint(String instring, int line, int textSize, bool clear) {
- if (clear) {
- display.clearDisplay(); //only clear if specified
- }
- display.setTextSize(textSize); //set text size
- display.setTextColor(WHITE); //one-color displays are always "white" even if the oled is a different color
- display.setCursor(0, line * 8 * textSize); //sets text line, left aligned,
- display.println(instring); //print data
- }
- //Pass Int
- void displayPrint(int instring, int line, int textSize, bool clear) {
- if (clear) {
- display.clearDisplay();
- }
- display.setTextSize(textSize);
- display.setTextColor(WHITE);
- display.setCursor(0, line * 8 * textSize);
- display.println(instring);
- }
- //Pass Bool
- void displayPrint(bool instring, int line, int textSize, bool clear) {
- if (clear) {
- display.clearDisplay();
- }
- display.setTextSize(textSize);
- display.setTextColor(WHITE);
- display.setCursor(0, line * 8 * textSize);
- if ( instring ) {
- display.println("True");
- } else {
- display.println("False");
- }
- }
- //Pass Float
- void displayPrint(float instring, int line, int textSize, bool clear) {
- if (clear) {
- display.clearDisplay();
- }
- display.setTextSize(textSize);
- display.setTextColor(WHITE);
- display.setCursor(0, line * 8 * textSize); //character line is 7 pixels high
- display.println(instring);
- }
- 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)
- 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
- display.drawRect(gx, gy, gw, gh, WHITE); //Draw a rectangle aroung the graph
- 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
- display.drawLine((gx + i + 1), (gy + (gh) - graphBuffer[i + 1] + 1), (gx + i), (gy + (gh) - graphBuffer[i] + 1), WHITE);
- graphBuffer[i + 1] = graphBuffer[i];
- }
- }
- void grapher2(int gx, int gy, int gw, int gh, int inVal) {
- graphBuffer2[0] = ((inVal * gh) / graphMax);
- display.drawRect(gx, gy, gw, gh, WHITE);
- for (int i = gw - 2; i >= 0; i--) {
- display.drawLine((gx + i + 1), (gy + (gh) - graphBuffer2[i + 1] + 1), (gx + i), (gy + (gh) - graphBuffer2[i] + 1), WHITE);
- graphBuffer2[i + 1] = graphBuffer2[i];
- }
- }
- void grapher3(int gx, int gy, int gw, int gh, int inVal) {
- graphBuffer3[0] = ((inVal * gh) / graphMax);
- display.drawRect(gx, gy, gw, gh, WHITE);
- for (int i = gw - 2; i >= 0; i--) {
- display.drawLine((gx + i + 1), (gy + (gh) - graphBuffer3[i + 1] + 1), (gx + i), (gy + (gh) - graphBuffer3[i] + 1), WHITE);
- graphBuffer3[i + 1] = graphBuffer3[i];
- }
- }
- void grapher4(int gx, int gy, int gw, int gh, int inVal) {
- graphBuffer4[0] = ((inVal * gh) / graphMax);
- display.drawRect(gx, gy, gw, gh, WHITE);
- for (int i = gw - 2; i >= 0; i--) {
- display.drawLine((gx + i + 1), (gy + (gh) - graphBuffer4[i + 1] + 1), (gx + i), (gy + (gh) - graphBuffer4[i] + 1), WHITE);
- graphBuffer4[i + 1] = graphBuffer4[i];
- }
- }
- //FASTER GRAPH, ORIGINAL, NO LINE DRAW, GOOD FOR SLOW-CHANGING DATA
- void fastGrapher(int gx, int gy, int gw, int gh, int inVal) {
- graphBuffer[0] = ((inVal * gh) / graphMax);
- display.drawRect(gx, gy, gw, gh, WHITE);
- for (int i = gw - 2; i >= 0; i--) {
- display.drawPixel((gx + i + 1), (gy + (gh) - graphBuffer[i + 1] + 1), WHITE);
- graphBuffer[i + 1] = graphBuffer[i];
- }
- }
- //FPS Counter, basically updates once a second and displays how many times the program has looped since then
- void fps() {
- if (timer + 1000 <= (millis() )) {
- loops[1] = loops[0];
- timer = millis();
- loops[0] = 0;
- } else {
- loops[0]++;
- }
- displayPrint(loops[1], 0, TEXT_SMALL, false);
- }
- /*
- * Comment out to prevent running on every loop if not needed
- void serialEvent() {
- while (Serial.available()) {
- // get the new byte:
- char inChar = (char)Serial.read();
- if (inChar == '\n') {
- stringComplete = true;
- if (enableDebugSpew) {
- Serial.println("\n\n_______________________________");
- Serial.print("Received via Serial: ");
- Serial.println(inputString);
- }
- } else {
- inputString += inChar;
- }
- }
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement