Skip to content

Commit

Permalink
Apply automatic code formatting after rebase
Browse files Browse the repository at this point in the history
Rebase on dev finished. Applied automatic code formatting and removed duplicated test code, fix warning and reomve prototype code and unused queues.
  • Loading branch information
laszloh committed Nov 29, 2023
1 parent f43e0b0 commit 67f90a7
Show file tree
Hide file tree
Showing 15 changed files with 649 additions and 736 deletions.
30 changes: 15 additions & 15 deletions src/AudioPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@

#include <esp_task_wdt.h>
#include <freertos/task.h>
#include <mutex>

#define AUDIOPLAYER_VOLUME_MAX 21u
#define AUDIOPLAYER_VOLUME_MIN 0u
#define AUDIOPLAYER_VOLUME_INIT 3u

playProps gPlayProperties;
TaskHandle_t AudioTaskHandle;
//uint32_t cnt123 = 0;
// uint32_t cnt123 = 0;

// Playlist
static std::unique_ptr<Playlist> playlist;
Expand Down Expand Up @@ -249,7 +250,7 @@ void Audio_setTitle(const char *format, ...) {

const String AudioPlayer_getCurrentTrackPath(size_t track) {
std::lock_guard guard(playlist_mutex);
if(track >= playlist->size()) {
if (track >= playlist->size()) {
// requested track is larger than the array
return String();
}
Expand Down Expand Up @@ -936,15 +937,15 @@ void AudioPlayer_PauseOnMinVolume(const uint8_t oldVolume, const uint8_t newVolu
// Receives de-serialized RFID-data (from NVS) and dispatches playlists for the given
// playmode to the track-queue.
void AudioPlayer_TrackQueueDispatcher(const char *_itemToPlay, const uint32_t _lastPlayPos, const uint32_t _playMode, const uint16_t _trackLastPlayed) {
// Make sure last playposition for audiobook is saved when new RFID-tag is applied
#ifdef SAVE_PLAYPOS_WHEN_RFID_CHANGE
if (!gPlayProperties.pausePlay && (gPlayProperties.playMode == AUDIOBOOK || gPlayProperties.playMode == AUDIOBOOK_LOOP)) {
AudioPlayer_TrackControlToQueueSender(PAUSEPLAY);
while (!gPlayProperties.pausePlay) { // Make sure to wait until playback is paused in order to be sure that playposition saved in NVS
vTaskDelay(portTICK_PERIOD_MS * 100u);
}
// Make sure last playposition for audiobook is saved when new RFID-tag is applied
#ifdef SAVE_PLAYPOS_WHEN_RFID_CHANGE
if (!gPlayProperties.pausePlay && (gPlayProperties.playMode == AUDIOBOOK || gPlayProperties.playMode == AUDIOBOOK_LOOP)) {
AudioPlayer_TrackControlToQueueSender(PAUSEPLAY);
while (!gPlayProperties.pausePlay) { // Make sure to wait until playback is paused in order to be sure that playposition saved in NVS
vTaskDelay(portTICK_PERIOD_MS * 100u);
}
#endif
}
#endif

gPlayProperties.startAtFilePos = _lastPlayPos;
gPlayProperties.currentTrackNumber = _trackLastPlayed;
Expand All @@ -953,9 +954,9 @@ void AudioPlayer_TrackQueueDispatcher(const char *_itemToPlay, const uint32_t _l

if (_playMode != WEBSTREAM) {
if (_playMode == RANDOM_SUBDIRECTORY_OF_DIRECTORY) {
auto tmp = SdCard_pickRandomSubdirectory(_itemToPlay); // *filename (input): target-directory // *filename (output): random subdirectory
if (tmp) { // If error occured while extracting random subdirectory
newPlaylist = SdCard_ReturnPlaylist(tmp.value().c_str(), _playMode); // Provide random subdirectory in order to enter regular playlist-generation
auto tmp = SdCard_pickRandomSubdirectory(_itemToPlay); // get a random subdirectory
if (tmp) { // If error occured while extracting random subdirectory
newPlaylist = SdCard_ReturnPlaylist(tmp.value().c_str(), _playMode); // Provide random subdirectory in order to enter regular playlist-generation
}
} else {
newPlaylist = SdCard_ReturnPlaylist(_itemToPlay, _playMode);
Expand Down Expand Up @@ -1077,10 +1078,9 @@ void AudioPlayer_TrackQueueDispatcher(const char *_itemToPlay, const uint32_t _l
default:
Log_Println(modeInvalid, LOGLEVEL_ERROR);
error = true;

}

if(!error) {
if (!error) {
// transfer ownership of the new playlist to the audio thread
std::lock_guard guard(playlist_mutex);
playlist = std::move(newPlaylist.value());
Expand Down
52 changes: 26 additions & 26 deletions src/AudioPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@
#include "Playlist.h"

typedef struct { // Bit field
uint8_t playMode: 4; // playMode
char title[255]; // current title
bool repeatCurrentTrack: 1; // If current track should be looped
bool repeatPlaylist: 1; // If whole playlist should be looped
uint16_t currentTrackNumber: 9; // Current tracknumber
uint16_t numberOfTracks: 9; // Number of tracks in playlist
unsigned long startAtFilePos; // Offset to start play (in bytes)
double currentRelPos; // Current relative playPosition (in %)
bool sleepAfterCurrentTrack: 1; // If uC should go to sleep after current track
bool sleepAfterPlaylist: 1; // If uC should go to sleep after whole playlist
bool sleepAfter5Tracks: 1; // If uC should go to sleep after 5 tracks
bool saveLastPlayPosition: 1; // If playposition/current track should be saved (for AUDIOBOOK)
char playRfidTag[13]; // ID of RFID-tag that started playlist
bool pausePlay: 1; // If pause is active
bool trackFinished: 1; // If current track is finished
bool playlistFinished: 1; // If whole playlist is finished
uint8_t playUntilTrackNumber: 6; // Number of tracks to play after which uC goes to sleep
uint8_t seekmode: 2; // If seekmode is active and if yes: forward or backwards?
bool newPlayMono: 1; // true if mono; false if stereo (helper)
bool currentPlayMono: 1; // true if mono; false if stereo
bool isWebstream: 1; // Indicates if track currenty played is a webstream
uint8_t tellMode: 2; // Tell mode for text to speech announcments
bool currentSpeechActive: 1; // If speech-play is active
bool lastSpeechActive: 1; // If speech-play was active
size_t coverFilePos; // current cover file position
size_t coverFileSize; // current cover file size
uint8_t playMode : 4; // playMode
char title[255]; // current title
bool repeatCurrentTrack : 1; // If current track should be looped
bool repeatPlaylist : 1; // If whole playlist should be looped
uint16_t currentTrackNumber : 9; // Current tracknumber
uint16_t numberOfTracks : 9; // Number of tracks in playlist
unsigned long startAtFilePos; // Offset to start play (in bytes)
double currentRelPos; // Current relative playPosition (in %)
bool sleepAfterCurrentTrack : 1; // If uC should go to sleep after current track
bool sleepAfterPlaylist : 1; // If uC should go to sleep after whole playlist
bool sleepAfter5Tracks : 1; // If uC should go to sleep after 5 tracks
bool saveLastPlayPosition : 1; // If playposition/current track should be saved (for AUDIOBOOK)
char playRfidTag[13]; // ID of RFID-tag that started playlist
bool pausePlay : 1; // If pause is active
bool trackFinished : 1; // If current track is finished
bool playlistFinished : 1; // If whole playlist is finished
uint8_t playUntilTrackNumber : 6; // Number of tracks to play after which uC goes to sleep
uint8_t seekmode : 2; // If seekmode is active and if yes: forward or backwards?
bool newPlayMono : 1; // true if mono; false if stereo (helper)
bool currentPlayMono : 1; // true if mono; false if stereo
bool isWebstream : 1; // Indicates if track currenty played is a webstream
uint8_t tellMode : 2; // Tell mode for text to speech announcments
bool currentSpeechActive : 1; // If speech-play is active
bool lastSpeechActive : 1; // If speech-play was active
size_t coverFilePos; // current cover file position
size_t coverFileSize; // current cover file size
} playProps;

extern playProps gPlayProperties;
Expand Down
6 changes: 3 additions & 3 deletions src/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ inline bool isNumber(const char *str) {
}

inline const char *getPath(File &f) {
#if ESP_ARDUINO_VERSION_MAJOR >= 2
if constexpr (ESP_ARDUINO_VERSION_MAJOR >= 2) {
return f.path();
#else
} else {
return f.name();
#endif
}
}

// Checks if string starts with prefix
Expand Down
71 changes: 35 additions & 36 deletions src/Playlist.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
#pragma once

#include <WString.h>
#include <string.h>
#include <algorithm>
#include "cpp.h"

#if MEM_DEBUG == 1
#warning Memory access guards are enabled. Disable MEM_DEBUG for production builds
#endif
#include <WString.h>
#include <algorithm>
#include <string.h>

using sortFunc = int(*)(const void*,const void*);
using sortFunc = int (*)(const void *, const void *);

class Playlist {
public:
Expand All @@ -25,8 +22,8 @@ class Playlist {
virtual const String getFilename(size_t idx) const = 0;

static int alphabeticSort(const void *x, const void *y) {
const char *a = static_cast<const char*>(x);
const char *b = static_cast<const char*>(y);
const char *a = static_cast<const char *>(x);
const char *b = static_cast<const char *>(y);

return strcmp(a, b);
}
Expand All @@ -37,68 +34,68 @@ class Playlist {

// Check if file-type is correct
static bool fileValid(const String _fileItem) {
constexpr size_t maxExtLen = strlen(*std::max_element(audioFileSufix.begin(), audioFileSufix.end(), [](const char *a, const char *b){
constexpr size_t maxExtLen = strlen(*std::max_element(audioFileSufix.begin(), audioFileSufix.end(), [](const char *a, const char *b) {
return strlen(a) < strlen(b);
}));

if(!_fileItem)
if (!_fileItem) {
return false;
}

// check for http address
if(_fileItem.startsWith("http://") || _fileItem.startsWith("https://")) {
// check for http address
if (_fileItem.startsWith("http://") || _fileItem.startsWith("https://")) {
return true;
}

// Ignore hidden files starting with a '.'
// lastIndex is -1 if '/' is not found --> first index will be 0
int fileNameIndex = _fileItem.lastIndexOf('/') + 1;
if(_fileItem[fileNameIndex] == '.') {
int fileNameIndex = _fileItem.lastIndexOf('/') + 1;
if (_fileItem[fileNameIndex] == '.') {
return false;
}

String extBuf;
const size_t extStart = _fileItem.lastIndexOf('.');
const size_t extLen = _fileItem.length() - extStart;
if(extLen > maxExtLen) {
if (extLen > maxExtLen) {
// we either did not find a . or extension was too long
return false;
}
extBuf = _fileItem.substring(extStart);
extBuf.toLowerCase();

for(const auto e:audioFileSufix) {
if(extBuf.equals(e)) {
for (const auto e : audioFileSufix) {
if (extBuf.equals(e)) {
return true;
}
}
return false;
}

protected:

template <typename T>
class PsramAllocator {
public:
typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef value_type *pointer;
typedef const value_type *const_pointer;
typedef value_type &reference;
typedef const value_type &const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;

public:
template<typename U>
template <typename U>
struct rebind {
typedef PsramAllocator<U> other;
};

public:
inline explicit PsramAllocator() {}
inline ~PsramAllocator() {}
inline PsramAllocator(PsramAllocator const&) {}
template<typename U>
inline explicit PsramAllocator(PsramAllocator<U> const&) {}
inline explicit PsramAllocator() { }
inline ~PsramAllocator() { }
inline PsramAllocator(PsramAllocator const &) { }
template <typename U>
inline explicit PsramAllocator(PsramAllocator<U> const &) { }

// address
inline pointer address(reference r) { return &r; }
Expand All @@ -108,10 +105,10 @@ class Playlist {
// memory allocation
inline pointer allocate(size_type cnt, typename std::allocator<void>::const_pointer = 0) {
T *ptr = nullptr;
if(psramFound()) {
ptr = (T*)ps_malloc(cnt * sizeof(T));
if (psramFound()) {
ptr = (T *) ps_malloc(cnt * sizeof(T));
} else {
ptr = (T*)malloc(cnt * sizeof(T));
ptr = (T *) malloc(cnt * sizeof(T));
}
return ptr;
}
Expand All @@ -126,22 +123,23 @@ class Playlist {
}

// construction/destruction
inline void construct(pointer p, const T& t) {
new(p) T(t);
inline void construct(pointer p, const T &t) {
new (p) T(t);
}

inline void destroy(pointer p) {
p->~T();
}

inline bool operator==(PsramAllocator const& a) { return this == &a; }
inline bool operator!=(PsramAllocator const& a) { return !operator==(a); }
inline bool operator==(PsramAllocator const &a) { return this == &a; }
inline bool operator!=(PsramAllocator const &a) { return !operator==(a); }
};

using pstring = std::basic_string<char, std::char_traits<char>, PsramAllocator<char>>;

virtual void destroy() { }

// clang-format off
static constexpr auto audioFileSufix = std::to_array<const char*>({
".mp3",
".aac",
Expand All @@ -155,4 +153,5 @@ class Playlist {
".pls",
".asx"
});
// clang-format on
};
17 changes: 0 additions & 17 deletions src/Queues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,7 @@
#include "Log.h"
#include "Rfid.h"

#include "Queues.h"
#include "Playlist.h"

SharedObject<int> gVolume;
SharedObject<uint8_t> gTrackControl;
SharedObject<std::unique_ptr<Playlist>> gTrack;
SharedObject<char[cardIdStringSize]> gRfidCard;



QueueHandle_t gVolumeQueue;
QueueHandle_t gTrackQueue;
QueueHandle_t gTrackControlQueue;
QueueHandle_t gRfidCardQueue;

Expand All @@ -35,10 +24,4 @@ void Queues_Init(void) {
if (gTrackControlQueue == NULL) {
Log_Println(unableToCreateMgmtQ, LOGLEVEL_ERROR);
}

char **playlistArray;
gTrackQueue = xQueueCreate(1, sizeof(playlistArray));
if (gTrackQueue == NULL) {
Log_Println(unableToCreatePlayQ, LOGLEVEL_ERROR);
}
}
29 changes: 0 additions & 29 deletions src/Queues.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,3 @@ extern QueueHandle_t gTrackControlQueue;
extern QueueHandle_t gRfidCardQueue;

void Queues_Init(void);

#include <mutex>
#include <memory>
#include "Rfid.h"

template<typename T>
class SharedObject {
public:
SharedObject() = default;
~SharedObject() = default;

const T &get() {
std::lock_guard guard(mutex);
return obj;
}

void put(T newObj) {
std::lock_guard guard(mutex);
obj = newObj;
}

private:
T obj{};
std::mutex mutex{};
};

extern SharedObject<int> gVolume;
extern SharedObject<uint8_t> gTrackControl;
extern SharedObject<char[cardIdStringSize]> gRfidCard;
Loading

0 comments on commit 67f90a7

Please sign in to comment.