tetris/game-ctrl/buttons.c

136 lines
2.5 KiB
C
Raw Normal View History

2024-03-19 16:46:52 +01:00
#include <stddef.h>
#include <stdint.h>
2024-04-15 16:46:24 +02:00
#include <stdbool.h>
2024-03-19 16:46:52 +01:00
#include <msp430g2553.h>
2024-03-14 14:46:31 +01:00
#include "buttons.h"
2024-03-18 12:51:57 +01:00
#include "scheduler.h"
2024-03-14 14:46:31 +01:00
#include "shapes.h"
#include "canvas.h"
#include "sound.h"
2024-05-21 13:43:40 +02:00
#include "eeprom.h"
2024-03-14 14:46:31 +01:00
2024-04-19 11:38:32 +02:00
bool mutedFlag = true;
2024-03-14 14:46:31 +01:00
static uint8_t buttonsMoveLeftPressed() {
2024-03-19 16:46:52 +01:00
static uint8_t last = 0;
uint8_t current = (P2IN & BIT4);
uint8_t res = (current != 0) && (current != last);
last = current;
return res;
2024-03-14 14:46:31 +01:00
}
2024-05-18 20:09:21 +02:00
bool buttonsConfig1Pressed() {
return buttonsMoveLeftPressed();
}
2024-03-14 14:46:31 +01:00
static uint8_t buttonsMoveRightPressed() {
2024-03-19 16:46:52 +01:00
static uint8_t last = 0;
uint8_t current = (P2IN & BIT0);
uint8_t res = (current != 0) && (current != last);
last = current;
return res;
2024-03-14 14:46:31 +01:00
}
2024-05-18 20:09:21 +02:00
bool buttonsConfig4Pressed() {
return buttonsMoveRightPressed();
}
2024-03-14 14:46:31 +01:00
static uint8_t buttonsRotateLeftPressed() {
2024-03-19 16:46:52 +01:00
static uint8_t last = 0;
uint8_t current = (P2IN & BIT3);
uint8_t res = (current != 0) && (current != last);
last = current;
return res;
2024-03-15 12:54:32 +01:00
}
2024-05-18 20:09:21 +02:00
bool buttonsConfig2Pressed() {
return buttonsRotateLeftPressed();
}
2024-03-15 12:54:32 +01:00
static uint8_t buttonsRotateRightPressed() {
2024-03-19 16:46:52 +01:00
static uint8_t last = 0;
uint8_t current = (P2IN & BIT1);
uint8_t res = (current != 0) && (current != last);
last = current;
return res;
}
2024-05-18 20:09:21 +02:00
bool buttonsConfig3Pressed() {
return buttonsRotateRightPressed();
}
2024-03-19 16:46:52 +01:00
static uint8_t buttonsMoveDownPressed() {
2024-03-19 17:42:25 +01:00
return P2IN & BIT2;
2024-03-14 17:25:45 +01:00
}
2024-05-19 21:47:31 +02:00
bool isConfigMode() {
return (P2IN & BIT2);
}
2024-03-14 14:46:31 +01:00
void buttonsExec(void *handle) {
static uint32_t unmuteTimestamp;
uint32_t currentTimestamp = getSeconds();
if (! stoneIsValid()) {
2024-03-14 14:46:31 +01:00
// don't do anything, the stone has not been initialized
return;
}
uint8_t buttonPressed = 0;
if (buttonsMoveLeftPressed()) {
stoneMoveLeft();
buttonPressed = 1;
}
if (buttonsMoveRightPressed()) {
stoneMoveRight();
buttonPressed = 1;
}
if (buttonsRotateLeftPressed()) {
stoneRotateLeft();
buttonPressed = 1;
}
if (buttonsRotateRightPressed()) {
stoneRotateRight();
buttonPressed = 1;
}
2024-03-19 16:46:52 +01:00
if (buttonsMoveDownPressed()) {
stoneMoveDown();
buttonPressed = 1;
}
2024-03-14 14:46:31 +01:00
if (buttonPressed == 1) {
canvasShow();
2024-04-18 13:48:36 +02:00
if (mutedFlag) {
2024-05-21 13:43:40 +02:00
eepromIncGameCounter();
2024-04-17 15:30:45 +02:00
soundCtrl(SOUND_UNMUTE);
2024-04-18 13:48:36 +02:00
mutedFlag = false;
}
unmuteTimestamp = currentTimestamp;
}
2024-04-18 13:48:36 +02:00
if ((! mutedFlag) && (unmuteTimestamp + MUTE_DELAY < currentTimestamp)) {
2024-04-17 15:30:45 +02:00
soundCtrl(SOUND_MUTE);
2024-04-18 13:48:36 +02:00
mutedFlag = true;
2024-03-14 14:46:31 +01:00
}
}
void buttonsInit() {
2024-03-19 16:46:52 +01:00
P2DIR &= ~(BIT0|BIT1|BIT2|BIT3|BIT4);
2024-05-18 20:09:21 +02:00
}
2024-03-14 14:46:31 +01:00
2024-05-18 20:09:21 +02:00
void buttonsStart() {
2024-03-19 17:42:25 +01:00
schAdd(buttonsExec, NULL, 0, 25);
2024-03-14 14:46:31 +01:00
}
2024-04-19 11:38:32 +02:00
bool isGameActive() {
return ! mutedFlag;
}