add getSeconds, sound controller, mute/unmute switches

This commit is contained in:
Wolfgang Hottgenroth 2024-04-15 16:23:51 +02:00
parent 53e538b112
commit 2404910870
Signed by: wn
GPG Key ID: 836E9E1192A6B132
5 changed files with 87 additions and 0 deletions

View File

@ -6,6 +6,7 @@
#include "scheduler.h"
#include "shapes.h"
#include "canvas.h"
#include "sound.h"
static uint8_t buttonsMoveLeftPressed() {
@ -45,6 +46,11 @@ static uint8_t buttonsMoveDownPressed() {
}
void buttonsExec(void *handle) {
static uint32_t unmuteTimestamp;
uint32_t currentTimestamp = getSeconds();
static bool unmuteFlag = false;
if (! stoneIsValid()) {
// don't do anything, the stone has not been initialized
return;
@ -75,6 +81,17 @@ void buttonsExec(void *handle) {
if (buttonPressed == 1) {
canvasShow();
if (! unmuteFlag) {
soundCtrl(e_SOUND_UNMUTE);
unmuteFlag = true;
}
unmuteTimestamp = currentTimestamp;
}
if (unmuteFlag && (unmuteTimestamp + MUTE_DELAY < currentTimestamp)) {
soundCtrl(e_SOUND_MUTE);
unmuteFlag = false;
}
}

View File

@ -12,6 +12,7 @@
tTask tasks[MAX_NUM_OF_TASKS];
uint32_t seconds;
void schInit() {
TACCR0 = 32;
@ -25,9 +26,18 @@ void schInit() {
tasks[i].exec = NULL;
tasks[i].handle = NULL;
}
seconds = 0;
}
void __attribute__ ((interrupt (TIMER0_A0_VECTOR))) schUpdate() {
static uint16_t milliSeconds = 0;
if (milliSeconds >= 1000) {
seconds += 1;
milliSeconds = 0;
}
milliSeconds += 1;
for (uint16_t i = 0; i < MAX_NUM_OF_TASKS; i++) {
if (tasks[i].exec != NULL) {
if (tasks[i].delay == 0) {
@ -89,4 +99,13 @@ void schExec() {
}
}
uint32_t getSeconds() {
uint32_t s;
__disable_interrupt();
s = seconds;
__enable_interrupt();
return s;
}

View File

@ -31,6 +31,7 @@ void schDel(void (*exec)(void *), void *handle);
void schExec();
void schUpdate();
uint8_t schTaskCnt();
uint32_t getSeconds();
#endif /* PONTCOOPSCHEDULER_H_ */

17
game-ctrl/sound.c Normal file
View File

@ -0,0 +1,17 @@
#include "sound.h"
#include "spi.h"
void soundInit() {
}
void soundCtrl(t_SoundCmd cmd) {
spiSendBegin(e_SPI_SOUND);
spiSendOctet((uint8_t)cmd);
spiSendEnd(e_SPI_SOUND);
}

33
game-ctrl/sound.h Normal file
View File

@ -0,0 +1,33 @@
#ifndef _SOUND_H_
#define _SOUND_H_
#define MUTE_DELAY 30 // seconds
typedef enum {
e_SOUND_IDLE = 0,
e_SOUND_MUTE = 1,
e_SOUND_UNMUTE = 2,
e_SOUND_START_BACKGROUND = 3,
e_SOUND_STOP_BACKGROUND = 4,
e_SOUND_START_GAMEOVER = 5,
e_SOUND_STOP GAMEOVER = 6,
e_SOUND_SPEED_UP = 7,
e_SOUND_FANFARE_1 = 8,
e_SOUND_FANFARE_2 = 9,
e_SOUND_FANFARE_3 = 10,
e_SOUND_FANFARE_4 = 11,
e_SOUND_STONE_LOCKED = 12,
e_SOUND_STONE_MOVE_LEFT = 13,
e_SOUND_STONE_MOVE_RIGHT = 14,
e_SOUND_STONE_ROTATE_LEFT = 15,
e_SOUND_STONE_ROTATE_RIGHT = 16,
e_SOUND_DROPPING_START = 17,
e_SOUND_DROPPING_STOP = 18,
} t_SoundCmd;
void soundInit();
void soundCtrl(t_SoundCmd cmd);
#endif // _SOUND_H_