blink.c 1.48 KB
#include <stdio.h>
#include <wiringPi.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

void add_caract(char word[], char caract)
{
	char str[2];
	str[0] = caract;
	str[1] = '\0';
	strcat(word, str);
}

void read_xml(FILE* fp, char sensor[])
{
  char c;
  int cpt=0;
  while(1)
    {
      c = fgetc(fp);
      if(c=='>') cpt++;
      if(cpt==2) break;
    }
  while(1)
    {
      c = fgetc(fp);
      if(c=='<') break;
      add_caract(sensor, c);
    }
}

bool verif()
{
    FILE* fpv = fopen("verif.txt", "r");
    FILE* fpd = fopen("demande.txt", "r");
    
    if(fpv==NULL || fpd==NULL) return false;
    
    char v[2];
    char d[2];
    
    fgets(v, 3, fpv);
    fgets(d, 3, fpd);
    
    //char cv = fgetc(fpv);
    //char cd = fgetc(fpd);
    
    fclose(fpv);
    fclose(fpd);
    FILE* fp = fopen("verif.txt", "w+");
    
    //add_caract(v, cv);
    //add_caract(d, cd);
    
    int verif = atoi(v);
    int demande = atoi(d);
    
    if(verif!=demande) 
      fputs(d, fp);
    else
      fputs(v, fp);
    
    return(verif!=demande);
}

int pin_num()
{
  FILE* fp = fopen("sensor.xml", "r");
  if(fp==NULL) return EXIT_FAILURE;
  
  char sensor[15] = "";
  char nb[2]="";
  read_xml(fp, sensor);

  // Vérification présence du capteur/pin sur la RPi
  
  add_caract(nb, sensor[strlen(sensor)-1]);
  if(verif())
    return atoi(nb);
  else
    return 4;
}

int main(void)
{
  int pin=pin_num();
  FILE* fp = fopen("stock.txt", "w+");
  fprintf(fp, "%d", pin);
  return 0;
}