Skip to content
Snippets Groups Projects
Commit f0de9096 authored by Moritz Perschke's avatar Moritz Perschke
Browse files

moved caching to namespace, using strings now, compiles

parent c012753a
No related branches found
No related tags found
4 merge requests!39Merge Develop into Main,!19development into master,!17Inital Host, initial Client,!6Espnow
#include "ram_caching.hpp" #include "ram_caching.hpp"
// 2D array of 20 Strings with maxLength of 251 namespace RtcMemory{
RTC_DATA_ATTR char storage[maxSize_][maxLength_]; // 2D array of 20 Strings with maxLength of 251
RTC_DATA_ATTR int headElement = 0; RTC_DATA_ATTR char storage[maxSize_][maxLength_];
RTC_DATA_ATTR int tailElement = 0; RTC_DATA_ATTR int headElement = 0;
RTC_DATA_ATTR int tailElement = 0;
static const char* TAG = "CACHING"; static const char* TAG = "CACHING";
void put(ClientDataPackage data){ void store(std::string message){
// turn data into char array // turn data into char array
const char* jsonString = data.getDataPackageAsMinifiedJsonString().c_str(); const char* jsonString = message.c_str();
// move head to beginning of new element // move head to beginning of new element
// head = (head + (sizeof(char) * 251)) % sizeof(storage); // head = (head + (sizeof(char) * 251)) % sizeof(storage);
headElement = (headElement + 1) % maxSize_; headElement = (headElement + 1) % maxSize_;
// copy data to earliest free block // copy data to earliest free block
memcpy( &storage + headElement*maxSize_, memcpy( &storage + headElement*maxSize_,
jsonString, jsonString,
sizeof(jsonString) sizeof(jsonString)
); );
} }
String get_from_storage(){ String get_from_storage(){
// remove element pointed at by tail // remove element pointed at by tail
String buf = ""; String buf = "";
char current; char current;
for(int i = 0; i<251; i++){ for(int i = 0; i<251; i++){
// i dont like this // i dont like this
memcpy(&current, &storage + (tailElement*maxSize_) + i, sizeof(char)); memcpy(&current, &storage + (tailElement*maxSize_) + i, sizeof(char));
buf += current; buf += current;
if(current = '\0'){ if(current = '\0'){
break; break;
}
} }
// move tail to next element
tailElement = (tailElement + 1) % maxLength_;
Serial.println(buf);
return buf;
} }
// move tail to next element
tailElement = (tailElement + 1) % maxLength_;
Serial.println(buf);
return buf;
} }
//RTC_DATA_ATTR int cachedAmount = -1; //RTC_DATA_ATTR int cachedAmount = -1;
//RTC_DATA_ATTR ClientDataPackage backup[NUM_SENSORS]; //RTC_DATA_ATTR ClientDataPackage backup[NUM_SENSORS];
// //
......
#ifndef _RAM_CACHE #ifndef _RAM_CACHE
#define _RAM_CACHE #define _RAM_CACHE
#include <ClientDataPackage.hpp>
#include "esp_log.h" #include "esp_log.h"
#include <ESP32Time.h> #include <ESP32Time.h>
#define maxSize_ 20 #define maxSize_ 20
#define maxLength_ 251 #define maxLength_ 251
void put(ClientDataPackage data); namespace RtcMemory {
ClientDataPackage get(); void store(std::string message);
String get_from_storage();
}
// bool ram_cache_is_empty(); // bool ram_cache_is_empty();
// bool ram_cache_is_full(); // bool ram_cache_is_full();
......
...@@ -32,7 +32,9 @@ void loop() ...@@ -32,7 +32,9 @@ void loop()
auto messages = drs26.buildMessages(); auto messages = drs26.buildMessages();
for (const Message &message : messages) { for (const Message &message : messages) {
message.send(); if(message.send() != ESP_OK){
RtcMemory::store(message.getMessageAsMinifiedJsonString());
}
} }
} catch (const NoDataAvailableException &e) { } catch (const NoDataAvailableException &e) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment