BangleApps/modules/buzz.js

34 lines
1.4 KiB
JavaScript
Raw Normal View History

2022-05-26 20:03:32 +00:00
/**
* Buzz the passed `pattern` out on the internal vibration motor.
*
2022-05-28 21:30:59 +00:00
* A pattern is a sequence of `.`, `,`, `-`, `:`, `;` and `=` where
2022-05-26 20:03:32 +00:00
* - `.` is one short and weak vibration
2022-05-28 21:30:59 +00:00
* - `,` is one medium and weak vibration
2022-05-26 20:03:32 +00:00
* - `-` is one long and weak vibration
2022-05-28 21:30:59 +00:00
* - `:` is one short and strong vibration
* - `;` is one medium and strong vibration
* - `=` is one long and strong vibration
2022-05-26 20:03:32 +00:00
*
* You can use the `buzz_menu` module to display a menu where some common patterns can be chosen.
*
* @param {string} pattern A string like `.-.`, `..=`, `:.:`, `..`, etc.
* @returns a Promise
*/
2022-04-01 12:27:37 +00:00
exports.pattern = pattern => new Promise(resolve => {
2022-05-26 20:03:32 +00:00
function doBuzz() {
if (pattern == "") resolve();
2022-04-01 12:27:37 +00:00
var c = pattern[0];
pattern = pattern.substr(1);
2022-06-06 10:16:41 +00:00
const BUZZ_WEAK = 0.25, BUZZ_STRONG = 1;
const SHORT_MS = 100, MEDIUM_MS = 200, LONG_MS = 500;
2022-05-26 20:03:32 +00:00
if (c == ".") Bangle.buzz(SHORT_MS, BUZZ_WEAK).then(() => setTimeout(doBuzz, 100));
2022-05-28 21:30:59 +00:00
else if (c == ",") Bangle.buzz(MEDIUM_MS, BUZZ_WEAK).then(() => setTimeout(doBuzz, 100));
2022-05-26 20:03:32 +00:00
else if (c == "-") Bangle.buzz(LONG_MS, BUZZ_WEAK).then(() => setTimeout(doBuzz, 100));
else if (c == ":") Bangle.buzz(SHORT_MS, BUZZ_STRONG).then(() => setTimeout(doBuzz, 100));
2022-05-28 21:30:59 +00:00
else if (c == ";") Bangle.buzz(MEDIUM_MS, BUZZ_STRONG).then(() => setTimeout(doBuzz, 100));
2022-05-26 20:03:32 +00:00
else if (c == "=") Bangle.buzz(LONG_MS, BUZZ_STRONG).then(() => setTimeout(doBuzz, 100));
else setTimeout(doBuzz, 100);
2022-04-01 12:27:37 +00:00
}
2022-05-26 20:03:32 +00:00
doBuzz();
2022-04-01 12:27:37 +00:00
});