#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
#define I2C_SDA 36
#define I2C_SCL 35
#define MAX_GRAPH_DATAPOINTS 30
#define GRAPH_X_PADDING 37
#define GRAPH_Y_PADDING 2
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ I2C_SCL, /* data=*/ I2C_SDA); // ESP32 Thing, HW I2C with pin remapping
int graphMin = 0;
int graphThird, graphTwoThirds;
int graphMax = 256;
int dataPoints[MAX_GRAPH_DATAPOINTS][2];
int dataTracker;
int dataFeed[100] = { 8, 16, 24, 32, 32, 24, 32, 24, 16, 8,
8, 16, 24, 32, 32, 24, 32, 24, 16, 8,
8, 16, 24, 32, 32, 24, 32, 24, 16, 8,
8, 16, 24, 32, 32, 24, 32, 24, 16, 8,
8, 16, 24, 32, 32, 24, 32, 24, 16, 8,
768, 812, 768, 783, 909, 1023, 1023, 899, 820, 768,
768, 812, 768, 783, 909, 1023, 1023, 899, 820, 768,
768, 812, 768, 783, 909, 1023, 1023, 899, 820, 768,
768, 812, 768, 783, 909, 1023, 1023, 899, 820, 768,
768, 812, 768, 783, 909, 1023, 1023, 899, 820, 768 };
bool generateRandomData = true;
bool scaleMin;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Wire.begin(I2C_SDA, I2C_SCL);
Serial.println("Hello, ESP32-S3!");
randomSeed(0);
u8g2.begin();
for (byte i = 0; i < 29; i++)
{ Add_New_Data(random(16, 65)); }
}
void loop() {
// put your main code here, to run repeatedly:
//delay(10); // this speeds up the simulation
u8g2.clearBuffer();
//u8g2.setCursor(32, 16);
//u8g2.setFont(u8g2_font_6x10_tf);
//u8g2.print("Hello world!");
Add_New_Data(Generate_New_Data());
Draw_Graph();
u8g2.sendBuffer();
delay(100);
}
void Draw_Graph()
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 31; j++)
{
u8g2.drawPixel(3 * j + GRAPH_X_PADDING, 10 * i + GRAPH_Y_PADDING);
}
}
u8g2.setFont(u8g2_font_squeezed_b7_tr);
u8g2.setCursor(16 - Number_Pixel_Length(graphMax), 8);
u8g2.print(graphMax);
/*u8g2.setCursor(16 - Number_Pixel_Length(graphThird), 20);
u8g2.print(graphThird);
u8g2.setCursor(16 - Number_Pixel_Length(graphTwoThirds), 32);
u8g2.print(graphTwoThirds);*/
u8g2.setCursor(16 - Number_Pixel_Length(graphMin), 44);
u8g2.print(graphMin);
for (int i = 0; i < MAX_GRAPH_DATAPOINTS - 1; i++)
{
u8g2.drawLine(3 * i + GRAPH_X_PADDING, 42 - dataPoints[i][1], 3 * i + GRAPH_X_PADDING + 3, 42 - dataPoints[i + 1][1]);
}
}
void Add_New_Data(int newDataPoint)
{
int newGraphMin = graphMax;
int newGraphMax = graphMin;
for (byte i = 0; i < MAX_GRAPH_DATAPOINTS - 1; i++)
{
for (byte j = 0; j < 2; j++)
{
dataPoints[i][j] = dataPoints[i + 1][j];
}
if (dataPoints[i][0] < newGraphMin) //restrict the minimum range of datapoints to be the lowest value on the graph
{ newGraphMin = dataPoints[i][0]; }
if (dataPoints[i][0] > newGraphMax)
{ newGraphMax = dataPoints[i][0]; }
}
if (newDataPoint > graphMax)
{ newGraphMax = newDataPoint; }
if (scaleMin)
{graphMin = newGraphMin;}
graphMax = newGraphMax + ((newDataPoint - graphMin) * 0.125);
dataPoints[MAX_GRAPH_DATAPOINTS - 1][0] = newDataPoint;
graphThird = graphMin + ((graphMax - graphMin) * 0.333);
graphTwoThirds = graphMin + ((graphMax - graphMin) * 0.666);
for (byte i = 0; i < MAX_GRAPH_DATAPOINTS; i++)
{ dataPoints[i][1] = map(dataPoints[i][0], graphMin, graphMax, 3, 42); }
}
int Generate_New_Data()
{
int theNewData;
if (generateRandomData)
{
if ((dataPoints[MAX_GRAPH_DATAPOINTS - 1][0] <= 1023 - 32) && dataPoints[MAX_GRAPH_DATAPOINTS - 1][0] >= 16)
{
if (dataPoints[MAX_GRAPH_DATAPOINTS - 1][0] < 341)
{ theNewData = dataPoints[MAX_GRAPH_DATAPOINTS - 1][0] + Roll_5d12(1); }
else if (dataPoints[MAX_GRAPH_DATAPOINTS - 1][0] > 682)
{ theNewData = dataPoints[MAX_GRAPH_DATAPOINTS - 1][0] + Roll_5d12(2); }
else
{ theNewData = dataPoints[MAX_GRAPH_DATAPOINTS - 1][0] + Roll_5d12(0); }
}
else
{
if (dataPoints[MAX_GRAPH_DATAPOINTS - 1][0] > 1023 - 32)
{ theNewData = dataPoints[MAX_GRAPH_DATAPOINTS - 1][0] + Roll_5d12(4); }
else
{ theNewData = dataPoints[MAX_GRAPH_DATAPOINTS - 1][0] + Roll_5d12(3); }
}
}
else
{
theNewData = dataFeed[dataTracker];
if (dataTracker < 99)
{ dataTracker++; }
else
{ dataTracker = 0; }
}
Serial.println(theNewData);
return theNewData;
}
/*if (dataPoints[MAX_GRAPH_DATAPOINTS - 1][0] < 341)
{ theNewData = dataPoints[MAX_GRAPH_DATAPOINTS - 1][0] + random(-8, 24); }
else if (dataPoints[MAX_GRAPH_DATAPOINTS - 1][0] > 682)
{ theNewData = dataPoints[MAX_GRAPH_DATAPOINTS - 1][0] + random(-23, 8); }
else
{ theNewData = dataPoints[MAX_GRAPH_DATAPOINTS - 1][0] + random(-15, 16); }
}
else
{
if (dataPoints[MAX_GRAPH_DATAPOINTS - 1][0] > 1023 - 32)
{ theNewData = dataPoints[MAX_GRAPH_DATAPOINTS - 1][0] + random(-31, 1); }
else
{ theNewData = dataPoints[MAX_GRAPH_DATAPOINTS - 1][0] + random(0, 33); }
*/
int Number_Pixel_Length(float num)
{
int w;
static char outstr[6];
dtostrf(num, 1, 0, outstr);
w = u8g2.getStrWidth(outstr);
return w;
}
int Roll_5d12(byte s)
{
int n;
switch (s)
{
case 0:
n = (random(13) + random(13)) - (random(13) + random(13)); //average
break;
case 1:
n = (random(13) + random(13) + random(13)) - random(13); //roll up
break;
case 2:
n = random(13) - (random(13) + random(13) + random(13)); //roll down
break;
case 3:
n = random(13) + random(13) + random(13) + random(13); //hard roll up
case 4:
n = -1 * (random(13) + random(13) + random(13) + random(13)); //hard roll down
break;
}
return n;
}
Loading
esp32-s3-devkitc-1
esp32-s3-devkitc-1