Haiku Basic Scripting help with Bridge API sample

jmorris644

Active Member
Here is a copy of the sample that is in the API document:
 
Adding special actions to UPB, ALC, compose switches
 
var swHist = Array(); // Initialize array, put at top of script
 
function onUPBSwitch(swtch, unit) { // Callback called by haiku when a unit’s switch is pressed
  // Is it the second time the switch was pressed in a row?
     if(swHist[unit] != null && swHist[unit] == swtch) {
        if(swtch == 1) { // Lets check if the on switch was pressed twice
        // Take the alert line out if you don’t want the pause in between because
        // HaikuHelper will wait for the alert to be dismissed before continuing
       alert('unit ' + unit + ' was tapped ON a second time!');
       controller.unitWithNumber(unit).off(); // Lets do something special
      }
      swHist[unit] = null; // Clear to prevent repeated second press behavior
   } else {
      swHist[unit] = swtch; // Store the switch that was pressed on the unit
  }
}
 
I was expecting that with this code I could turn a specific switch on 2 times in a row and it would execute the action. It does. However, if I turn the switch on, then use another switch, then turn the 1st switch on again it still activates the code.
 
This isn't really the desired action. At least not for me. If I use a different switch in-between the actions of the first switch I would want the array for the first switch to go to null.
 
So, other than adding a loop to Nullify all of the non-activated switches, is there any other way to achieve the function that I am looking for? It seems that a loop is rather cludgy as the total number of switches can change which means I would have to keep the loop count manually updated.
 
I hope this made some sense :)
 
Joe
 
If it's a UPB switch, you can designate double-tap actions right on the switch itself using UpStart.
 
OK... yeah this script is kind of strange.  I assume lupinglade will provide some insight, but it looks like he array will always store the first hit on each switch.... so the second time it's hit regardless of what's in between it will fire.
 
If you want to ignore it if another light is hit in between, the script could actually be much easier.  However... you will have to have the discipline to hit the switch once, wait for it to report status over the wire to the panel, and then hit it again.  If you double tap quickly it will just snap the light on to full and not report the second press.  Is that an acceptable situation for you?
 
What you're looking to do is a bit different from what this script does.

What you can do I think is store a timestamp (Date) along with each switch. And if too much time has elapsed,ignore the second press.

Example:
Code:
// Initialize array, put at top of script
var unitHistory = Array(); 
 
// Callback called by haiku when a unit’s switch is pressed
function onUPBSwitch(action, unit) {
  // Is it the second time the switch was pressed in a row within 15 seconds?
  var now = new Date().getTime();

  if(unitHistory[unit] && unitHistory[unit].action == action && (Math.abs(unitHistory[unit].timestamp-now)/1000) < 15) {
    // Lets check if the on switch was pressed twice
    if(action == 1) {
      // Take the alert line out if you don’t want the pause in between because
      // HaikuHelper will wait for the alert to be dismissed before continuing
       alert('unit ' + unit + ' was tapped ON a second time!');
      controller.unitWithNumber(unit).off(); // Lets do something special (turn off the same unit)
    }
    // Clear to prevent repeated second press behavior (optional)
    unitHistory[unit] = null; 
  } else {
    // Store the switch that was pressed on the unit along with the timestamp
    unitHistory[unit] = { action: action, timestamp: new Date().getTime() };
  }
}
 
lupinglade said:
What you're looking to do is a bit different from what this script does.

What you can do I think is store a timestamp (Date) along with each switch. And if too much time has elapsed,ignore the second press.

Example:

// Initialize array, put at top of script
var unitHistory = Array();

// Callback called by haiku when a unit’s switch is pressed
function onUPBSwitch(action, unit) {
// Is it the second time the switch was pressed in a row within 15 seconds?
var now = new Date().getTime();

if(unitHistory[unit] && unitHistory[unit].action == action && (Math.abs(unitHistory[unit].timestamp-now)/1000) < 15) {
// Lets check if the on switch was pressed twice
if(action == 1) {
// Take the alert line out if you don’t want the pause in between because
// HaikuHelper will wait for the alert to be dismissed before continuing
alert('unit ' + unit + ' was tapped ON a second time!');
controller.unitWithNumber(unit).off(); // Lets do something special (turn off the same unit)
}
// Clear to prevent repeated second press behavior (optional)
unitHistory[unit] = null;
} else {
// Store the switch that was pressed on the unit along with the timestamp
unitHistory[unit] = { action: action, timestamp: new Date().getTime() };
}
}
Yes, That will work great. Thanks. But it makes me wonder, what is the practical use for the original script? Just curious as I may wish to use it as is. :)
 
Joe
 
The original script lets you use the second button press to toggle things in the room, for example it could toggle between scenes.
 
Back
Top