Skip to content

Instantly share code, notes, and snippets.

@jarrodbell
Created February 10, 2012 12:57
Show Gist options
  • Save jarrodbell/1789532 to your computer and use it in GitHub Desktop.
Save jarrodbell/1789532 to your computer and use it in GitHub Desktop.
Toggle power state with feedback
/*
NOTE: Change "System Name" and "Feedback Name" to the correct info for your project first.
NOTE: Change "d1" to the digital join number of your power button.
NOTE: on your power button, set the javascript to: togglePower();
*/
var powerState = 0;
var powerStateRegex = /\x21\x01\x00\x00\x01(\x00|\x01)\x0D/;
function togglePower () {
if (powerState == 1) {
// Turn power off
CF.send("System Name", "\x21\x01\x08\x02\x10\x7C\x0D");
} else {
// Turn power on
CF.send("System Name", "\x21\x01\x08\x02\x10\x7B\x0D");
}
// Request power state for next press
CF.send("System Name", "\x21\x01\x00\x01\xF0\x0D");
}
CF.userMain = function () {
CF.watch(CF.FeedbackMatchedEvent, "System Name", "Feedback Name", function (feedbackName, matchedString) {
// Match the data against regex to capture the power state
var matches = powerStateRegex.exec(matchedString);
if (matches[1] == "\x00") {
powerState = 1;
} else {
powerState = 0;
}
CF.setJoin("d1", powerState);
});
// Request initial power state
CF.send("System Name", "\x21\x01\x00\x01\xF0\x0D");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment