irda_monitor.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <stdio.h>
  2. #include <furi.h>
  3. #include <api-hal-irda.h>
  4. #include <api-hal.h>
  5. #define IRDA_TIMINGS_SIZE 2000
  6. typedef struct {
  7. uint32_t timing_cnt;
  8. struct {
  9. uint8_t level;
  10. uint32_t duration;
  11. } timing[IRDA_TIMINGS_SIZE];
  12. } IrdaDelaysArray;
  13. static void irda_rx_callback(void* ctx, bool level, uint32_t duration) {
  14. IrdaDelaysArray* delays = ctx;
  15. if(delays->timing_cnt < IRDA_TIMINGS_SIZE) {
  16. if(delays->timing_cnt > 1)
  17. furi_check(level != delays->timing[delays->timing_cnt - 1].level);
  18. delays->timing[delays->timing_cnt].level = level;
  19. delays->timing[delays->timing_cnt].duration = duration;
  20. delays->timing_cnt++; // Read-Modify-Write in ISR only: no need to add synchronization
  21. }
  22. }
  23. int32_t irda_monitor_app(void* p) {
  24. (void)p;
  25. static uint32_t counter = 0;
  26. IrdaDelaysArray* delays = furi_alloc(sizeof(IrdaDelaysArray));
  27. api_hal_irda_rx_irq_init();
  28. api_hal_irda_rx_irq_set_callback(irda_rx_callback, delays);
  29. while(1) {
  30. delay(20);
  31. if(counter != delays->timing_cnt) {
  32. api_hal_light_set(LightRed, 0x00);
  33. api_hal_light_set(LightGreen, 0x00);
  34. api_hal_light_set(LightBlue, 0xFF);
  35. delay(20);
  36. api_hal_light_set(LightRed, 0x00);
  37. api_hal_light_set(LightGreen, 0x00);
  38. api_hal_light_set(LightBlue, 0x00);
  39. counter = delays->timing_cnt;
  40. }
  41. if(delays->timing_cnt >= IRDA_TIMINGS_SIZE) {
  42. api_hal_irda_rx_irq_deinit();
  43. printf("== IRDA MONITOR FOUND (%d) records) ==\r\n", IRDA_TIMINGS_SIZE);
  44. printf("{");
  45. for(int i = 0; i < IRDA_TIMINGS_SIZE; ++i) {
  46. printf(
  47. "%s%lu, ",
  48. (delays->timing[i].duration > 15000) ? "\r\n" : "",
  49. delays->timing[i].duration);
  50. }
  51. printf("\r\n};\r\n");
  52. break;
  53. }
  54. }
  55. free(delays);
  56. return 0;
  57. }