Skip to content

Commit

Permalink
manchester decoder sketch
Browse files Browse the repository at this point in the history
  • Loading branch information
merlokk committed May 28, 2024
1 parent 77590e7 commit 892edaa
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 23 deletions.
74 changes: 60 additions & 14 deletions firmware/application/src/rfid/reader/lf/utils/manchester_decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,79 @@
#include <stdint.h>

static const uint8_t transitions[] = {0b00000001, 0b10010001, 0b10011011, 0b11111011};
static const ManchesterState manchester_reset_state = ManchesterStateMid1;
static const ManchesterState manchester_reset_state = ManchesterResetState;

bool manchester_advance(
ManchesterState state,
ManchesterEvent event,
ManchesterState* next_state,
bool* data) {
BitArray* data) {
bool result = false;
ManchesterState new_state;
bitarray_clear(data);

if(event == ManchesterEventReset) {
new_state = manchester_reset_state;
} else {
new_state = transitions[state] >> event & 0x3;
if(new_state == state) {
new_state = manchester_reset_state;
} else {
if(new_state == ManchesterStateMid0) {
if(data) *data = false;
result = true;
} else if(new_state == ManchesterStateMid1) {
if(data) *data = true;
result = true;
}
}



}

*next_state = new_state;
return result;
}

uint8_t mcst(RAWBUF_TYPE_S *Pdata) {
uint8_t sync = 1; //After the current interval process is processed, is it on the judgment line
uint8_t cardindex = 0; //Record change number
for (int i = Pdata->startbit; i < RAW_BUF_SIZE * 8; i++) {
uint8_t thisbit = readbit(Pdata->rawa, Pdata->rawb, i);
switch (sync) {
case 1: //Synchronous state
switch (thisbit) {
case 0: //TheSynchronousState1T,Add1Digit0,StillSynchronize
writebit(Pdata->hexbuf, Pdata->hexbuf, cardindex, 0);
cardindex++;
break;
case 1: // Synchronous status 1.5T, add 1 digit 1, switch to non -synchronized state
writebit(Pdata->hexbuf, Pdata->hexbuf, cardindex, 1);
cardindex++;
sync = 0;
break;
case 2: //Synchronous2T,Add2Digits10,StillSynchronize
writebit(Pdata->hexbuf, Pdata->hexbuf, cardindex, 1);
cardindex++;
writebit(Pdata->hexbuf, Pdata->hexbuf, cardindex, 0);
cardindex++;
break;
default:
return 0;
}
break;
case 0: //Non -synchronous state
switch (thisbit) {
case 0: //1TInNonSynchronousState,Add1Digit1,StillNonSynchronous
writebit(Pdata->hexbuf, Pdata->hexbuf, cardindex, 1);
cardindex++;
break;
case 1: // In non -synchronous status 1.5T, add 2 digits 10, switch to the synchronous state
writebit(Pdata->hexbuf, Pdata->hexbuf, cardindex, 1);
cardindex++;
writebit(Pdata->hexbuf, Pdata->hexbuf, cardindex, 0);
cardindex++;
sync = 1;
break;
case 2: //The2TOfTheNonSynchronousState,ItIsImpossibleToOccur,ReportAnError
return 0;
default:
return 0;
}
break;
}
if (cardindex >= CARD_BUF_SIZE * 8)
break;
}
return 1;
}

23 changes: 14 additions & 9 deletions firmware/application/src/rfid/reader/lf/utils/manchester_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,31 @@
extern "C" {
#endif

#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "bitarray.h"

typedef enum {
ManchesterEventShortLow = 0,
ManchesterEventShortHigh = 2,
ManchesterEventLongLow = 4,
ManchesterEventLongHigh = 6,
ManchesterEvent1T = 0,
ManchesterEvent15T = 2,
ManchesterEvent2T = 4,
ManchesterEventReset = 8
} ManchesterEvent;

typedef enum {
ManchesterStateStart1 = 0,
ManchesterStateMid1 = 1,
ManchesterStateMid0 = 2,
ManchesterStateStart0 = 3
ManchesterStateSync = 0,
ManchesterStateNoSync = 1,
ManchesterResetState = 2,
} ManchesterState;

ManchesterEvent manchester_length_decode(int interval_length, int _1t_length, int _deviation);

bool manchester_advance(
ManchesterState state,
ManchesterEvent event,
ManchesterState* next_state,
bool* data);
BitArray* data);

#ifdef __cplusplus
}
Expand Down

0 comments on commit 892edaa

Please sign in to comment.