I've been playing around with an Arduino Nano connected to a 128x32 SSD1306 I2C LCD I got off of WISH (A treasure trove of dirt-cheap Arduino/Adafruit/Sparkfun clones). I've been working on relatively fast and flexible graphing function, I've got it to the point that it'll allow me to run 4 30x30 graphs simultaneously at about 25FPS. They update when they are called, and I have plans to implement a blanking that only appears over the area of the graph so no display.clearDisplay is necessary. (I tried to use a black square to fill over it but it halved the performance). Due to the way it works, if my predefined buffer array is smaller than the width of the graph the array will be exceeded and it will start writing to areas it shouldn't. I want to make prevent an unnecessary IF check every time it is called.
My question is this:
#define overflowProtect true
CALLED IN LOOP AS: grapher(0, 0, 31, 32, random(1023));
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)
if (overflowProtect) {
if (gw + 1 < graphBufferSize) {
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];
}
} else {
display.setTextSize(1);
display.setCursor(gx + 1,gy + 1);
display.println("Buff");
display.setCursor(gx + 1,gy + 9);
display.println("Too");
display.setCursor(gx + 1,gy + 17);
display.println("Small");
}
} else {
graphBuffer[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) - graphBuffer[i + 1] + 1), (gx + i), (gy + (gh) - graphBuffer[i] + 1), WHITE);
graphBuffer[i + 1] = graphBuffer[i];
}
}
}
At the if (overflowProtect) what would be compiled since overflowProtect is #defined?
EDIT:
My long term goal is to make this set up a graph with dimensions set up via a handshake from a master controller over serial then receive the data for each graph over a structured packet to allow a $6 nano clone/lcd combo to offload much of the processing to the nano. A lot of this drawing code could easily be ported to other displays.
Full Code Here new function not implemented: