/*
============================================
code is placed under the MIT license
Copyright (c) 2024 J-M-L & NETERASER
For the Arduino Forum :
Merci à J-M-L pour le code de chainage des matrices et de la "logique" de gestion des MAX 7219
et de son support super sympa...
Code de gestion du chronometrage des cycles de tir sportif pour l'entrainement.
ce code inclut le fichier SYMBOLE.h qui contient tous les symboles et chiffres du sketch.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
06 Octobre 2024 - J-M-L & NETERASER ( C )
===============================================
*/
#include <SPI.h>
#include "SYMBOLES.h"
// Relais
#define RELAIS 5
// Buzzer
#define BUZZER 3 //buzzer pin
#define DELAI1 150000
#define DELAI2 20000
#define DELAI3 10000
// Définition des constantes pour les broches et le nombre de segments
constexpr byte clkPin = 13;
constexpr byte dataPin = 11;
constexpr byte csPin = 10;
constexpr byte nbModulesX = 4; // 32 pixels de large (4 segments de 8 pixels)
constexpr byte nbModulesY = 3; // 24 pixels de haut (3 segments de 8 pixels)
const byte tps1Pin = 6;
const byte tps2Pin = 7;
const byte tps3Pin = 8;
// ============== ajout sélection tempo modifiable
// suite valeur tempo
//int tempo = 0;
// ============== FIN ajout sélection tempo modifiable
// Un framebuffer pour stocker l'état de toute la matrice de LED
// Chaque octet représente une colonne de 8 pixels (dans un module)
uint8_t frameBuffer[nbModulesX][nbModulesY * 8]; // Taille 32x24
// Fonction pour envoyer des données à un MAX7219 spécifique
void envoiSPI(byte segment, byte adresse, byte donnees) {
digitalWrite(csPin, LOW);
for (byte i = 0; i < nbModulesX * nbModulesY; i++) {
if (i == segment) {
SPI.transfer(adresse); // Envoyer l'adresse et les données pour le bon segment
SPI.transfer(donnees);
} else {
SPI.transfer(0); // Ne rien envoyer aux autres segments
SPI.transfer(0);
}
}
digitalWrite(csPin, HIGH);
}
// Fonction pour allumer ou éteindre un pixel aux coordonnées (x, y)
void definirPixel(int x, int y, bool alllume) {
if (x < 0 || x >= nbModulesX * 8 || y < 0 || y >= nbModulesY * 8) return; // Vérifie les limites
// Calcul du segment (bloc 8x8) et de la position dans le segment
int moduleX = x / 8; // module horizontal (bloc de 8x8 pixels)
int colonneDansModule = x % 8; // Colonne dans le module
int ligneSegment = y; // Ligne dans la grille
// Allume ou éteint le bit correspondant dans le framebuffer
if (alllume) {
frameBuffer[moduleX][ligneSegment] |= (1 << (7 - colonneDansModule)); // on met le bit à 1
} else {
frameBuffer[moduleX][ligneSegment] &= ~(1 << (7 - colonneDansModule)); // on met le bit à 0
}
}
// Fonction pour envoyer le framebuffer aux MAX7219
void afficher() {
for (byte ligne = 0; ligne < 8; ligne++) { // Pour chaque ligne dans un module
for (byte moduleX = 0; moduleX < nbModulesX; moduleX++) { // Parcourt chaque segment horizontal
for (byte segmentY = 0; segmentY < nbModulesY; segmentY++) { // Parcourt chaque segment vertical
byte colonne = frameBuffer[moduleX][ligne + (segmentY * 8)];
envoiSPI(moduleX + segmentY * nbModulesX, 8 - ligne, colonne);
}
}
}
}
void effacer() {
memset(frameBuffer, 0, sizeof frameBuffer);
afficher(); // met à jour l'affichage
}
void chargerBitmap8x8(const byte bitmap[], int positionX, int positionY) {
for (int ligne = 0; ligne < 8; ligne++) {
byte octet = pgm_read_byte_near(bitmap + (7 - ligne)); // Inverser les lignes (pour corriger la tête en bas)
for (int colonne = 0; colonne < 8; colonne++) {
bool alllume = (octet & (1 << (7 - colonne))) != 0; // Lire les bits de droite à gauche pour corriger le flip horizontal
definirPixel(positionX + colonne, positionY + ligne, alllume); // Définir le pixel dans le framebuffer
}
}
afficher(); // Mettre à jour l'affichage
}
void afficherBitmap8x8(const byte bitmap[], int positionX, int positionY) {
chargerBitmap8x8(bitmap, positionX, positionY);
afficher(); // Mettre à jour l'affichage
}
void setup() {
Serial.begin(115200);
pinMode(clkPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(csPin, OUTPUT);
SPI.beginTransaction(SPISettings(9800000, MSBFIRST, SPI_MODE0));
pinMode(RELAIS, OUTPUT);
pinMode(BUZZER, OUTPUT);
pinMode(tps1Pin, INPUT_PULLUP);
pinMode(tps2Pin, INPUT_PULLUP);
pinMode(tps3Pin, INPUT_PULLUP);
// Configuration initiale pour chaque MAX7219
for (byte i = 0; i < nbModulesX * nbModulesY; i++) {
envoiSPI(i, 0x0f, 0x00); // registre de test - mode test désactivé
envoiSPI(i, 0x0b, 0x07); // registre de limite de scan - affiche les chiffres 0 à 7
envoiSPI(i, 0x0c, 0x01); // registre de mise en marche - fonctionnement normal
envoiSPI(i, 0x0a, 0x0f); // registre d'intensité - luminosité maximale
envoiSPI(i, 0x09, 0x00); // registre de mode de décodeur - pas de décodeur
}
// exemple d'affichage
//===============================X
// Buzzer alerte de compte à rebours
digitalWrite(BUZZER, HIGH); // Buzzer alerte de DEBUT de cycle
delay(200);
digitalWrite(BUZZER, LOW); // Buzzer DEBUT alerte de compte à rebours
delay(200);
digitalWrite(BUZZER, HIGH); // Buzzer alerte de DEBUT de cycle
delay(200);
digitalWrite(BUZZER, LOW); // Buzzer DEBUT alerte de compte à rebours
delay(200);
digitalWrite(BUZZER, HIGH); // Buzzer DEBUT alerte de compte à rebours
delay(200);
digitalWrite(BUZZER, LOW); // Buzzer DEBUT alerte de compte à rebours
delay(1000);
//================================X
// on parcourt chaque pixel de la matrice
for (int y = 0; y < nbModulesY * 8; y++) {
for (int x = 0; x < nbModulesX * 8; x++) {
definirPixel(x, y, true); // Allume le pixel
}
afficher(); // Met à jour l'affichage
}
for (int x = 0; x < nbModulesX * 8; x++) {
effacer();
}
// Pause préparation
// barre BLEUE UP avec symbole SOUSTRACTION
afficherBitmap8x8(SYMBOLE_SOUSTRACTION, 0, 16);
afficherBitmap8x8(SYMBOLE_SOUSTRACTION, 8, 16);
afficherBitmap8x8(SYMBOLE_SOUSTRACTION, 16, 16);
afficherBitmap8x8(SYMBOLE_SOUSTRACTION, 24, 16);
// Pause préparation
delay(3000);
effacer();
// barre VERTE UP
afficherBitmap8x8(SYMBOLE_PLEIN, 0, 0);
afficherBitmap8x8(SYMBOLE_PLEIN, 8, 0);
afficherBitmap8x8(SYMBOLE_PLEIN, 16, 0);
afficherBitmap8x8(SYMBOLE_PLEIN, 24, 0);
// affichage le compte à rebours des 10 chiffres de 9 à 0
afficherBitmap8x8(CHIFFRE_9, 0, 16);
delay(1000);
afficherBitmap8x8(CHIFFRE_8, 0, 16);
delay(1000);
afficherBitmap8x8(CHIFFRE_7, 0, 16);
delay(1000);
afficherBitmap8x8(CHIFFRE_6, 0, 16);
afficherBitmap8x8(SYMBOLE_TIERS, 8, 16);
afficherBitmap8x8(SYMBOLE_TIERS, 16, 16);
afficherBitmap8x8(SYMBOLE_TIERS, 24, 16);
delay(1000);
afficherBitmap8x8(CHIFFRE_5, 0, 16);
delay(1000);
afficherBitmap8x8(CHIFFRE_4, 0, 16);
delay(1000);
afficherBitmap8x8(CHIFFRE_3, 0, 16);
afficherBitmap8x8(SYMBOLE_2TIERS, 8, 16);
afficherBitmap8x8(SYMBOLE_2TIERS, 16, 16);
afficherBitmap8x8(SYMBOLE_2TIERS, 24, 16);
digitalWrite(BUZZER, HIGH); // Buzzer alerte de compte à rebours
delay(1000);
afficherBitmap8x8(CHIFFRE_2, 0, 16);
digitalWrite(BUZZER, HIGH); // Buzzer alerte de compte à rebours
delay(1000);
afficherBitmap8x8(CHIFFRE_1, 0, 16);
delay(1000);
afficherBitmap8x8(CHIFFRE_0, 0, 16);
afficherBitmap8x8(SYMBOLE_3TIERS, 8, 16);
afficherBitmap8x8(SYMBOLE_3TIERS, 16, 16);
afficherBitmap8x8(SYMBOLE_3TIERS, 24, 16);
digitalWrite(BUZZER, HIGH); // Buzzer alerte de compte à rebours
// barre VERTE down
afficherBitmap8x8(SYMBOLE_OFF, 0, 0);
afficherBitmap8x8(SYMBOLE_OFF, 8, 0);
afficherBitmap8x8(SYMBOLE_OFF, 16, 0);
afficherBitmap8x8(SYMBOLE_OFF, 24, 0);
// barre rouge et relais UP
digitalWrite(RELAIS, HIGH); // relais pour utilisation accessoire extérieur ou signal lumineux
digitalWrite(BUZZER, LOW); // Buzzer FIN alerte de compte à rebours
afficherBitmap8x8(SYMBOLE_PLEIN, 0, 8);
afficherBitmap8x8(SYMBOLE_PLEIN, 8, 8);
afficherBitmap8x8(SYMBOLE_PLEIN, 16, 8);
afficherBitmap8x8(SYMBOLE_PLEIN, 24, 8);
// barre compte a rebours DOWN
afficherBitmap8x8(SYMBOLE_OFF, 0, 16);
afficherBitmap8x8(SYMBOLE_OFF, 8, 16);
afficherBitmap8x8(SYMBOLE_OFF, 16, 16);
afficherBitmap8x8(SYMBOLE_OFF, 24, 16);
//=============================================================================
// temporisation durée du cycle de tir à modifier en fonction de la discipline
// temporisation à mofifier : de 150 secondes à 10 secondes suivant sélection
// un bouton selectionné sera lu à LOW
//=============================================================================
if (digitalRead(tps1Pin) == LOW) delay(DELAI1);
else if (digitalRead(tps2Pin) == LOW) delay(DELAI2);
else if (digitalRead(tps3Pin) == LOW) delay(DELAI3);
else delay(DELAI3); // délai par défaut
//=============================================================================
// ========================
digitalWrite(RELAIS, LOW);
effacer();
//==============================X
// barre VERTE UP
afficherBitmap8x8(SYMBOLE_PLEIN, 0, 0);
afficherBitmap8x8(SYMBOLE_PLEIN, 8, 0);
afficherBitmap8x8(SYMBOLE_PLEIN, 16, 0);
afficherBitmap8x8(SYMBOLE_PLEIN, 24, 0);
//===============================X
// Buzzer alerte de compte à rebours
digitalWrite(BUZZER, HIGH); // Buzzer alerte de fin de cycle
delay(200);
digitalWrite(BUZZER, LOW); // Buzzer FIN alerte de compte à rebours
delay(200);
digitalWrite(BUZZER, HIGH); // Buzzer alerte de fin de cycle
delay(200);
digitalWrite(BUZZER, LOW); // Buzzer FIN alerte de compte à rebours
delay(200);
digitalWrite(BUZZER, HIGH); // Buzzer alerte de fin de cycle
delay(200);
digitalWrite(BUZZER, LOW); // Buzzer FIN alerte de compte à rebours
//================================X
// temporisation sortie du cycle
delay(5000);
//=================================X
for (int x = 0; x < nbModulesX * 8; x++) {
effacer();
chargerBitmap8x8(SYMBOLE_VERTICAL, x, 0);
chargerBitmap8x8(SYMBOLE_VERTICAL, x, 8);
chargerBitmap8x8(SYMBOLE_VERTICAL, x, 16);
chargerBitmap8x8(SYMBOLE_VERTICAL, x, 24);
afficher();
delay(5); // Délai entre chaque déplacement
digitalWrite(BUZZER, LOW); // Buzzer FIN alerte de compte à rebours
}
for (int x = 0; x < nbModulesX * 8; x++) {
effacer();
chargerBitmap8x8(SYMBOLE_SOUSTRACTION, x, 0);
chargerBitmap8x8(SYMBOLE_SOUSTRACTION, x, 8);
chargerBitmap8x8(SYMBOLE_SOUSTRACTION, x, 16);
chargerBitmap8x8(SYMBOLE_SOUSTRACTION, x, 24);
afficher();
delay(5); // Délai entre chaque déplacement
}
}
void loop() {}