update game

This commit is contained in:
2021-12-11 20:21:44 +01:00
parent 16410f5d6a
commit 59c60b765e
6 changed files with 80 additions and 34 deletions

View File

@@ -9,7 +9,7 @@ uint8_t audio_current_Song = 0;
const char* nextAudioFile = "";
uint8_t n = 0;
bool audio_start = false;
bool audioState = false;
bool audioInitOk = false;
@@ -57,6 +57,9 @@ void playSong(String filename)
id3->RegisterMetadataCB(MDCallback, (void *)"ID3TAG");
mp3->begin(id3, out);
}
else{
log_e("no filenae specified");
}
}
@@ -76,35 +79,33 @@ void initAudio()
//playSong(audio_current_Song);
}
void setAudioState(bool state)
{
audioState = state;
}
bool getAudioState(void)
{
return audioState;
}
bool getAudioInitStatus(void)
{
return audioInitOk;
}
void setAudioFileName(String filename)
{
nextAudioFile = filename;
}
void handleAudio()
{
if (hallIsIdle())
if (!audioState)
{
if (mp3->isRunning())
{
log_w("Audio: stop playback");
mp3->stop();
audio_start = false;
}
}
else
{
if(!audio_start)
{
playSong(audio_current_Song);
audio_start = true;
}
if (mp3->isRunning())
{
if (!mp3->loop())

View File

@@ -17,7 +17,10 @@
void initAudio(void);
void handleAudio(void);
bool getAudioInitStatus(void);
void playSong(String filename);
void setAudioState(bool state);
bool getAudioState(void);

View File

@@ -63,22 +63,17 @@ void handleConfig(void)
{
}
String getConfigSong(String uid)
void getConfigSong(const char* uid, const char* filename)
{
JsonArray array = tagDoc["tags"].as<JsonArray>();
const char* filename = "";
for (JsonVariant v : array)
{
const char *taguid = v["TagUID"];
if (!strcmp(uid.c_str(), taguid))
if (!strcmp(uid, taguid))
{
filename = v["audiofile"];
String retval(filename);
return retval;
}
}
log_e("taguid %s not found",uid );
return "";
}

View File

@@ -1,6 +1,7 @@
#pragma once
void getConfigSong(const char* uid, const char* filename);
void initConfig(void);
void handleConfig(void);

View File

@@ -1,6 +1,6 @@
#include "game.h"
#include "math.h"
//#include "math.h"
uint32_t last_hall_read;
uint16_t last_hall_sample;
@@ -16,7 +16,7 @@ void initGame(void)
log_i("Game: init: done");
}
void setState(GamneStates newstate)
void setGameState(GamneStates newstate)
{
gameState = newstate;
newState = true;
@@ -28,14 +28,14 @@ void handleGame(void)
{
case stateInit:
{
if(newState)
if (newState)
{
log_i("activeState = Init");
newState = false;
}
if (getSensorInitStatus() && getAudioInitStatus() && getRFIDInitStatus())
{
setState(stateIdle);
setGameState(stateIdle);
log_i("nextState = idle");
}
}
@@ -43,7 +43,7 @@ void handleGame(void)
case stateIdle:
{
uint32_t timeNow = millis();
if(newState)
if (newState)
{
log_i("activeState = Idle");
newState = false;
@@ -52,17 +52,17 @@ void handleGame(void)
if (getRFIDlastUID() == "")
{
setState(stateScanning);
setGameState(stateScanning);
log_i("nextState = Scanning");
}
else
{
if(!hallIsIdle())
if (!hallIsIdle())
{
setState(stateStartPlaying);
setGameState(stateStartPlaying);
log_i("nextState = Start playing");
}
if(timeNow - idleTime > TIMEOUT_IDLE)
if (timeNow - idleTime > TIMEOUT_IDLE)
{
clearRFIDlastUID();
}
@@ -71,7 +71,7 @@ void handleGame(void)
break;
case stateScanning:
{
if(newState)
if (newState)
{
log_i("activeState = RFID scanning");
setRFIDscanState(true);
@@ -80,7 +80,7 @@ void handleGame(void)
if (getRFIDlastUID() != "")
{
setState(stateIdle);
setGameState(stateIdle);
setRFIDscanState(false);
log_i("nextState = idle");
}
@@ -88,25 +88,70 @@ void handleGame(void)
break;
case stateStartPlaying:
{
if(newState)
if (newState)
{
log_i("activeState = startPlaying");
newState = false;
}
setState(statePlaying);
if (hallIsIdle())
{
setGameState(stateIdle);
}
else
{
// get filefrom config
String lastUID = getRFIDlastUID();
log_i("uid=%s", lastUID.c_str());
const char* nextSong = "";
getConfigSong(lastUID.c_str(), nextSong);
log_i("nextSong=%s", nextSong);
playSong(nextSong);
setAudioState(true);
setGameState(statePlaying);
}
}
break;
case statePlaying:
{
if (newState)
{
log_i("activeState = Playing");
newState = false;
}
if (hallIsIdle())
{
setAudioState(false);
setGameState(stateStopPlaying);
}
}
break;
case stateStopPlaying:
{
if (newState)
{
log_i("activeState = stopPlaying");
newState = false;
}
if (!getAudioState())
{
setGameState(stateStopped);
}
}
break;
case stateStopped:
{
if (newState)
{
log_i("activeState = stopped");
newState = false;
}
if (!getAudioState())
{
setGameState(stateIdle);
}
}
break;
default:

View File

@@ -5,6 +5,7 @@
#include "sensor.h"
#include "audio.h"
#include "rfid.h"
#include "config.h"
#define HALLINTERVAL 100
#define HALLIDLETHRESHOLD 4