Skip to content

Commit

Permalink
process event steps
Browse files Browse the repository at this point in the history
  • Loading branch information
thiccnfun committed Mar 19, 2024
1 parent 8861b3b commit f4e3f9f
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 6 deletions.
94 changes: 88 additions & 6 deletions src/MicStateService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ struct sum_queue_t {
float pitch;
};

struct event_queue_t {
bool passed;
float dbPassRate;
};


//
// I2S Reader Task
Expand Down Expand Up @@ -214,6 +219,7 @@ void MicStateService::setupReader() {

// Create FreeRTOS queue
samplesQueue = xQueueCreate(8, sizeof(sum_queue_t));
eventsQueue = xQueueCreate(8, sizeof(sum_queue_t));

// Create the I2S reader FreeRTOS task
// NOTE: Current version of ESP-IDF will pin the task
Expand All @@ -232,6 +238,16 @@ void MicStateService::setupReader() {
ESP32SVELTEKIT_RUNNING_CORE // Pin to application core
);

xTaskCreatePinnedToCore(
this->_eventsTask, // Function that should be called
"Mic I2S Reader", // Name of the task (for debugging)
I2S_TASK_STACK, // Stack size (bytes)
this, // Pass reference to this class instance
(tskIDLE_PRIORITY), // task priority
NULL, // Task handle
ESP32SVELTEKIT_RUNNING_CORE // Pin to application core
);

sum_queue_t q;
uint32_t Leq_samples = 0;
double Leq_sum_sqr = 0;
Expand Down Expand Up @@ -336,12 +352,18 @@ void MicStateService::setupReader() {
if (doEvaluation) {
doEvaluation = false;

event_queue_t eq;
eq.passed = evaluatePassed(dbPassRate);
eq.dbPassRate = dbPassRate;

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

xQueueSend(eventsQueue, &eq, portMAX_DELAY);
}

if (resetConditions) {
Expand Down Expand Up @@ -480,7 +502,7 @@ void MicStateService::readerTask() {
// q.proc_ticks = xTaskGetTickCount() - start_tick;

// Calculate pitch
q.pitch = calculatePitch();
// q.pitch = calculatePitch();

// analogRead PIEZO_PIN
// Serial.println(analogRead(PIEZO_PIN));
Expand All @@ -491,6 +513,50 @@ void MicStateService::readerTask() {
}
}

void MicStateService::eventsTask() {
event_queue_t eq;
while (xQueueReceive(eventsQueue, &eq, portMAX_DELAY)) {
std::vector<EventStep> steps = std::vector<EventStep>();

if (eq.passed) {
assignAffirmationSteps(steps);
// handleAffirmation(eq.dbPassRate);
} else {
assignCorrectionSteps(steps);
// handleCorrection(eq.dbPassRate);
}

// iterate through steps and execute
for (EventStep step : steps) {
// execute step
processStep(step, eq.dbPassRate);
}
}
}

void MicStateService::processStep(EventStep step, float passRate) {
// execute step

switch (step.type) {
case EventType::COLLAR_VIBRATION:
Serial.println("Vibrating collar");
vibrateCollar(50, 1000);
break;
case EventType::COLLAR_SHOCK:
Serial.println("Shocking collar");
beepCollar(1000);
break;
case EventType::COLLAR_BEEP:
Serial.println("Beeping collar");
beepCollar(1000);
break;
default:
// unknown event type
break;
}

}


// Function to calculate the pitch using FFT
// float MicStateService::calculatePitch(const std::vector<float>& samples, int sampleRate) {
Expand Down Expand Up @@ -731,6 +797,22 @@ void MicStateService::assignConditionValues(
});
}

void MicStateService::assignAffirmationSteps(
std::vector<EventStep> &affirmationSteps
) {
_appSettingsService->read([&](AppSettings &settings) {
affirmationSteps = settings.affirmationSteps;
});
}

void MicStateService::assignCorrectionSteps(
std::vector<EventStep> &correctionSteps
) {
_appSettingsService->read([&](AppSettings &settings) {
correctionSteps = settings.correctionSteps;
});
}




Expand Down
10 changes: 10 additions & 0 deletions src/MicStateService.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ class MicStateService : public StatefulService<MicState>

protected:
void readerTask();
void eventsTask();
static void _readerTask(void *_this) { static_cast<MicStateService *>(_this)->readerTask(); }
static void _eventsTask(void *_this) { static_cast<MicStateService *>(_this)->eventsTask(); }
void assignDurationValues(
int &idleDuration,
int &actDuration
Expand All @@ -112,6 +114,13 @@ class MicStateService : public StatefulService<MicState>
bool evaluatePassed(float passRate);
void handleAffirmation(float passRate);
void handleCorrection(float passRate);
void assignAffirmationSteps(
std::vector<EventStep> &affirmationSteps
);
void assignCorrectionSteps(
std::vector<EventStep> &correctionSteps
);
void processStep(EventStep step, float passRate);

private:
HttpEndpoint<MicState> _httpEndpoint;
Expand All @@ -128,6 +137,7 @@ class MicStateService : public StatefulService<MicState>
int32_t* intSamples = new int32_t[SAMPLES_SHORT];

QueueHandle_t samplesQueue;
QueueHandle_t eventsQueue;
float _real[SAMPLES_SHORT];
float _imag[SAMPLES_SHORT];
float _weighingFactors[SAMPLES_SHORT];
Expand Down

0 comments on commit f4e3f9f

Please sign in to comment.