#include <Arduino.h>
#include <U8g2lib.h>
// initialization for the 128x64px OLED display
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
// U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
static const unsigned char full_btn_bits[] = {0xfc,0xff,0xff,0xff,0x0f,0xfe,0xff,0xff,0xff,0x1f,0xff,0xff,0xff,0xff,0x3f,0xff,0xff,0xff,0xff,0x3f,0xff,0xff,0xff,0xff,0x3f,0xff,0xff,0xff,0xff,0x3f,0xff,0xff,0xff,0xff,0x3f,0xff,0xff,0xff,0xff,0x3f,0xff,0xff,0xff,0xff,0x3f,0xff,0xff,0xff,0xff,0x3f};
static const unsigned char frame_btn_bits[] = {0xfc,0xff,0xff,0xff,0x07,0x02,0x00,0x00,0x00,0x08,0x01,0x00,0x00,0x00,0x10,0x01,0x00,0x00,0x00,0x10,0x01,0x00,0x00,0x00,0x10,0x01,0x00,0x00,0x00,0x10,0x01,0x00,0x00,0x00,0x10,0x01,0x00,0x00,0x00,0x10,0x01,0x00,0x00,0x00,0x10,0x01,0x00,0x00,0x00,0x10};
static const uint8_t NUM_SLIDERS = 1;
static const uint8_t analogInputs[NUM_SLIDERS] = {4};
unsigned short analogSliderValues[NUM_SLIDERS];
void setup() {
Serial.begin(9600);
u8g2.begin();
u8g2.clear();
for (int i = 0; i < NUM_SLIDERS; i++)
pinMode(analogInputs[i], INPUT);
u8g2.setFontMode(1);
u8g2.setBitmapMode(1);
u8g2.setFont(u8g2_font_4x6_tr);
}
void loop() {
updateSliderValues();
drawScreen();
}
void drawScreen() {
u8g2.clearBuffer();
u8g2.drawRFrame(1, 1, 126, 46, 3);
u8g2.setFont(u8g2_font_NokiaSmallBold_tr);
u8g2.drawStr(15, 14, "Slider Configuration");
// convert the slider value to a string
const String sliderStr = String(analogSliderValues[0]);
// calculate the width of the slider string
unsigned short sliderWidth = u8g2.getStrWidth(sliderStr.c_str());
// calculate the X position to center the text
unsigned short valueX = (128 - sliderWidth) / 2;
// draw the value in the center of the screen
u8g2.drawStr(valueX, 39, sliderStr.c_str());
// adjust the line start and end based on the sliderStr width + padding
unsigned short lineStartX = valueX - 2; // start the line 2 pixels before the string
unsigned short lineEndX = valueX + sliderWidth + 1; // end the line 1 pixels after the string
// draw the line with dynamic width
u8g2.drawLine(lineStartX, 41, lineEndX, 41);
u8g2.setFont(u8g2_font_5x7_tf);
u8g2.drawStr(40, 26, "max value:");
u8g2.setFont(u8g2_font_6x10_tr);
u8g2.drawStr(7, 63, "Back");
u8g2.drawStr(98, 63, "Next");
u8g2.setDrawColor(2);
u8g2.drawStr(53, 63, "Save");
u8g2.drawXBM(0, 54, 37, 10, frame_btn_bits);
u8g2.drawXBM(91, 54, 37, 10, frame_btn_bits);
u8g2.drawXBM(45, 54, 38, 10, full_btn_bits);
u8g2.sendBuffer();
}
void updateSliderValues() {
for (int i = 0; i < NUM_SLIDERS; i++)
analogSliderValues[i] = analogRead(analogInputs[i]);
}
Loading
esp32-c3-devkitm-1
esp32-c3-devkitm-1