#include "U8glib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST); // Fast I2C / TWI
int nombredePiece = 0;
int currentIndex = 0;
// 'cadre auto', 16x16px
const unsigned char cadre_auto [] PROGMEM = {
0xff, 0xff, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01,
0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0xff, 0xff
};
const char* consign[] = {
"1",
"10",
"OK"
};
int currentIndex = 0;
#define pin_up 4
#define pin_down 7
#define pin_enter 12
void setup() {
u8g.setFont(u8g_font_7x14B); // Set font
u8g.setColorIndex(1); // Set color to white
pinMode(pin_up, INPUT_PULLUP);
pinMode(pin_down, INPUT_PULLUP);
pinMode(pin_enter, INPUT_PULLUP);
}
void loop() {
// Handle button presses
if (digitalRead(pin_up) == LOW) {
currentIndex++;
if (currentIndex >= 3) currentIndex = 0; // Wrap around to the first item
delay(200); // Debounce delay
}
if (digitalRead(pin_down) == LOW) {
if (currentIndex <= 0) currentIndex = 2; // Wrap around to the last item
else currentIndex--;
delay(200); // Debounce delay
}
if (digitalRead(pin_enter) == LOW) {
// Increment nombredePiece based on current index
if (currentIndex == 0) {
nombredePiece += 1;
} else if (currentIndex == 1) {
nombredePiece += 10;
}
delay(200); // Debounce delay
}
char buffer[10];
itoa(nombredePiece, buffer, 10); // Convert integer to string
u8g.firstPage();
do {
u8g.setFont(u8g_font_7x14B); // Set the original font
u8g.drawStr(6, 22, "Nombre de pieces");
u8g.drawBitmapP(104, 35, 16 / 8, 16, cadre_auto); // Draw frame
u8g.setFont(u8g_font_6x10); // Set smaller font
// Calculate the width and height of the text
uint8_t text_width = u8g.getStrWidth(consign[currentIndex]);
uint8_t text_height = u8g.getFontAscent() - u8g.getFontDescent();
// Center the text inside the 16x16 box
uint8_t x_pos = 104 + (16 - text_width) / 2;
uint8_t y_pos = 35 + (16 + text_height) / 2;
u8g.drawStr(x_pos, y_pos, consign[currentIndex]); // Draw text from consign array
u8g.setFont(u8g_font_7x14B); // Set the original font back
u8g.drawStr(33, 47, buffer); // Draw the nombredePiece value
} while ( u8g.nextPage() );
delay(50); // Delay for smooth progress visualization
}