save db to flash

This commit is contained in:
geeksville
2020-02-07 09:36:15 -08:00
parent 2671599b90
commit 3e87e60d43
10 changed files with 169 additions and 28 deletions

View File

@@ -4,6 +4,7 @@
#include <pb_encode.h>
#include <pb_decode.h>
#include <assert.h>
#include "FS.h"
/// helper function for encoding a record as a protobuf, any failures to encode are fatal and we will panic
/// returns the encoded packet size
@@ -37,3 +38,33 @@ bool pb_decode_from_bytes(const uint8_t *srcbuf, size_t srcbufsize, const pb_msg
return true;
}
}
/// Read from an Arduino File
bool readcb(pb_istream_t *stream, uint8_t *buf, size_t count)
{
File *file = (File *)stream->state;
bool status;
if (buf == NULL)
{
while (count-- && file->read() != EOF)
;
return count == 0;
}
status = (file->read(buf, count) == count);
if (file->available() == 0)
stream->bytes_left = 0;
return status;
}
/// Write to an arduino file
bool writecb(pb_ostream_t *stream, const uint8_t *buf, size_t count)
{
File *file = (File*) stream->state;
return file->write(buf, count) == count;
}