#include <U8g2lib.h>
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE); // Fast I2C / TWI
int progress = 0;
int up = 3;
int down = 4;
const uint8_t* fonts[] = {u8g_font_7x14B, u8g_font_10x20, u8g_font_chikita};
int currentFontIndex = 0;
void changeFont(int direction);
void setup() {
u8g2.begin();
u8g2.clearBuffer();
u8g2.setColorIndex(1);
pinMode(up, INPUT_PULLUP);
pinMode(down, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
int rUp = digitalRead(up);
int rDown = digitalRead(down);
if (!rUp) {
Serial.println("UP");
changeFont(1); // Change to the next font
delay(100);
}
if (!rDown) {
Serial.println("Down");
changeFont(-1); // Change to the previous font
delay(100);
}
u8g2.firstPage();
do {
u8g2.setFont(fonts[currentFontIndex]);
// u8g2.drawStr(0, 50, "Progress Bar");
// u8g2.drawFrame(0, 0, 128, 64);
u8g2.setFont(u8g2_font_9x15B_mf);
int x = centerX("SP.HI");
u8g2.drawStr(x, 15, "SP.HI");
//Value
u8g2.setFont(u8g2_font_9x15_mf);
x = centerX("2000");
u8g2.drawStr(x, 15+20+2, "2000");
//Unit
u8g2.setFont(u8g2_font_7x13B_mf);
x = centerX("CMH");
u8g2.drawStr(x, 15+20+20+2+2, "CMH");
} while (u8g2.nextPage());
if (progress < 108) {
progress++;
} else {
progress = 0;
}
}
int centerX(String text) {
int x = 128/2 - (u8g2.getStrWidth(text.c_str()) / 2);
return x;
}
void changeFont(int direction) {
currentFontIndex += direction;
// Ensure the index stays within bounds
if (currentFontIndex < 0) {
currentFontIndex = sizeof(fonts) / sizeof(fonts[0]) - 1;
} else if (currentFontIndex >= sizeof(fonts) / sizeof(fonts[0])) {
currentFontIndex = 0;
}
}