-
Notifications
You must be signed in to change notification settings - Fork 38
/
encoding.c
89 lines (79 loc) · 2.62 KB
/
encoding.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "encoding.h"
#include "manchester.h"
#include "fourbsixb.h"
// Passthrough encoder
static void passthrough_init_encoder(EncoderState *state) {
state->passthrough.count = 0;
}
static void passthrough_add_raw_byte(EncoderState *state, uint8_t raw) __reentrant {
state->passthrough.count = 1;
state->passthrough.data = raw;
}
static uint8_t passthrough_next_encoded_byte(EncoderState *state, uint8_t *encoded, bool flush) __reentrant {
if (state->passthrough.count < 1) {
return 0;
}
*encoded = state->passthrough.data;
state->passthrough.count--;
return 1;
}
// passthrough decoder funcs
static void passthrough_init_decoder(DecoderState *state) {
state->passthrough.count = 0;
}
static uint8_t passthrough_add_encoded_byte(DecoderState *state, uint8_t encoded) __reentrant {
state->passthrough.data = encoded;
state->passthrough.count = 1;
if (encoded == 0) {
return 1;
}
return 0;
}
static uint8_t passthrough_next_decoded_byte(DecoderState *state, uint8_t *decoded) __reentrant {
if (state->passthrough.count < 1) {
return 0;
}
*decoded = state->passthrough.data;
state->passthrough.count = 0;
return 1;
}
// Init Encoder
void init_encoder(EncodingType encoding_type, Encoder *encoder, EncoderState *state) {
switch (encoding_type) {
case EncodingTypeNone:
encoder->add_raw_byte = passthrough_add_raw_byte;
encoder->next_encoded_byte = passthrough_next_encoded_byte;
passthrough_init_encoder(state);
break;
case EncodingTypeManchester:
encoder->add_raw_byte = manchester_add_raw_byte;
encoder->next_encoded_byte = manchester_next_encoded_byte;
manchester_init_encoder(state);
break;
case EncodingTypeFourbSixb:
encoder->add_raw_byte = fourbsixb_add_raw_byte;
encoder->next_encoded_byte = fourbsixb_next_encoded_byte;
fourbsixb_init_encoder(state);
break;
}
}
// Init Decoder
void init_decoder(EncodingType encoding_type, Decoder *decoder, DecoderState *state) {
switch (encoding_type) {
case EncodingTypeNone:
decoder->add_encoded_byte = passthrough_add_encoded_byte;
decoder->next_decoded_byte = passthrough_next_decoded_byte;
passthrough_init_decoder(state);
break;
case EncodingTypeManchester:
decoder->add_encoded_byte = manchester_add_encoded_byte;
decoder->next_decoded_byte = manchester_next_decoded_byte;
manchester_init_decoder(state);
break;
case EncodingTypeFourbSixb:
decoder->add_encoded_byte = fourbsixb_add_encoded_byte;
decoder->next_decoded_byte = fourbsixb_next_decoded_byte;
fourbsixb_init_decoder(state);
break;
}
}