flipperzero-firmware/applications/infrared/infrared_remote.c
Georgii Surkov a8acfcabb4
[FL-2568] Infrared C port (#1326)
* Add skeleton for infrared C port, rename old app
* Add scene stubs
* Add more views
* Misc changes
* Add remote and signal class stubs
* Complete infrared signal class
* Add remote button class stub
* Check if button contains a signal during destruction
* Complete infrared signal class more
* Implement remote storing
* Implement remote loading
* Fix error handling
* Implement remote transmitting
* Rename scene
* Canonise event consumption
* Implement remote learning (stub)
* Implement learn success screen (stub)
* Implement AskBack scene
* Improve remote saving&loading
* Fix remote file name
* Add LearnDone scene
* Switch from Remote scene correctly
* Add SceneButtonSelect
* Remove unneeded assert
* Add new SceneManager method
* Use new SceneManager method in Infrared
* Implement renaming of buttons and remotes
* Implement deleting of buttons and remotes
* Add universal remotes list
* Add brute force code
* Brute force code improvements
* Partially implement Universal Remote GUI
* Fix wrong singnal handling
* Fully implement Universal Remote
* Use standard custom events everywhere
* Return infrared CLI
* Remove old Infrared app
* Change container name
* Fix scene order
* Put ButtonPanel into stack only when needed
* Show loading animation during slow operations
* Do not hardcode Loading widget coordinates
* Switch Loading widget orientation as needed
* Save Start scene state
* Save Remote scene state
* Save Edit scene state
* Save EditButtonSelect scene state
* Do not use scene state
* Use string_t instead of const char* for brevity
* Fix memory leak
* Fix saving raw remotes
* Add Infrared debug menu
* Add debug view
* Move Infrared monitor into Infrared application
* Remove old Infrared monitor app
* Use common signal received callback everywhere
2022-06-21 21:45:50 +09:00

177 lines
5.8 KiB
C

#include "infrared_remote.h"
#include <stdlib.h>
#include <m-string.h>
#include <m-array.h>
#include <toolbox/path.h>
#include <storage/storage.h>
#include <furi/common_defines.h>
#define TAG "InfraredRemote"
ARRAY_DEF(InfraredButtonArray, InfraredRemoteButton*, M_PTR_OPLIST);
struct InfraredRemote {
InfraredButtonArray_t buttons;
string_t name;
string_t path;
};
static void infrared_remote_clear_buttons(InfraredRemote* remote) {
InfraredButtonArray_it_t it;
for(InfraredButtonArray_it(it, remote->buttons); !InfraredButtonArray_end_p(it);
InfraredButtonArray_next(it)) {
infrared_remote_button_free(*InfraredButtonArray_cref(it));
}
InfraredButtonArray_reset(remote->buttons);
}
InfraredRemote* infrared_remote_alloc() {
InfraredRemote* remote = malloc(sizeof(InfraredRemote));
InfraredButtonArray_init(remote->buttons);
string_init(remote->name);
string_init(remote->path);
return remote;
}
void infrared_remote_free(InfraredRemote* remote) {
infrared_remote_clear_buttons(remote);
InfraredButtonArray_clear(remote->buttons);
string_clear(remote->path);
string_clear(remote->name);
free(remote);
}
void infrared_remote_reset(InfraredRemote* remote) {
infrared_remote_clear_buttons(remote);
string_reset(remote->name);
string_reset(remote->path);
}
void infrared_remote_set_name(InfraredRemote* remote, const char* name) {
string_set_str(remote->name, name);
}
const char* infrared_remote_get_name(InfraredRemote* remote) {
return string_get_cstr(remote->name);
}
void infrared_remote_set_path(InfraredRemote* remote, const char* path) {
string_set_str(remote->path, path);
}
const char* infrared_remote_get_path(InfraredRemote* remote) {
return string_get_cstr(remote->path);
}
size_t infrared_remote_get_button_count(InfraredRemote* remote) {
return InfraredButtonArray_size(remote->buttons);
}
InfraredRemoteButton* infrared_remote_get_button(InfraredRemote* remote, size_t index) {
furi_assert(index < InfraredButtonArray_size(remote->buttons));
return *InfraredButtonArray_get(remote->buttons, index);
}
bool infrared_remote_add_button(InfraredRemote* remote, const char* name, InfraredSignal* signal) {
InfraredRemoteButton* button = infrared_remote_button_alloc();
infrared_remote_button_set_name(button, name);
infrared_remote_button_set_signal(button, signal);
InfraredButtonArray_push_back(remote->buttons, button);
return infrared_remote_store(remote);
}
bool infrared_remote_rename_button(InfraredRemote* remote, const char* new_name, size_t index) {
furi_assert(index < InfraredButtonArray_size(remote->buttons));
InfraredRemoteButton* button = *InfraredButtonArray_get(remote->buttons, index);
infrared_remote_button_set_name(button, new_name);
return infrared_remote_store(remote);
}
bool infrared_remote_delete_button(InfraredRemote* remote, size_t index) {
furi_assert(index < InfraredButtonArray_size(remote->buttons));
InfraredRemoteButton* button;
InfraredButtonArray_pop_at(&button, remote->buttons, index);
infrared_remote_button_free(button);
return infrared_remote_store(remote);
}
bool infrared_remote_store(InfraredRemote* remote) {
Storage* storage = furi_record_open("storage");
FlipperFormat* ff = flipper_format_file_alloc(storage);
const char* path = string_get_cstr(remote->path);
FURI_LOG_I(TAG, "store file: \'%s\'", path);
bool success = flipper_format_file_open_always(ff, path) &&
flipper_format_write_header_cstr(ff, "IR signals file", 1);
if(success) {
InfraredButtonArray_it_t it;
for(InfraredButtonArray_it(it, remote->buttons); !InfraredButtonArray_end_p(it);
InfraredButtonArray_next(it)) {
InfraredRemoteButton* button = *InfraredButtonArray_cref(it);
success = infrared_signal_save(
infrared_remote_button_get_signal(button),
ff,
infrared_remote_button_get_name(button));
if(!success) {
break;
}
}
}
flipper_format_free(ff);
furi_record_close("storage");
return success;
}
bool infrared_remote_load(InfraredRemote* remote, string_t path) {
Storage* storage = furi_record_open("storage");
FlipperFormat* ff = flipper_format_file_alloc(storage);
string_t buf;
string_init(buf);
FURI_LOG_I(TAG, "load file: \'%s\'", string_get_cstr(path));
bool success = flipper_format_file_open_existing(ff, string_get_cstr(path));
if(success) {
uint32_t version;
success = flipper_format_read_header(ff, buf, &version) &&
!string_cmp_str(buf, "IR signals file") && (version == 1);
}
if(success) {
path_extract_filename(path, buf, true);
infrared_remote_clear_buttons(remote);
infrared_remote_set_name(remote, string_get_cstr(buf));
infrared_remote_set_path(remote, string_get_cstr(path));
for(bool can_read = true; can_read;) {
InfraredRemoteButton* button = infrared_remote_button_alloc();
can_read = infrared_signal_read(infrared_remote_button_get_signal(button), ff, buf);
if(can_read) {
infrared_remote_button_set_name(button, string_get_cstr(buf));
InfraredButtonArray_push_back(remote->buttons, button);
} else {
infrared_remote_button_free(button);
}
}
}
string_clear(buf);
flipper_format_free(ff);
furi_record_close("storage");
return success;
}
bool infrared_remote_remove(InfraredRemote* remote) {
Storage* storage = furi_record_open("storage");
FS_Error status = storage_common_remove(storage, string_get_cstr(remote->path));
infrared_remote_reset(remote);
furi_record_close("storage");
return (status == FSE_OK || status == FSE_NOT_EXIST);
}