pulse_decoder.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <stdlib.h>
  2. #include "pulse_decoder.h"
  3. #include <string.h>
  4. #include <furi/check.h>
  5. #define MAX_PROTOCOL 5
  6. struct PulseDecoder {
  7. PulseProtocol* protocols[MAX_PROTOCOL];
  8. };
  9. PulseDecoder* pulse_decoder_alloc() {
  10. PulseDecoder* decoder = malloc(sizeof(PulseDecoder));
  11. memset(decoder, 0, sizeof(PulseDecoder));
  12. return decoder;
  13. }
  14. void pulse_decoder_free(PulseDecoder* reader) {
  15. furi_assert(reader);
  16. free(reader);
  17. }
  18. void pulse_decoder_add_protocol(PulseDecoder* reader, PulseProtocol* protocol, int32_t index) {
  19. furi_check(index < MAX_PROTOCOL);
  20. furi_check(reader->protocols[index] == NULL);
  21. reader->protocols[index] = protocol;
  22. }
  23. void pulse_decoder_process_pulse(PulseDecoder* reader, bool polarity, uint32_t length) {
  24. furi_assert(reader);
  25. for(size_t index = 0; index < MAX_PROTOCOL; index++) {
  26. if(reader->protocols[index] != NULL) {
  27. pulse_protocol_process_pulse(reader->protocols[index], polarity, length);
  28. }
  29. }
  30. }
  31. int32_t pulse_decoder_get_decoded_index(PulseDecoder* reader) {
  32. furi_assert(reader);
  33. int32_t decoded = -1;
  34. for(size_t index = 0; index < MAX_PROTOCOL; index++) {
  35. if(reader->protocols[index] != NULL) {
  36. if(pulse_protocol_decoded(reader->protocols[index])) {
  37. decoded = index;
  38. break;
  39. }
  40. }
  41. }
  42. return decoded;
  43. }
  44. void pulse_decoder_reset(PulseDecoder* reader) {
  45. furi_assert(reader);
  46. for(size_t index = 0; index < MAX_PROTOCOL; index++) {
  47. if(reader->protocols[index] != NULL) {
  48. pulse_protocol_reset(reader->protocols[index]);
  49. }
  50. }
  51. }
  52. void pulse_decoder_get_data(PulseDecoder* reader, int32_t index, uint8_t* data, size_t length) {
  53. furi_assert(reader);
  54. furi_check(reader->protocols[index] != NULL);
  55. pulse_protocol_get_data(reader->protocols[index], data, length);
  56. }