manchester-decoder.c 986 B

12345678910111213141516171819202122232425262728293031323334
  1. #include "manchester-decoder.h"
  2. #include <stdint.h>
  3. static const uint8_t transitions[] = {0b00000001, 0b10010001, 0b10011011, 0b11111011};
  4. static const ManchesterState manchester_reset_state = ManchesterStateMid1;
  5. bool manchester_advance(
  6. ManchesterState state,
  7. ManchesterEvent event,
  8. ManchesterState* next_state,
  9. bool* data) {
  10. bool result = false;
  11. ManchesterState new_state;
  12. if(event == ManchesterEventReset) {
  13. new_state = manchester_reset_state;
  14. } else {
  15. new_state = transitions[state] >> event & 0x3;
  16. if(new_state == state) {
  17. new_state = manchester_reset_state;
  18. } else {
  19. if(new_state == ManchesterStateMid0) {
  20. if (data) *data = false;
  21. result = true;
  22. } else if(new_state == ManchesterStateMid1) {
  23. if (data) *data = true;
  24. result = true;
  25. }
  26. }
  27. }
  28. *next_state = new_state;
  29. return result;
  30. }