2022-01-05 16:10:18 +00:00
|
|
|
#include "manchester_encoder.h"
|
2024-03-19 14:43:52 +00:00
|
|
|
#include <furi.h>
|
2021-10-10 14:35:10 +00:00
|
|
|
|
|
|
|
void manchester_encoder_reset(ManchesterEncoderState* state) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(state);
|
2021-10-10 14:35:10 +00:00
|
|
|
state->step = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool manchester_encoder_advance(
|
|
|
|
ManchesterEncoderState* state,
|
|
|
|
const bool curr_bit,
|
|
|
|
ManchesterEncoderResult* result) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(state);
|
|
|
|
furi_check(result);
|
|
|
|
|
2021-10-10 14:35:10 +00:00
|
|
|
bool advance = false;
|
|
|
|
switch(state->step) {
|
|
|
|
case 0:
|
|
|
|
state->prev_bit = curr_bit;
|
|
|
|
if(state->prev_bit) {
|
|
|
|
*result = ManchesterEncoderResultShortLow;
|
|
|
|
} else {
|
|
|
|
*result = ManchesterEncoderResultShortHigh;
|
|
|
|
}
|
|
|
|
state->step = 1;
|
|
|
|
advance = true;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
*result = (state->prev_bit << 1) + curr_bit;
|
|
|
|
if(curr_bit == state->prev_bit) {
|
|
|
|
state->step = 2;
|
|
|
|
} else {
|
|
|
|
state->prev_bit = curr_bit;
|
|
|
|
advance = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if(curr_bit) {
|
|
|
|
*result = ManchesterEncoderResultShortLow;
|
|
|
|
} else {
|
|
|
|
*result = ManchesterEncoderResultShortHigh;
|
|
|
|
}
|
|
|
|
state->prev_bit = curr_bit;
|
|
|
|
state->step = 1;
|
|
|
|
advance = true;
|
|
|
|
break;
|
|
|
|
default:
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_crash();
|
2021-10-10 14:35:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return advance;
|
|
|
|
}
|
|
|
|
|
|
|
|
ManchesterEncoderResult manchester_encoder_finish(ManchesterEncoderState* state) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(state);
|
|
|
|
|
2021-10-10 14:35:10 +00:00
|
|
|
state->step = 0;
|
2024-03-19 14:43:52 +00:00
|
|
|
|
2021-10-10 14:35:10 +00:00
|
|
|
return (state->prev_bit << 1) + state->prev_bit;
|
2022-05-06 13:37:10 +00:00
|
|
|
}
|