How to use Circular Buffer in Flashee?

This may be a noobish question, but how do you use the Circular Buffer mode of the Flashee library (https://github.com/m-mcgowan/spark-flashee-eeprom)? I can get it to store a single String, but if I store two in a row and then try to retrieve them I just get the last String I put in there back twice.

#include "flashee-eeprom.h"
using namespace Flashee;

String text;

void setup() {
    Serial.begin(9600);

    CircularBuffer* buffer = Devices::createCircularBuffer(4096*0, 4096*384);

    text = "Hello world!";
    bool success = buffer->write(&text, sizeof(text));
    Serial.println(success);

    text = "World hello!";
    success = buffer->write(&text, sizeof(text));
    Serial.println(success);

    String output;

    delay(10000);

    success = buffer->read(&output, sizeof(text));
    Serial.println(success);
    Serial.println(output);

    success = buffer->read(&output, sizeof(text));
    Serial.println(success);
    Serial.println(output);
}

Hey @TDeVries!

The problem is that you’re taking the address of a string and using that as a pointer to memory to write to, which is not correct. This will essentially store the binary contents of the string object, which is a pointer to the string data.

To fix this, please use c string arrays, like this:

char text[64];

void setup() {
    
Serial.begin(9600);

    CircularBuffer* buffer = Devices::createCircularBuffer(4096*0, 4096*384);

    strcpy(text, "Hello world!");
    bool success = buffer->write(text, sizeof(text));
    Serial.println(success);

    strcpy(text, "World hello!");
    success = buffer->write(text, sizeof(text));
    Serial.println(success);

    char output[64];

    delay(10000);

    success = buffer->read(output, sizeof(text));
    Serial.println(success);
    Serial.println(output);

    success = buffer->read(output, sizeof(text));
    Serial.println(success);
    Serial.println(output);
}

HTH :smile:

2 Likes

Works perfectly. Thank you! :smile:

1 Like