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

View File

@@ -17,7 +17,10 @@
void initAudio(void); void initAudio(void);
void handleAudio(void); void handleAudio(void);
bool getAudioInitStatus(void); bool getAudioInitStatus(void);
void playSong(String filename); 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>(); JsonArray array = tagDoc["tags"].as<JsonArray>();
const char* filename = "";
for (JsonVariant v : array) for (JsonVariant v : array)
{ {
const char *taguid = v["TagUID"]; const char *taguid = v["TagUID"];
if (!strcmp(uid.c_str(), taguid)) if (!strcmp(uid, taguid))
{ {
filename = v["audiofile"]; filename = v["audiofile"];
String retval(filename);
return retval;
} }
} }
log_e("taguid %s not found",uid ); log_e("taguid %s not found",uid );
return "";
} }

View File

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

View File

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

View File

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