Files
pi500_keyboard/drapeauBelgeOndulant.py

94 lines
2.9 KiB
Python
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
# ============================================================
# Nom : clavier_drapeau_belge_wave.py
# Version : 1.2.0
# Auteur : framboise314 (effet d’ondulation)
# Licence : MIT
# Objet : Afficher le drapeau belge (noir-jaune-rouge)
# avec un effet d’ondulation animée
# ============================================================
import time
import math
from RPiKeyboardConfig import RPiKeyboardConfig
# ---- Couleurs HSV de base (H, S, V) ----
NOIR = (0, 0, 20) # noir légèrement lumineux
JAUNE = (60, 255, 255) # jaune vif
ROUGE = (0, 255, 255) # rouge vif
# ---- Paramètres de l’effet ----
FREQUENCE_VAGUE = 0.25 # vitesse de la vague (Hz)
AMPLITUDE = 0.4 # amplitude de variation (0–1)
PERIODE = 0.05 # délai entre images (s)
def initialiser_clavier():
clavier = RPiKeyboardConfig()
clavier.set_led_direct_effect()
return clavier
def lire_matrice_leds(clavier):
return clavier.get_leds()
def bornes_colonnes(leds):
cols = [led.matrix[1] for led in leds]
return min(cols), max(cols)
def couleur_base_pour_colonne(col, col_min, col_max):
"""Retourne la couleur de base selon la colonne."""
etendue = col_max - col_min + 1
tiers = etendue / 3.0
idx = (col - col_min) + 0.0001
if idx < tiers:
return NOIR
elif idx < 2 * tiers:
return JAUNE
else:
return ROUGE
def appliquer_ondulation(clavier, duree=None, luminosite=255):
"""Affiche le drapeau belge avec un effet d’ondulation."""
leds = lire_matrice_leds(clavier)
col_min, col_max = bornes_colonnes(leds)
t0 = time.time()
clavier.set_brightness(int(luminosite))
print("🇧🇪 Drapeau belge ondulant... (Ctrl+C pour arrêter)")
try:
while True:
t = time.time() - t0
for led in leds:
col, row = led.matrix[1], led.matrix[0]
h, s, v = couleur_base_pour_colonne(col, col_min, col_max)
# Variation sinusoïdale selon la ligne et le temps
onde = (math.sin(2 * math.pi * FREQUENCE_VAGUE * t + row / 3.0)
* AMPLITUDE + 1 - AMPLITUDE)
v_mod = int(v * onde)
clavier.set_led_by_idx(idx=led.idx, colour=(h, s, v_mod))
clavier.send_leds()
time.sleep(PERIODE)
if duree and (t > duree):
break
except KeyboardInterrupt:
print("\n🛑 Animation arrêtée par l’utilisateur.")
# Éteindre doucement les LED
for led in leds:
clavier.set_led_by_idx(idx=led.idx, colour=(0, 0, 0))
clavier.send_leds()
def main():
luminosite_globale = 255
duree_animation = None # ou en secondes (ex: 30 pour 30s)
clavier = initialiser_clavier()
appliquer_ondulation(clavier, duree=duree_animation, luminosite=luminosite_globale)
if __name__ == "__main__":
main()