Skip to content

Commit

Permalink
Update MicStateService.cpp and MicStateService.h
Browse files Browse the repository at this point in the history
  • Loading branch information
thiccnfun committed Mar 12, 2024
1 parent ecfd276 commit c3014e2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
23 changes: 14 additions & 9 deletions src/MicStateService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ void MicStateService::setupReader() {
int ticksPassed = 0;
bool stopOnPass = true;
bool doEvaluation = false;
float passRate = 1;
float dbPassRate = 0;

// Read sum of samaples, calculated by 'i2s_reader_task'
while (xQueueReceive(samplesQueue, &q, portMAX_DELAY)) {
Expand Down Expand Up @@ -427,14 +427,14 @@ void MicStateService::setupReader() {
// if setup to stop on the first pass, proceed to evaluation
// otherwise, continue to accumulate ticks and evaluate at the end
if (stopOnPass) {
passRate = 1;
dbPassRate = 1;
doEvaluation = true;
resetConditions = true;
}
}

if (!doEvaluation) {
passRate = (float)ticksPassed / ticks;
dbPassRate = (float)ticksPassed / ticks;
}
}

Expand All @@ -448,16 +448,18 @@ void MicStateService::setupReader() {
Leq_dB,
q.pitch,
elapsedTime <= idleDuration ? -1 : eventCountdown,
thresholdDb
thresholdDb,
dbPassRate
);

if (doEvaluation) {
doEvaluation = false;

if (evaluatePassed(passRate)) {
handleAffirmation(passRate);
// NOTE: collar seems to block in these...
if (evaluatePassed(dbPassRate)) {
handleAffirmation(dbPassRate);
} else {
handleCorrection(passRate);
handleCorrection(dbPassRate);
}
}

Expand All @@ -472,6 +474,7 @@ void MicStateService::setupReader() {
// sequenceDuration = actDuration;
ticks = 0;
ticksPassed = 0;
dbPassRate = 0;
}
}

Expand Down Expand Up @@ -771,16 +774,18 @@ void MicStateService::updateState(
float dbValue,
float pitchValue,
int eventCountdown,
int thresholdDb
int thresholdDb,
float dbPassRate
) {
update([&](MicState& state) {
if (state.dbValue == dbValue && state.eventCountdown == eventCountdown) {
return StateUpdateResult::UNCHANGED;
}
state.dbValue = dbValue;
state.eventCountdown = eventCountdown;
state.dbThreshold = eventCountdown == -1 ? 0 : thresholdDb;
state.dbPassRate = dbPassRate;
state.pitchValue = pitchValue;
state.eventCountdown = eventCountdown;
return StateUpdateResult::CHANGED;
}, "db_set");
}
Expand Down
13 changes: 9 additions & 4 deletions src/MicStateService.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ class MicState
double pitchThreshold = 0;
double pitchValue = 0;
int eventCountdown = 0;
int actDuration = 0;
int collarId = 0;
// int actDuration = 0;
// int collarId = 0;
float dbPassRate = 0;
float pitchPassRate = 0;

bool enabled = false;

Expand All @@ -61,10 +63,12 @@ class MicState
root["dbt"] = settings.dbThreshold;
root["dbv"] = settings.dbValue;
root["ecd"] = settings.eventCountdown;
root["acd"] = settings.actDuration;
// root["acd"] = settings.actDuration;
root["pv"] = settings.pitchValue;
root["pt"] = settings.pitchThreshold;
root["en"] = settings.enabled;
root["dpr"] = settings.dbPassRate;
root["ppr"] = settings.pitchPassRate;
}

static StateUpdateResult update(JsonObject &root, MicState &micState)
Expand Down Expand Up @@ -130,7 +134,8 @@ class MicStateService : public StatefulService<MicState>
float dbValue,
float pitchValue,
int actCountdown,
int thresholdDb
int thresholdDb,
float dbPassRate
);
float calculatePitch(float samples[], int sampleRate);
void printVector(double *vData, uint16_t bufferSize, uint8_t scaleType);
Expand Down

0 comments on commit c3014e2

Please sign in to comment.