action_ir.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Methods for IR transmission
  2. #include "quac.h"
  3. #include "action_i.h"
  4. #include "action_ir_utils.h"
  5. void action_ir_tx(void* context, const FuriString* action_path, FuriString* error) {
  6. UNUSED(error);
  7. App* app = context;
  8. const char* file_name = furi_string_get_cstr(action_path);
  9. InfraredSignal* signal = infrared_utils_signal_alloc();
  10. FlipperFormat* fff_data_file = flipper_format_file_alloc(app->storage);
  11. FuriString* temp_str;
  12. temp_str = furi_string_alloc();
  13. // uint32_t temp_data32;
  14. // https://developer.flipper.net/flipperzero/doxygen/infrared_file_format.html
  15. // TODO: Right now we only read the first signal found in the file. Add support
  16. // for reading any signal by 'name'?
  17. do {
  18. if(!flipper_format_file_open_existing(fff_data_file, file_name)) {
  19. ACTION_SET_ERROR("IR: Error opening %s", file_name);
  20. break;
  21. }
  22. uint32_t index = 0;
  23. if(!infrared_utils_read_signal_at_index(fff_data_file, index, signal, temp_str)) {
  24. ACTION_SET_ERROR("IR: Failed to read from file");
  25. break;
  26. }
  27. if(app->settings.ir_use_ext_module) action_ir_power_otg(true);
  28. if(signal->is_raw) {
  29. // raw
  30. FURI_LOG_I(
  31. TAG,
  32. "IR: Sending (%s) type=raw => %d timings, %lu Hz, %f",
  33. file_name,
  34. signal->payload.raw.timings_size,
  35. signal->payload.raw.frequency,
  36. (double)signal->payload.raw.duty_cycle);
  37. infrared_send_raw_ext(
  38. signal->payload.raw.timings,
  39. signal->payload.raw.timings_size,
  40. true,
  41. signal->payload.raw.frequency,
  42. signal->payload.raw.duty_cycle);
  43. FURI_LOG_I(TAG, "IR: Send complete");
  44. } else {
  45. //parsed
  46. FURI_LOG_I(
  47. TAG,
  48. "IR: Sending (%s) type=parsed => %s %lu %lu",
  49. file_name,
  50. infrared_get_protocol_name(signal->payload.message.protocol),
  51. signal->payload.message.address,
  52. signal->payload.message.command);
  53. infrared_send(&signal->payload.message, 1);
  54. FURI_LOG_I(TAG, "IR: Send complete");
  55. }
  56. if(app->settings.ir_use_ext_module) action_ir_power_otg(false);
  57. } while(false);
  58. furi_string_free(temp_str);
  59. flipper_format_free(fff_data_file);
  60. infrared_utils_signal_free(signal);
  61. }
  62. void action_ir_power_otg(bool enable) {
  63. FuriHalInfraredTxPin tx_pin_detected = furi_hal_infrared_detect_tx_output();
  64. furi_hal_infrared_set_tx_output(tx_pin_detected);
  65. if(tx_pin_detected == FuriHalInfraredTxPinInternal) return;
  66. if(enable)
  67. furi_hal_power_enable_otg();
  68. else
  69. furi_hal_power_disable_otg();
  70. }