Skip to content

Commit

Permalink
AudioLogger compile errors for ESP32
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed Jan 7, 2025
1 parent b4bdb1a commit 3f1a579
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 34 deletions.
17 changes: 8 additions & 9 deletions src/AudioTools/Concurrency/LockGuard.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace audio_tools {


/**
* @brief RAII implementaion using a Mutex: Only a few microcontrollers provide
* lock guards, so I decided to roll my own solution where we can just use a
Expand All @@ -14,25 +13,25 @@ namespace audio_tools {
* @author Phil Schatzmann
* @copyright GPLv3 *
*/

class LockGuard {
public:
public:
LockGuard(MutexBase &mutex) {
p_mutex = &mutex;
p_mutex->lock();
}

LockGuard(MutexBase *mutex) {
p_mutex = mutex;
if (p_mutex != nullptr)
p_mutex->lock();
if (p_mutex != nullptr) p_mutex->lock();
}

~LockGuard() {
if (p_mutex != nullptr)
p_mutex->unlock();
if (p_mutex != nullptr) p_mutex->unlock();
}

protected:
protected:
MutexBase *p_mutex = nullptr;
};

}

} // namespace audio_tools
26 changes: 12 additions & 14 deletions src/AudioTools/Concurrency/Mutex.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#pragma once
#include "AudioConfig.h"
#include <atomic>

#include "AudioConfig.h"

#ifdef USE_STD_CONCURRENCY
# include <mutex>
#include <mutex>
#endif

namespace audio_tools {
Expand All @@ -15,14 +16,12 @@ namespace audio_tools {
* @copyright GPLv3
*/
class MutexBase {
public:
public:
virtual void lock() {}
virtual void unlock() {}
};

class SpinLock : public MutexBase {
volatile std::atomic<bool> lock_ = {0};

void lock() {
for (;;) {
// Optimistically assume the lock is free on the first try
Expand All @@ -46,13 +45,13 @@ class SpinLock : public MutexBase {
!lock_.exchange(true, std::memory_order_acquire);
}

void unlock() {
lock_.store(false, std::memory_order_release);
}
};
void unlock() { lock_.store(false, std::memory_order_release); }

protected:
volatile std::atomic<bool> lock_ = {0};
};

#if defined(USE_STD_CONCURRENCY)
#if defined(USE_STD_CONCURRENCY)

/**
* @brief Mutex implemntation based on std::mutex
Expand All @@ -61,15 +60,14 @@ class SpinLock : public MutexBase {
* @copyright GPLv3
*/
class StdMutex : public MutexBase {
public:
public:
void lock() override { std_mutex.lock(); }
void unlock() override { std_mutex.unlock(); }

protected:
protected:
std::mutex std_mutex;
};

#endif


}
} // namespace audio_tools
4 changes: 0 additions & 4 deletions src/AudioTools/Concurrency/RTOS/MutexRTOS.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,16 @@ namespace audio_tools {
class MutexRTOS : public MutexBase {
public:
MutexRTOS() {
TRACED();
xSemaphore = xSemaphoreCreateBinary();
xSemaphoreGive(xSemaphore);
}
virtual ~MutexRTOS() {
TRACED();
vSemaphoreDelete(xSemaphore);
}
void lock() override {
TRACED();
xSemaphoreTake(xSemaphore, portMAX_DELAY);
}
void unlock() override {
TRACED();
xSemaphoreGive(xSemaphore);
}

Expand Down
14 changes: 7 additions & 7 deletions src/AudioTools/CoreAudio/AudioLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,22 @@
# include "Print.h"
#endif

#include "AudioTools/Concurrency/LockGuard.h"
#if defined(RP2040)
# include "AudioTools/Concurrency/RP2040.h"
# include "AudioTools/Concurrency/RP2040/MutexRP2040.h"
#elif defined(ESP32)
# include "AudioTools/Concurrency/RTOS.h"
#else
# include "AudioTools/Concurrency/LockGuard.h"
# include "AudioTools/Concurrency/RTOS/MutexRTOS.h"
#endif

// Logging Implementation
#if USE_AUDIO_LOGGING

namespace audio_tools {

#if defined(RP2040)
#if defined(ESP32)
MutexRTOS audio_logger_mutex;
#elif defined(RP2040)
MutexRP2040 audio_logger_mutex;
#elif defined(ESP32)
MutexESP32 audio_logger_mutex;
#else
MutexBase audio_logger_mutex; // no locking
#endif
Expand Down Expand Up @@ -72,6 +71,7 @@ class AudioLogger {
fprintf( stderr, "%s\n", print_buffer);
#else
log_print_ptr->println(print_buffer);
log_print_ptr->flush();
#endif
print_buffer[0]=0;
}
Expand Down

0 comments on commit 3f1a579

Please sign in to comment.