pulse_protocol.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "pulse_protocol.h"
  2. #include <stdlib.h>
  3. #include <string.h>
  4. struct PulseProtocol {
  5. void* context;
  6. PulseProtocolPulseCallback pulse_cb;
  7. PulseProtocolResetCallback reset_cb;
  8. PulseProtocolGetDataCallback get_data_cb;
  9. PulseProtocolDecodedCallback decoded_cb;
  10. };
  11. PulseProtocol* pulse_protocol_alloc() {
  12. PulseProtocol* protocol = malloc(sizeof(PulseProtocol));
  13. memset(protocol, 0, sizeof(PulseProtocol));
  14. return protocol;
  15. }
  16. void pulse_protocol_set_context(PulseProtocol* protocol, void* context) {
  17. protocol->context = context;
  18. }
  19. void pulse_protocol_set_pulse_cb(PulseProtocol* protocol, PulseProtocolPulseCallback callback) {
  20. protocol->pulse_cb = callback;
  21. }
  22. void pulse_protocol_set_reset_cb(PulseProtocol* protocol, PulseProtocolResetCallback callback) {
  23. protocol->reset_cb = callback;
  24. }
  25. void pulse_protocol_set_get_data_cb(PulseProtocol* protocol, PulseProtocolGetDataCallback callback) {
  26. protocol->get_data_cb = callback;
  27. }
  28. void pulse_protocol_set_decoded_cb(PulseProtocol* protocol, PulseProtocolDecodedCallback callback) {
  29. protocol->decoded_cb = callback;
  30. }
  31. void pulse_protocol_free(PulseProtocol* protocol) {
  32. free(protocol);
  33. }
  34. void pulse_protocol_process_pulse(PulseProtocol* protocol, bool polarity, uint32_t length) {
  35. if(protocol->pulse_cb != NULL) {
  36. protocol->pulse_cb(protocol->context, polarity, length);
  37. }
  38. }
  39. void pulse_protocol_reset(PulseProtocol* protocol) {
  40. if(protocol->reset_cb != NULL) {
  41. protocol->reset_cb(protocol->context);
  42. }
  43. }
  44. bool pulse_protocol_decoded(PulseProtocol* protocol) {
  45. bool result = false;
  46. if(protocol->decoded_cb != NULL) {
  47. result = protocol->decoded_cb(protocol->context);
  48. }
  49. return result;
  50. }
  51. void pulse_protocol_get_data(PulseProtocol* protocol, uint8_t* data, size_t length) {
  52. if(protocol->get_data_cb != NULL) {
  53. protocol->get_data_cb(protocol->context, data, length);
  54. }
  55. }