My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

heatshrink_decoder.h 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * libs/heatshrink/heatshrink_decoder.h
  24. */
  25. #pragma once
  26. #include "heatshrink_common.h"
  27. #include "heatshrink_config.h"
  28. #include <stdint.h>
  29. #include <stddef.h>
  30. typedef enum {
  31. HSDR_SINK_OK, /* data sunk, ready to poll */
  32. HSDR_SINK_FULL, /* out of space in internal buffer */
  33. HSDR_SINK_ERROR_NULL=-1, /* NULL argument */
  34. } HSD_sink_res;
  35. typedef enum {
  36. HSDR_POLL_EMPTY, /* input exhausted */
  37. HSDR_POLL_MORE, /* more data remaining, call again w/ fresh output buffer */
  38. HSDR_POLL_ERROR_NULL=-1, /* NULL arguments */
  39. HSDR_POLL_ERROR_UNKNOWN=-2,
  40. } HSD_poll_res;
  41. typedef enum {
  42. HSDR_FINISH_DONE, /* output is done */
  43. HSDR_FINISH_MORE, /* more output remains */
  44. HSDR_FINISH_ERROR_NULL=-1, /* NULL arguments */
  45. } HSD_finish_res;
  46. #if HEATSHRINK_DYNAMIC_ALLOC
  47. #define HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(BUF) \
  48. ((BUF)->input_buffer_size)
  49. #define HEATSHRINK_DECODER_WINDOW_BITS(BUF) \
  50. ((BUF)->window_sz2)
  51. #define HEATSHRINK_DECODER_LOOKAHEAD_BITS(BUF) \
  52. ((BUF)->lookahead_sz2)
  53. #else
  54. #define HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(_) \
  55. HEATSHRINK_STATIC_INPUT_BUFFER_SIZE
  56. #define HEATSHRINK_DECODER_WINDOW_BITS(_) \
  57. (HEATSHRINK_STATIC_WINDOW_BITS)
  58. #define HEATSHRINK_DECODER_LOOKAHEAD_BITS(BUF) \
  59. (HEATSHRINK_STATIC_LOOKAHEAD_BITS)
  60. #endif
  61. typedef struct {
  62. uint16_t input_size; /* bytes in input buffer */
  63. uint16_t input_index; /* offset to next unprocessed input byte */
  64. uint16_t output_count; /* how many bytes to output */
  65. uint16_t output_index; /* index for bytes to output */
  66. uint16_t head_index; /* head of window buffer */
  67. uint8_t state; /* current state machine node */
  68. uint8_t current_byte; /* current byte of input */
  69. uint8_t bit_index; /* current bit index */
  70. #if HEATSHRINK_DYNAMIC_ALLOC
  71. /* Fields that are only used if dynamically allocated. */
  72. uint8_t window_sz2; /* window buffer bits */
  73. uint8_t lookahead_sz2; /* lookahead bits */
  74. uint16_t input_buffer_size; /* input buffer size */
  75. /* Input buffer, then expansion window buffer */
  76. uint8_t buffers[];
  77. #else
  78. /* Input buffer, then expansion window buffer */
  79. uint8_t buffers[(1 << HEATSHRINK_DECODER_WINDOW_BITS(_)) + HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(_)];
  80. #endif
  81. } heatshrink_decoder;
  82. #if HEATSHRINK_DYNAMIC_ALLOC
  83. /* Allocate a decoder with an input buffer of INPUT_BUFFER_SIZE bytes,
  84. * an expansion buffer size of 2^WINDOW_SZ2, and a lookahead
  85. * size of 2^lookahead_sz2. (The window buffer and lookahead sizes
  86. * must match the settings used when the data was compressed.)
  87. * Returns NULL on error. */
  88. heatshrink_decoder *heatshrink_decoder_alloc(uint16_t input_buffer_size, uint8_t expansion_buffer_sz2, uint8_t lookahead_sz2);
  89. /* Free a decoder. */
  90. void heatshrink_decoder_free(heatshrink_decoder *hsd);
  91. #endif
  92. /* Reset a decoder. */
  93. void heatshrink_decoder_reset(heatshrink_decoder *hsd);
  94. /* Sink at most SIZE bytes from IN_BUF into the decoder. *INPUT_SIZE is set to
  95. * indicate how many bytes were actually sunk (in case a buffer was filled). */
  96. HSD_sink_res heatshrink_decoder_sink(heatshrink_decoder *hsd, uint8_t *in_buf, size_t size, size_t *input_size);
  97. /* Poll for output from the decoder, copying at most OUT_BUF_SIZE bytes into
  98. * OUT_BUF (setting *OUTPUT_SIZE to the actual amount copied). */
  99. HSD_poll_res heatshrink_decoder_poll(heatshrink_decoder *hsd, uint8_t *out_buf, size_t out_buf_size, size_t *output_size);
  100. /* Notify the dencoder that the input stream is finished.
  101. * If the return value is HSDR_FINISH_MORE, there is still more output, so
  102. * call heatshrink_decoder_poll and repeat. */
  103. HSD_finish_res heatshrink_decoder_finish(heatshrink_decoder *hsd);