infrared_encoder_rc6.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <core/memmgr.h>
  2. #include "infrared.h"
  3. #include "common/infrared_common_i.h"
  4. #include "infrared_protocol_defs_i.h"
  5. #include <stdint.h>
  6. #include "../infrared_i.h"
  7. typedef struct InfraredEncoderRC6 {
  8. InfraredCommonEncoder* common_encoder;
  9. bool toggle_bit;
  10. } InfraredEncoderRC6;
  11. void infrared_encoder_rc6_reset(void* encoder_ptr, const InfraredMessage* message) {
  12. furi_assert(encoder_ptr);
  13. InfraredEncoderRC6* encoder = encoder_ptr;
  14. InfraredCommonEncoder* common_encoder = encoder->common_encoder;
  15. infrared_common_encoder_reset(common_encoder);
  16. uint32_t* data = (void*)common_encoder->data;
  17. *data |= 0x01; // start bit
  18. (void)*data; // 3 bits for mode == 0
  19. *data |= encoder->toggle_bit ? 0x10 : 0;
  20. *data |= reverse(message->address) << 5;
  21. *data |= reverse(message->command) << 13;
  22. common_encoder->bits_to_encode = common_encoder->protocol->databit_len[0];
  23. encoder->toggle_bit ^= 1;
  24. }
  25. InfraredStatus infrared_encoder_rc6_encode(void* encoder_ptr, uint32_t* duration, bool* level) {
  26. InfraredEncoderRC6* encoder = encoder_ptr;
  27. return infrared_common_encode(encoder->common_encoder, duration, level);
  28. }
  29. void* infrared_encoder_rc6_alloc(void) {
  30. InfraredEncoderRC6* encoder = malloc(sizeof(InfraredEncoderRC6));
  31. encoder->common_encoder = infrared_common_encoder_alloc(&protocol_rc6);
  32. encoder->toggle_bit = false;
  33. return encoder;
  34. }
  35. void infrared_encoder_rc6_free(void* encoder_ptr) {
  36. furi_assert(encoder_ptr);
  37. InfraredEncoderRC6* encoder = encoder_ptr;
  38. free(encoder->common_encoder);
  39. free(encoder);
  40. }
  41. InfraredStatus infrared_encoder_rc6_encode_manchester(
  42. InfraredCommonEncoder* common_encoder,
  43. uint32_t* duration,
  44. bool* polarity) {
  45. InfraredStatus status = InfraredStatusError;
  46. bool toggle_bit = (common_encoder->bits_encoded == 4);
  47. status = infrared_common_encode_manchester(common_encoder, duration, polarity);
  48. if(toggle_bit) *duration *= 2;
  49. return status;
  50. }