diff options
author | Daniel Friesel <derf@finalrewind.org> | 2016-02-05 17:33:02 +0100 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2016-02-05 17:33:02 +0100 |
commit | f88c1fde18146fd1bf543399da0f6584c3fd4a81 (patch) | |
tree | d93b2d30ac2b83cc739e1ee550179c1792b1e37e /src/fecmodem.cc | |
parent | 4bf560b6f3ef6089af04dfc56d078b9978496b07 (diff) |
add (untested) Hamming forward error correction code and corresponding class
The system now uses a FECModem instance, which inherits the receive methods
etc. from Modem.
Up next: Make the modem's buffer read methods private and expose them in
error-corrected FECModem methods instead
Diffstat (limited to 'src/fecmodem.cc')
-rw-r--r-- | src/fecmodem.cc | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/fecmodem.cc b/src/fecmodem.cc new file mode 100644 index 0000000..5323135 --- /dev/null +++ b/src/fecmodem.cc @@ -0,0 +1,49 @@ +#include <avr/io.h> +#include <stdlib.h> +#include "fecmodem.h" + +uint8_t FECModem::parity128(uint8_t byte) +{ + return pgm_read_byte(&hammingParityLow[byte & 0x0f]) ^ pgm_read_byte(&hammingParityHigh[byte >> 4]); +} + +uint8_t FECModem::parity2416(uint8_t byte1, uint8_t byte2) +{ + return parity128(byte1) | (parity128(byte2) << 4); +} + +uint8_t FECModem::correct128(uint8_t *byte, uint8_t err) +{ + uint8_t result = pgm_read_byte(&hammingParityCheck[err & 0x0f]); + + if (result != NO_ERROR) { + if (result == UNCORRECTABLE || byte == NULL) { + return 3; + } else { + if (result != ERROR_IN_PARITY) { + *byte ^= result; + } + return 1; + } + } + return 0; +} + +uint8_t FECModem::hamming2416(uint8_t *byte1, uint8_t *byte2, uint8_t parity) +{ + uint8_t err; + + if (byte1 == NULL || byte2 == NULL) { + return 3; + } + + err = parity2416(*byte1, *byte2) ^ parity; + + if (err) { + return correct128(byte1, err) + correct128(byte2, err >> 4); + } + + return 0; +} + +FECModem modem; |