Skip to content

Commit

Permalink
updated examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tobozo committed Jan 28, 2025
1 parent 8feb5bc commit 5c03b15
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 159 deletions.
20 changes: 14 additions & 6 deletions examples/Test_deflate/Test_deflate.ino
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ void testStreamToStream()

verify(fzFileName, inputFilename);

tarGzFS.remove(fzFileName);

}


Expand Down Expand Up @@ -90,6 +92,8 @@ void testBufferToBuffer()

verify(fzFileName, inputFilename);

tarGzFS.remove(fzFileName);

}


Expand Down Expand Up @@ -132,6 +136,8 @@ void testStreamToBuffer()

verify(fzFileName, inputFilename);

tarGzFS.remove(fzFileName);

}


Expand Down Expand Up @@ -175,6 +181,8 @@ void testBufferToStream()

verify(fzFileName, inputFilename);

tarGzFS.remove(fzFileName);

}


Expand All @@ -198,12 +206,12 @@ void setup()
{
testStreamToStream(); // tested OK on ESP32/RP2040/ESP8266 (any file size)
printMem();
testBufferToBuffer(); // tested OK on ESP32/RP2040/ESP8266 (small file size)
printMem();
testBufferToStream(); // tested OK on ESP32/RP2040/ESP8266 (small file size)
printMem();
testStreamToBuffer(); // tested OK on ESP32/RP2040/ESP8266 (small file size)
printMem();
// testBufferToBuffer(); // tested OK on ESP32/RP2040/ESP8266 (small file size)
// printMem();
// testBufferToStream(); // tested OK on ESP32/RP2040/ESP8266 (small file size)
// printMem();
// testStreamToBuffer(); // tested OK on ESP32/RP2040/ESP8266 (small file size)
// printMem();

Serial.println();
Serial.println("All tests completed");
Expand Down
167 changes: 99 additions & 68 deletions examples/Test_tar_packer/Test_tar_packer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,54 @@
//#define DEST_FS_USES_FFAT // ESP32 only
//#define DEST_FS_USES_SD_MMC // ESP32 only

// #define ESP32_TARGZ_DISABLE_COMPRESSION
// #define ESP32_TARGZ_DISABLE_DECOMPRESSION
#include <ESP32-targz.h>

// Uncomment this macro to download the generated files when using SPIFFS/LittleFS
// Comment out this macro if you get a "sketch too big" compilation error
// Uncomment `USE_WEBSERVER` to download the generated files when using SPIFFS/LittleFS
// Comment out `USE_WEBSERVER` if you get a "sketch too big" compilation error
#define USE_WEBSERVER
// Comment out `USE_ETHERNET` to use WiFi instead of Ethernet
// #define USE_ETHERNET

#if defined USE_WEBSERVER
#include <WiFi.h>
#if !defined USE_ETHERNET
#include <WiFi.h>
#else
#include <ETH.h>

static bool eth_connected = false;

// WARNING: onEvent is called from a separate FreeRTOS task (thread)!
void onEvent(arduino_event_id_t event) {
switch (event) {
case ARDUINO_EVENT_ETH_START: Serial.println("ETH Started"); ETH.setHostname("esp32-ethernet"); break;
case ARDUINO_EVENT_ETH_CONNECTED: Serial.println("ETH Connected"); break;
case ARDUINO_EVENT_ETH_GOT_IP: Serial.println("ETH Got IP"); Serial.println(ETH); eth_connected = true; break;
case ARDUINO_EVENT_ETH_LOST_IP: Serial.println("ETH Lost IP"); eth_connected = false; break;
case ARDUINO_EVENT_ETH_DISCONNECTED: Serial.println("ETH Disconnected"); eth_connected = false; break;
case ARDUINO_EVENT_ETH_STOP: Serial.println("ETH Stopped"); eth_connected = false; break;
default: break;
}
}

#endif

#include <WebServer.h>
#include <ESPmDNS.h>
WebServer server(80);
#endif



const char* src_path = "/"; // source folder (no ending slash except if root)
const char* tar_path = "/test.tar"; // output tar archive to create
const char* targz_path = "/test.tar.gz"; // output tar archive to create
const char* dst_path = "data"; // optional virtual folder in the tar archive, no ending slash, set to NULL to put everything in the root
const char* dst_path = nullptr; // optional virtual folder in the tar archive, no ending slash, set to NULL to put everything in the root

std::vector<TAR::dir_entity_t> dirEntities; // storage for scanned dir entities


void removeTempFiles()
{
Serial.println("Cleaning up temporary files");
// cleanup test from other examples to prevent SPIFFS from exploding :)
if(tarGzFS.exists(tar_path))
tarGzFS.remove(tar_path);
Expand All @@ -39,103 +61,112 @@ void removeTempFiles()
}


bool testTarGzPackerFS()
void testTarPacker()
{
// Read source directory "/", append content under "data/" in tar output, and compress to "test.tar.gz"
// src_path: source, path to dir (will recursively fetch files)
// targz_path: destination, .tar.gz compressed file
// dst_path: optional, rootdir name in tar archive
// NOTE: Source and destination are on the same filesystem.
removeTempFiles();
// tar_path: destination, .tar packed file
// dst_path: optional path prefix in tar archive
removeTempFiles(); // cleanup previous examples
Serial.println();
Serial.println("TarGzPacker::compress(&tarGzFS, src_path, targz_path, dst_path )");
size_t dstLen = TarGzPacker::compress(&tarGzFS, src_path, targz_path, dst_path );
Serial.printf("Source folder '%s' compressed to %d bytes in %s\n", src_path, dstLen, targz_path);
Serial.printf("Free heap: %lu bytes\n", ESP.getFreeHeap() );
return dstLen > 0;
Serial.printf("TarPacker::pack_files(&tarGzFS, dirEntities, &tarGzFS, tar_path, dst_path); Free heap: %lu bytes\n", HEAP_AVAILABLE() );
TarPacker::setProgressCallBack( LZPacker::defaultProgressCallback );
auto ret = TarPacker::pack_files(&tarGzFS, dirEntities, &tarGzFS, tar_path, dst_path);
Serial.printf("Wrote %d bytes to %s\n", ret, tar_path);
}

void testTarPackerStream()
{
// tar_path: destination, .tar packed file
// dst_path: optional path prefix in tar archive
removeTempFiles(); // cleanup previous examples
Serial.println();
Serial.printf("TarPacker::pack_files(&tarGzFS, dirEntities, &tar, dst_path); Free heap: %lu bytes\n", HEAP_AVAILABLE() );
TarPacker::setProgressCallBack( LZPacker::defaultProgressCallback );
auto tar = tarGzFS.open(tar_path, "w");
if(!tar)
return;
auto ret = TarPacker::pack_files(&tarGzFS, dirEntities, &tar, dst_path);
tar.close();
Serial.printf("Wrote %d bytes to %s\n", ret, tar_path);
}


bool testTarGzPackerStream()
void testTarGzPacker()
{
// Read source directory "/", put content under "data/" in tar archive, and compress to stream
// src_path: source, path to dir (will recursively fetch files)
// targz_path: optional, name of the .tar.gz compressed file to prevent self-inclusion
// dst_path: optional, rootdir name in tar archive
removeTempFiles();
removeTempFiles(); // cleanup previous examples
// targz_path: name of the .tar.gz compressed file
// dst_path: optional path prefix in tar archive
Serial.println();
Serial.printf("TarGzPacker::compress( &tarStream, &tarGzOutput ); Free heap: %lu bytes\n", ESP.getFreeHeap() );
TarGzStream tarStream(&tarGzFS, &dirEntities, src_path, targz_path, dst_path);
Serial.printf("Free heap: %lu bytes\n", ESP.getFreeHeap() );
size_t srcLen = tarStream.size();
File tarGzOutput = tarGzFS.open(targz_path, "w"); // NOTE: tarGzOutput can also be a network client, or even Serial
size_t dstLen = TarGzPacker::compress( &tarStream, &tarGzOutput );
tarGzOutput.close();
Serial.printf("Compressed %d bytes to %s (%d bytes)\n", srcLen, targz_path, dstLen);
Serial.printf("Free heap: %lu bytes\n", ESP.getFreeHeap() );
return dstLen > 0;
Serial.printf("TarGzPacker::compress(&tarGzFS, dirEntities, &tarGzFS, targz_path, dst_path); Free heap: %lu bytes\n", HEAP_AVAILABLE() );
auto ret = TarGzPacker::compress(&tarGzFS, dirEntities, &tarGzFS, targz_path, dst_path);
Serial.printf("Wrote %d bytes to %s\n", ret, targz_path);
}


bool testTarPacker()
void testTarGzPackerStream()
{
// Read source directory "/", put contents under "data/" in tar archive, and pack to "test.tar"
// src_path: source, path to dir (will recursively fetch files)
// tar_path: destination, .tar packed file
// NOTE: Source and destination are on the same filesystem.
removeTempFiles();
// targz_path: name of the .tar.gz compressed file
// dst_path: optional path prefix in tar archive
removeTempFiles(); // cleanup previous examples
Serial.println();
Serial.printf("TarPacker::pack_files(&tarGzFS, &dirEntities, tar_path, dst_path); Free heap: %lu bytes\n", ESP.getFreeHeap() );
ssize_t dstLen = TarPacker::pack_files(&tarGzFS, &dirEntities, tar_path, dst_path);
if( dstLen <= 0 ) {// test failed
Serial.printf("Packing failed (ret=%d)\n", dstLen);
return false;
}
File tar = tarGzFS.open(tar_path);
size_t tar_size = tar.size();
tar.close();
Serial.printf("Wrote %d bytes to %s, file size=%d\n", dstLen, tar_path, tar_size );
return ( tar_size == dstLen );
Serial.printf("TarGzPacker::compress(&tarGzFS, dirEntities, &gz, dst_path); Free heap: %lu bytes\n", HEAP_AVAILABLE() );
auto gz = tarGzFS.open(targz_path, "w");
if(!gz)
return;
auto ret = TarGzPacker::compress(&tarGzFS, dirEntities, &gz, dst_path);
gz.close();
Serial.printf("Wrote %d bytes to %s\n", ret, targz_path);
}


void setup()
{
Serial.begin(115200);
Serial.println("TarPacker test");

delay(5000); // NOTE: USB-CDC is a drag

Serial.println("TarPacker/TarGzPacker test");

if(! tarGzFS.begin() ) {
Serial.println("Failed to start filesystem, halting");
while(1) yield();
}

removeTempFiles(); // cleanup previous examples
Serial.println("Gathering directory entitites");
TarPacker::collectDirEntities(&dirEntities, &tarGzFS, src_path, 3); // collect dir and files at %{src_path}
LZPacker::setProgressCallBack( LZPacker::defaultProgressCallback );

Serial.printf("Free heap: %lu bytes\n", ESP.getFreeHeap() );
Serial.printf("Free heap: %lu bytes\n", HEAP_AVAILABLE() );

// testTarGzPackerFS();
testTarPacker();
testTarPackerStream();
testTarGzPacker();
testTarGzPackerStream();
// testTarPacker();

Serial.printf("Free heap: %lu bytes\n", ESP.getFreeHeap() );
Serial.printf("Free heap: %lu bytes\n", HEAP_AVAILABLE() );


#if defined USE_WEBSERVER
// start WiFI
WiFi.mode(WIFI_STA);
WiFi.begin();
printf("Connect to WiFi...\n");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
printf(".");
}
printf("connected.\n");
#if !defined USE_ETHERNET // start WiFI
WiFi.mode(WIFI_STA);
WiFi.begin();
Serial.printf("Connect to WiFi...\n");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.printf(".");
}
Serial.printf("connected.\n");
Serial.printf("open <http://%s> or <http://%s>\n", WiFi.getHostname(), WiFi.localIP().toString().c_str());
#else // start ethernet
Network.onEvent(onEvent); // Will call onEvent() from another thread.
ETH.begin();
while (!eth_connected) {
delay(500);
printf(".");
}
#endif
// start webserver, serve filesystem at root
server.serveStatic("/", tarGzFS, "/", nullptr);
server.begin();
printf("open <http://%s> or <http://%s>\n", WiFi.getHostname(), WiFi.localIP().toString().c_str());
#endif
}

Expand Down
Binary file removed examples/Test_tar_packer/data/ESP32-targz.bmp
Binary file not shown.
Loading

0 comments on commit 5c03b15

Please sign in to comment.