pulse_glue.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "pulse_glue.h"
  2. struct PulseGlue {
  3. int32_t hi_period;
  4. int32_t low_period;
  5. int32_t next_hi_period;
  6. };
  7. PulseGlue* pulse_glue_alloc() {
  8. PulseGlue* pulse_glue = malloc(sizeof(PulseGlue));
  9. pulse_glue_reset(pulse_glue);
  10. return pulse_glue;
  11. }
  12. void pulse_glue_free(PulseGlue* pulse_glue) {
  13. free(pulse_glue);
  14. }
  15. void pulse_glue_reset(PulseGlue* pulse_glue) {
  16. pulse_glue->hi_period = 0;
  17. pulse_glue->low_period = 0;
  18. pulse_glue->next_hi_period = 0;
  19. }
  20. bool pulse_glue_push(PulseGlue* pulse_glue, bool polarity, uint32_t length) {
  21. bool pop_ready = false;
  22. if(polarity) {
  23. if(pulse_glue->low_period == 0) {
  24. // stage 1, accumulate hi period
  25. pulse_glue->hi_period += length;
  26. } else {
  27. // stage 3, accumulate next hi period and be ready for pulse_glue_pop
  28. pulse_glue->next_hi_period = length;
  29. // data is ready
  30. pop_ready = true;
  31. }
  32. } else {
  33. if(pulse_glue->hi_period != 0) {
  34. // stage 2, accumulate low period
  35. pulse_glue->low_period += length;
  36. }
  37. }
  38. return pop_ready;
  39. }
  40. void pulse_glue_pop(PulseGlue* pulse_glue, uint32_t* length, uint32_t* period) {
  41. *length = pulse_glue->hi_period + pulse_glue->low_period;
  42. *period = pulse_glue->hi_period;
  43. pulse_glue->hi_period = pulse_glue->next_hi_period;
  44. pulse_glue->low_period = 0;
  45. pulse_glue->next_hi_period = 0;
  46. }