ripple-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t...@apache.org
Subject incubator-ripple git commit: RIPPLE-99 Updated emulation for Vibration API
Date Mon, 19 Oct 2015 18:01:38 GMT
Repository: incubator-ripple
Updated Branches:
  refs/heads/master 1dacde265 -> 7578c4713


RIPPLE-99 Updated emulation for Vibration API

The Vibration API has added two new native functions: vibrateWithPattern
and cancelVibration.  The vibrateWithPattern function takes an array of
integers and alternately waits and then shakes by the indicated number of
milliseconds.  The cancelVibration function stops the current vibration
action, either initiated by the old vibrate function or
vibrateWithPattern.

Formally vibrateWithPattern requires the shaking to occur for a specific
period of time, rather than just "N shakes".  It's not critical that we
get that exactly right, but it is necessary that the shaking not go on
too long as that can cause two shaking patterns to run into one another.
Accordingly I changed the estimate that one shake will last 160 ms
rather than 100 ms.

This closes #72


Project: http://git-wip-us.apache.org/repos/asf/incubator-ripple/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ripple/commit/7578c471
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ripple/tree/7578c471
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ripple/diff/7578c471

Branch: refs/heads/master
Commit: 7578c4713ac39a9f04a872a38e83ea2324c23956
Parents: 1dacde2
Author: Julian Horn <julian.c.horn@intel.com>
Authored: Mon Aug 10 10:55:13 2015 -0400
Committer: Tim Barham <tim.barham@microsoft.com>
Committed: Mon Oct 19 11:00:48 2015 -0700

----------------------------------------------------------------------
 .../platform/cordova/3.0.0/bridge/vibration.js  | 65 ++++++++++++++++++++
 lib/client/ui/plugins/goodVibrations.js         |  9 ++-
 2 files changed, 73 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ripple/blob/7578c471/lib/client/platform/cordova/3.0.0/bridge/vibration.js
----------------------------------------------------------------------
diff --git a/lib/client/platform/cordova/3.0.0/bridge/vibration.js b/lib/client/platform/cordova/3.0.0/bridge/vibration.js
index f7bca79..7db2b97 100644
--- a/lib/client/platform/cordova/3.0.0/bridge/vibration.js
+++ b/lib/client/platform/cordova/3.0.0/bridge/vibration.js
@@ -19,10 +19,75 @@
  *
  */
 var goodVibrations = ripple('ui/plugins/goodVibrations');
+var event = ripple('event');
+var timerId = null;
+var pattern = [];
+var repeat = -1;
+
+function cancelVibrationPattern() {
+    // Abort the present animation, if any is active
+    goodVibrations.stop();
+    // Clear any pending timers
+    if (timerId) clearTimeout(timerId);
+    // reinitialize the data structures;
+    timerId = null;
+    pattern = [];
+    repeat = -1;
+}
+
+function patternIsFinished(i) {
+    if (i >= pattern.length) { // no more steps left in this pattern
+        if (repeat > 0) {
+            repeat = repeat - 1; // go on to next iteration of this pattern
+            runNextPatternStep(0);
+        }
+        return true;
+    }
+    return false;
+}
+
+function runNextPatternStep(i) {
+    var ms;
+    if (patternIsFinished(i)) { // bail out if more steps left in this pattern
+        return;
+    }
+    ms = pattern[i];
+    if (ms > 0) goodVibrations.vibrateDevice(ms);
+    // Pattern needs at least two more entries to keep going
+    // (the next entry is just a delay time).
+    i = i + 2;
+    if (patternIsFinished(i)) { // bail out if more steps left in this pattern
+        return;
+    }
+    // Wait for the shaking interval to elapse and the time between
+    // shaking intervals to elapse before running the next pattern step
+    timerId = setTimeout(
+        function() {
+            runNextPatternStep(i);
+        }, pattern[i - 1] + pattern[i - 2]);
+}
 
 module.exports = {
+    // A vibration pattern is an array of microsecond values.
+    // You turn on for array[0] ms, then off for array[1] ms,
+    // then on for array[2] ms and so on.
+    vibrateWithPattern: function (win, fail, args) {
+        if (pattern.length > 0) {
+            cancelVibrationPattern(); // Stop current pattern before starting a new one
+        }
+        pattern = args[0];
+        repeat  = args[1];
+        runNextPatternStep(0);
+    },
+    // Cancel the current pattern (if any)
+    cancelVibration: function (win, fail, args) {
+        cancelVibrationPattern();
+    },
     vibrate: function (win, fail, args) {
         var ms = args[0] || 500;
+        cancelVibrationPattern();
         goodVibrations.vibrateDevice(ms);
     }
 };
+
+event.on("EmulatorUnload", cancelVibrationPattern);

http://git-wip-us.apache.org/repos/asf/incubator-ripple/blob/7578c471/lib/client/ui/plugins/goodVibrations.js
----------------------------------------------------------------------
diff --git a/lib/client/ui/plugins/goodVibrations.js b/lib/client/ui/plugins/goodVibrations.js
index 39b21e8..d21fdb1 100644
--- a/lib/client/ui/plugins/goodVibrations.js
+++ b/lib/client/ui/plugins/goodVibrations.js
@@ -18,12 +18,13 @@
  * under the License.
  *
  */
+
 module.exports = {
     vibrateDevice: function (milliseconds) {
         var node = jQuery("#device-container"),
             x, times;
 
-        times = Math.floor(milliseconds / 100);
+        times = Math.floor(milliseconds / 160);
 
         for (x = 1; x <= times; x++) {
             node.animate({ left: -10 }, 5)
@@ -47,5 +48,11 @@ module.exports = {
             .animate({ left: 25 }, 50)
             .animate({ left: 0 }, 30);
         }
+    },
+    stop: function() {
+        // flush the queue of animation actions
+        var node = jQuery("#device-container");
+        node.clearQueue();
+        node.stop(true, true);
     }
 };


Mime
View raw message