Skip to content

Instantly share code, notes, and snippets.

@sushruth
Last active May 13, 2019 07:23
Show Gist options
  • Save sushruth/573d4087b6a3b5ad83b210416f44a2e8 to your computer and use it in GitHub Desktop.
Save sushruth/573d4087b6a3b5ad83b210416f44a2e8 to your computer and use it in GitHub Desktop.
Pseudo logic for temple doors
// To keep track of timestamps
const timestamps = {
inside: null,
outside: null,
};
// `undefined` could be a third symbol like '5' or something
const insideRegister = [undefined, undefined];
const outsideRegister = [undefined, undefined];
// Following three lines are existing in your code. Check it out.
declare var signalInside;
declare var signalOutside;
declare var mixer;
// For later use
let lastPlayBackTime = null;
function eventProcessorInside(event) {
// Update inside registers
if(signalInside != insideRegister[1]) {
insideRegister.shift();
insideRegister[1] = signalInside;
timestamps.inside = Date.now();
}
perform_the_logic();
}
function eventProcessorOutside(event) {
// Update outside registers
if(signalOutside != outsideRegister[1]) {
outsideRegister.shift();
outsideRegister[1] = signalInside;
timestamps.outside = Date.now();
}
perform_the_logic();
}
function perform_the_logic() {
// Detect rising edge in inside registers.
// Outside registers is irrelevant because timestamp difference is all that matters
let isInsideRisingEdge = insideRegister[0] == 0 && insideRegister[1] == 1;
// If the conditions are correct and inside and outside timestamps are present
if(isInsideRisingEdge && timestamps.inside != null && timestamps.outside != null) {
let timeDiff = timestamps.inside - timestamps.outside;
if(timeDiff > 0) {
// someone is coming inside because
// inside timestamp is later than outside timestamp
timestamps.inside = null;
timestamps.outside = null;
// Make sure nothing is playing before attempting to start
if(!mixer.isPlaying()) {
mixer.startPlaying();
}
// You could use this last playback time to
// see how many seconds have elapsed and if mixer is still playing
// You could take a decision on when to stop playing.
// When playing is stopped reset the value of lastPlayBackTime back to null.
lastPlayBackTime = Date.now();
}
}
}
attachEventListener('insideSensorEvent', eventProcessorInside);
attachEventListener('outsideSensorEvent', eventProcessorOutside);
// To keep track of timestamps
const timestamps = {
inside: null,
outside: null,
};
// `undefined` could be a third symbol like '5' or something
const insideRegister = [undefined, undefined];
const outsideRegister = [undefined, undefined];
// Following three lines are existing in your code. Check it out.
declare var signalInside;
declare var signalOutside;
declare var mixer;
// For later use
let lastPlayBackTime = null;
while(true) {
// Update inside registers
if(signalInside != insideRegister[1]) {
insideRegister.shift();
insideRegister[1] = signalInside;
timestamps.inside = Date.now();
}
// Update outside registers
if(signalOutside != outsideRegister[1]) {
outsideRegister.shift();
outsideRegister[1] = signalInside;
timestamps.outside = Date.now();
}
// Detect rising edge in inside registers.
// Outside registers is irrelevant because timestamp difference is all that matters
let isInsideRisingEdge = insideRegister[0] == 0 && insideRegister[1] == 1;
// If the conditions are correct and inside and outside timestamps are present
if(isInsideRisingEdge && timestamps.inside != null && timestamps.outside != null) {
let timeDiff = timestamps.inside - timestamps.outside;
if(timeDiff > 0) {
// someone is coming inside because
// inside timestamp is later than outside timestamp
timestamps.inside = null;
timestamps.outside = null;
// Make sure nothing is playing before attempting to start
if(!mixer.isPlaying()) {
mixer.startPlaying();
}
// You could use this last playback time to
// see how many seconds have elapsed and if mixer is still playing
// You could take a decision on when to stop playing.
// When playing is stopped reset the value of lastPlayBackTime back to null.
lastPlayBackTime = Date.now();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment