// simple project using Arduino UNO and 128x64 OLED Display,
// created by upir, 2022
// youtube channel: https://www.youtube.com/upir_upir
// FULL TUTORIAL: https://youtu.be/NPfaLKKsf_Q
// u8g fonts (fonts available for u8g library): https://nodemcu-build.com/u8g-fonts.php
// u8g documentation: https://github.com/olikraus/u8glib/wiki/userreference
// Arduino uno: http://store.arduino.cc/products/arduino-uno-rev3
// Arduino breadboard prototyping shield: https://www.adafruit.com/product/2077
// Wokwi starting project: https://wokwi.com/arduino/projects/300867986768527882
// Transparent display buy: https://a.aliexpress.com/_mKGmhKg
// Photopea (online Photoshop-like tool): https://www.photopea.com/
// image2cpp (convert images into C code): https://javl.github.io/image2cpp/
#include "U8glib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST); // Fast I2C / TWI
//U8GLIB_SSD1306_128X64 u8g(13, 11, 8, 9, 10); // SPI communication: SCL = 13, SDA = 11, RES = 10, DC = 9, CS = 8
// DECLACRATION VARIABLE
//
#define clkPin 2
#define dtPin 3
#define SwPin 4
bool clkLast=HIGH;
unsigned long TimeMenu = 0;
unsigned long TimeSwPressed=0;
byte EtapeEcranLed = 0;
// CLASSE
//
class MENU
{
public:
String TitreMenu="";
String DescriptionMenu ="";
String Item[10]={};
byte NombreItem =0;
void Setup(String aTitre,String aDescription);
void AjoutItem(String aText);
};
void MENU::Setup(String aTitre,String aDescription)
{
TitreMenu = aTitre;
DescriptionMenu = aDescription;
NombreItem = 0;
}
void MENU::AjoutItem(String aText)
{
if ( aText != "")
{
Item[NombreItem]=aText;
NombreItem++;
}
}
MENU MenuPrincipal;
MENU *pMenuAAfficher = NULL;
void setup() {
Serial.begin(115200);
//Rotary
pinMode(clkPin,INPUT);
pinMode(dtPin, INPUT);
pinMode(SwPin, INPUT_PULLUP);
//Ecran
u8g.begin();
MenuPrincipal.Setup("MENU","Menu princpal");
MenuPrincipal.AjoutItem("Language");
MenuPrincipal.AjoutItem("Led");
MenuPrincipal.AjoutItem("Rotary Encoder");
MenuPrincipal.AjoutItem("Embrayage");
MenuPrincipal.AjoutItem("Ecran Led");
MenuPrincipal.AjoutItem("Test Bouton");
//LANGUAGE.Setup("Language","Choix de la langue");
//CHOIXLANGUAGE.Setup("Francais");
}
void loop() {
byte lectureswitch = ReadRotaryMenu();
if ( lectureswitch == 2)
EtapeEcranLed=1;
if ( EtapeEcranLed == 1)
{
pMenuAAfficher = &MenuPrincipal;
//Serial.println(pMenuAAfficher->Item[0]);
// FONCTION POUR AFFICHER LE MENU UNIQUEMENT
//
u8g.firstPage(); // required for u8g library
do { // --//--
//Varialbe utilser plusieur fois
byte line_spacing;
// TITRE
// CALCUL HAUTEUR TOTAL FONT
// avec u8g.getFontAscent() dessus de la ligne u8g.getFontDescent() dessous ligne NEGATIF
u8g.setFont(u8g_font_helvB10); // choix du font
line_spacing = (u8g.getFontAscent() - u8g.getFontDescent()); // calcul hauteur total font
byte HL = line_spacing; //pour calculer les escpaces entre lien
byte centre = Centrer(pMenuAAfficher->TitreMenu); // pour obtenir le centre
u8g.setPrintPos(centre,HL);
u8g.print(pMenuAAfficher->TitreMenu);
HL=HL+1;
LigneEntiere(HL); // Affiche une ligne horizontal
// changement de font pour afficher menu
u8g.setFont(u8g_font_helvB08); // choix du font
line_spacing = (u8g.getFontAscent() - u8g.getFontDescent()); // calcul hauteur total font
if (pMenuAAfficher->NombreItem >0)
{
byte x,y;
x = 13;
y = HL + line_spacing+1;
for (byte i=0; i<pMenuAAfficher->NombreItem ; i++)
{
if ( y < 64){
u8g.setPrintPos(x,y);
u8g.print(pMenuAAfficher->Item[i]);
y=y+line_spacing+2;}
}
}
} while ( u8g.nextPage() ); // required for u8g library
delay(1000);
}
}
// FONCTION AFFICHAGE MENU
void AnimationTitre()
{
}
void LigneEntiere(byte ydep)
{
u8g.drawLine(0,ydep,127,ydep);
}
byte Centrer(String aText)
{
byte centre =0;
centre = u8g.getStrWidth(aText.c_str());
centre = 64 - centre /2;
return centre;
}
void AfficherDeroulant(byte aNombreItem,byte aHL,byte aPosActuel)
{
}
// FONCTION ROTARY ENCODER
byte ReadRotaryMenu()
{
int Increment = 0;
bool clk = digitalRead(clkPin);
if ( clk !=clkLast && clk == LOW)
{
if ( digitalRead(dtPin) == HIGH)
{
Increment++;
Serial.println(Increment);
}else
{
Increment--;
Serial.println(Increment);
}
}
clkLast = clk;
if (digitalRead(SwPin) == LOW)
{
if ( (millis() - TimeSwPressed) >10)
{
Increment = 2;
Serial.println(Increment);
}
TimeSwPressed = millis();
}
return Increment;
}