Browse Source

Allocate heap-memory from PSRAM if available

master
Torsten Stauder 4 years ago
parent
commit
9351fd5e45
  1. 75
      src/main.cpp

75
src/main.cpp

@ -86,7 +86,7 @@
// Serial-logging buffer // Serial-logging buffer
uint8_t serialLoglength = 200; uint8_t serialLoglength = 200;
char *logBuf = (char*) calloc(serialLoglength, sizeof(char)); // Buffer for all log-messages
char *logBuf; // Defintion in setup()
// FilePathLength // FilePathLength
#define MAX_FILEPATH_LENTGH 256 #define MAX_FILEPATH_LENTGH 256
@ -1155,7 +1155,12 @@ char ** returnPlaylistFromWebstream(const char *_webUrl) {
freeMultiCharArray(url, strtoul(*url, NULL, 10)); freeMultiCharArray(url, strtoul(*url, NULL, 10));
} }
if (psramInit()) {
url = (char **) ps_malloc(sizeof(char *) * 2);
} else {
url = (char **) malloc(sizeof(char *) * 2); url = (char **) malloc(sizeof(char *) * 2);
}
url[0] = strdup("1"); // Number of files is always 1 in url-mode url[0] = strdup("1"); // Number of files is always 1 in url-mode
url[1] = strdup(webUrl); url[1] = strdup(webUrl);
@ -1188,7 +1193,11 @@ char ** returnPlaylistFromSD(File _fileOrDirectory) {
// File-mode // File-mode
if (!_fileOrDirectory.isDirectory()) { if (!_fileOrDirectory.isDirectory()) {
if (psramInit()) {
files = (char **) ps_malloc(sizeof(char *) * 2);
} else {
files = (char **) malloc(sizeof(char *) * 2); // +1 because [0] is used for number of elements; [1] -> [n] is used for payload files = (char **) malloc(sizeof(char *) * 2); // +1 because [0] is used for number of elements; [1] -> [n] is used for payload
}
if (files == NULL) { if (files == NULL) {
loggerNl(serialDebug, (char *) FPSTR(unableToAllocateMemForPlaylist), LOGLEVEL_ERROR); loggerNl(serialDebug, (char *) FPSTR(unableToAllocateMemForPlaylist), LOGLEVEL_ERROR);
#ifdef NEOPIXEL_ENABLE #ifdef NEOPIXEL_ENABLE
@ -1199,7 +1208,12 @@ char ** returnPlaylistFromSD(File _fileOrDirectory) {
loggerNl(serialDebug, (char *) FPSTR(fileModeDetected), LOGLEVEL_INFO); loggerNl(serialDebug, (char *) FPSTR(fileModeDetected), LOGLEVEL_INFO);
strncpy(fileNameBuf, (char *) _fileOrDirectory.name(), sizeof(fileNameBuf) / sizeof(fileNameBuf[0])); strncpy(fileNameBuf, (char *) _fileOrDirectory.name(), sizeof(fileNameBuf) / sizeof(fileNameBuf[0]));
if (fileValid(fileNameBuf)) { if (fileValid(fileNameBuf)) {
if (psramInit()) {
files = (char **) ps_malloc(sizeof(char *) * 2);
} else {
files = (char **) malloc(sizeof(char *) * 2); files = (char **) malloc(sizeof(char *) * 2);
}
files[1] = strdup(fileNameBuf); files[1] = strdup(fileNameBuf);
} }
files[0] = strdup("1"); // Number of files is always 1 in file-mode files[0] = strdup("1"); // Number of files is always 1 in file-mode
@ -1210,7 +1224,14 @@ char ** returnPlaylistFromSD(File _fileOrDirectory) {
// Directory-mode // Directory-mode
uint16_t allocCount = 1; uint16_t allocCount = 1;
uint16_t allocSize = 512; uint16_t allocSize = 512;
char *serializedPlaylist = (char*) calloc(allocSize, sizeof(char));
char *serializedPlaylist;
if (psramInit()) {
serializedPlaylist = (char*) ps_calloc(allocSize, sizeof(char));
} else {
serializedPlaylist = (char*) calloc(allocSize, sizeof(char));
}
while (true) { while (true) {
File fileItem = _fileOrDirectory.openNextFile(); File fileItem = _fileOrDirectory.openNextFile();
@ -1252,7 +1273,12 @@ char ** returnPlaylistFromSD(File _fileOrDirectory) {
} }
// Alloc only necessary number of playlist-pointers // Alloc only necessary number of playlist-pointers
if (psramInit()) {
files = (char **) ps_malloc(sizeof(char *) * cnt + 1);
} else {
files = (char **) malloc(sizeof(char *) * cnt + 1); files = (char **) malloc(sizeof(char *) * cnt + 1);
}
if (files == NULL) { if (files == NULL) {
loggerNl(serialDebug, (char *) FPSTR(unableToAllocateMemForPlaylist), LOGLEVEL_ERROR); loggerNl(serialDebug, (char *) FPSTR(unableToAllocateMemForPlaylist), LOGLEVEL_ERROR);
#ifdef NEOPIXEL_ENABLE #ifdef NEOPIXEL_ENABLE
@ -1273,7 +1299,12 @@ char ** returnPlaylistFromSD(File _fileOrDirectory) {
free(serializedPlaylist); free(serializedPlaylist);
if (psramInit()) {
files[0] = (char *) ps_malloc(sizeof(char) * 5);
} else {
files[0] = (char *) malloc(sizeof(char) * 5); files[0] = (char *) malloc(sizeof(char) * 5);
}
if (files[0] == NULL) { if (files[0] == NULL) {
loggerNl(serialDebug, (char *) FPSTR(unableToAllocateMemForPlaylist), LOGLEVEL_ERROR); loggerNl(serialDebug, (char *) FPSTR(unableToAllocateMemForPlaylist), LOGLEVEL_ERROR);
#ifdef NEOPIXEL_ENABLE #ifdef NEOPIXEL_ENABLE
@ -1760,7 +1791,12 @@ void rfidScanner(void *parameter) {
mfrc522.PICC_HaltA(); mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1(); mfrc522.PCD_StopCrypto1();
if (psramInit()) {
cardIdString = (char *) ps_malloc(cardIdSize*3 +1);
} else {
cardIdString = (char *) malloc(cardIdSize*3 +1); cardIdString = (char *) malloc(cardIdSize*3 +1);
}
if (cardIdString == NULL) { if (cardIdString == NULL) {
logger(serialDebug, (char *) FPSTR(unableToAllocateMem), LOGLEVEL_ERROR); logger(serialDebug, (char *) FPSTR(unableToAllocateMem), LOGLEVEL_ERROR);
#ifdef NEOPIXEL_ENABLE #ifdef NEOPIXEL_ENABLE
@ -1828,7 +1864,12 @@ void rfidScanner(void *parameter) {
nfc14443.setupRF(); nfc14443.setupRF();
uint8_t uid[10]; uint8_t uid[10];
if (nfc14443.isCardPresent() && nfc14443.readCardSerial(uid)) { if (nfc14443.isCardPresent() && nfc14443.readCardSerial(uid)) {
if (psramInit()) {
cardIdString = (char *) ps_malloc(cardIdSize*3 +1);
} else {
cardIdString = (char *) malloc(cardIdSize*3 +1); cardIdString = (char *) malloc(cardIdSize*3 +1);
}
if (cardIdString == NULL) { if (cardIdString == NULL) {
logger(serialDebug, (char *) FPSTR(unableToAllocateMem), LOGLEVEL_ERROR); logger(serialDebug, (char *) FPSTR(unableToAllocateMem), LOGLEVEL_ERROR);
#ifdef NEOPIXEL_ENABLE #ifdef NEOPIXEL_ENABLE
@ -1872,7 +1913,12 @@ void rfidScanner(void *parameter) {
// try to read ISO15693 inventory // try to read ISO15693 inventory
ISO15693ErrorCode rc = nfc15693.getInventory(uid); ISO15693ErrorCode rc = nfc15693.getInventory(uid);
if (rc == ISO15693_EC_OK) { if (rc == ISO15693_EC_OK) {
if (psramInit()) {
cardIdString = (char *) ps_malloc(cardIdSize*3 +1);
} else {
cardIdString = (char *) malloc(cardIdSize*3 +1); cardIdString = (char *) malloc(cardIdSize*3 +1);
}
if (cardIdString == NULL) { if (cardIdString == NULL) {
logger(serialDebug, (char *) FPSTR(unableToAllocateMem), LOGLEVEL_ERROR); logger(serialDebug, (char *) FPSTR(unableToAllocateMem), LOGLEVEL_ERROR);
#ifdef NEOPIXEL_ENABLE #ifdef NEOPIXEL_ENABLE
@ -2480,7 +2526,13 @@ void trackControlToQueueSender(const uint8_t trackCommand) {
// Receives de-serialized RFID-data (from NVS) and dispatches playlists for the given // Receives de-serialized RFID-data (from NVS) and dispatches playlists for the given
// playmode to the track-queue. // playmode to the track-queue.
void trackQueueDispatcher(const char *_itemToPlay, const uint32_t _lastPlayPos, const uint32_t _playMode, const uint16_t _trackLastPlayed) { void trackQueueDispatcher(const char *_itemToPlay, const uint32_t _lastPlayPos, const uint32_t _playMode, const uint16_t _trackLastPlayed) {
char *filename = (char *) malloc(sizeof(char) * 255);
char *filename;
if (psramInit()) {
filename = (char *) ps_malloc(sizeof(char) * 255);
} else {
filename = (char *) malloc(sizeof(char) * 255);
}
strncpy(filename, _itemToPlay, 255); strncpy(filename, _itemToPlay, 255);
playProperties.startAtFilePos = _lastPlayPos; playProperties.startAtFilePos = _lastPlayPos;
playProperties.currentTrackNumber = _trackLastPlayed; playProperties.currentTrackNumber = _trackLastPlayed;
@ -3665,9 +3717,14 @@ bool processJsonRequest(char *_serialJson) {
return true; return true;
} }
char *jBuf = (char *) calloc(255, sizeof(char)); // In heap to save static memory
// Sends JSON-answers via websocket // Sends JSON-answers via websocket
void sendWebsocketData(uint32_t client, uint8_t code) { void sendWebsocketData(uint32_t client, uint8_t code) {
char *jBuf;
if (psramInit()) {
jBuf = (char *) ps_calloc(255, sizeof(char));
} else {
jBuf = (char *) calloc(255, sizeof(char)); // In heap to save static memory
}
const size_t CAPACITY = JSON_OBJECT_SIZE(1) + 20; const size_t CAPACITY = JSON_OBJECT_SIZE(1) + 20;
StaticJsonDocument<CAPACITY> doc; StaticJsonDocument<CAPACITY> doc;
JsonObject object = doc.to<JsonObject>(); JsonObject object = doc.to<JsonObject>();
@ -3689,6 +3746,7 @@ void sendWebsocketData(uint32_t client, uint8_t code) {
} else { } else {
ws.printf(client, jBuf); ws.printf(client, jBuf);
} }
free(jBuf);
} }
@ -4085,7 +4143,7 @@ void explorerHandleListRequest(AsyncWebServerRequest *request) {
char filePath[MAX_FILEPATH_LENTGH]; char filePath[MAX_FILEPATH_LENTGH];
JsonArray obj = jsonBuffer.createNestedArray(); JsonArray obj = jsonBuffer.createNestedArray();
File root; File root;
if(request->hasParam("path")){
if (request->hasParam("path")){
param = request->getParam("path"); param = request->getParam("path");
convertUtf8ToAscii(param->value(), filePath); convertUtf8ToAscii(param->value(), filePath);
root = FSystem.open(filePath); root = FSystem.open(filePath);
@ -4465,6 +4523,11 @@ void printWakeUpReason() {
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
if (psramInit()) {
logBuf = (char*) ps_calloc(serialLoglength, sizeof(char)); // Buffer for all log-messages
} else {
logBuf = (char*) calloc(serialLoglength, sizeof(char)); // Buffer for all log-messages
}
#if (WAKEUP_BUTTON <= 39) #if (WAKEUP_BUTTON <= 39)
esp_sleep_enable_ext0_wakeup((gpio_num_t) WAKEUP_BUTTON, 0); esp_sleep_enable_ext0_wakeup((gpio_num_t) WAKEUP_BUTTON, 0);
#endif #endif
@ -4952,7 +5015,7 @@ void setup() {
wifiEnabled = getWifiEnableStatusFromNVS(); wifiEnabled = getWifiEnableStatusFromNVS();
#ifdef BLUETOOTH_ENABLE #ifdef BLUETOOTH_ENABLE
if(operationMode == OPMODE_BLUETOOTH) {
if (operationMode == OPMODE_BLUETOOTH) {
a2dp_sink = new BluetoothA2DPSink(); a2dp_sink = new BluetoothA2DPSink();
i2s_pin_config_t pin_config = { i2s_pin_config_t pin_config = {
.bck_io_num = I2S_BCLK, .bck_io_num = I2S_BCLK,

Loading…
Cancel
Save