Merge branch 'as5600'

This commit is contained in:
2023-04-16 21:35:19 +02:00
24 changed files with 25149 additions and 411 deletions

30
.gitmodules vendored
View File

@@ -1,35 +1,37 @@
[submodule "FW/leo_muziekdoos_esp32/lib/ESP8266Audio"] [submodule "FW/leo_muziekdoos_esp32/lib/ESP8266Audio"]
path = FW/leo_muziekdoos_esp32/lib/ESP8266Audio path = FW/leo_muziekdoos_esp32/lib/ESP8266Audio
url = http://debianvm.local:3000/libs/ESP8266Audio.git url = http://git.oldemans.nl/libs/ESP8266Audio.git
branch = master branch = master
[submodule "FW/leo_muziekdoos_esp32/lib/littleFS_esp32"] [submodule "FW/leo_muziekdoos_esp32/lib/littleFS_esp32"]
path = FW/leo_muziekdoos_esp32/lib/littleFS_esp32 path = FW/leo_muziekdoos_esp32/lib/littleFS_esp32
url = http://debianvm.local:3000/libs/LittleFS_esp32 url = http://git.oldemans.nl/libs/LittleFS_esp32
branch = master
[submodule "FW/leo_muziekdoos_esp32/lib/JCButton"] [submodule "FW/leo_muziekdoos_esp32/lib/JCButton"]
path = FW/leo_muziekdoos_esp32/lib/JCButton path = FW/leo_muziekdoos_esp32/lib/JCButton
url = http://debianvm.local:3000/libs/JCButton.git url = http://git.oldemans.nl/libs/JCButton.git
branch = master branch = master
[submodule "FW/leo_muziekdoos_esp32/lib/NDEF"] [submodule "FW/leo_muziekdoos_esp32/lib/NDEF"]
path = FW/leo_muziekdoos_esp32/lib/NDEF path = FW/leo_muziekdoos_esp32/lib/NDEF
url = http://debianvm.local:3000/libs/rfid.NDEF.git url = http://git.oldemans.nl/libs/rfid.NDEF.git
[submodule "FW/leo_muziekdoos_esp32/lib/PN532"] [submodule "FW/leo_muziekdoos_esp32/lib/PN532"]
path = FW/leo_muziekdoos_esp32/lib/PN532 path = FW/leo_muziekdoos_esp32/lib/PN532
url = http://debianvm.local:3000/libs/rfid.PN532.git url = http://git.oldemans.nl/libs/rfid.PN532.git
branch = master branch = master
[submodule "FW/leo_muziekdoos_esp32/lib/PN532_SPI"] [submodule "FW/leo_muziekdoos_esp32/lib/PN532_SPI"]
path = FW/leo_muziekdoos_esp32/lib/PN532_SPI path = FW/leo_muziekdoos_esp32/lib/PN532_SPI
url = http://debianvm.local:3000/libs/rfid.PN532_SPI.git url = http://git.oldemans.nl/libs/rfid.PN532_SPI.git
branch = master
[submodule "FW/leo_muziekdoos_esp32/lib/ADC_ADS1x15"]
path = FW/leo_muziekdoos_esp32/lib/ADC_ADS1x15
url = http://debianvm.local:3000/libs/ADC_ADS1X15.git
branch = master
[submodule "FW/leo_muziekdoos_esp32/lib/BatterySense"] [submodule "FW/leo_muziekdoos_esp32/lib/BatterySense"]
path = FW/leo_muziekdoos_esp32/lib/BatterySense path = FW/leo_muziekdoos_esp32/lib/BatterySense
url = http://debianvm.local:3000/libs/BatterySense.git url = http://git.oldemans.nl/libs/BatterySense.git
branch = master branch = master
[submodule "CAD"] [submodule "CAD"]
path = CAD path = CAD
url = http://debianvm.local:3000/projects_leo/muziekdooos_hardware.git url = http://git.oldemans.nl/projects_leo/muziekdooos_hardware.git
branch = master
[submodule "FW/leo_muziekdoos_esp32/lib/ADC_ADS1x15"]
path = FW/leo_muziekdoos_esp32/lib/ADC_ADS1x15
url = http://git.oldemans.nl/libs/ADC_ADS1X15.git
branch = master
[submodule "CAD"]
path = CAD
url = http://git.oldemans.nl/projects_leo/muziekdooos_hardware.git
branch = master branch = master

1
CAD

Submodule CAD deleted from 04fb595533

File diff suppressed because one or more lines are too long

View File

@@ -1,16 +1,17 @@
{ {
"tags": [{ "tags": [
"TagUID" : "B3 26 D0 15", {
"audiofile" : "/Billy-Jean.mp3" "TagUID" : "B3 26 D0 15",
}, "audiofile" : "/Billy-Jean.mp3"
{ },
"TagUID" : "1F E8 20 00", {
"audiofile" : "/Let_it_be.mp3" "TagUID" : "1F E8 20 00",
}, "audiofile" : "/Let_it_be.mp3"
{ },
"TagUID" : "23 0E 2C 19", {
"audiofile" : "/ringoffire.mp3" "TagUID" : "23 0E 2C 19",
}], "audiofile" : "/ringoffire.mp3"
}],
"WiFi": [ "WiFi": [
{ {
"ssid": "poes", "ssid": "poes",
@@ -19,6 +20,13 @@
{ {
"ssid": "wifi2", "ssid": "wifi2",
"psk": "pass2" "psk": "pass2"
} }],
]
"AudioGain": 0.5,
"ScanTimeout": 50,
"HardwareVersion": 2,
"GameTimeout": 10000,
"PowerTimeout": 420000,
"PowerOTADelay": 4000,
"Brightness": 20
} }

View File

@@ -8,18 +8,30 @@
; Please visit documentation for the other options and examples ; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html ; https://docs.platformio.org/page/projectconf.html
[env]
src_filter = +<*> -<.git/> -<.svn/> -<example/> -<examples/> -<test/> -<tests/>
[env:esp32-pico] [env:esp32-pico]
platform = espressif32 platform = espressif32
board = m5stack-atom #board = m5stack-atom
board = pico32
framework = arduino framework = arduino
lib_deps = lib_deps =
bblanchon/ArduinoJson@^6.18.5 bblanchon/ArduinoJson@^6.20.0
fastled/FastLED@^3.4.0 fastled/FastLED@^3.5.0
monitor_speed = 115200 #robtillaart/AS5600 @ ^0.3.4
robtillaart/AS5600 @ ^0.3.6
src_filter = ${env.build_src_filter}
lib_ldf_mode = deep+ lib_ldf_mode = deep+
extra_scripts = ./littlefsbuilder.py
build_flags = build_flags =
-DHARDWARE=2 -DHARDWARE=2
-DCORE_DEBUG_LEVEL=4 -DCORE_DEBUG_LEVEL=3
-DNDEF_DEBUG=1
extra_scripts = ./littlefsbuilder.py
board_build.filesystem = littlefs
monitor_speed = 115200
#upload_protocol = esptool
upload_protocol = espota upload_protocol = espota
upload_port = muziekdoos.local upload_port = muziekdoos.local

View File

@@ -10,7 +10,6 @@ String nextAudioFile = "";
uint8_t n = 0; uint8_t n = 0;
bool audioState = false; bool audioState = false;
bool audioInitOk = false; bool audioInitOk = false;
const char *waveFile[] = const char *waveFile[] =
@@ -82,7 +81,7 @@ void playSong(String filename)
} }
else else
{ {
log_e("no filenae specified"); log_e("no filename specified");
} }
} }
@@ -93,7 +92,7 @@ void initAudio()
delay(500); delay(500);
out = new AudioOutputI2S(); out = new AudioOutputI2S();
out->SetPinout(I2S_BCLK, I2S_WCLK, I2S_DATA); // bclk, wclk, data out->SetPinout(I2S_BCLK, I2S_WCLK, I2S_DATA); // bclk, wclk, data
out->SetGain(AUDIOGAIN); out->SetGain(getFloatParam("AudioGain", AUDIOGAIN));
pinMode(DAC_SDMODE, OUTPUT); pinMode(DAC_SDMODE, OUTPUT);
setAudioState(false); setAudioState(false);
mp3 = new AudioGeneratorMP3(); mp3 = new AudioGeneratorMP3();
@@ -103,6 +102,8 @@ void initAudio()
void setAudioState(bool state) void setAudioState(bool state)
{ {
if(state == audioState) return;
audioState = state; audioState = state;
if (state) if (state)
{ {

View File

@@ -8,12 +8,9 @@
#include "AudioOutputI2S.h" #include "AudioOutputI2S.h"
#include "game.h" #include "game.h"
#include "config.h"
#define AUDIOGAIN 0.5 #define AUDIOGAIN 0.5
#define AUDIONSONGS 3
#define AUDIOREPEATS 3
void initAudio(void); void initAudio(void);
void handleAudio(void); void handleAudio(void);

View File

@@ -5,7 +5,7 @@
#include "ArduinoJson.h" #include "ArduinoJson.h"
const char *tagConfigfile = "/settings.json"; const char *tagConfigfile = "/settings.json";
DynamicJsonDocument settingsDoc(512); DynamicJsonDocument settingsDoc(1024);
bool configInitOK = false; bool configInitOK = false;
struct tagConfig struct tagConfig
@@ -48,6 +48,41 @@ String GetWifiPassword(String ssid)
return ""; return "";
} }
int GetIntparam(String param, int def)
{
log_i("Get param %s",param );
if(param == "")
{
log_e("No param(%s) given", param);
return def;
}
if(!settingsDoc.containsKey(param))
{
log_e("param(%s) not found",param);
return def;
}
return settingsDoc[param];
}
float getFloatParam(String param, int def)
{
log_i("Get param %s",param );
if( param == "")
{
log_e("No param(%s) given",param);
return def;
}
if(!settingsDoc.containsKey(param))
{
log_e("param(%s)",param);
return def;
}
return settingsDoc[param];
}
void loadConfig(const char *fname) void loadConfig(const char *fname)
{ {
log_i("config: load"); log_i("config: load");
@@ -80,6 +115,9 @@ void loadConfig(const char *fname)
} }
configInitOK = !hasError; configInitOK = !hasError;
} }
//test values
int test = GetIntparam("ScanTimeout");
log_i("retval(%i)",test);
log_i("config: load done"); log_i("config: load done");
} }
@@ -95,6 +133,26 @@ void handleConfig(void)
{ {
} }
bool getUIDvalid(String uid)
{
JsonArray array = settingsDoc["tags"].as<JsonArray>();
for (JsonVariant v : array)
{
String taguid((const char*)v["TagUID"]);
uint16_t result = uid.compareTo(taguid);
log_v("compare %s(config) with %s(read) = %d",taguid.c_str(), uid.c_str(), result);
if (!result)
{
String filename((const char*)v["audiofile"]);
log_i("Tag found in config");
return true;
}
}
log_e("taguid %s not found",uid.c_str() );
return false;
}
String getConfigSong(String uid) String getConfigSong(String uid)
{ {
JsonArray array = settingsDoc["tags"].as<JsonArray>(); JsonArray array = settingsDoc["tags"].as<JsonArray>();

View File

@@ -3,8 +3,10 @@
#include "Arduino.h" #include "Arduino.h"
String getConfigSong(String uid); String getConfigSong(String uid);
bool getUIDvalid(String uid);
String GetWifiPassword(String ssid); String GetWifiPassword(String ssid);
int GetIntparam(String param, int def = -1);
float getFloatParam( String param, int def = -1);
void initConfig(void); void initConfig(void);
void handleConfig(void); void handleConfig(void);

View File

@@ -5,91 +5,171 @@
bool newState = true; bool newState = true;
uint32_t idleTime = 0; uint32_t idleTime = 0;
GamneStates gameState = GamneStates::stateInit;
static const char *Gameenum_str[]
{
"stateInit",
"stateIdle",
"stateScanning",
"stateArmed",
"stateStartPlaying",
"statePlaying",
"stateStopPlaying",
"stateStopped"
};
GameStates gameState = GameStates::stateInit;
void initGame(void) void initGame(void)
{ {
log_i("Game: init: done"); log_i("Game: init: done");
} }
void setGameState(GamneStates newstate) void setGameState(GameStates newstate)
{ {
gameState = newstate; gameState = newstate;
newState = true; newState = true;
log_i("NewState = %s", StateToString(newstate).c_str());
} }
void handleNewState(void)
{
if (newState)
{
log_i("ActiveState = %s" , StateToString(gameState).c_str());
newState = false;
}
}
String StateToString(GameStates gamestate)
{
try
{
return Gameenum_str[gamestate];
}
catch(const std::exception& e)
{
log_e("statestring size does not match");
return "statestringerror";
}
}
void GameStartPlayingSong(void)
{
// get filefrom config
String lastUID = getRFIDlastUID();
log_i("uid=%s", lastUID.c_str());
String nextSong = getConfigSong(lastUID);
log_i("nextSong=%s", nextSong.c_str());
playSong(nextSong);
setAudioState(true);
}
void handleGame(void) void handleGame(void)
{ {
switch (gameState) switch (gameState)
{ {
case stateInit: case stateInit:
{ {
SetLedColor(CRGB::White);
setLedBlink(true);
if (newState) if (newState)
{ {
log_i("activeState = Init"); handleNewState();
newState = false;
} }
if (getSensorInitStatus() && getAudioInitStatus() && getRFIDInitStatus()) if (getSensorInitStatus() && getAudioInitStatus() && getRFIDInitStatus())
{ {
log_i("All systems init OK, Game ready");
setGameState(stateIdle); setGameState(stateIdle);
log_i("nextState = idle");
} }
} }
break; break;
case stateIdle: case stateIdle:
{ {
SetLedColor(CRGB::Purple);
setLedBlink(false);
if (newState) if (newState)
{ {
log_i("activeState = Idle"); handleNewState();
newState = false; setRFIDscanState(false);
idleTime = millis(); setAudioState(false);
//idleTime = millis();
} }
if (getRFIDlastUID() == "") idleTime = getLastTagTime();
if (!getRFIDlastUIDValid())
{ {
// no UID found, go RFID scanning
setGameState(stateScanning); setGameState(stateScanning);
log_i("nextState = Scanning");
} }
else else
{ {
if (!hallIsIdle()) // valid RFID found arm system
{ setGameState(stateArmed);
setGameState(stateStartPlaying);
log_i("nextState = Start playing");
}
else
{
setRFIDscanState(true);
}
} }
} }
break; break;
case stateScanning: case stateScanning:
{ {
SetLedColor(CRGB::Green);
setLedBlink(true);
if (newState) if (newState)
{ {
log_i("activeState = RFID scanning"); handleNewState();
setRFIDscanState(true); setRFIDscanState(true);
newState = false;
} }
if (getRFIDlastUID() != "") if (getRFIDlastUIDValid())
{ {
setGameState(stateIdle); // valid RFID found arm system
setRFIDscanState(false); setRFIDscanState(false);
log_i("nextState = idle"); setGameState(stateArmed);
} }
} }
break; break;
case stateStartPlaying:
case stateArmed:
{ {
SetLedColor(CRGB::Green);
setLedBlink(false);
if (newState) if (newState)
{ {
log_i("activeState = startPlaying"); handleNewState();
newState = false; setRFIDscanState(true);
idleTime = millis();
}
if (!hallIsIdle())
{
// hall has detected movement, start playing
setGameState(stateStartPlaying);
}
else
{
uint32_t now = millis();
if(now - idleTime > TIMEOUT_ARMED)
{
log_i("Armed timed-out, clear current UID and go back to Idle");
clearRFIDlastUID();
setGameState(stateIdle);
}
}
}
break;
case stateStartPlaying:
{
SetLedColor(CRGB::Pink);
setLedBlink(true);
if (newState)
{
handleNewState();
setRFIDscanState(false); setRFIDscanState(false);
PowerKeepAlive(); PowerKeepAlive();
} }
if (hallIsIdle()) if (hallIsIdle())
@@ -98,53 +178,52 @@ void handleGame(void)
} }
else else
{ {
// get filefrom config GameStartPlayingSong();
String lastUID = getRFIDlastUID();
log_i("uid=%s", lastUID.c_str());
String nextSong = getConfigSong(lastUID);
log_i("nextSong=%s", nextSong.c_str());
playSong(nextSong);
setAudioState(true);
setGameState(statePlaying); setGameState(statePlaying);
} }
} }
break; break;
case statePlaying: case statePlaying:
{ {
SetLedColor(CRGB::Pink);
setLedBlink(false);
if (newState) if (newState)
{ {
log_i("activeState = Playing"); handleNewState();
newState = false;
} }
if (hallIsIdle()) if (hallIsIdle())
{ {
setAudioState(false);
setGameState(stateStopPlaying); setGameState(stateStopPlaying);
} }
} }
break; break;
case stateStopPlaying: case stateStopPlaying:
{ {
SetLedColor(CRGB::Teal);
setLedBlink(true);
if (newState) if (newState)
{ {
log_i("activeState = stopPlaying"); setAudioState(false);
newState = false; handleNewState();
} }
if (!getAudioState()) if (!getAudioState())
{ {
setAudioState(false);
setGameState(stateStopped); setGameState(stateStopped);
} }
} }
break; break;
case stateStopped: case stateStopped:
{ {
SetLedColor(CRGB::Teal);
setLedBlink(false);
if (newState) if (newState)
{ {
log_i("activeState = stopped"); handleNewState();
newState = false;
} }
if (!getAudioState()) if (!getAudioState())
{ {
setGameState(stateIdle); setGameState(stateIdle);
@@ -153,6 +232,8 @@ void handleGame(void)
break; break;
default: default:
{ {
log_w("Unknown state go to Idle");
setGameState(stateIdle);
} }
break; break;
} }

View File

@@ -9,17 +9,19 @@
#include "power.h" #include "power.h"
#define TIMEOUT_IDLE 20000 #define TIMEOUT_IDLE 20000
#define TIMEOUT_ARMED 10000
typedef enum{ typedef enum{
stateInit, stateInit,
stateIdle, stateIdle,
stateScanning, stateScanning,
stateArmed,
stateStartPlaying, stateStartPlaying,
statePlaying, statePlaying,
stateStopPlaying, stateStopPlaying,
stateStopped, stateStopped,
stateLAST stateLAST
}GamneStates; }GameStates;
void initGame(void); void initGame(void);
void handleGame(void); void handleGame(void);
@@ -27,3 +29,6 @@ bool hallIsIdle(void);
void handleHallSensor(void); void handleHallSensor(void);
String StateToString(GameStates state);

View File

@@ -1,5 +1,11 @@
#include "led.h" #include "led.h"
#ifdef STEALTH
const bool LED_STEALTH = true;
#else
const bool LED_STEALTH = false;
#endif
CRGB leds[NUM_LEDS]; CRGB leds[NUM_LEDS];
bool ledstate = false; bool ledstate = false;
@@ -28,7 +34,7 @@ void SetLedColor(CRGB color, bool blink)
void initLed(void) void initLed(void)
{ {
FastLED.addLeds<SK6812, LED_PIN, GRB>(leds, NUM_LEDS); // GRB ordering is typical FastLED.addLeds<SK6812, LED_PIN, GRB>(leds, NUM_LEDS); // GRB ordering is typical
FastLED.setBrightness(LEDBRIGHTNESS); FastLED.setBrightness(GetIntparam("Brightness", LEDDEFBRIGHT));
} }
void handleLed(void) void handleLed(void)
@@ -56,7 +62,8 @@ void handleLed(void)
{ {
leds[0] = ledcolor; leds[0] = ledcolor;
} }
FastLED.show(); FastLED.show();
lastLedTime = timeNow; lastLedTime = timeNow;
} }
} }

View File

@@ -4,11 +4,12 @@
#include "power.h" #include "power.h"
#include "audio.h" #include "audio.h"
#include "rfid.h" #include "rfid.h"
#include "config.h"
#define NUM_LEDS 1 #define NUM_LEDS 1
#define LEDTIMEOUT 100 #define LEDTIMEOUT 100
#define LEDBLINKTIME 500 #define LEDBLINKTIME 500
#define LEDBRIGHTNESS 75 #define LEDDEFBRIGHT 20
void initLed(void); void initLed(void);
void handleLed(void); void handleLed(void);

View File

@@ -36,6 +36,11 @@ bool OtaProcess_class::initialize(void)
m_ssid = WiFi.SSID(i); m_ssid = WiFi.SSID(i);
m_psk = tmppsk; m_psk = tmppsk;
} }
else{
log_w("using fallback SSID %s", SECRET_SSID);
m_ssid = SECRET_SSID;
m_psk = SECRET_PASS;
}
log_i("[%d] %s %s (%d) [%s]", i, WiFi.SSID(i), (WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*", WiFi.RSSI(i), (tmppsk != "")? "OK": "Not congigured"); log_i("[%d] %s %s (%d) [%s]", i, WiFi.SSID(i), (WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*", WiFi.RSSI(i), (tmppsk != "")? "OK": "Not congigured");
} }
} }
@@ -64,10 +69,10 @@ bool OtaProcess_class::initialize(void)
case otaConnect: case otaConnect:
{ {
log_i("Otastate = initialize(connect)");
uint32_t timeTemp = millis(); uint32_t timeTemp = millis();
if (timeTemp - m_lastconnectTime > WIFICONNECTINTERVAL) if (timeTemp - m_lastconnectTime > WIFICONNECTINTERVAL)
{ {
log_i("Otastate = initialize(connect)");
if (WiFi.status() != WL_CONNECTED) if (WiFi.status() != WL_CONNECTED)
{ {
log_e("Connection Failed! Retry..."); log_e("Connection Failed! Retry...");
@@ -112,7 +117,7 @@ bool OtaProcess_class::initialize(void)
.onEnd([]() .onEnd([]()
{ log_i("End"); ota.m_otaState = otaDone; }) { log_i("End"); ota.m_otaState = otaDone; })
.onProgress([](unsigned int progress, unsigned int total) .onProgress([](unsigned int progress, unsigned int total)
{ log_i("Progress: %u%%\r", (progress / (total / 100))); ota.m_otaState = otaBusy; }) { log_i("Progress: %u%%\r", (progress / (total / 100))); ota.m_otaState = otaBusy; PowerKeepAlive();})
.onError([](ota_error_t error) .onError([](ota_error_t error)
{ {
log_e("Error[%u]: ", error); log_e("Error[%u]: ", error);
@@ -130,8 +135,9 @@ bool OtaProcess_class::initialize(void)
{ {
log_i("Otastate = initialize(start)"); log_i("Otastate = initialize(start)");
ArduinoOTA.begin(); ArduinoOTA.begin();
log_i("Ota ready, IPaddress:%s", WiFi.localIP()); log_i("Ota ready, IPaddress:%s", WiFi.localIP().toString());
m_otaState = otaInitDone; m_otaState = otaInitDone;
setLedBlink(true);
} }
break; break;
case otaInitDone: case otaInitDone:

View File

@@ -38,7 +38,7 @@ class OtaProcess_class : public processClass
public: public:
OtaProcess_class(uint32_t timeout):processClass(timeout){} OtaProcess_class(uint32_t timeout):processClass(timeout){}
OTASTATES m_otaState = otaInit; OTASTATES m_otaState = otaInit;
}; };

View File

@@ -121,6 +121,7 @@ void handlePowerState(void)
// init all // init all
log_i("powerinit"); log_i("powerinit");
SetLedColor(CRGB::Green); SetLedColor(CRGB::Green);
//initConfig();
powerstate = on; powerstate = on;
} }
break; break;

View File

@@ -10,9 +10,9 @@
#define TIMEOUT_POWER (7 * 1000 * 60) // 7 minutes timeout #define TIMEOUT_POWER (7 * 1000 * 60) // 7 minutes timeout
#define POWERBUTTONDELAY 1000 #define POWERBUTTONDELAY 400
//#define BATTERYMEASUREDELAY 60000 //#define BATTERYMEASUREDELAY 60000
#define POWERBUTTONOTADELAY 7000 #define POWERBUTTONOTADELAY 4000
#define POWEROFFOFFDELAY 3000 #define POWEROFFOFFDELAY 3000
typedef enum typedef enum

View File

@@ -4,6 +4,11 @@ PN532_SPI pn532spi(SPI, NFC_SS, NFC_SCK, NFC_MISO, NFC_MOSI);
NfcAdapter nfc = NfcAdapter(pn532spi); NfcAdapter nfc = NfcAdapter(pn532spi);
uint32_t lastRFID = 0; uint32_t lastRFID = 0;
uint32_t lastRFIDlog = 0;
uint32_t lastTagTime = 0;
//settings
uint32_t scantimeout = 0;
String lastUid = ""; String lastUid = "";
@@ -15,6 +20,7 @@ void initRfid()
{ {
log_i("RFID init:"); // shows in serial that it is ready to read log_i("RFID init:"); // shows in serial that it is ready to read
nfc.begin(true); nfc.begin(true);
scantimeout = GetIntparam("ScanTimeout", RFIDTIMEOUT );
RfidinitOK = true; RfidinitOK = true;
log_i("RFID init: OK"); // shows in serial that it is ready to read log_i("RFID init: OK"); // shows in serial that it is ready to read
} }
@@ -23,19 +29,30 @@ void initRfid()
void handleRfid() void handleRfid()
{ {
uint32_t timeNow = millis(); uint32_t timeNow = millis();
if (lastRFID - timeNow > RFIDINTERVAL && RfidScanActive) if (timeNow - lastRFID > RFIDINTERVAL && RfidScanActive)
{ {
log_i("scanning"); if(timeNow - lastRFIDlog > RFIDLOGINTERVAL)
if (nfc.tagPresent(100)) {
log_i("scanning");
lastRFIDlog = timeNow;
}
if (nfc.tagPresent(RFIDTIMEOUT))
{ {
NfcTag tag = nfc.read(); NfcTag tag = nfc.read();
lastUid = tag.getUidString(); lastUid = tag.getUidString();
lastTagTime = millis();
log_i("found tag %s",lastUid.c_str()); log_i("found tag %s",lastUid.c_str());
} }
lastRFID = timeNow; lastRFID = timeNow;
} }
} }
uint32_t getLastTagTime(void)
{
return lastTagTime;
}
bool getRFIDInitStatus(void) bool getRFIDInitStatus(void)
{ {
return RfidinitOK; return RfidinitOK;
@@ -46,6 +63,15 @@ String getRFIDlastUID(void)
return lastUid; return lastUid;
} }
bool getRFIDlastUIDValid(void)
{
if(lastUid == "")
{
return false;
}
return (getUIDvalid(lastUid));
}
void setRFIDscanState(bool state) void setRFIDscanState(bool state)
{ {
RfidScanActive = state; RfidScanActive = state;

View File

@@ -2,6 +2,7 @@
#include "board.h" #include "board.h"
#include "game.h" #include "game.h"
#include "config.h"
#include <SPI.h> #include <SPI.h>
#include "PN532_SPI.h" #include "PN532_SPI.h"
@@ -9,11 +10,15 @@
#include "NfcAdapter.h" #include "NfcAdapter.h"
#define NDEF_SUPPORT_MIFARE_CLASSIC #define NDEF_SUPPORT_MIFARE_CLASSIC
#define RFIDINTERVAL 800 #define RFIDINTERVAL 200
#define RFIDLOGINTERVAL 5000
#define RFIDTIMEOUT 50
void initRfid(void); void initRfid(void);
void handleRfid(void); void handleRfid(void);
bool getRFIDInitStatus(void); bool getRFIDInitStatus(void);
String getRFIDlastUID(void); String getRFIDlastUID(void);
void setRFIDscanState(bool state); void setRFIDscanState(bool state);
void clearRFIDlastUID(void); bool getRFIDlastUIDValid(void);
void clearRFIDlastUID(void);
uint32_t getLastTagTime(void);

View File

@@ -1,5 +1,7 @@
#include "sensor.h" #include "sensor.h"
AS5600 as5600; // use default Wire
ADS1115 ADS(0x48); ADS1115 ADS(0x48);
Battery battery(VBATTMIN, VBATTMAX, MEAS_ADC, &getvbatt); Battery battery(VBATTMIN, VBATTMAX, MEAS_ADC, &getvbatt);
@@ -11,30 +13,23 @@ uint16_t BatterySensor = 0;
uint16_t BatteryVoltage = 0; uint16_t BatteryVoltage = 0;
uint32_t BatteryWarningFirst = 0; uint32_t BatteryWarningFirst = 0;
uint16_t HallSensor = 0;
uint32_t last_hall_read = 0;
uint16_t last_hall_sample = 0;
uint16_t last_hall_Delta = 0;
uint8_t hall_idle_count = 0;
uint8_t hall_decrease_count = 0;
uint8_t hall_increase_count = 0;
bool hall_is_Idle = true; bool hall_is_Idle = true;
bool hallinitOK = false; bool ADSinitOK = false;
bool ASinitOK = false;
bool getSensorInitStatus(void) bool getSensorInitStatus(void)
{ {
return hallinitOK; return (ADSinitOK & ASinitOK);
}
uint16_t getHall(void)
{
return HallSensor;
} }
uint16_t getvbatt(uint8_t dummy = 0) uint16_t getvbatt(uint8_t dummy = 0)
{ {
if(!ADSinitOK)
{
log_e("battery not initialized due to ADS error");
return 0;
}
int16_t readbatt = ADS.readADC(MEAS_ADC); int16_t readbatt = ADS.readADC(MEAS_ADC);
log_i("readvbat=%d", readbatt); log_i("readvbat=%d", readbatt);
return readbatt; return readbatt;
@@ -45,26 +40,49 @@ bool hallIsIdle(void)
return hall_is_Idle; return hall_is_Idle;
} }
bool initADS(void)
{
log_i("sensor init ADS1x15:");
bool result = ADS.begin(I2C_SDA, I2C_SCL);
ADS.setMode(0);
result = ADS.isConnected();
ADSinitOK = result;
if(!result)
{
log_e("Failed to init ADS1x15");
}
return result;
}
void initBattery(void) void initBattery(void)
{ {
if(!ADSinitOK)
{
log_e("battery not initialized due to ADS error");
return;
}
battery.onDemand(MEAS_EN, LOW); battery.onDemand(MEAS_EN, LOW);
battery.begin(VBATTREF, (R12 + R13) / R13, &sigmoidal); // R1 = 220K, R2 = 100K, factor = (R1+R2)/R2 battery.begin(VBATTREF, (R12 + R13) / R13, &sigmoidal); // R1 = 220K, R2 = 100K, factor = (R1+R2)/R2
} }
bool initAS5600(void)
{
as5600.begin(); // set direction pin.
as5600.setDirection(AS5600_CLOCK_WISE); // default, just be explicit.
bool result = as5600.isConnected();
ASinitOK = result;
if(!result)
{
log_e("error initilizing AS5600");
}
return result;
}
void initSensor(void) void initSensor(void)
{ {
log_i("sensor init ADS1x15:"); initADS();
bool result = ADS.begin(I2C_SDA, I2C_SCL);
initBattery(); initBattery();
if (!result) initAS5600();
{
log_e("sensor init: FAIL");
}
else
{
log_i("sensor init: OK");
hallinitOK = true;
}
} }
bool CheckBattery(void) bool CheckBattery(void)
@@ -108,6 +126,11 @@ bool getLowBatt(void)
void handleBatterySensor(void) void handleBatterySensor(void)
{ {
uint32_t timeNow = millis(); uint32_t timeNow = millis();
if(getAudioState())
{
return;
}
if (timeNow - lastVbatt > (VBATTINTERVALL - VBATTMEASPRECHARGE)) if (timeNow - lastVbatt > (VBATTINTERVALL - VBATTMEASPRECHARGE))
{ {
@@ -118,7 +141,7 @@ void handleBatterySensor(void)
if (timeNow - lastVbatt > VBATTINTERVALL) if (timeNow - lastVbatt > VBATTINTERVALL)
{ {
CheckBattery(); CheckBattery();
log_i("vbatt voltage = %d mv", BatteryVoltage); log_i("vbatt level = %d %%", BatterySensor);
if (BatterySensor > 80) if (BatterySensor > 80)
SetLedColor(CRGB::Green); SetLedColor(CRGB::Green);
else if (BatterySensor > 50) else if (BatterySensor > 50)
@@ -129,187 +152,36 @@ void handleBatterySensor(void)
} }
} }
HALLSENSORSTATES hall_sensor_state = HALLSENSORSTATES::hall_idle;
uint32_t hall_timeout = 0;
uint32_t hall_stateTimer = 0;
HALLSENSORSTATES previous_hall_state = hall_sensor_state;
#define HALLSTATETIMEOUT 300
#define HALLTIMEOUT 1000
void handleHallSensor(void) void handleHallSensor(void)
{ {
uint32_t timeNow = millis(); uint32_t timeNow = millis();
if (timeNow - last_hall_read > HALLINTERVAL)
if (timeNow - lastADS < ADSINTERVAL)
{ {
// get sample //wait for timer
uint16_t hall_sample = ADS.readADC(HALL_INPUT); return;
int hall_delta = hall_sample - last_hall_sample;
switch (hall_sensor_state)
{
case hall_idle:
{
if (hall_delta > HALLTHRESHOLD)
{
hall_decrease_count = 0;
if (hall_increase_count++ > HALLTHRESHOLD)
{
hall_sensor_state = hall_increasing;
log_i("next state = increasing");
}
}
else if (hall_delta < HALLTHRESHOLD)
{
hall_increase_count = 0;
if (hall_decrease_count++ > HALLTHRESHOLD)
{
hall_sensor_state = hall_decreasing;
log_i("next state = decreasing");
}
}
else
{
/* stay idle */
}
}
break;
case hall_decreasing:
{
// lets see if we are bottoming out or stalling
if (hall_delta < HALLIDLETHRESHOLD)
{
// reset timer
hall_timeout = timeNow;
hall_stateTimer = timeNow;
}
if (timeNow - hall_stateTimer > HALLSTATETIMEOUT)
{
// we are stalling or tipping under
hall_sensor_state = hall_tipunder;
hall_stateTimer = 0;
}
}
break;
case hall_tipunder:
{
// check if we are stalling or moving into the increasing state
if (!(hall_delta > HALLIDLETHRESHOLD))
{
hall_stateTimer = timeNow;
}
if (timeNow - hall_stateTimer > HALLSTATETIMEOUT)
{
// samples detected to the upside, move to increasing state
hall_sensor_state = hall_increasing;
hall_stateTimer = 0;
}
if(timeNow - hall_timeout > HALLTIMEOUT)
{
//timeed out, move to idle
hall_sensor_state = hall_idle;
hall_stateTimer = 0;
hall_timeout = 0;
}
}
break;
case hall_increasing:
{
// check if we are increasing
if (hall_delta > HALLIDLETHRESHOLD)
{
hall_stateTimer = timeNow;
}
if (timeNow - hall_stateTimer > HALLSTATETIMEOUT)
{
// samples detected to the upside, move to increasing state
hall_sensor_state = hall_tipover;
hall_stateTimer = 0;
}
}
break;
case hall_tipover:
{
// check if we are stalling or moving into the increasing state
if (!(hall_delta > HALLIDLETHRESHOLD))
{
hall_stateTimer = timeNow;
}
if (timeNow - hall_stateTimer > HALLSTATETIMEOUT)
{
// samples detected to the upside, move to increasing state
hall_sensor_state = hall_decreasing;
hall_stateTimer = 0;
}
if(timeNow - hall_timeout > HALLTIMEOUT)
{
//timeed out, move to idle
hall_sensor_state = hall_idle;
hall_stateTimer = 0;
hall_timeout = 0;
}
}
break;
default:
{
}
break;
}
bool skipfirstSample = false;
if (!last_hall_Delta)
{
skipfirstSample = true;
}
uint16_t hall_delta = (last_hall_sample > hall_sample) ? (last_hall_sample - hall_sample) : (hall_sample - last_hall_sample);
hall_delta = (hall_delta + last_hall_Delta) / 2;
last_hall_Delta = hall_delta;
if (skipfirstSample)
{
log_v("First sample skipped");
if (hall_idle_count)
{
hall_idle_count--;
}
return;
}
if (hall_delta > HALLIDLETHRESHOLD)
{
if (hall_idle_count > HALLIDLESAMPLES)
{
hall_is_Idle = false;
hall_idle_count = HALLPLAYSAMPLES;
log_i("Game: playing, delta = %d", hall_delta);
}
else
{
hall_idle_count++;
}
}
else
{
if (hall_idle_count == 0)
{
hall_is_Idle = true;
}
else
{
hall_idle_count--;
}
}
log_i("HallSensor: val=%d, delta=%d, count=%d, idle=%s\n",
hall_sample,
hall_delta,
hall_idle_count,
(hall_is_Idle ? "yes" : "no"));
last_hall_sample = hall_sample;
last_hall_read = timeNow;
} }
lastADS = timeNow;
int angle = as5600.getCumulativePosition();
log_v("sensor angle %d", angle);
if( angle > HALLTHRESHOLD)
{
if(hall_is_Idle)
{
log_i("hall state changed to active(%i)", angle);
}
hall_is_Idle = false;
}
else
{
if(!hall_is_Idle)
{
log_i("hall state changed to idle (%i)", angle);
}
hall_is_Idle = true;
}
as5600.resetPosition();
} }

View File

@@ -1,30 +1,21 @@
#pragma once #pragma once
#include "Arduino.h"
#include "ADS1X15.h" #include "ADS1X15.h"
#include "Battery.h" #include "Battery.h"
#include "board.h" #include "board.h"
#include "led.h" #include "led.h"
#include "AS5600.h"
#include "Wire.h"
#include "audio.h"
#define ADSINTERVAL 100 #define ADSINTERVAL 100
#define VBATTINTERVALL 15000 #define VBATTINTERVALL 15000
#define VBATTMEASPRECHARGE 500 #define VBATTMEASPRECHARGE 500
#define LOWBATTPERIOD 30000 #define LOWBATTPERIOD 30000
#define HALLINTERVAL 100 #define HALLINTERVAL 100
#define HALLTHRESHOLD 5 #define HALLTHRESHOLD 5
#define HALLIDLETHRESHOLD 10
#define HALLIDLESAMPLES 15
#define HALLPLAYSAMPLES 24
typedef enum
{
hall_idle,
hall_increasing,
hall_tipover,
hall_decreasing,
hall_tipunder,
HALL_LAST
}HALLSENSORSTATES;
static String HALLSENESORSTATES_ENUM2STR[HALL_LAST] static String HALLSENESORSTATES_ENUM2STR[HALL_LAST]
{ {
@@ -39,7 +30,6 @@ void initSensor(void);
void handleBatterySensor(void); void handleBatterySensor(void);
void handleHallSensor(void); void handleHallSensor(void);
uint16_t getHall( void ); uint16_t getHall( void );
bool hallIsIdle(void); bool hallIsIdle(void);
uint16_t getvbatt(uint8_t dummy); uint16_t getvbatt(uint8_t dummy);
@@ -47,5 +37,7 @@ bool getSensorInitStatus(void);
bool CheckBattery(void); bool CheckBattery(void);
bool getLowBatt(void); bool getLowBatt(void);
void readsensor(void);

View File

@@ -5,7 +5,7 @@
#include "FS.h" #include "FS.h"
#if defined ESP_ARDUINO_VERSION_VAL #if defined ESP_ARDUINO_VERSION_VAL
#if (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(2, 0, 0) #if (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(2, 0, 0))
#include <LittleFS.h> #include <LittleFS.h>
#define ESP_V2 #define ESP_V2
#endif #endif

View File

@@ -1,85 +0,0 @@
{
"cod": "200",
"message": 0,
"list": [
{
"dt": 1581498000,
"main": {
"temp": 3.23,
"feels_like": -3.63,
"temp_min": 3.23,
"temp_max": 4.62,
"pressure": 1014,
"sea_level": 1014,
"grnd_level": 1010,
"humidity": 58,
"temp_kf": -1.39
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01d"
}
],
"clouds": {
"all": 0
},
"wind": {
"speed": 6.19,
"deg": 266
},
"sys": {
"pod": "d"
},
"dt_txt": "2020-02-12 09:00:00"
},
{
"dt": 1581508800,
"main": {
"temp": 6.09,
"feels_like": -1.07,
"temp_min": 6.09,
"temp_max": 7.13,
"pressure": 1015,
"sea_level": 1015,
"grnd_level": 1011,
"humidity": 48,
"temp_kf": -1.04
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01d"
}
],
"clouds": {
"all": 9
},
"wind": {
"speed": 6.64,
"deg": 268
},
"sys": {
"pod": "d"
},
"dt_txt": "2020-02-12 12:00:00"
}
],
"city": {
"id": 2643743,
"name": "London",
"coord": {
"lat": 51.5085,
"lon": -0.1257
},
"country": "GB",
"population": 1000000,
"timezone": 0,
"sunrise": 1581492085,
"sunset": 1581527294
}
}