From 864c5d7dc4025f22fbd50300c8816e8e5b0e195c Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Sun, 27 Apr 2014 23:06:05 +0200 Subject: [PATCH] Compression on/off compile option. --- Modem/config.h | 2 +- Modem/main.c | 2 +- Modem/protocol/mp1.c | 60 +++++++++++++++++++++++++++----------------- Modem/protocol/mp1.h | 11 ++++++-- buildrev.h | 2 +- 5 files changed, 49 insertions(+), 28 deletions(-) diff --git a/Modem/config.h b/Modem/config.h index 794b85e..d3e171a 100644 --- a/Modem/config.h +++ b/Modem/config.h @@ -19,7 +19,7 @@ #define CONFIG_AFSK_RXTIMEOUT 0 // How long a read operation from the modem // will wait for data before timing out. -#define CONFIG_AFSK_PREAMBLE_LEN 350UL // The length of the packet preamble in milliseconds +#define CONFIG_AFSK_PREAMBLE_LEN 150UL // The length of the packet preamble in milliseconds #define CONFIG_AFSK_TRAILER_LEN 50UL // The length of the packet tail in milliseconds #endif \ No newline at end of file diff --git a/Modem/main.c b/Modem/main.c index 51b368f..7851ca4 100644 --- a/Modem/main.c +++ b/Modem/main.c @@ -28,7 +28,7 @@ static Serial ser; // Declare a serial interface struct // for the ADC (this is A0 on arduino) -static uint8_t serialBuffer[MP1_MAX_DATA_SIZE]; // This is a buffer for incoming serial data +static uint8_t serialBuffer[MP1_MAX_DATA_SIZE]; // This is a buffer for incoming serial data static int sbyte; // For holding byte read from serial port static size_t serialLen = 0; // Counter for counting length of data from serial diff --git a/Modem/protocol/mp1.c b/Modem/protocol/mp1.c index 36659c3..4fcc95d 100644 --- a/Modem/protocol/mp1.c +++ b/Modem/protocol/mp1.c @@ -20,11 +20,15 @@ static uint8_t lastByte = 0x00; // We also need a buffer for compressing and // decompressing packet data. -static uint8_t compressionBuffer[MP1_MAX_FRAME_LENGTH+10]; +#if MP1_ENABLE_COMPRESSION + static uint8_t compressionBuffer[MP1_MAX_DATA_SIZE]; +#endif +#if SERIAL_DEBUG // An int to hold amount of free RAM updated // by the FREE_RAM function; static int FREE_RAM; +#endif // The GET_BIT macro is used in the interleaver // and deinterleaver to access single bits of a @@ -84,14 +88,16 @@ static void mp1Decode(MP1 *mp1) { packet.dataLength = mp1->packetLength - 2 - (header & MP1_HEADER_PADDED)*padding; // Check if we have received a compressed packet - if (header & MP1_HEADER_COMPRESSION) { + if (MP1_ENABLE_COMPRESSION && (header & MP1_HEADER_COMPRESSION)) { // If we have, we decompress it and use the // decompressed data for the packet - if (SERIAL_DEBUG) kprintf("[CS=%d] ", packet.dataLength); - size_t decompressedSize = decompress(buffer, packet.dataLength); - if (SERIAL_DEBUG) kprintf("[DS=%d]", decompressedSize); - packet.dataLength = decompressedSize; - memcpy(mp1->buffer, compressionBuffer, decompressedSize); + #if MP1_ENABLE_COMPRESSION + if (SERIAL_DEBUG) kprintf("[CS=%d] ", packet.dataLength); + size_t decompressedSize = decompress(buffer, packet.dataLength); + if (SERIAL_DEBUG) kprintf("[DS=%d]", decompressedSize); + packet.dataLength = decompressedSize; + memcpy(mp1->buffer, compressionBuffer, decompressedSize); + #endif } else { // If the packet was not compressed, we shift // the data in our buffer back down to the actual @@ -420,22 +426,24 @@ void mp1Send(MP1 *mp1, void *_buffer, size_t length) { // We then try to compress the data to see // if we can save some space with compression. - size_t compressedSize = compress(buffer, length); - if (compressedSize != 0 && compressedSize < length) { - // Compression saved us some space, we'll - // send the paket compressed - packetCompression = true; - // Write the compressed data into the - // outgoing data buffer - memcpy(buffer, compressionBuffer, compressedSize); - - // Make sure to set the length of the - // data to the new (compressed) length - length = compressedSize; - } else { - // We are not going to use compression, - // so we don't do anything. - } + #if MP1_ENABLE_COMPRESSION + size_t compressedSize = compress(buffer, length); + if (compressedSize != 0 && compressedSize < length) { + // Compression saved us some space, we'll + // send the paket compressed + packetCompression = true; + // Write the compressed data into the + // outgoing data buffer + memcpy(buffer, compressionBuffer, compressedSize); + + // Make sure to set the length of the + // data to the new (compressed) length + length = compressedSize; + } else { + // We are not going to use compression, + // so we don't do anything. + } + #endif // Transmit the HDLC_FLAG to signify start of TX kfile_putc(HDLC_FLAG, mp1->modem); @@ -568,15 +576,18 @@ bool mp1CarrierSense(MP1 *mp1) { // A handy debug function that can determine // how much available memory we have left. +#if SERIAL_DEBUG int freeRam(void) { extern int __heap_start, *__brkval; int v; FREE_RAM = (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); return FREE_RAM; } +#endif // This function compresses data using // the Heatshrink library +#if MP1_ENABLE_COMPRESSION size_t compress(uint8_t *input, size_t length) { heatshrink_encoder *hse = heatshrink_encoder_alloc(8, 4); if (hse == NULL) { @@ -601,9 +612,11 @@ size_t compress(uint8_t *input, size_t length) { heatshrink_encoder_free(hse); return written; } +#endif // This function decompresses data using // the Heatshrink library +#if MP1_ENABLE_COMPRESSION size_t decompress(uint8_t *input, size_t length) { heatshrink_decoder *hsd = heatshrink_decoder_alloc(MP1_MAX_FRAME_LENGTH, 8, 4); if (hsd == NULL) { @@ -628,6 +641,7 @@ size_t decompress(uint8_t *input, size_t length) { heatshrink_decoder_free(hsd); return written; } +#endif // Following is the functions responsible diff --git a/Modem/protocol/mp1.h b/Modem/protocol/mp1.h index 7b15a1d..f5fae32 100644 --- a/Modem/protocol/mp1.h +++ b/Modem/protocol/mp1.h @@ -4,9 +4,17 @@ #include #include +// Options +#define MP1_ENABLE_COMPRESSION false +#define MP1_ENABLE_CSMA true + // Frame sizing & checksum #define MP1_INTERLEAVE_SIZE 12 -#define MP1_MAX_FRAME_LENGTH 22 * MP1_INTERLEAVE_SIZE +#if MP1_ENABLE_COMPRESSION + #define MP1_MAX_FRAME_LENGTH 22 * MP1_INTERLEAVE_SIZE +#else + #define MP1_MAX_FRAME_LENGTH 56 * MP1_INTERLEAVE_SIZE +#endif #define MP1_HEADER_SIZE 1 #define MP1_CHECKSUM_SIZE 1 #define MP1_MAX_DATA_SIZE MP1_MAX_FRAME_LENGTH - MP1_HEADER_SIZE - MP1_CHECKSUM_SIZE @@ -16,7 +24,6 @@ // These two parameters are used for // P-persistent CSMA -#define MP1_ENABLE_CSMA false #define MP1_SETTLE_TIME 100UL // The minimum wait time before considering sending #define MP1_P_PERSISTENCE 85UL // The probability (between 0 and 255) for sending #define MP1_TXDELAY 150UL // Delay between turning on the transmitter and sending diff --git a/buildrev.h b/buildrev.h index d1b1ff9..b27b4ef 100644 --- a/buildrev.h +++ b/buildrev.h @@ -1,2 +1,2 @@ -#define VERS_BUILD 1576 +#define VERS_BUILD 1598 #define VERS_HOST "shard"