Bonjour avec node js je fais sonner un buzzer, j'ai créer une fonction sonne() , mais je n'arrive pas à trouver la bonne solution pour qu'elle soit réactiver indéfinement une fois que cette fonction est terminé ? voici le code:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 
 
let Gpio = require('onoff').Gpio; // Gpio class
let button = new Gpio(23, 'in', 'rising', { debounceTimeout: 500 });
let BUZ = new Gpio(18, 'out'); //On instancie un objet qui va utiliser la broche du processeur qui se nomme GPIO4
 
 
 
function sonne(){
        console.log('Attente sonnerie ...');
 
        let sonne = button.watch((err, value) => {
                if (err) {
                    throw err;
                }
                console.log('Appel commander le buzzer ... , sortie du GPIO 23:' + value);
                let blinkInterval = setInterval(blinkBUZ, 0.5); //on active la fonction setInterval toute les 500 millisecondes
 
                function blinkBUZ() { //function to start blinking
                    if (BUZ.readSync() === 0) { //check the pin state, if the state is 0 (or off)
                        BUZ.writeSync(1); //set pin state to 1 (turn BUZ on)
                    } else {
                        BUZ.writeSync(0); //set pin state to 0 (turn BUZ off)
                    }
 
                }
 
                function endBuz() { //function to stop blinking
                    clearInterval(blinkInterval); // Stop blink intervals
                    BUZ.writeSync(0); // Turn BUZ off
                    BUZ.unexport(); // Unexport GPIO to free resources
                    return this;
                }
                setTimeout(endBuz, 3000); //stop blinking after 15 seconds
                button.unexport();
            });
    };
 
    sonne();
j'ai tenter un return this , mais cela ne fonctionne pas .
merci de vos réponses

edit, j'ai trouvé il fallais juste fair eun calback

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
function start(callback){
  //... some work ... 
  if(work_finished){
    callback();
  }
}
 
function run() {
    start(function(){
        console.log('END');
        run();
    });
}