Skip to content

Commit

Permalink
Try and fix SPIFFS not always writing files
Browse files Browse the repository at this point in the history
  • Loading branch information
cgreening committed Oct 3, 2021
1 parent 446247e commit b6a0b68
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 34 deletions.
27 changes: 20 additions & 7 deletions lib/Epub/EpubList/EpubList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,34 @@ bool EpubList::load(const char *path)
void EpubList::dehydrate()
{
ESP_LOGI(TAG, "Dehydrating epub list");
FILE *fp = fopen("/fs/.epublist", "wb");
fwrite(state, sizeof(EpubListState), 1, fp);
fclose(fp);
ESP_LOGI(TAG, "Dehydrated epub list");
FILE *fp = fopen("/fs/.epublist", "w");
if (fp)
{
int written = fwrite(state, sizeof(EpubListState), 1, fp);
if (written == 0)
{
ESP_LOGI(TAG, "Failed to write epub list state - trying again");
// try again?
written = fwrite(state, sizeof(EpubListState), 1, fp);
}
fclose(fp);
ESP_LOGI(TAG, "Dehydrated epub list %d", written);
}
else
{
ESP_LOGE(TAG, "Failed to dehydrate epub list");
}
}

bool EpubList::hydrate()
{
ESP_LOGI(TAG, "Hydrating epub list");
FILE *fp = fopen("/fs/.epublist", "rb");
FILE *fp = fopen("/fs/.epublist", "r");
if (fp)
{
fread(state, sizeof(EpubListState), 1, fp);
int read = fread(state, sizeof(EpubListState), 1, fp);
fclose(fp);
ESP_LOGI(TAG, "Hydrated epub list");
ESP_LOGI(TAG, "Hydrated epub list %d", read);
return true;
}
ESP_LOGE(TAG, "Failed to hydrate");
Expand Down
74 changes: 53 additions & 21 deletions lib/Epub/Renderer/EpdRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,33 +157,47 @@ class EpdRenderer : public Renderer
{
ESP_LOGI("EPD", "Dehydrating EPD");
// save the two buffers - the front and the back buffers
size_t compressed_size = 0;
void *compressed = tdefl_compress_mem_to_heap(m_frame_buffer, EPD_WIDTH * EPD_HEIGHT / 2, &compressed_size, 0);
if (compressed)
size_t front_buffer_compressed_size = 0;
void *front_buffer_compressed = tdefl_compress_mem_to_heap(m_frame_buffer, EPD_WIDTH * EPD_HEIGHT / 2, &front_buffer_compressed_size, 0);
if (front_buffer_compressed)
{
ESP_LOGD("EPD", "Front buffer compressed size: %d", compressed_size);
FILE *fp = fopen("/fs/front_buffer.z", "wb");
if (fp)
ESP_LOGI("EPD", "Front buffer compressed size: %d", front_buffer_compressed_size);
FILE *front_fp = fopen("/fs/front_buffer.z", "w");
if (front_fp)
{
fwrite(compressed, 1, compressed_size, fp);
fclose(fp);
int written = fwrite(front_buffer_compressed, 1, front_buffer_compressed_size, front_fp);
if (written == 0)
{
// try again?
ESP_LOGI("EPD", "Retrying front buffer save");
written = fwrite(front_buffer_compressed, 1, front_buffer_compressed_size, front_fp);
}
fclose(front_fp);
ESP_LOGI("EPD", "Front buffer saved %d", written);
}
free(compressed);
free(front_buffer_compressed);
}
else
{
ESP_LOGE("EPD", "Failed to compress front buffer");
}
compressed_size = 0;
compressed = tdefl_compress_mem_to_heap(m_hl.back_fb, EPD_WIDTH * EPD_HEIGHT / 2, &compressed_size, 0);
if (compressed)
size_t back_buffer_compressed_size = 0;
void *back_buffer_compressed = tdefl_compress_mem_to_heap(m_hl.back_fb, EPD_WIDTH * EPD_HEIGHT / 2, &back_buffer_compressed_size, 0);
if (back_buffer_compressed)
{
ESP_LOGD("EPD", "Back buffer compressed size: %d", compressed_size);
FILE *fp = fopen("/fs/back_buffer.z", "wb");
if (fp)
ESP_LOGI("EPD", "Back buffer compressed size: %d", back_buffer_compressed_size);
FILE *back_fp = fopen("/fs/back_buffer.z", "w");
if (back_fp)
{
fwrite(compressed, 1, compressed_size, fp);
fclose(fp);
int written = fwrite(back_buffer_compressed, 1, back_buffer_compressed_size, back_fp);
if (written == 0)
{
// try again?
ESP_LOGI("EPD", "Retrying back buffer save");
written = fwrite(back_buffer_compressed, 1, back_buffer_compressed_size, back_fp);
}
fclose(back_fp);
ESP_LOGI("EPD", "Back buffer saved %d", written);
}
}
else
Expand All @@ -197,19 +211,28 @@ class EpdRenderer : public Renderer
{
ESP_LOGI("EPD", "Hydrating EPD");
// load the two buffers - the front and the back buffers
FILE *fp = fopen("/fs/front_buffer.z", "rb");
FILE *fp = fopen("/fs/front_buffer.z", "r");
if (fp)
{
fseek(fp, 0, SEEK_END);
size_t compressed_size = ftell(fp);
ESP_LOGI("EPD", "Front buffer compressed size: %d", compressed_size);
fseek(fp, 0, SEEK_SET);
void *compressed = malloc(compressed_size);
if (compressed)
{
fread(compressed, 1, compressed_size, fp);
tinfl_decompress_mem_to_mem(m_hl.front_fb, EPD_HEIGHT * EPD_WIDTH / 2, compressed, compressed_size, 0);
int result = tinfl_decompress_mem_to_mem(m_hl.front_fb, EPD_HEIGHT * EPD_WIDTH / 2, compressed, compressed_size, 0);
if (result == TINFL_DECOMPRESS_MEM_TO_MEM_FAILED)
{
ESP_LOGE("EPD", "Failed to decompress front buffer");
}
free(compressed);
}
else
{
ESP_LOGE("EPD", "Failed to allocate memory for front buffer");
}
fclose(fp);
}
else
Expand All @@ -218,19 +241,28 @@ class EpdRenderer : public Renderer
reset();
return;
}
fp = fopen("/fs/back_buffer.z", "rb");
fp = fopen("/fs/back_buffer.z", "r");
if (fp)
{
fseek(fp, 0, SEEK_END);
size_t compressed_size = ftell(fp);
fseek(fp, 0, SEEK_SET);
ESP_LOGI("EPD", "Back buffer compressed size: %d", compressed_size);
void *compressed = malloc(compressed_size);
if (compressed)
{
fread(compressed, 1, compressed_size, fp);
tinfl_decompress_mem_to_mem(m_hl.back_fb, EPD_HEIGHT * EPD_WIDTH / 2, compressed, compressed_size, 0);
int result = tinfl_decompress_mem_to_mem(m_hl.back_fb, EPD_HEIGHT * EPD_WIDTH / 2, compressed, compressed_size, 0);
if (result == TINFL_DECOMPRESS_MEM_TO_MEM_FAILED)
{
ESP_LOGE("EPD", "Failed to decompress back buffer");
}
free(compressed);
}
else
{
ESP_LOGE("EPD", "Failed to allocate memory for back buffer");
}
fclose(fp);
}
else
Expand Down
1 change: 1 addition & 0 deletions lib/Epub/RubbishHtmlParser/RubbishHtmlParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,6 @@ void RubbishHtmlParser::layout(Renderer *renderer, Epub *epub)
void RubbishHtmlParser::render_page(int page_index, Renderer *renderer, Epub *epub)
{
renderer->clear_screen();
// renderer->fill_rect(0, 0, renderer->get_page_width(), renderer->get_page_height(), 0xFF);
pages[page_index]->render(renderer, epub);
}
2 changes: 1 addition & 1 deletion sdkconfig.lilygo_t5_47
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,7 @@ CONFIG_SPIFFS_CACHE_WR=y
# end of SPIFFS Cache Configuration

CONFIG_SPIFFS_PAGE_CHECK=y
CONFIG_SPIFFS_GC_MAX_RUNS=10
CONFIG_SPIFFS_GC_MAX_RUNS=20
# CONFIG_SPIFFS_GC_STATS is not set
CONFIG_SPIFFS_PAGE_SIZE=256
CONFIG_SPIFFS_OBJ_NAME_LEN=32
Expand Down
6 changes: 1 addition & 5 deletions sdkconfig.lilygo_t5_47.old
Original file line number Diff line number Diff line change
Expand Up @@ -873,11 +873,7 @@ CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN=4096
#
# Certificate Bundle
#
CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=y
CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=y
# CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_CMN is not set
# CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_NONE is not set
# CONFIG_MBEDTLS_CUSTOM_CERTIFICATE_BUNDLE is not set
# CONFIG_MBEDTLS_CERTIFICATE_BUNDLE is not set
# end of Certificate Bundle

# CONFIG_MBEDTLS_ECP_RESTARTABLE is not set
Expand Down

0 comments on commit b6a0b68

Please sign in to comment.