xremote_ir_remote.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "xremote_ir_remote.h"
  2. #define TAG "XremoteInfraredRemote"
  3. ARRAY_DEF(InfraredButtonArray, InfraredRemoteButton*, M_PTR_OPLIST);
  4. struct InfraredRemote {
  5. InfraredButtonArray_t buttons;
  6. FuriString* name;
  7. FuriString* path;
  8. };
  9. InfraredRemote* xremote_ir_remote_alloc() {
  10. InfraredRemote* remote = malloc(sizeof(InfraredRemote));
  11. InfraredButtonArray_init(remote->buttons);
  12. remote->name = furi_string_alloc();
  13. remote->path = furi_string_alloc();
  14. return remote;
  15. }
  16. const char* xremote_ir_remote_get_name(InfraredRemote* remote) {
  17. return furi_string_get_cstr(remote->name);
  18. }
  19. static void xremote_ir_remote_clear_buttons(InfraredRemote* remote) {
  20. InfraredButtonArray_it_t it;
  21. for(InfraredButtonArray_it(it, remote->buttons); !InfraredButtonArray_end_p(it);
  22. InfraredButtonArray_next(it)) {
  23. xremote_ir_remote_button_free(*InfraredButtonArray_cref(it));
  24. }
  25. InfraredButtonArray_reset(remote->buttons);
  26. }
  27. void xremote_ir_remote_free(InfraredRemote* remote) {
  28. furi_string_free(remote->path);
  29. furi_string_free(remote->name);
  30. free(remote);
  31. }
  32. InfraredRemoteButton* xremote_ir_get_button(InfraredRemote* remote, size_t index) {
  33. furi_assert(index < InfraredButtonArray_size(remote->buttons));
  34. return *InfraredButtonArray_get(remote->buttons, index);
  35. }
  36. bool xremote_ir_remote_load(InfraredRemote* remote, FuriString* path) {
  37. Storage* storage = furi_record_open(RECORD_STORAGE);
  38. FlipperFormat* ff = flipper_format_buffered_file_alloc(storage);
  39. FuriString* buf;
  40. buf = furi_string_alloc();
  41. FURI_LOG_I(TAG, "loading IR Remote: \'%s\'", furi_string_get_cstr(path));
  42. bool success = false;
  43. do {
  44. if(!flipper_format_buffered_file_open_existing(ff, furi_string_get_cstr(path))) break;
  45. uint32_t version;
  46. if(!flipper_format_read_header(ff, buf, &version)) break;
  47. if(!furi_string_equal(buf, "IR signals file") || (version != 1)) break;
  48. path_extract_filename(path, buf, true);
  49. xremote_ir_remote_clear_buttons(remote);
  50. for(bool can_read = true; can_read;) {
  51. InfraredRemoteButton* button = xremote_ir_remote_button_alloc();
  52. can_read =
  53. xremote_ir_signal_read(xremote_ir_remote_button_get_signal(button), ff, buf);
  54. if(can_read) {
  55. xremote_ir_remote_button_set_name(button, furi_string_get_cstr(buf));
  56. InfraredButtonArray_push_back(remote->buttons, button);
  57. } else {
  58. xremote_ir_remote_button_free(button);
  59. }
  60. }
  61. success = true;
  62. } while(false);
  63. furi_string_free(buf);
  64. flipper_format_free(ff);
  65. furi_record_close(RECORD_STORAGE);
  66. return success;
  67. }
  68. size_t xremote_ir_remote_get_button_count(InfraredRemote* remote) {
  69. return InfraredButtonArray_size(remote->buttons);
  70. }
  71. InfraredRemoteButton* xremote_ir_remote_get_button(InfraredRemote* remote, size_t index) {
  72. furi_assert(index < InfraredButtonArray_size(remote->buttons));
  73. return *InfraredButtonArray_get(remote->buttons, index);
  74. }