#include <Wire.h>
#include <U8g2lib.h>
// Configuration de l'écran OLED en I2C (128x64)
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE);
// Configuration des boutons
const int playPauseButton = 12;
const int nextButton = 14;
const int prevButton = 27;
// Variables pour la gestion de l’état
bool isPlaying = false;
int currentTrack = 1; // Piste actuelle
unsigned long previousMillis = 0;
unsigned long playStartTime = 0;
int trackDuration = 180; // Durée de la piste en secondes (3 minutes)
// Variables pour détection des transitions des boutons
bool playPauseLastState = HIGH;
bool nextLastState = HIGH;
bool prevLastState = HIGH;
// Vos images en format XBM
static const uint8_t image_download_bits[] = {0x03,0x00,0x07,0x00,0x19,0x00,0x61,0x00,0x81,0x01,0x01,0x06,0x01,0x18,0x01,0x60,0x01,0x18,0x01,0x06,0x81,0x01,0x61,0x00,0x19,0x00,0x07,0x00,0x03,0x00,0x00,0x00};
static const uint8_t image_ButtonRight_bits[] = {0x01,0x03,0x07,0x0f,0x07,0x03,0x01};
static const uint8_t image_download_1_bits[] = {0xe0,0x03,0xf8,0x0f,0x7c,0x1f,0x7e,0x3e,0x6e,0x3d,0x5f,0x7b,0x3f,0x7d,0x7f,0x7e,0x3f,0x7d,0x5f,0x7b,0x6e,0x3d,0x7e,0x3e,0x7c,0x1f,0xf8,0x0f,0xe0,0x03};
static const uint8_t image_download_2_bits[] = {0x00,0x00,0x00,0xf0,0xff,0x7f,0x08,0x00,0x80,0x08,0xdb,0xb6,0x0e,0xdb,0xb6,0x01,0xdb,0xb6,0x01,0xdb,0xb6,0x01,0xdb,0xb6,0x01,0xdb,0xb6,0x0e,0xdb,0xb6,0x08,0xdb,0xb6,0x08,0x00,0x80,0xf0,0xff,0x7f,0x00,0x00,0x00,0x00,0x00,0x00};
static const uint8_t image_download_3_bits[] = {0xfc,0xff,0xff,0xff,0x7f,0x00,0x10,0x02,0x00,0x00,0x00,0x80,0x00,0x20,0x01,0x00,0x00,0x00,0x18,0x01,0x42,0x01,0x03,0x6c,0xdb,0x24,0x01,0x44,0x01,0x03,0x6c,0xdb,0x24,0x01,0x88,0x01,0x03,0x0c,0x00,0x18,0x81,0x88,0x01,0x00,0x60,0xdb,0x00,0x07,0x89,0x71,0x3b,0x6c,0xdb,0x00,0x09,0x89,0x71,0x3b,0x0c,0x00,0x00,0x09,0x89,0x01,0x00,0x6c,0xdb,0x00,0x07,0x89,0x01,0x03,0x60,0xdb,0x18,0x81,0x88,0x01,0x03,0x00,0x00,0x3c,0x01,0x88,0x01,0xe3,0xbd,0xf7,0x3c,0x01,0x44,0x01,0xe0,0xbd,0xf7,0x18,0x01,0x42,0x02,0x00,0x00,0x00,0x80,0x00,0x20,0xfc,0xff,0xff,0xff,0x7f,0x00,0x10};
void setup() {
u8g2.begin();
// Configuration des boutons
pinMode(playPauseButton, INPUT_PULLUP);
pinMode(nextButton, INPUT_PULLUP);
pinMode(prevButton, INPUT_PULLUP);
Serial.begin(9600);
}
void simulatePlayTrack(int track) {
Serial.print("Lecture de la piste ");
Serial.println(track);
}
void loop() {
bool playPauseState = digitalRead(playPauseButton);
bool nextState = digitalRead(nextButton);
bool prevState = digitalRead(prevButton);
// Gestion du bouton Play/Pause
if (playPauseState == LOW && playPauseLastState == HIGH) {
delay(200); // Débounce
isPlaying = !isPlaying;
if (isPlaying) {
playStartTime = millis();
simulatePlayTrack(currentTrack);
} else {
Serial.println("Pause");
}
}
playPauseLastState = playPauseState;
// Gestion du bouton Suivant
if (nextState == LOW && nextLastState == HIGH) {
delay(200); // Débounce
currentTrack = (currentTrack % 10) + 1; // Remise à 1 après la 10ème piste
simulatePlayTrack(currentTrack);
isPlaying = true;
playStartTime = millis();
}
nextLastState = nextState;
// Gestion du bouton Précédent
if (prevState == LOW && prevLastState == HIGH) {
delay(200); // Débounce
currentTrack = (currentTrack == 1) ? 10 : currentTrack - 1; // Retour à 10 si inférieur à 1
simulatePlayTrack(currentTrack);
isPlaying = true;
playStartTime = millis();
}
prevLastState = prevState;
// Calcul du temps de lecture
unsigned long currentMillis = millis();
int secondsPlayed = isPlaying ? (currentMillis - playStartTime) / 1000 : 0;
int minutes = secondsPlayed / 60;
int seconds = secondsPlayed % 60;
// Affichage sur l'écran OLED
u8g2.clearBuffer();
u8g2.setBitmapMode(1);
u8g2.setFontMode(1);
u8g2.drawXBM(11, 36, 15, 16, image_download_bits);
// Affiche le temps de lecture
char timeString[6];
sprintf(timeString, "%02d:%02d", minutes, seconds);
u8g2.setFont(u8g2_font_profont29_tr);
u8g2.drawStr(40, 62, timeString);
// Affiche le nom de la piste audio
u8g2.setFont(u8g2_font_profont10_tr);
u8g2.drawStr(40, 39, "Piste audio");
u8g2.setFont(u8g2_font_profont15_tr);
u8g2.drawStr(41, 30, "Nom de la piste");
u8g2.drawXBM(29, 0, 15, 15, image_download_1_bits);
u8g2.drawXBM(1, 0, 24, 16, image_download_2_bits);
u8g2.drawXBM(72, 0, 56, 16, image_download_3_bits);
u8g2.sendBuffer();
delay(100); // Temps d'attente pour rafraîchissement
}