User Tools

Site Tools


script_timers

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
script_timers [2016/12/01 15:14]
be.ing
script_timers [2016/12/01 15:37] (current)
be.ing
Line 12: Line 12:
 (Note the escaped quotes in the target function call.) (Note the escaped quotes in the target function call.)
 <code javascript>​ <code javascript>​
- +if (engine.beginTimer(1000,"​MyController.lightUp(0x3A,​\"​red\"​)",​true) == 0) { 
-... +    print("​LightUp timer setup failed"​);​ 
-    ​if (engine.beginTimer(1000,"​MyController.lightUp(0x3A,​\"​red\"​)",​true) == 0) { +}
-        print("​LightUp timer setup failed"​);​ +
-    } +
-...+
  
 MyController.lightUp = function (led,color) { MyController.lightUp = function (led,color) {
Line 55: Line 52:
    //​Sets our variable to zero    //​Sets our variable to zero
    ​MyController.MyTimer = 0;    ​MyController.MyTimer = 0;
-   +
    //​begin timer     //​begin timer 
    ​MyController.MyTimer = engine.beginTimer(250,"​MyController.flash()"​);​    ​MyController.MyTimer = engine.beginTimer(250,"​MyController.flash()"​);​
-   +
    if (MyController.MyTimer === 0) {    if (MyController.MyTimer === 0) {
       print("​The timer is not running or failed to start"​);​       print("​The timer is not running or failed to start"​);​
    } else {    } else {
-      print("​My timer is running and its ID is : " + MyController.MyTimer);​+      print("​My timer is running and its ID is: " + MyController.MyTimer);​
    }    }
 </​code>​ </​code>​
Line 69: Line 66:
   * Automatically,​ when the timer has ran out, the callback function is then called by the scripting engine.   * Automatically,​ when the timer has ran out, the callback function is then called by the scripting engine.
   * Manually, by using the **engine.stopTimer()** function   * Manually, by using the **engine.stopTimer()** function
-In both cases the value of your timer variable is **not** reset to zero, and if you don't do it in your code, you will never be able to know if your timer is still running or not ! This can be a problem if you try a to stop a timer wich is already stopped and do no longer exist (it generates warnings internally and that can be seen in your terminal window in debug mode (if you have started mixxx from the command line : see [[http://​www.mixxx.org/​wiki/​doku.php/​command_line_options|Command line options]]).+In both cases the value of your timer variable is **not** reset to zero, and if you don't do it in your code, you will never be able to know if your timer is still running or not ! This can be a problem if you try a to stop a timer wich is already stopped and do no longer exist (it generates warnings internally and that can be seen in your terminal window in debug mode (if you have started mixxx from the command line: see [[http://​www.mixxx.org/​wiki/​doku.php/​command_line_options|Command line options]]).
  
-Example 1 of bad use :+Example 1 of bad use:
 <code javascript>​ <code javascript>​
-   //​begin a timer   ​+   //​begin a timer
    ​MyController.MyTimer = engine.beginTimer(250,"​MyController.ACallback()",​true);​    ​MyController.MyTimer = engine.beginTimer(250,"​MyController.ACallback()",​true);​
-   ...+
    //​Later on in your code    //​Later on in your code
-   //​begin timer : you start a timer, but maybe the previous is still in use+   //​begin timer: you start a timer, but maybe the previous is still in use
    ​MyController.MyTimer = engine.beginTimer(100,"​MyController.flash()"​); ​      ​MyController.MyTimer = engine.beginTimer(100,"​MyController.flash()"​); ​  
 </​code>​ </​code>​
Line 86: Line 83:
    ​MyController.MyTimer = engine.beginTimer(250,"​MyController.flash()"​);​    ​MyController.MyTimer = engine.beginTimer(250,"​MyController.flash()"​);​
    //​Let'​s say it has started successfully,​ MyController.MyTimer has now a value different from 0.    //​Let'​s say it has started successfully,​ MyController.MyTimer has now a value different from 0.
-    + 
-   //​stop timer : +   //​stop timer: ​
    ​engine.stopTimer(MyController.MyTimer); ​    ​engine.stopTimer(MyController.MyTimer); ​
    //​MyController.MyTimer is still different from 0.    //​MyController.MyTimer is still different from 0.
-   +
    //​stop it again    //​stop it again
    //the timer represented by the MyController.MyTimer value does no longer exist, ​    //the timer represented by the MyController.MyTimer value does no longer exist, ​
Line 97: Line 94:
 </​code>​ </​code>​
  
-How to fix this ?+How to fix this?
   * The first reflex is to set the variable ​ associated to the timer to zero, so that elsewhere if needed in your code you know if the timer is runnig and exist (value different from 0) or not.    * The first reflex is to set the variable ​ associated to the timer to zero, so that elsewhere if needed in your code you know if the timer is runnig and exist (value different from 0) or not. 
     * The variable must be set to zero in the callback function associated to a "​one-shot"​ timer.     * The variable must be set to zero in the callback function associated to a "​one-shot"​ timer.
Line 108: Line 105:
     //​Initialize your timer variable to zero at the beginning of the script     //​Initialize your timer variable to zero at the beginning of the script
     MyController.Timer = 0;     MyController.Timer = 0;
-     +
-    ... +
-    ​+
     MyController.MyButton = function(channel,​ control, value, status, group) {     MyController.MyButton = function(channel,​ control, value, status, group) {
         if (value===0x7F) {         if (value===0x7F) {
             //Button pressed, let's light up a LED             //Button pressed, let's light up a LED
             midi.sendShortMsg(0x90,​ 0x3A, 0x7F);             midi.sendShortMsg(0x90,​ 0x3A, 0x7F);
-            ​ 
         } else {         } else {
             //Button released, let's turn off the LED within 5 seconds.             //Button released, let's turn off the LED within 5 seconds.
-            //For this, we will use a "one shot" timer"  +            //For this, we will use a "one shot" timer"​ 
-            +
             //1) test any pending timer              //1) test any pending timer 
             if (MyController.MyTimer !==0) {              if (MyController.MyTimer !==0) { 
Line 129: Line 123:
                ​MyController.Timer = 0;                ​MyController.Timer = 0;
              }              }
-             +
              //2) safely start your timer :              //2) safely start your timer :
              ​MyController.Timer = engine.beginTimer(250,"​MyController.FlashOff()",​true);​              ​MyController.Timer = engine.beginTimer(250,"​MyController.FlashOff()",​true);​
Line 142: Line 136:
 </​code>​ </​code>​
  
-Example 2 : We want to implement a button in such a way that if we press it loads the selected track, and if we keep it longer, it ejects the track, for this, we need a "one shot" timer.+Example 2: We want to implement a button in such a way that if we press it loads the selected track, and if we keep it longer, it ejects the track, for this, we need a "one shot" timer.
 <code javascript>​ <code javascript>​
-    // Part that handles the behaviour of the Load button :  +    // Part that handles the behaviour of the Load button:  
-    // Long press (>500 ms) : eject the track +    // Long press (>500 ms): eject the track 
-    // Quick press : Load the selected track+    // Quick press: Load the selected track
     // *****     // *****
-    ​+
     // Last Time the LOAD Btn was pressed before released     // Last Time the LOAD Btn was pressed before released
     MyController.LOADlongpress = false;     MyController.LOADlongpress = false;
Line 175: Line 169:
     };     };
     MyController.LoadBtn = function(channel,​ control, value, status, group) {     MyController.LoadBtn = function(channel,​ control, value, status, group) {
- //LOAD hold <500ms : load track, >500ms : eject +    ​//LOAD hold <500ms: load track, >500ms: eject 
- if (value == DOWN) { +    if (value == DOWN) { 
-     ​MyController.LOADdown();​ +        MyController.LOADdown();​ 
-     ​} else { +        } else { 
- MyController.LOADup(group);​ +        MyController.LOADup(group);​ 
-     ​}+        }
     };     };
 </​code>​ </​code>​
  
-Example 3 of good usage : This example is much more complex. With one button we want to start a led flashing, and with a second button, we want to stop that flashing led. For this, we need two timers, one permanent, one "one shot".+Example 3 of good usage: This example is much more complex. With one button we want to start a led flashing, and with a second button, we want to stop that flashing led. For this, we need two timers, one permanent, one "one shot".
 <code javascript>​ <code javascript>​
-    ​//​Initialize your timer variable to zero at the beginning of the script +//​Initialize your timer variable to zero at the beginning of the script 
- MyController.flashTimer = 0; +MyController.flashTimer = 0; 
- MyController.flashOnceTimer = 0; +MyController.flashOnceTimer = 0; 
- MyController.num_ms_on = 0;+MyController.num_ms_on = 0;
  
- // make a light flashing +// make a light flashing 
- //​---------------------- +//​---------------------- 
- // num_ms_on : number of ms the light should stay enlighted when blinking +// num_ms_on: number of ms the light should stay enlighted when blinking 
- // num_ms_off : number of ms the light should be switched off when blinking +// num_ms_off: number of ms the light should be switched off when blinking 
- MyController.flashOn = function(num_ms_on,​ num_ms_off) { +MyController.flashOn = function(num_ms_on,​ num_ms_off) { 
- //stop pending timers +    //stop pending timers 
- MyController.flashOff();​+    MyController.flashOff();​
  
- // init +    ​// init 
- MyController.num_ms_on = num_ms_on;+    MyController.num_ms_on = num_ms_on;
  
- // 1st flash +    ​// 1st flash 
- // This is because the permanent timer below takes  +    // This is because the permanent timer below takes  
- // num_ms_on milisecs before first flash. +    // num_ms_on milisecs before first flash. 
- MyController.flashOnceOn();​+    MyController.flashOnceOn();​
  
- // flashcount =0 means permanent flash, +    ​// flashcount =0 means permanent flash, 
- // flashcount>​0 , means temporary flash, first flash already done, +    // flashcount>​0 , means temporary flash, first flash already done, 
- // so we don't need this part  if flashcount=1 +    // so we don't need this part  if flashcount=1 
- // permanent timer +    // permanent timer 
- MyController.flashTimer = engine.beginTimer((num_ms_on + num_ms_off),​ "​MyController.flashOnceOn()"​);​ +    MyController.flashTimer = engine.beginTimer((num_ms_on + num_ms_off),​ "​MyController.flashOnceOn()"​);​ 
- };+};
  
  
- // stops the light flashing +// stops the light flashing 
- MyController.FlashOff = function() { +MyController.FlashOff = function() { 
- // stop pending flashing effects now +    // stop pending flashing effects now 
- if (MyController.flashTimer !== 0) { +    if (MyController.flashTimer !== 0) { 
- engine.stopTimer(MyController.flashTimer);​ +        engine.stopTimer(MyController.flashTimer);​ 
- MyController.flashTimer = 0; +        MyController.flashTimer = 0; 
- MyController.num_ms_on = 0; +        MyController.num_ms_on = 0; 
- }+    }
  
- if (MyController.flashOnceTimer !== 0) { +    ​if (MyController.flashOnceTimer !== 0) { 
- engine.stopTimer(MyController.flashOnceTimer);​ +        engine.stopTimer(MyController.flashOnceTimer);​ 
- MyController.flashOnceTimer = 0; +        MyController.flashOnceTimer = 0; 
- }+    }
  
- //Turn off the LED +    ​//Turn off the LED 
- midi.sendShortMsg(0x90,​ 0x3A, 0x00); +    midi.sendShortMsg(0x90,​ 0x3A, 0x00); 
- };+};
  
- // Call back function (called in flashon() ) +// Call back function (called in flashon() ) 
- MyController.flashOnceOn = function() { +MyController.flashOnceOn = function() { 
- //Light up the LED +    //Light up the LED 
- midi.sendShortMsg(0x90,​ 0x3A, 0x7F); +    midi.sendShortMsg(0x90,​ 0x3A, 0x7F); 
- MyController.flashOnceTimer = engine.beginTimer(MyController.flashDuration,​ "​MyController.flashOnceOff()",​ true); +    MyController.flashOnceTimer = engine.beginTimer(MyController.flashDuration,​ "​MyController.flashOnceOff()",​ true); 
- };+};
  
- // Call back function (called in flashOnceOn() ) +// Call back function (called in flashOnceOn() ) 
- MyController.flashOnceOff = function() { +MyController.flashOnceOff = function() { 
- MyController.flashOnceTimer = 0;+    MyController.flashOnceTimer = 0;
  
- //Turn off the LED +    ​//Turn off the LED 
- midi.sendShortMsg(0x90,​ 0x3A, 0x00); +    midi.sendShortMsg(0x90,​ 0x3A, 0x00); 
- };+};
  
 MyController.MyButtonFlashOn = function(channel,​ control, value, status, group) { MyController.MyButtonFlashOn = function(channel,​ control, value, status, group) {
script_timers.txt · Last modified: 2016/12/01 15:37 by be.ing