xremote_ir_remote.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 = xremote_ir_signal_read(xremote_ir_remote_button_get_signal(button), ff, buf);
  53. if (can_read) {
  54. xremote_ir_remote_button_set_name(button, furi_string_get_cstr(buf));
  55. InfraredButtonArray_push_back(remote->buttons, button);
  56. } else {
  57. xremote_ir_remote_button_free(button);
  58. }
  59. }
  60. success = true;
  61. } while(false);
  62. furi_string_free(buf);
  63. flipper_format_free(ff);
  64. furi_record_close(RECORD_STORAGE);
  65. return success;
  66. }
  67. size_t xremote_ir_remote_get_button_count(InfraredRemote* remote) {
  68. return InfraredButtonArray_size(remote->buttons);
  69. }
  70. InfraredRemoteButton* xremote_ir_remote_get_button(InfraredRemote* remote, size_t index) {
  71. furi_assert(index < InfraredButtonArray_size(remote->buttons));
  72. return *InfraredButtonArray_get(remote->buttons, index);
  73. }