[Résolu]-Comment rendre la portée d'une structure globale

Bonjour à tous,

j' ai déclarer un modèle de structure avec les structures associées dans un fichier main.h.

Pas de problème au niveau du main.cpp, le programme passe les structures par référence sans souci.
Mais j'ai besoin de lire des données dans un fichier myFct.cpp (pour l'exemple...) et là ça ne fonctionne pas.

J'étais persuadé qu'en déclarant mon modèle dans un fichier d'entête, puis en l'incluant dans les fichiers sources où j'en ai besoin cela fonctionnerais.

main.cpp:

#include "main.h"

int pageid=0; // Voir pour gérer l'absence de réponse de l'écran

void loop(){
    getDht(&DhtVal, &VTAir, &VHAir);  
}

void getDht(struct DhtValue* DhtValY, struct valeur* VTA,struct valeur* VHA)
{
    DhtValY->status=dht.getStatusString();
    DhtValY->temp=dht.getTemperature();
    DhtValY->hum=dht.getHumidity();

    if(DhtVal.temp>VTAir.max) VTA->max=DhtVal.temp;
    if(DhtVal.temp<VTAir.min) VTA->min=DhtVal.temp;

    if(DhtVal.hum>VHAir.max) VHA->max=DhtVal.hum;
    if(DhtVal.hum<VHAir.min) VHA->min=DhtVal.hum;
}

main.h:

#include "myFct/myLog.h"
#include "myFct/myNextion.h"

struct DhtValue{
  const char *status;
  float hum;
  float temp;
}DhtVal;

struct valeur{
  String name="";
  float min;
  float max;
  float current;
}VTAir, VHAir;

myNextion.cpp (myFCT):

#include "myNextion.h"
#include "main.h"

extern int pageid;

void  sendAirToNextion(){
  if(pageid == 0){ //Si on est sur la page d'accueil 
    String command ="t10.txt=\""+String(DhtVal.hum)+"\""; 
    Serial1.print(command);
    endNextionCommand();
    command ="t11.txt=\""+String(VHAir.min)+"\""; 
    Serial1.print(command);
    endNextionCommand();    
    command ="t12.txt=\""+String(VHAir.max)+"\""; 
    Serial1.print(command);
    endNextionCommand();   

    command ="t7.txt=\""+String(DhtVal.temp)+"\""; 
    Serial1.print(command);
    endNextionCommand();
    command = "t8.txt=\""+String(VTAir.min)+"\"";
    Serial1.print(command);
    endNextionCommand();
    command = "t9.txt=\""+String(VTAir.max)+"\"";
    Serial1.print(command);
    endNextionCommand();
  }
  if(pageid==1){
    //if(){

   // }
  }
}

J'ai aussi essayé ça pour "myNextion.cpp":

#include "myNextion.h"
#ifndef h_main_h
#define h_main_h

extern int pageid;

void  sendAirToNextion(){
  if(pageid == 0){ //Si on est sur la page d'accueil 
    String command ="t10.txt=\""+String(DhtVal.hum)+"\""; 
    Serial1.print(command);
    endNextionCommand();
    command ="t11.txt=\""+String(VHAir.min)+"\""; 
    Serial1.print(command);
    endNextionCommand();    
    command ="t12.txt=\""+String(VHAir.max)+"\""; 
    Serial1.print(command);
    endNextionCommand();   

    command ="t7.txt=\""+String(DhtVal.temp)+"\""; 
    Serial1.print(command);
    endNextionCommand();
    command = "t8.txt=\""+String(VTAir.min)+"\"";
    Serial1.print(command);
    endNextionCommand();
    command = "t9.txt=\""+String(VTAir.max)+"\"";
    Serial1.print(command);
    endNextionCommand();
  }
  if(pageid==1){
    //if(){

   // }
  }
}
#endif

Dans tout les cas j'ai des erreurs, soit d'inclusion, ou de porté de structure...

Qu'est-ce qui m’échappe?
Merci d'avance.

ps: la doc sur la portée des structures ou des inclusions multiples sont assez rare et peu détaillé en français.

Salut

Tu ne crois pas qu'en postant les messages d'erreur ce serait plus facile ?

Pas de souci hbachetti,

La version avec "#include "main.h" dans "myNextion.h"

// Partie tronqué car les trois ligne suivantes se répètent plusieurs fois...
         from C:\Users\weetoz\AppData\Local\Temp\arduino_build_926388\sketch\main.h:4,

                 from C:\Users\weetoz\AppData\Local\Temp\arduino_build_926388\sketch\myNextion.h:3,

                 from C:\Users\weetoz\AppData\Local\Temp\arduino_build_926388\sketch\main.h:4,

                 from C:\Users\weetoz\Documents\Arduino\sketch_aug31a\sketch_aug31a.ino:1:

main.h:1: error: #include nested too deeply

 #include <Nextion.h>

                     ^

main.h:4: error: #include nested too deeply

 #include "myNextion.h"

                       ^

Utilisation de la bibliothèque arduino_975481 version 2.2.0 dans le dossier: C:\Users\weetoz\Documents\Arduino\libraries\arduino_975481 
exit status 1
#include nested too deeply

Ici problème d'inclusion. (Du moins pour ce que j'en comprends).

La version avec inclusion ifndef define et endif: (toujours dans le header)

myNextion.cpp:12: error: 'Serial1' was not declared in this scope

     Serial1.print(command);

     ^

myNextion.cpp:12: error: 'command' was not declared in this scope

     Serial1.print(command);

                   ^

myNextion.cpp:14: error: 'VHAir' was not declared in this scope

     command ="t11.txt=\""+String(VHAir.min)+"\"";

Ici, problème de portée.

Pour l'instant, mes fonctions ne renvoient rien et ne prennent pas de paramètres, ce qui, je pense m'autorise à tester les inclusions dans le fichiers source "myNextion.cpp". (même si je pense que ce n'est pas une bonne idée....)

Avec les inclusions commentés dans le header:
Dans le myNextion.cpp avec "#include "main.h" ":

C:\Users\weetoz\AppData\Local\Temp\arduino_build_926388\sketch\sketch_aug31a.ino.cpp.o (symbol from plugin): In function `VHAir':

(.text+0x0): multiple definition of `VHAir'

C:\Users\weetoz\AppData\Local\Temp\arduino_build_926388\sketch\myNextion.cpp.o (symbol from plugin):(.text+0x0): first defined here

C:\Users\weetoz\AppData\Local\Temp\arduino_build_926388\sketch\sketch_aug31a.ino.cpp.o (symbol from plugin): In function `VHAir':

(.text+0x0): multiple definition of `VTAir'

C:\Users\weetoz\AppData\Local\Temp\arduino_build_926388\sketch\myNextion.cpp.o (symbol from plugin):(.text+0x0): first defined here

C:\Users\weetoz\AppData\Local\Temp\arduino_build_926388\sketch\sketch_aug31a.ino.cpp.o (symbol from plugin): In function `VHAir':

(.text+0x0): multiple definition of `DhtVal'

C:\Users\weetoz\AppData\Local\Temp\arduino_build_926388\sketch\myNextion.cpp.o (symbol from plugin):(.text+0x0): first defined here

C:\Users\weetoz\AppData\Local\Temp\arduino_build_926388\sketch\sketch_aug31a.ino.cpp.o (symbol from plugin): In function `VHAir':

(.text+0x0): multiple definition of `HumAir'

C:\Users\weetoz\AppData\Local\Temp\arduino_build_926388\sketch\myNextion.cpp.o (symbol from plugin):(.text+0x0): first defined here

C:\Users\weetoz\AppData\Local\Temp\arduino_build_926388\sketch\sketch_aug31a.ino.cpp.o (symbol from plugin): In function `VHAir':

(.text+0x0): multiple definition of `TempAir'

C:\Users\weetoz\AppData\Local\Temp\arduino_build_926388\sketch\myNextion.cpp.o (symbol from plugin):(.text+0x0): first defined here

collect2.exe: error: ld returned 1 exit status

Utilisation de la bibliothèque arduino_975481 version 2.2.0 dans le dossier: C:\Users\weetoz\Documents\Arduino\libraries\arduino_975481 
exit status 1
Erreur de compilation pour la carte "maniacbug" Mighty 1284p 16MHz using Optiboot

Et avec ifndef...:

C:\Users\weetoz\AppData\Local\Temp\arduino_build_926388\sketch\myNextion.cpp: In function 'void sendAirToNextion()':

myNextion.cpp:12: error: 'String' was not declared in this scope

     String command ="t10.txt=\""+String(DhtVal.hum)+"\""; 

     ^

myNextion.cpp:13: error: 'Serial1' was not declared in this scope

     Serial1.print(command);

     ^

myNextion.cpp:13: error: 'command' was not declared in this scope

     Serial1.print(command);

                   ^

myNextion.cpp:15: error: 'VHAir' was not declared in this scope

     command ="t11.txt=\""+String(VHAir.min)+"\""; 

                                  ^

myNextion.cpp:22: error: 'DhtVal' was not declared in this scope

     command ="t7.txt=\""+String(DhtVal.temp)+"\""; 

                                 ^

myNextion.cpp:25: error: 'VTAir' was not declared in this scope

     command = "t8.txt=\""+String(VTAir.min)+"\"";

                                  ^

C:\Users\weetoz\AppData\Local\Temp\arduino_build_926388\sketch\myNextion.cpp: At global scope:

myNextion.cpp:42: error: 'byte' does not name a type

 byte* readNextNextionMessage() {

 ^

myNextion.cpp:103: error: variable or field 'printMessage' declared void

 void printMessage(byte *message) {

                   ^

myNextion.cpp:103: error: 'byte' was not declared in this scope

myNextion.cpp:103: error: 'message' was not declared in this scope

 void printMessage(byte *message) {

                         ^

myNextion.cpp:121: error: variable or field 'manageMessage' declared void

 void manageMessage(byte* message) {

                    ^

myNextion.cpp:121: error: 'byte' was not declared in this scope

myNextion.cpp:121: error: 'message' was not declared in this scope

 void manageMessage(byte* message) {

                          ^

Utilisation de la bibliothèque arduino_975481 version 2.2.0 dans le dossier: C:\Users\weetoz\Documents\Arduino\libraries\arduino_975481 
exit status 1
'String' was not declared in this scope

...Je mets le code dans le post suivant...

main.ino:

#include "main.h"

Nextion nex(Serial1);
void setup() {
  // put your setup code here, to run once:

}

void loop() {
  getDht(&DhtVal, &VTAir, &VHAir);

}

void getDht(struct DhtValue* DhtValY, struct valeur* VTA,struct valeur* VHA)
{
  DhtValY->status="1";
  DhtValY->temp=2;
  DhtValY->hum=3;
  if(DhtVal.temp>VTAir.max) VTA->max=DhtVal.temp;
  if(DhtVal.temp<VTAir.min) VTA->min=DhtVal.temp;

  if(DhtVal.hum>VHAir.max) VHA->max=DhtVal.hum;
  if(DhtVal.hum<VHAir.min) VHA->min=DhtVal.hum;
}

main.h:

#include <Arduino.h>
#include <Nextion.h>


#include "myNextion.h"


#define DELAY_BETWEEN_DATALOG 900000  
#define DELAY_BETWEEN_MONITOR 10000

void mySetup();
//void printDateTime(const RtcDateTime& dt);
void affectVal(struct parametre*);                         // Valeur à modifier par l'encodeur
void getDht(struct DhtValue* ,struct valeur*, struct valeur*);
void sendToMonitor();
void encAnalyse();



struct DhtValue{
  const char *status;
  float hum;
  float temp;
}DhtVal;

struct parametre{
  byte set;
  byte lowTrig;
  byte highTrig;
  } TempAir, HumAir ;

struct valeur{
  String name="";
  float min;
  float max;
  float current;
}VTAir, VHAir;

myNextion.h:

//#ifndef h_main_h
//#define h_main_h
//#include "main.h"
#ifndef h_Nextion_h
#define h_Nextion_h

#ifndef h_Arduino_h
#define h_Arduino_h


void sendAirToNextion();
void endNextionCommand();

#endif 
#endif 
//#endif

myNextion.cpp:

#include "myNextion.h"
//#include "main.h"
#ifndef h_main_h
#define h_main_h

extern int pageid;

//***************************************        sendToNextion         ******************************
void  sendAirToNextion(){
  if(pageid == 0){ //Si on est sur la page d'accueil 
    String command ="t10.txt=\""+String(DhtVal.hum)+"\""; 
    Serial1.print(command);
    endNextionCommand();
    command ="t11.txt=\""+String(VHAir.min)+"\""; 
    Serial1.print(command);
    endNextionCommand();    
    command ="t12.txt=\""+String(VHAir.max)+"\""; 
    Serial1.print(command);
    endNextionCommand();   

    command ="t7.txt=\""+String(DhtVal.temp)+"\""; 
    Serial1.print(command);
    endNextionCommand();
    command = "t8.txt=\""+String(VTAir.min)+"\"";
    Serial1.print(command);
    endNextionCommand();
    command = "t9.txt=\""+String(VTAir.max)+"\"";
    Serial1.print(command);
    endNextionCommand();
  }
  if(pageid==1){
    //if(){

   // }
  }
}

byte* readNextNextionMessage() {
  byte _byte;
  byte byteBuffer[32];
  int byteIndex = 0;
  int endPatternCount = 0;


  bool lastByteWasEndPattern = false;
 
  Serial.println("Reading message from Nextion ...");
 

  while(true) {

    while (Serial1.available() <= 0) {
      delay(10);
      continue;
    }
 

    _byte = Serial1.read();
    byteBuffer[byteIndex++] = _byte;


  }
 

  return byteBuffer;
}
 

void printMessage(byte *message) {
  // Le byte lu.
  byte nextByte;
  
  int index = 0;
 

  Serial.println();
}
 
void manageMessage(byte* message) {
  Serial.print("Parsing message: ");
  printMessage(message);
 
  int componentID, componentValue;
 
  switch(message[0]) {
    // Le message indique qu'un composant a été activé sur la tablette Nextion.
    case 1:
      // Ici on se contente d'afficher un message.
      Serial.println("Touch event message ... ");
      break;
 

    case 2:

      Serial.print("Numeric value message ... ");
      componentID = (int) message[1];
      componentValue = (int) message[5];
      Serial.print(" ID = ");
      Serial.print(componentID);
      Serial.print("; Value = ");
      Serial.println(componentValue);

      /*
      if (componentID == NETXION_RED_SLIDER_ID)
        redValue = componentValue;
      else if (componentID == NETXION_GREEN_SLIDER_ID)
        greenValue = componentValue;
      else if (componentID == NETXION_BLUE_SLIDER_ID)
        blueValue = componentValue;
      */
      break;
  
    default :
      // Dans ce petit programme, on ne gère aucun des autres messages.
      Serial.print("Unknown message ... First Byte=");
      Serial.println(String(message[0], HEX));
      break;
  }  
}
void endNextionCommand()
{
  Serial1.write(0xff);
  Serial1.write(0xff);
  Serial1.write(0xff);
}

#endif
(.text+0x0): multiple definition of `VHAir'

Ceci est du aux déclarations de tes variables dans un .h

Dans le .h définis simplement les structures et déclare les variables "extern".

struct DhtValue{
  const char *status;
  float hum;
  float temp;
};

struct valeur{
  String name="";
  float min;
  float max;
  float current;
};
extern struct DhtValue DhtVal;
extern struct valeur VTAir, VHAir;

Dans le code .ino ou .cpp :

struct DhtValue DhtVal;
struct valeur VTAir, VHAir;

@+

Merci hbachetti,

c'est nickel une fois les directives d'inclusions dans leurs fichiers respectifs.

Un truc que j'ai mal fais, c'est de mettre:

// Fichier main.h
#ifndef h_main_h
#define h_main_h

// Les structures....

#endif

Alors que comme l'ignorant que je suis je faisais comme ceci:

// Fichier myNextion.h
#ifndef h_main_h
#define h_main_h

#endif

J'suis un gros inculte des directives d'inclusions. :fearful:

Là du coup j'ai compris comment on les utilise...

Et j'avais pas compris non plus pour le mot clé "extern". En fait, si j'ai compris, le modèle de structure ne pouvant être "extern" on déclare une structure (un type donc) qui elle peut-être "extern".

Merci encore une fois pour ton aide hbachetti.