#include <Arduino.h>
#include <U8g2lib.h>
/////
// Déclaration des énumérations
enum Couleur
{
NOIR,
BLANC
};
/////
// Déclarations des constantes
const int32_t VIDE = -1;
const int32_t FIN = -2;
const uint8_t LARGEUR_VAISSEAU = 5;
const uint16_t TAILLE_TABLEAU = 140;
const uint8_t LARGEUR_ECRAN = 128;
const uint8_t HAUTEUR_ECRAN = 64;
const uint32_t PAS_DE_TEMPS = 100;
const uint16_t DELAI_DE_GRACE = 30;
/////
// Déclaration des variables globales
int8_t trace[TAILLE_TABLEAU];
U8G2_SSD1306_128X64_NONAME_F_HW_I2C afficheur(U8G2_R2, /* orientation */
SCL, /* clock=*/
SDA, /* data=*/
U8X8_PIN_NONE /* reset=*/
); // High speed I2C
/////
// Fonctions
void initTrace()
{
srand(analogRead(A0));
for (int idx = 0 ; idx < DELAI_DE_GRACE ; idx++)
{
trace[idx] = VIDE;
}
trace[DELAI_DE_GRACE] = rand() % 64;
for (int idx = DELAI_DE_GRACE+1 ; idx < TAILLE_TABLEAU-1 ; idx++)
{
int32_t deltaY = rand() % 3 - 1;
trace[idx] = trace[idx-1] + deltaY;
if (trace[idx] > 63)
{
trace[idx] = 63;
}
else if (trace[idx] < 0)
{
trace[idx] = 0;
}
}
trace[TAILLE_TABLEAU-1] = FIN;
}
void initAfficheur()
{
afficheur.begin();
afficheur.setFont(u8g2_font_unifont_t_symbols);
afficheur.setFontDirection(0);
afficheur.clearBuffer();
afficheur.setDrawColor(BLANC);
}
void defilerTrace()
{
for (int idx = 0 ; idx < TAILLE_TABLEAU-1 ; idx++)
{
trace[idx] = trace[idx+1];
if (trace[idx] == FIN)
break;
}
}
uint8_t lirePotar()
{
return analogRead(A0) >> 4;
}
void afficherTrace()
{
for (int idx = 0 ; idx < LARGEUR_ECRAN ; idx++)
{
if (trace[idx] >= 0 )
{
afficheur.drawPixel(idx, trace[idx]);
}
}
}
void afficheVaisseau(uint32_t yVais)
{
afficheur.drawTriangle(0, yVais - LARGEUR_VAISSEAU,
0, yVais + LARGEUR_VAISSEAU,
LARGEUR_VAISSEAU, yVais);
}
void setup() {
Serial.begin(115200);
Serial.println("Boot");
initTrace();
initAfficheur();
Serial.println("Init OK");
afficheur.drawStr(40, 32, "Welcome!");
afficheur.updateDisplay();
}
void loop() {
static uint32_t temps_precedent = millis();
static bool termine = false;
static uint8_t yVaiss = 32;
if (termine == false)
{
uint32_t temps_courant = millis();
if ( (temps_courant - temps_precedent) > PAS_DE_TEMPS)
{
// MàJ variables
defilerTrace();
uint8_t yVaisseau = lirePotar();
// Effacer l'écran
afficheur.clearBuffer();
// Préparer le buffer
afficherTrace();
afficheVaisseau(yVaisseau);
// Afficher le buffer
afficheur.updateDisplay();
// Détecter la fin
if (trace[0] == FIN)
{
afficheur.clearBuffer();
afficheur.drawStr(30, 40, "Zi einde!");
afficheur.updateDisplay();
termine = true;
}
// Mettre à jour le temps
temps_precedent = temps_courant;
}
}
}