Ajout messages DEBUG + .h
This commit is contained in:
+32
-101
@@ -3,46 +3,9 @@
|
||||
#include <NmraDcc.h>
|
||||
#include <EEPROM.h>
|
||||
|
||||
// Affectation des PINs Arduino
|
||||
#define DCC_SIGNAL_PIN 2 // Pin pour le signal DCC
|
||||
#define LED_PIN 3 // Pin pour la LED de signalisation
|
||||
#define PWM_PIN 5 // Pin pour le signal PWM du moteur des pales
|
||||
#define SERVO_PIN 9 // Pin pour le signal du servo moteur
|
||||
|
||||
// Valeurs des CV (Configuration Variables)
|
||||
#define CV_ADDRESS_LSB 513
|
||||
#define CV_ADDRESS_MSB 521
|
||||
#define CV_PALES_SPEED 100
|
||||
#define CV_MAX_UP 101
|
||||
#define CV_MAX_DOWN 102
|
||||
#define CV_RESET 512 // CV pour réinitialiser les valeurs par défaut (standard NMRA DCC)
|
||||
|
||||
// Valeurs par défaut des CV
|
||||
#define DEFAULT_PALES_SPEED 255 // Vitesse par défaut du moteur des pales (0-255)
|
||||
#define DEFAULT_MAX_UP 180 // Position maximale par défaut pour le servo en haut (0-180)
|
||||
#define DEFAULT_MAX_DOWN 0 // Position maximale par défaut pour le servo en bas (0-180)
|
||||
#define DEFAULT_ADDRESS 1 // Adresse DCC par défaut du décodeur d'accessoire
|
||||
#define VITESSE 40 // Temps en millisecondes entre chaque pas de servo
|
||||
|
||||
#define EEPROM_SIG_ADDR 0 // Position dans l'EEPROM pour stocker la signature de validation de la structure de configuration
|
||||
#define EEPROM_SIG_VALUE 0xA5 // Valeur de signature pour vérifier la validité de la structure de configuration dans l'EEPROM
|
||||
#define DIY_MANUFACTURER_ID 200 // Non utilisé actuellement d'après la spécification NMRA DCC,
|
||||
// mais peut être utilisé pour identifier le fabricant du décodeur.
|
||||
#define DIY_MODEL_ID 1 // Carte pilote Helicoptère, non utilisé actuellement d'après la spécification NMRA DCC,
|
||||
// mais peut être utilisé pour identifier le modèle du décodeur.
|
||||
|
||||
#define DEBUG 1
|
||||
|
||||
#ifdef DEBUG
|
||||
#define DEBUG_INITIALIZE_SERIAL Serial.begin(115200)
|
||||
#define DEBUG_PRINT(x) Serial.print(x)
|
||||
#define DEBUG_PRINTLN(x) Serial.println(x)
|
||||
#else
|
||||
#define DEBUG_INITIALIZE_SERIAL
|
||||
#define DEBUG_PRINT(x)
|
||||
#define DEBUG_PRINTLN(x)
|
||||
#endif
|
||||
#include "main.h"
|
||||
|
||||
// Variables globales
|
||||
struct DCCAccessoryConfig {
|
||||
byte Sig; // Signature pour vérifier la validité de la structure (0x45)
|
||||
byte ManufacturerID; // Manufacturer ID (0-255)
|
||||
@@ -52,19 +15,14 @@ struct DCCAccessoryConfig {
|
||||
byte MotorSpeed; // Speed of the motor (0-255)
|
||||
byte ServoMaxUp; // Maximum position for servo up (0-180)
|
||||
byte ServoMaxDown; // Maximum position for servo down (0-180)
|
||||
} MyConfig;
|
||||
} MyConfig; // Structure de configuration pour stocker les valeurs des CV dans l'EEPROM
|
||||
|
||||
|
||||
NmraDcc Dcc;
|
||||
Servo servoMotor;
|
||||
uint8_t PalesSpeed = DEFAULT_PALES_SPEED;
|
||||
uint8_t ServoMaxUp = DEFAULT_MAX_UP;
|
||||
uint8_t ServoMaxDown = DEFAULT_MAX_DOWN;
|
||||
uint16_t currentAddress = DEFAULT_ADDRESS;
|
||||
int STROBE_ON = 0;
|
||||
int HelicoUP = 0;
|
||||
int currentServoPos = 90;
|
||||
int targetServoPos = 90;
|
||||
NmraDcc Dcc; // Instance du décodeur DCC pour gérer les signaux DCC et les CV.
|
||||
Servo servoMotor; // Instance du servo moteur pour contrôler la position du servo.
|
||||
int StrobeState = 0; // Strobe state for LED signaling (0 = off, 1 = on)
|
||||
int HelicoUP = 0; // Helico position state (0 = down, 1 = up)
|
||||
int currentServoPos = 90; // Current position of the servo (0-180)
|
||||
int targetServoPos = 90; // Target position of the servo (0-180)
|
||||
unsigned long servoNextMillis = 0;
|
||||
|
||||
/**
|
||||
@@ -72,6 +30,7 @@ unsigned long servoNextMillis = 0;
|
||||
*/
|
||||
void notifyCVAck(void)
|
||||
{
|
||||
DEBUG_PRINTLN("CV Acknowledged");
|
||||
digitalWrite(LED_PIN, HIGH);
|
||||
digitalWrite(LED_BUILTIN, HIGH);
|
||||
delay(6);
|
||||
@@ -87,25 +46,30 @@ void notifyCVAck(void)
|
||||
*/
|
||||
void notifyCVChange(uint16_t CV, uint8_t Value)
|
||||
{
|
||||
DEBUG_PRINTLN("CV Change: CV=" + String(CV) + ", Value=" + String(Value));
|
||||
if (CV == CV_PALES_SPEED)
|
||||
{
|
||||
// Taux du PWM de 0 à 255 pour la vitesse du moteur des pales
|
||||
MyConfig.MotorSpeed = Value;
|
||||
DEBUG_PRINTLN("MotorSpeed updated to: " + String(MyConfig.MotorSpeed));
|
||||
} else if (CV == CV_MAX_UP)
|
||||
{
|
||||
// La valeur maximale pour la position du servo en haut (0-180)
|
||||
if (Value > 180) Value = 180; // Limite la valeur à 180
|
||||
MyConfig.ServoMaxUp = Value;
|
||||
DEBUG_PRINTLN("ServoMaxUp updated to: " + String(MyConfig.ServoMaxUp));
|
||||
} else if (CV == CV_MAX_DOWN) {
|
||||
// La valeur maximale pour la position du servo en bas (0-180)
|
||||
if (Value > 180) Value = 180; // Limite la valeur à 180
|
||||
MyConfig.ServoMaxDown = Value;
|
||||
MyConfig.ServoMaxDown = Value;
|
||||
DEBUG_PRINTLN("ServoMaxDown updated to: " + String(MyConfig.ServoMaxDown));
|
||||
} else if (CV == CV_ADDRESS_LSB || CV == CV_ACCESSORY_DECODER_ADDRESS_LSB) {
|
||||
MyConfig.address = (MyConfig.address & 0xFF00) | Value;
|
||||
} else if (CV == CV_ADDRESS_MSB || CV == CV_ACCESSORY_DECODER_ADDRESS_MSB) {
|
||||
MyConfig.address = (MyConfig.address & 0x00FF) | ((Value & 0x07) << 8);
|
||||
} else if (CV == CV_RESET && Value == 8) {
|
||||
// Réinitialiser les valeurs par défaut
|
||||
DEBUG_PRINTLN("Resetting to default configuration...");
|
||||
initDefaultConfig();
|
||||
}
|
||||
// Sauver les nouvelles valeurs dans l'EEPROM
|
||||
@@ -124,9 +88,11 @@ uint8_t notifyCVValid (uint16_t CV, uint8_t Writable)
|
||||
if (CV == CV_PALES_SPEED || CV == CV_MAX_UP || CV == CV_MAX_DOWN ||
|
||||
CV == CV_ADDRESS_LSB || CV == CV_ADDRESS_MSB ||
|
||||
CV == CV_RESET) {
|
||||
DEBUG_PRINTLN("CV " + String(CV) + " is valid and writable.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
DEBUG_PRINTLN("CV " + String(CV) + " is not valid or writable.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -146,6 +112,7 @@ void initDefaultConfig()
|
||||
*/
|
||||
void notifyCVResetFactoryDefault()
|
||||
{
|
||||
DEBUG_PRINTLN("Resetting to default configuration...");
|
||||
Dcc.setCV(CV_PALES_SPEED, DEFAULT_PALES_SPEED);
|
||||
Dcc.setCV(CV_MAX_UP, DEFAULT_MAX_UP);
|
||||
Dcc.setCV(CV_MAX_DOWN, DEFAULT_MAX_DOWN);
|
||||
@@ -165,6 +132,7 @@ void loadCv()
|
||||
bool configValid = EEPROM.read(EEPROM_SIG_ADDR) == EEPROM_SIG_VALUE;
|
||||
|
||||
if (!configValid) {
|
||||
DEBUG_PRINTLN("EEPROM signature not found, checking for stored configuration...");
|
||||
DCCAccessoryConfig storedConfig;
|
||||
EEPROM.get(1, storedConfig);
|
||||
if (storedConfig.Sig == EEPROM_SIG_VALUE) {
|
||||
@@ -199,44 +167,6 @@ void loadCv()
|
||||
Dcc.setCV(CV_ACCESSORY_DECODER_ADDRESS_MSB, (MyConfig.address >> 8) & 0x07);
|
||||
}
|
||||
|
||||
/**
|
||||
* Charge les valeurs des CV depuis le bus DCC.
|
||||
*/
|
||||
void loadCvFromDcc()
|
||||
{
|
||||
PalesSpeed = Dcc.getCV(CV_PALES_SPEED);
|
||||
if (PalesSpeed == 0) {
|
||||
PalesSpeed = DEFAULT_PALES_SPEED;
|
||||
}
|
||||
|
||||
ServoMaxUp = Dcc.getCV(CV_MAX_UP);
|
||||
if (ServoMaxUp == 0) {
|
||||
ServoMaxUp = DEFAULT_MAX_UP;
|
||||
}
|
||||
|
||||
ServoMaxDown = Dcc.getCV(CV_MAX_DOWN);
|
||||
if (ServoMaxDown == 0) {
|
||||
ServoMaxDown = DEFAULT_MAX_DOWN;
|
||||
}
|
||||
|
||||
uint8_t addrLo = Dcc.getCV(CV_ADDRESS_LSB);
|
||||
uint8_t addrHi = Dcc.getCV(CV_ADDRESS_MSB) & 0x07;
|
||||
uint16_t address = ((uint16_t)addrHi << 8) | addrLo;
|
||||
if (address == 0) {
|
||||
addrLo = Dcc.getCV(CV_ACCESSORY_DECODER_ADDRESS_LSB);
|
||||
addrHi = Dcc.getCV(CV_ACCESSORY_DECODER_ADDRESS_MSB) & 0x07;
|
||||
address = ((uint16_t)addrHi << 8) | addrLo;
|
||||
}
|
||||
if (address == 0) {
|
||||
address = DEFAULT_ADDRESS;
|
||||
}
|
||||
currentAddress = address;
|
||||
|
||||
Dcc.setCV(CV_ACCESSORY_DECODER_ADDRESS_LSB, addrLo);
|
||||
Dcc.setCV(CV_ACCESSORY_DECODER_ADDRESS_MSB, addrHi);
|
||||
Dcc.setCV(CV_ADDRESS_LSB, addrLo);
|
||||
Dcc.setCV(CV_ADDRESS_MSB, addrHi);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notifie la sortie d'une voie d'accès DCC.
|
||||
@@ -247,12 +177,15 @@ void loadCvFromDcc()
|
||||
void notifyDccAccTurnoutOutput(uint16_t Addr, uint8_t Direction, uint8_t OutputPower)
|
||||
{
|
||||
if (Addr == 1) {
|
||||
STROBE_ON = Direction;
|
||||
DEBUG_PRINTLN("StrobeState updated to: " + String(Direction));
|
||||
StrobeState = Direction;
|
||||
} else if (Addr == 2) {
|
||||
analogWrite(PWM_PIN, Direction ? PalesSpeed : 0);
|
||||
DEBUG_PRINTLN("MotorSpeed updated to: " + String(Direction ? MyConfig.MotorSpeed : 0));
|
||||
analogWrite(PWM_PIN, Direction ? MyConfig.MotorSpeed : 0);
|
||||
} else if (Addr == 3) {
|
||||
DEBUG_PRINTLN("HelicoUP updated to: " + String(Direction));
|
||||
HelicoUP = Direction;
|
||||
targetServoPos = Direction ? ServoMaxUp : ServoMaxDown;
|
||||
targetServoPos = Direction ? MyConfig.ServoMaxUp : MyConfig.ServoMaxDown;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -261,10 +194,10 @@ void notifyDccAccTurnoutOutput(uint16_t Addr, uint8_t Direction, uint8_t OutputP
|
||||
* @param Addr L'adresse de la sortie.
|
||||
* @param State L'état de la sortie.
|
||||
*/
|
||||
void notifyDccSigOutputState(uint16_t Addr, uint8_t State)
|
||||
{
|
||||
digitalWrite(LED_PIN, State ? HIGH : LOW);
|
||||
}
|
||||
// void notifyDccSigOutputState(uint16_t Addr, uint8_t State)
|
||||
// {
|
||||
// digitalWrite(LED_PIN, State ? HIGH : LOW);
|
||||
// }
|
||||
|
||||
/**
|
||||
* Initialise le système.
|
||||
@@ -281,7 +214,7 @@ void setup()
|
||||
servoMotor.write(90);
|
||||
|
||||
// Initialise les valeurs de la platine
|
||||
STROBE_ON = 1;
|
||||
StrobeState = 1;
|
||||
HelicoUP = 0;
|
||||
currentServoPos = 0;
|
||||
targetServoPos = 180;
|
||||
@@ -296,9 +229,7 @@ void setup()
|
||||
loadCv();
|
||||
// Initialise le décodeur DCC en mode accessoire avec filtrage d'adresse sur l'adresse DCC courante.
|
||||
Dcc.init(MyConfig.ManufacturerID, MyConfig.Version, FLAGS_MY_ADDRESS_ONLY | FLAGS_DCC_ACCESSORY_DECODER | FLAGS_OUTPUT_ADDRESS_MODE, 0);
|
||||
|
||||
|
||||
// loadCvFromDcc();
|
||||
}
|
||||
|
||||
|
||||
@@ -328,7 +259,7 @@ void loop()
|
||||
}
|
||||
// servoMotor.write(90);
|
||||
|
||||
if (STROBE_ON) {
|
||||
if (StrobeState) {
|
||||
switch (strobePhase) {
|
||||
case 0:
|
||||
digitalWrite(LED_PIN, HIGH);
|
||||
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
// Affectation des PINs Arduino
|
||||
#define DCC_SIGNAL_PIN 2 // Pin pour le signal DCC
|
||||
#define LED_PIN 3 // Pin pour la LED de signalisation
|
||||
#define PWM_PIN 5 // Pin pour le signal PWM du moteur des pales
|
||||
#define SERVO_PIN 9 // Pin pour le signal du servo moteur
|
||||
|
||||
// Valeurs des CV (Configuration Variables)
|
||||
#define CV_ADDRESS_LSB 513
|
||||
#define CV_ADDRESS_MSB 521
|
||||
#define CV_PALES_SPEED 100
|
||||
#define CV_MAX_UP 101
|
||||
#define CV_MAX_DOWN 102
|
||||
#define CV_RESET 512 // CV pour réinitialiser les valeurs par défaut (standard NMRA DCC)
|
||||
|
||||
// Valeurs par défaut des CV
|
||||
#define DEFAULT_PALES_SPEED 255 // Vitesse par défaut du moteur des pales (0-255)
|
||||
#define DEFAULT_MAX_UP 180 // Position maximale par défaut pour le servo en haut (0-180)
|
||||
#define DEFAULT_MAX_DOWN 0 // Position maximale par défaut pour le servo en bas (0-180)
|
||||
#define DEFAULT_ADDRESS 1 // Adresse DCC par défaut du décodeur d'accessoire
|
||||
#define VITESSE 40 // Temps en millisecondes entre chaque pas de servo
|
||||
|
||||
#define EEPROM_SIG_ADDR 0 // Position dans l'EEPROM pour stocker la signature de validation de la structure de configuration
|
||||
#define EEPROM_SIG_VALUE 0xA5 // Valeur de signature pour vérifier la validité de la structure de configuration dans l'EEPROM
|
||||
#define DIY_MANUFACTURER_ID 200 // Non utilisé actuellement d'après la spécification NMRA DCC,
|
||||
// mais peut être utilisé pour identifier le fabricant du décodeur.
|
||||
#define DIY_MODEL_ID 1 // Carte pilote Helicoptère, non utilisé actuellement d'après la spécification NMRA DCC,
|
||||
// mais peut être utilisé pour identifier le modèle du décodeur.
|
||||
|
||||
#define DEBUG 1
|
||||
|
||||
#ifdef DEBUG
|
||||
#define DEBUG_INITIALIZE_SERIAL Serial.begin(115200)
|
||||
#define DEBUG_PRINT(x) Serial.print(x)
|
||||
#define DEBUG_PRINTLN(x) Serial.println(x)
|
||||
#else
|
||||
#define DEBUG_INITIALIZE_SERIAL
|
||||
#define DEBUG_PRINT(x)
|
||||
#define DEBUG_PRINTLN(x)
|
||||
#endif
|
||||
Reference in New Issue
Block a user