#include "U8glib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST); // Fast I2C / TWI
#define BTN_PIN 5
#define POTEN_1_PIN A0
#define POTEN_2_PIN A1
#define NUM_SCREENS 2
//int progress = 0;
int screenToggle = 0;
void setup() {
u8g.setFont(u8g_font_tpssb);
//u8g.setFont(u8g_font_unifont);
u8g.setColorIndex(1);
Serial.begin(115200);
pinMode(POTEN_1_PIN, INPUT);
pinMode(BTN_PIN, INPUT_PULLUP);
}
void loop() {
u8g.firstPage();
int toggleSwitch = digitalRead(BTN_PIN);
Serial.print("Screen: ");
Serial.println(screenToggle);
if (toggleSwitch == LOW) {
screenToggle = (screenToggle+1) % NUM_SCREENS;
delay(100);
}
do {
if (screenToggle == 0){
int readValue = analogRead(POTEN_1_PIN);
drawProgressBar(0, 10, 128, 20, 0, 1023, readValue, 0, 100);
}
else if (screenToggle == 1){
int readValue = analogRead(POTEN_2_PIN);
drawDial(64, 32, 30, 0, 1023, readValue, 0, 100);
}
} while ( u8g.nextPage() );
}
void drawDial(int x, int y, int radius, float inputMin, float inputMax, float inputValue, float guageMin, float guageMax){
// 270 sets travel angle of guage needle.
float DIAL_TRAVEL_DEGS = 270.00;
// -225 sets start position of guage needle.
float DIAL_START_POSITION = -225.00;
int progress = getGuageValue(inputMin, inputMax, inputValue, guageMin, guageMax);
progress = DIAL_TRAVEL_DEGS / guageMax * progress;
float angle = float(progress) + DIAL_START_POSITION;
float radAngle = radians(angle);
int endX = x + (cos(radAngle) * (radius-3));
int endY = y + (sin(radAngle) * (radius-3));
u8g.drawCircle(x, y, radius);
u8g.drawCircle(x, y, 2);
u8g.drawLine(x, y, endX, endY);
//u8g.drawStr(x-radius, y+radius+10, "Dial Guage");
}
void drawProgressBar(int x, int y, int w, int h, float inputMin, float inputMax, float inputValue, float guageMin, float guageMax){
int margin = 5;
u8g.drawFrame(x, y, w, h);
int progress = getGuageValue(inputMin, inputMax, inputValue, guageMin, guageMax);
progress = (w-(2*margin)) / guageMax * progress;
u8g.drawBox(x+margin, y+margin, progress, h-(margin*2));
u8g.drawStr(x, y+h+(margin*2)+5, "Progress Bar" );
}
int getGuageValue(float inputMin, float inputMax, float inputValue, float guageMin, float guageMax) {
float value = inputValue;
value = value / (inputMax - inputMin);
return int(value * (guageMax - guageMin));
}