Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5213

Français • Re: gpio c++

$
0
0
salut as tous :

donc j'ai fais cela , pour des essais et tous fonctionne bien :
pour la lecture :

Code:

# include <string.h># include <stdlib.h># include <stdio.h>int decod_ligne (char * buffer, float *t1, float *t2, float *t3, float *t4, float *t5) {  char * saveptr;  printf (" <%s>\n",  buffer);  const char * delimiters = " ,;\n\t";  char * token = strtok_r (buffer, delimiters, &saveptr);  printf ("token: <%s> ", token);  * t1 = atof (token);  token = strtok_r (NULL, delimiters, &saveptr);  printf ("token: <%s> ", token);  * t2 = atof (token);  token = strtok_r (NULL, delimiters, &saveptr);  printf ("token: <%s> ", token);  * t3 = atof (token);  token = strtok_r (NULL, delimiters, &saveptr);  printf ("token: <%s> ", token);  * t4 = atof (token);  token = strtok_r (NULL, delimiters, &saveptr);  printf ("token: <%s>\n", token);  * t5 = atof (token);//      token = strtok (NULL, ",;\n\t");//      printf ("tok: <%s>val: %f \n", token,  atof (token) );//      token = strtok (NULL, ",;\n\t");//      printf ("tok: <%s>val: %f \n", token,  atof (token) );  return 1;}int decod_fic (const char * nomFic, float *t1, float *t2, float *t3, float *t4, float *t5) {  char buffer[88];  FILE * handle = fopen (nomFic, "r");  if (NULL !=  fgets (buffer, sizeof (buffer), handle) ) {    return decod_ligne (buffer, t1, t2, t3, t4, t5) ;    printf ("On lit \n%s\n", buffer);    fclose (handle);    return(0);  } else {    printf ("erreur open %s\n", nomFic);    return -1;  }}int main() {    float t1, t2, t3, t4, t5;  int codret = decod_fic ( (char *) "/home/ludo/gpiod_cpp/testFichier/ecrit/temperaturesLue", &t1,  &t2, &t3, &t4, &t5);  printf ("t1:%5.2f t2:%5.2f t3:%5.2f t4:%5.2f t5:%5.2f\n", t1, t2, t3, t4, t5);  return codret;}
pour l'ecriture:

Code:

#include <string>#include <algorithm>#include <iostream>#include <fstream>#include <iomanip>#include "gpioPin.hpp"#include <unistd.h>#include <signal.h>#include <math.h>#include <array>#include "BB_DS18B20.hpp"#include <vector>#include <ctime>#include <stdio.h>using namespace std;BB_DS18B20 * ds18b20;std::vector < unsigned long long> ds_ID; //vector contenant l'ID des ds18b20std::vector < double > ds_temperature; //vector qui va contenir les températures des ds18b20void loadDSConfig (string filename, std::vector < unsigned long long > & array) {  unsigned long long ds_ID;  stringstream ss;  ifstream file (filename);  if (file.is_open() ) {    string line;    while (getline (file, line) ) {      // enleve espace      line.erase (remove (line.begin(), line.end(), ' '), line.end() );      //  avons-nous au moins 17 caracteres      if (line.size() == 17) {        // ok avons-nous 28-        if (line.substr (0, 3) == "28-") {          stringstream ss (line.substr (3, -1) );          ss >> hex >> ds_ID;          ds_ID = (ds_ID << 8) | 0x28;          array.push_back (            ds_ID); // valide donc insère ce capteur dans la matrice vector ds_ID        }      }    }    file.close();  }}void lireDS18B20() {  // partir une  conversion  ds18b20 -> GlobalStartConversion();  // lire les capteurs  // l'indentification des DS18B20  sont dans ds_ID  for (uint loop = 0; loop < ds_ID.size(); loop++) {    if (ds18b20 -> ReadSensor (ds_ID[loop]) ) {      // valeur valide      ds_temperature[loop] = ds18b20 -> temperature;    } else      ds_temperature[loop] = -9999.9;  }}void ecritTemps(const char * nomFic, float tempExtLue, float tempUnitExtLue, float tempEcExtLue, float tempUnitIntLue, float tempEcIntLue) {FILE * handle =fopen(nomFic, "w");fprintf(handle, "%5.2f, %5.2f, %5.2f, %5.2f, %5.2f\n", tempExtLue, tempUnitExtLue, tempEcExtLue, tempUnitIntLue, tempEcIntLue);//  impression de controleprintf("On ecrit \n%5.2f, %5.2f, %5.2f, %5.2f, %5.2f\n", tempExtLue, tempUnitExtLue, tempEcExtLue, tempUnitIntLue, tempEcIntLue); // a supprimer si on est satisfait        fclose(handle);}void my_ctrl_c_handler (int s) {  delete ds18b20;  release_gpiod();  exit (0);}int main (void) {    int DS_PIN = 4;  pinMode (DS_PIN, OPENDRAIN_PULLUP);  ds18b20 = new BB_DS18B20 (gpioline[DS_PIN]);  // charge info sur les ds18b20  loadDSConfig ("DS18B20.conf", ds_ID);  // charge info sur les ds18b20  char  ficConf[654] = "DS18B20.conf";  FILE * confHan = fopen(ficConf, "r");  if (NULL == confHan) {    std::cout << "Le fichier de configuration \n" << ficConf << "\n doit exister\n";    return (111);  }  fclose(confHan);  loadDSConfig("DS18B20.conf", ds_ID); // protégé contre le cas où il est absent   std::cout << "\n configure\n";  if (ds_ID.size() < 2) {    std::cout << "\nVous devez avoir au moins un thermomètre\n";    return(112);  }   // créer  le vecteur contenant la température des DS18b20  for (uint loop = 0; loop < ds_ID.size(); loop++) {    ds_temperature.push_back (-9999.9); //  enregistre une information invalide pour commencer  }        while (1) {        time_t rawtime;    time ( & rawtime);    cout << "date, heure, annee -> " << ctime ( & rawtime) << endl;    lireDS18B20();    ecritTemps("temperaturesLue", (int (ds_temperature[0] * 2) ) / 2.0, (int (ds_temperature[1] * 2) ) / 2.0, (int (ds_temperature[2] * 2) ) / 2.0, (int (ds_temperature[3] * 2) ) / 2.0, (int (ds_temperature[4] * 2) ) / 2.0);    cout << "ds_temperature[0] // temperatureExt = " << (int (ds_temperature[0] * 2) ) / 2.0 << " C " << endl; // sonde NORD    cout << "ds_temperature[1] // temperatureUniteExt = " << (int (ds_temperature[1] * 2) ) / 2.0 << " C " << endl;    cout << "ds_temperature[2] // temperatureEchangeurExt = " << (int (ds_temperature[2] * 2) ) / 2.0 << " C " << endl;    cout << "ds_temperature[3] // temperatureUniteInt = " << (int (ds_temperature[3] * 2) ) / 2.0 << " C " << endl;    cout << "ds_temperature[4] // temperatureEchangeurInt = " << (int (ds_temperature[4] * 2) ) / 2.0 << " C " << endl;    signal (SIGINT, my_ctrl_c_handler);  }  return 0;}

Statistics: Posted by ludoraspberry — Sun Mar 03, 2024 5:59 am



Viewing all articles
Browse latest Browse all 5213

Trending Articles