update sounds, killall aplay

This commit is contained in:
Kai Mosebach 2022-08-13 17:50:01 +01:00
parent 2e5225bfac
commit 2e26e628df
2 changed files with 55 additions and 73 deletions

128
game.js
View File

@ -37,8 +37,8 @@ const statusLED = 40;
const failSwitch = 35;
// warte auf spielstart wenn auf HIGH
const startButton = 32;
const startButton_GPIO = 12;
//const startButton = 32;
//const startButton_GPIO = 12;
// beende spiel wenn auf HIGH
// const finishButton = 31;
const finishButton = 22;
@ -48,15 +48,20 @@ const finishButton_GPIO = 25;
const failButton = 21;
const failButton_GPIO = 9;
// wechsle kind/erwachen Modus + LED
const toggleButton = 33;
const toggleButton_GPIO = 13;
//const toggleButton = 33;
//const toggleButton_GPIO = 13;
const startButton = 33;
const startButton_GPIO = 13;
// wird je nach modus gesetzt und abgespielt wenn start
const backgroundGrown = "jaws.wav";
const jeopardy = "jeopardy.wav";
const northsouth = "northsouth.wav";
const backgroundChild = "northsouth.wav";
const backgroundSounds = [ "jaws.wav", "jeopardy.wav", "northsouth.wav" ];
// wird zufallig zu zufaelligen momenten abgespielt (erw mode)
const erschreckSounds = [ "ziege.wav", "scream.wav", "laugh.wav", "cavallerie.wav", "gulp.wav" ];
const erschreckSounds = [ "ziege.wav", "scream.wav", "laugh.wav", "cavallerie.wav", "gulp.wav", "ziege.wav" ];
// wird nach fester zeit + random oder spaeter nach der besten zeit abgespielt (erw mode)
// const stresserSound = "sharks.wav";
// wird bei fail abgespielt (zonk)
@ -67,6 +72,8 @@ const finishSound = "kidscheering.wav";
const GROWN=1;
const CHILD=2;
let failTimer;
let successTimer;
// LOW EDGE
let preStart = true;
@ -159,9 +166,9 @@ function setup() {
gpio.setup(finishButton, gpio.DIR_IN, gpio.EDGE_FALLING, () => {
exec(`raspi-gpio set ${finishButton_GPIO} pu`);
});
gpio.setup(toggleButton, gpio.DIR_IN, gpio.EDGE_FALLING, () => {
exec(`raspi-gpio set ${toggleButton_GPIO} pu`);
});
//gpio.setup(toggleButton, gpio.DIR_IN, gpio.EDGE_FALLING, () => {
// exec(`raspi-gpio set ${toggleButton_GPIO} pu`);
//});
gpio.setup(startButton, gpio.DIR_IN, gpio.EDGE_BOTH, () => {
exec(`raspi-gpio set ${startButton_GPIO} pu`);
});
@ -184,9 +191,9 @@ function setup() {
case failButton:
fail(value);
break;
case toggleButton:
toggle(value);
break;
//case toggleButton:
// toggle(value);
// break;
default:
GlobalLog.warn('Alert! Unknown button change',channel);
}
@ -207,13 +214,13 @@ function gameTimer() {
gpio.output(statusLED, GAME.led);
}
function failTimer() {
function failTimerFunc() {
GAME.led = !GAME.led;
gpio.output(statusLED, GAME.led);
gpio.output(childLED, GAME.led);
gpio.output(erwLED, GAME.led);
}
function successTimer() {
function successTimerFunc() {
GAME.led = !GAME.led;
gpio.output(statusLED, GAME.led);
gpio.output(childLED, !GAME.led);
@ -225,61 +232,43 @@ function getRandomInt(max) {
}
function start(value) {
if(preStart === false || isRunning) {
GlobalLog.note('Prestart detect.');
preStart = true;
if(GAME.startplayer) GAME.startplayer.stop();
if(GAME.failTimer) clearInterval(GAME.failTimer);
if(GAME.successTimer) clearInterval(GAME.successTimer);
if(GAME.countTimer) clearInterval(GAME.countTimer)
if(player) player.stop();
if(MODE === GROWN) {
gpio.output(childLED, false);
gpio.output(erwLED, true);
} else {
gpio.output(childLED, true);
gpio.output(erwLED, false);
}
if(preStart === false) return;
}
if(GAME.startplayer) GAME.startplayer.stop();
if(failTimer) failTimer = clearInterval(failTimer);
if(successTimer) successTimer = clearInterval(successTimer);
if(GAME.countTimer) countTimer = clearInterval(GAME.countTimer)
exec(`killall aplay`);
if(isRunning === true) {
GlobalLog.note('Game reset.');
if(GAME.startplayer) GAME.startplayer.stop();
if(player) player.stop();
STATE = STATES.FINISH;
clearInterval(GAME.countTimer)
preStart = false;
isRunning = false;
if(GAME.failTimer) clearInterval(GAME.failTimer);
if(GAME.successTimer) clearInterval(GAME.successTimer);
gpio.output(statusLED, true);
GAME = {
gpio.output(childLED, false);
gpio.output(erwLED, true);
if(GAME.startplayer) GAME.startplayer.stop();
GlobalLog.note('Game reset.');
}
GAME = {
events: [],
runTime: 0
}
}
if(GAME.failTimer) clearInterval(GAME.failTimer);
if(GAME.successTimer) clearInterval(GAME.successTimer);
if(MODE === GROWN) {
gpio.output(erwLED, true);
} else {
gpio.output(childLED, true);
}
GAME.runTime = 0;
STATE = STATES.FINISH;
if(STATE === STATES.FINISH || STATE === STATES.FAIL) {
STATE = STATES.STARTED;
// set all params for next game, then start counter and event timer
if(MODE === GROWN) {
if(getRandomInt(7) > 2) {
GAME.events.push({ sound: jeopardy, start: 1});
} else {
GAME.events.push({ sound: backgroundGrown, start: 1});
}
GAME.events.push({ sound: erschreckSounds[getRandomInt(5)], start: 15+getRandomInt(10) });
GAME.events.push({ sound: erschreckSounds[getRandomInt(5)], start: 30+getRandomInt(10) });
GAME.events.push({ sound: erschreckSounds[getRandomInt(5)], start: 50+getRandomInt(10) });
GAME.events.push({ sound: backgroundSounds[getRandomInt(3)], start: 1}); //, start: 15+getRandomInt(10) });
// if(getRandomInt(10) > 7) {
// GAME.events.push({ sound: jeopardy, start: 1});
// } else {
// if(getRandomInt(10) > 7) {
// GAME.events.push({ sound: backgroundGrown, start: 1});
// } else {
// GAME.events.push({ sound: northsouth, start: 1});
// }
// }
GAME.events.push({ sound: erschreckSounds[getRandomInt(6)], start: 15+getRandomInt(10) });
GAME.events.push({ sound: erschreckSounds[getRandomInt(6)], start: 30+getRandomInt(10) });
GAME.events.push({ sound: erschreckSounds[getRandomInt(6)], start: 50+getRandomInt(10) });
GAME.events.push({ sound: erschreckSounds[getRandomInt(6)], start: 70+getRandomInt(10) });
GAME.mode = GROWN;
GAME.fail = failSound;
GAME.finish = finishSound;
@ -312,7 +301,7 @@ function finish(value) {
playSound(GAME.finish);
STATE = STATES.FINISH;
clearInterval(GAME.countTimer)
GAME.successTimer = setInterval(failTimer, 600);
successTimer = setInterval(successTimerFunc, 800);
GlobalLog.note('Getting new image!')
exec(`fswebcam -r 1024x768 /tmp/last.png`, (err) => {
let rt = GAME.runTime;
@ -325,7 +314,7 @@ function finish(value) {
if(MODE === GROWN) {
if(GAME.runTime < best.grown) {
best.grown = rt;
msg = `Gewinner in nur ${rt} Sekunden!\nNeue Bestzeit bei den Erwachsenen!`;
msg = `Gewinner in nur ${rt} Sekunden!\nNeue Bestzeit!`;
fs.copyFile('/tmp/last.png', '/tmp/bestgrown.png', (err) => {});
} else {
msg = `Gewinner in nur ${rt} Sekunden!`;
@ -345,10 +334,6 @@ function finish(value) {
});
preStart = false;
isRunning = false;
GAME = {
events: [],
runTime: 0
}
} else {
GlobalLog.warn('FINISH not in state', STATE);
}
@ -358,19 +343,16 @@ function finish(value) {
function fail(value) {
if(STATE === STATES.STARTED) {
GlobalLog.note('You failed after', GAME.runTime, 'seconds');
GAME.startplayer.stop();
player.stop();
if(GAME.startplayer) GAME.startplayer.stop();
if(player) player.stop();
playSound(GAME.fail);
STATE = STATES.FAIL;
clearInterval(GAME.countTimer)
GAME.failTimer = setInterval(failTimer, 100);
failTimer = setInterval(failTimerFunc, 100);
GAME.events = [];
preStart = false;
isRunning = false;
GAME = {
events: [],
runTime: 0
}
exec(`sleep 2; killall aplay`);
} else {
GlobalLog.debug('FAIL not in state', STATE);
}
@ -378,8 +360,8 @@ function fail(value) {
function toggle(value) {
if(STATE === STATES.FINISH || STATE === STATES.FAIL) {
if(GAME.failTimer) clearInterval(GAME.failTimer);
if(GAME.successTimer) clearInterval(GAME.successTimer);
if(failTimer) clearInterval(failTimer);
if(successTimer) clearInterval(successTimer);
if(MODE === GROWN) {
gpio.output(erwLED, true);
} else {

Binary file not shown.