Modifications

Sauter à la navigation Sauter à la recherche
669 octets ajoutés ,  4 avril 2019 à 17:17
aucun résumé de modification
Ligne 183 : Ligne 183 :  
{{ADFImage|IR-codecompareok.jpg}}
 
{{ADFImage|IR-codecompareok.jpg}}
   −
This loop, as it goes through each pulse, does a little math. It compares the absolute ('''abs()''') difference between the code we heard and the code we're trying to match abs(oncode - ApplePlaySignal[i*2 + 0]) and then makes sure that the error is less than FUZZINESS percent of the code length (oncode * FUZZINESS / 100)
+
Cette fonction {{fname|loop()}}, est exécutée pour chaque impulsion, fait quelques opérations mathématique. Elle compare la différence absolue ({{fname|abs()}}) entre ce que code réceptionne (IR reçu) et le code auquel nous essayons de le faire correspondre {{fname|abs(oncode - ApplePlaySignal[i*2 + 0])}} et ensuire s'assurer que l'erreur est inférieure FUZZINESS pourcent de la longueur du code
 +
( {{fname| oncode * FUZZINESS / 100}} )
   −
We found we had to tweak the stored values a little to make them match up 100% each time. IR is not a precision-timed protocol so having to make the FUZZINESS 20% or more is not a bad thing
+
Il est généralement nécessaire d'adapter un peu les valeurs stockées pour obtenir 100% correspondance à chaque fois. Le signal infrarouge n'est pas un protocol qui s'appuie sur la précision temporelle (''not a precision-timed protocol''), par conséquent une marge d'erreur de 20% (FUZZINESS=20%) et plus n'est pas une mauvaise chose.
   −
Finally, we can turn the '''loop()''' into its own function which will retunr '''true''' or '''false''' depending on whether it matched the code we ask it to. We also commented out the printing functions
+
Pour finir, Le contenu de {{fname|loop()}} est transformé en sa propre fonction qui retourne '''true''' ou '''false''' en fonction du code que nous luis avons demandé de vérifier. Les lignes {{fname|Serial.print()}} ont également été mises en commentaire.
    
  <nowiki>
 
  <nowiki>
    boolean IRcompare(int numpulses, int Signal[]) {
+
boolean IRcompare(int numpulses, int Signal[]) {
 
     for (int i=0; i< numpulses-1; i++) {
 
     for (int i=0; i< numpulses-1; i++) {
    int oncode = pulses[i][1] * RESOLUTION / 10;
+
        int oncode = pulses[i][1] * RESOLUTION / 10;
    int offcode = pulses[i+1][0] * RESOLUTION / 10;
+
        int offcode = pulses[i+1][0] * RESOLUTION / 10;
    /*
+
 
    Serial.print(oncode); // the ON signal we heard
+
        /*
    Serial.print(" - ");
+
        Serial.print(oncode); // Le signal ON reçu
    Serial.print(Signal[i*2 + 0]); // the ON signal we want
+
        Serial.print(" - ");
    */
+
        Serial.print(Signal[i*2 + 0]); // Le signal ON attendu
    // check to make sure the error is less than FUZZINESS percent
+
        */
    if ( abs(oncode - Signal[i*2 + 0]) <= (Signal[i*2 + 0] * FUZZINESS / 100)) {
+
        // Vérifier que l'erreur est inférieure à FUZZINESS pourcent
    //Serial.print(" (ok)");
+
        if ( abs(oncode - Signal[i*2 + 0]) <= (Signal[i*2 + 0] * FUZZINESS / 100)) {
    } else {
+
            //Serial.print(" (ok)");
    //Serial.print(" (x)");
+
        } else {
    // we didn't match perfectly, return a false match
+
            //Serial.print(" (x)");
     return false;
+
            // Pas de correspondance parfaite, retourner false
 +
            return false;
 +
        }
 +
      
 +
        /*
 +
        Serial.print(" \t"); // tabulation
 +
        Serial.print(offcode); // Le signal OFF reçu
 +
        Serial.print(" - ");
 +
        Serial.print(Signal[i*2 + 1]); // Le signal OFF que nous attendons
 +
        */
 +
        if ( abs(offcode - Signal[i*2 + 1]) <= (Signal[i*2 + 1] * FUZZINESS / 100)) {
 +
          //Serial.print(" (ok)");
 +
        } else {
 +
          //Serial.print(" (x)");
 +
          // Pas de correspondance parfaite, retourner false
 +
          return false;
 +
        }
 +
        //Serial.println();
 
     }
 
     }
     /*
+
     // Tout correspond!
    Serial.print(" \t"); // tab
  −
    Serial.print(offcode); // the OFF signal we heard
  −
    Serial.print(" - ");
  −
    Serial.print(Signal[i*2 + 1]); // the OFF signal we want
  −
    */
  −
    if ( abs(offcode - Signal[i*2 + 1]) <= (Signal[i*2 + 1] * FUZZINESS / 100)) {
  −
    //Serial.print(" (ok)");
  −
    } else {
  −
    //Serial.print(" (x)");
  −
    // we didn't match perfectly, return a false match
  −
    return false;
  −
    }
  −
    //Serial.println();
  −
    }
  −
    // Everything matched!
   
     return true;
 
     return true;
    }
+
}
 
</nowiki>
 
</nowiki>
    +
Ensuite, nous capturons plus de données infrarouge comme 'précédent' (''rewind'') ou 'suivant' (''fastforward'') et plaçons ces informations dans des tableaux stockés dans le fichier {{fname|ircodes.h}} pour éviter que le croquis/sketch principal ne devienne trop long et illisible ([http://github.com/adafruit/IR-Commander vous pouvez obtenir tout le code depuis le GitHib d'AdaFruit)
   −
 
+
A la fin, la boucle principale ressemble à ceci:
We then took more IR command data for the 'rewind' and 'fastforward' buttons and put all the code array data into ircodes.h to keep the main sketch from being too long and unreadable ([http://github.com/adafruit/IR-Commander vous pouvez obtenir tout le code depuis le GitHib d'AdaFruit)
  −
 
  −
Finally, the main loop looks like this:
      
  <nowiki>
 
  <nowiki>
    void loop(void) {
+
void loop(void) {
 
     int numberpulses;
 
     int numberpulses;
 
     numberpulses = listenForIR();
 
     numberpulses = listenForIR();
 +
 
     Serial.print("Heard ");
 
     Serial.print("Heard ");
 
     Serial.print(numberpulses);
 
     Serial.print(numberpulses);
 
     Serial.println("-pulse long IR signal");
 
     Serial.println("-pulse long IR signal");
 
     if (IRcompare(numberpulses, ApplePlaySignal)) {
 
     if (IRcompare(numberpulses, ApplePlaySignal)) {
    Serial.println("PLAY");
+
      Serial.println("PLAY");
 
     }
 
     }
 
     if (IRcompare(numberpulses, AppleRewindSignal)) {
 
     if (IRcompare(numberpulses, AppleRewindSignal)) {
    Serial.println("REWIND");
+
      Serial.println("REWIND");
 
     }
 
     }
 
     if (IRcompare(numberpulses, AppleForwardSignal)) {
 
     if (IRcompare(numberpulses, AppleForwardSignal)) {
    Serial.println("FORWARD");
+
      Serial.println("FORWARD");
    }
   
     }
 
     }
 +
}
 
</nowiki>
 
</nowiki>
    +
Nous vérifions les données réçues par rapport aux codes infrarouges que nous connaissons déjà et affichons un message avec ce que nous avons détecté.
   −
 
+
Voila, une fois nos tests réussis, il ne reste plus qu'a prendre ce code pour en faire quelque-chose de plus utile (en fonction du bouton pressé).
We check against all the codes we know about and print out whenever we get a match. You could now take this code and turn it into something else, like a robot that moves depending on what button is pressed.
  −
 
  −
After testing, success!
      
{{ADFImage|mulitirbutton.jpg}}
 
{{ADFImage|mulitirbutton.jpg}}
    
{{SenseurIR-TRAILER}}
 
{{SenseurIR-TRAILER}}
29 910

modifications

Menu de navigation