atomicobject / heatshrink

data compression library for embedded/real-time systems
ISC License
1.31k stars 176 forks source link

[Question] What should `heatshrink_encoder_poll` and `heatshrink_encoder_finish` return if nothing has been sunk? #75

Open cerilloderek opened 1 year ago

cerilloderek commented 1 year ago

New to heatshrink, and I'm running into an issue where I'm getting stuck in a while loop while polling on what I imagine is an empty encoder.

uint8_t buf[32];
heatshrink_encoder_reset(&hse);

// some logic here that reads some file contents and sinks it into the encoder - it's possible that `heatshrink_encoder_sink` is not called at all here

while (heatshrink_encoder_finish(&hse)==HSER_FINISH_MORE) {
    heatshrink_encoder_poll(&hse, buf, 32, &num_compressed_bytes_output);
    // do something with buf
}

In cases where the "some logic" part calls heatshrink_encoder_sink at least once, this works just fine When heatshrink_encoder_sink never gets called, that while loop at the end continues forever and seems to be filling the buf with stuff from memory. I had expected heatshrink_encoder_finish(&hse)==HSER_FINISH_MORE to be true if nothing was sunk into the encoder, but clearly I"m missing something.

My current fix is just to sink a blank string into the encoder before starting to poll, and that seems to work out

        size_t num_bytes_sunk = 0;
        uint8_t dummyBuf[1] = "";
        heatshrink_encoder_sink(&hse, dummyBuf, 1,&num_bytes_sunk);

Any advice on the proper way to poll when there might not be anything sunk into the encoder?

BenBE commented 1 year ago

Cf. issue #57.