Inspired from CodeLobe.com/tools/ascii-compress
Initially wrote in JavaScript by the first author (sysop@codelobe.com) to compress lengthy JSON data & WebGL shader, I converted this code in Python an C languages.
Even if it only works on text compatible with 7 bit ASCII, it is a small & light-weight code. The output is plain ASCII text, and can be stored anywhere or transfered thru any network, as well as BLE serial protocol.
My final objective was to compensate for the slowness of data transfers via BLE (2kb/s maximum): an embedded system stores data on microSD card in ASCII/CSV format; when it is idle (while battery is charging), it compresses these files which will be transferred via BLE in the end; the recipient decompresses and uses this data...
This repository is a Visual Studio Code / Platformio project.
- uncompressed.txt : a sample CSV file
- comressed.txt : the compressed version
- asciiCompressDecompress_original.html, just a extraxtion of the original javascript code
- asciiCompressDecompress.html, where I've extracted and reviewed the javascript code for a better rewriting in another language.
contains the same code written in Python (tested with python3 on MacOS).
Test : the following command lines must be silent: the goal is to compress or decompress and compare the result with what is expected.
python3 ./pythonVersion/asciiCompress.py < ./testFiles/uncompressed.txt > tmp.txt; diff tmp.txt ./testFiles/compressed.txt; rm tmp.txt
python3 ./pythonVersion/asciiDecompress.py < ./testFiles/compressed.txt > tmp.txt; diff tmp.txt ./testFiles/uncompressed.txt; rm tmp.txt
For microcontroller firmware's loop usage, accepts two extra arguments : maxMs and millisecond function. If maxMs == 0, the process compress or decompress in one call. If maxMs > 0, the processus will stops when running more than maxMs milli seconds. Calling it again will make it run maxMs more time, and so on until the job is done.
A sample C program is available : src/main.c
Once inside platformio, "pio run" will compile and generate .pio/build/native/program for your platform.
The following command lines must be silent: the goal is to compress or decompress and compare the result with what is expected.
./.pio/build/native/program compress < ./testFiles/uncompressed.txt > tmp.txt; diff tmp.txt ./testFiles/compressed.txt; rm tmp.txt
or
./.pio/build/native/program compress ./testFiles/uncompressed.txt > tmp.txt ; diff tmp.txt ./testFiles/compressed.txt; rm tmp.txt
./.pio/build/native/program decompress < ./testFiles/compressed.txt > tmp.txt; diff tmp.txt ./testFiles/uncompressed.txt; rm tmp.txt
or
./.pio/build/native/program decompress ./testFiles/compressed.txt > tmp.txt ; diff tmp.txt ./testFiles/uncompressed.txt; rm tmp.txt