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"
// 2D array of 20 Strings with maxLength of 251
RTC_DATA_ATTR char storage[maxSize_][maxLength_];
RTC_DATA_ATTR int headElement = 0;
RTC_DATA_ATTR int tailElement = 0;
namespace RtcMemory{
// 2D array of 20 Strings with maxLength of 251
RTC_DATA_ATTR char storage[maxSize_][maxLength_];
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
const char* jsonString = data.getDataPackageAsMinifiedJsonString().c_str();
// move head to beginning of new element
// head = (head + (sizeof(char) * 251)) % sizeof(storage);
headElement = (headElement + 1) % maxSize_;
// copy data to earliest free block
memcpy( &storage + headElement*maxSize_,
jsonString,
sizeof(jsonString)
);
}
// turn data into char array
const char* jsonString = message.c_str();
// move head to beginning of new element
// head = (head + (sizeof(char) * 251)) % sizeof(storage);
headElement = (headElement + 1) % maxSize_;
// copy data to earliest free block
memcpy( &storage + headElement*maxSize_,
jsonString,
sizeof(jsonString)
);
}
String get_from_storage(){
// remove element pointed at by tail
String buf = "";
char current;
for(int i = 0; i<251; i++){
// i dont like this
memcpy(&current, &storage + (tailElement*maxSize_) + i, sizeof(char));
buf += current;
if(current = '\0'){
break;
String get_from_storage(){
// remove element pointed at by tail
String buf = "";
char current;
for(int i = 0; i<251; i++){
// i dont like this
memcpy(&current, &storage + (tailElement*maxSize_) + i, sizeof(char));
buf += current;
if(current = '\0'){
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 ClientDataPackage backup[NUM_SENSORS];
//
......
#ifndef _RAM_CACHE
#define _RAM_CACHE
#include <ClientDataPackage.hpp>
#include "esp_log.h"
#include <ESP32Time.h>
#define maxSize_ 20
#define maxLength_ 251
void put(ClientDataPackage data);
ClientDataPackage get();
namespace RtcMemory {
void store(std::string message);
String get_from_storage();
}
// bool ram_cache_is_empty();
// bool ram_cache_is_full();
......
......@@ -32,7 +32,9 @@ void loop()
auto messages = drs26.buildMessages();
for (const Message &message : messages) {
message.send();
if(message.send() != ESP_OK){
RtcMemory::store(message.getMessageAsMinifiedJsonString());
}
}
} 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