2022-05-06 16:48:39 +00:00
|
|
|
#include "../ibutton_i.h"
|
2022-05-27 11:19:21 +00:00
|
|
|
#include "m-string.h"
|
|
|
|
#include "toolbox/path.h"
|
2022-05-06 16:48:39 +00:00
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
iButtonSceneWriteStateDefault,
|
|
|
|
iButtonSceneWriteStateBlinkYellow,
|
|
|
|
} iButtonSceneWriteState;
|
|
|
|
|
|
|
|
static void ibutton_scene_write_callback(void* context, iButtonWorkerWriteResult result) {
|
|
|
|
iButton* ibutton = context;
|
|
|
|
view_dispatcher_send_custom_event(ibutton->view_dispatcher, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ibutton_scene_write_on_enter(void* context) {
|
|
|
|
iButton* ibutton = context;
|
|
|
|
iButtonKey* key = ibutton->key;
|
2022-09-29 17:56:04 +00:00
|
|
|
Widget* widget = ibutton->widget;
|
2022-05-06 16:48:39 +00:00
|
|
|
iButtonWorker* worker = ibutton->key_worker;
|
|
|
|
|
|
|
|
const uint8_t* key_data = ibutton_key_get_data_p(key);
|
2022-05-27 11:19:21 +00:00
|
|
|
|
|
|
|
string_t key_name;
|
|
|
|
string_init(key_name);
|
|
|
|
if(string_end_with_str_p(ibutton->file_path, IBUTTON_APP_EXTENSION)) {
|
|
|
|
path_extract_filename(ibutton->file_path, key_name, true);
|
|
|
|
}
|
2022-05-06 16:48:39 +00:00
|
|
|
|
|
|
|
// check that stored key has name
|
2022-05-27 11:19:21 +00:00
|
|
|
if(!string_empty_p(key_name)) {
|
2022-09-29 17:56:04 +00:00
|
|
|
ibutton_text_store_set(ibutton, "%s", string_get_cstr(key_name));
|
2022-05-06 16:48:39 +00:00
|
|
|
} else {
|
|
|
|
// if not, show key data
|
|
|
|
switch(ibutton_key_get_type(key)) {
|
|
|
|
case iButtonKeyDS1990:
|
|
|
|
ibutton_text_store_set(
|
|
|
|
ibutton,
|
2022-09-29 17:56:04 +00:00
|
|
|
"%02X %02X %02X %02X\n%02X %02X %02X %02X",
|
2022-05-06 16:48:39 +00:00
|
|
|
key_data[0],
|
|
|
|
key_data[1],
|
|
|
|
key_data[2],
|
|
|
|
key_data[3],
|
|
|
|
key_data[4],
|
|
|
|
key_data[5],
|
|
|
|
key_data[6],
|
|
|
|
key_data[7]);
|
|
|
|
break;
|
|
|
|
case iButtonKeyCyfral:
|
2022-09-29 17:56:04 +00:00
|
|
|
ibutton_text_store_set(ibutton, "%02X %02X", key_data[0], key_data[1]);
|
2022-05-06 16:48:39 +00:00
|
|
|
break;
|
|
|
|
case iButtonKeyMetakom:
|
|
|
|
ibutton_text_store_set(
|
2022-09-29 17:56:04 +00:00
|
|
|
ibutton, "%02X %02X %02X %02X", key_data[0], key_data[1], key_data[2], key_data[3]);
|
2022-05-06 16:48:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-29 17:56:04 +00:00
|
|
|
widget_add_string_multiline_element(
|
|
|
|
widget, 90, 10, AlignCenter, AlignTop, FontPrimary, "iButton\nwriting");
|
|
|
|
widget_add_icon_element(widget, 3, 10, &I_iButtonKey_49x44);
|
|
|
|
widget_add_text_box_element(
|
|
|
|
widget, 54, 39, 75, 22, AlignCenter, AlignCenter, ibutton->text_store, true);
|
2022-05-06 16:48:39 +00:00
|
|
|
|
2022-09-29 17:56:04 +00:00
|
|
|
view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewWidget);
|
2022-05-06 16:48:39 +00:00
|
|
|
|
|
|
|
ibutton_worker_write_set_callback(worker, ibutton_scene_write_callback, ibutton);
|
|
|
|
ibutton_worker_write_start(worker, key);
|
2022-05-27 11:19:21 +00:00
|
|
|
|
|
|
|
string_clear(key_name);
|
2022-06-13 01:08:28 +00:00
|
|
|
|
|
|
|
ibutton_notification_message(ibutton, iButtonNotificationMessageEmulateStart);
|
2022-05-06 16:48:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ibutton_scene_write_on_event(void* context, SceneManagerEvent event) {
|
|
|
|
iButton* ibutton = context;
|
|
|
|
SceneManager* scene_manager = ibutton->scene_manager;
|
|
|
|
bool consumed = false;
|
|
|
|
|
|
|
|
if(event.type == SceneManagerEventTypeCustom) {
|
|
|
|
consumed = true;
|
|
|
|
if((event.event == iButtonWorkerWriteOK) || (event.event == iButtonWorkerWriteSameKey)) {
|
|
|
|
scene_manager_next_scene(scene_manager, iButtonSceneWriteSuccess);
|
|
|
|
} else if(event.event == iButtonWorkerWriteNoDetect) {
|
2022-06-13 01:08:28 +00:00
|
|
|
ibutton_notification_message(ibutton, iButtonNotificationMessageEmulateBlink);
|
2022-05-06 16:48:39 +00:00
|
|
|
} else if(event.event == iButtonWorkerWriteCannotWrite) {
|
2022-06-13 01:08:28 +00:00
|
|
|
ibutton_notification_message(ibutton, iButtonNotificationMessageYellowBlink);
|
2022-05-06 16:48:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if(event.type == SceneManagerEventTypeTick) {
|
|
|
|
consumed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return consumed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ibutton_scene_write_on_exit(void* context) {
|
|
|
|
iButton* ibutton = context;
|
|
|
|
ibutton_worker_stop(ibutton->key_worker);
|
2022-09-29 17:56:04 +00:00
|
|
|
widget_reset(ibutton->widget);
|
2022-06-13 01:08:28 +00:00
|
|
|
|
|
|
|
ibutton_notification_message(ibutton, iButtonNotificationMessageBlinkStop);
|
2022-05-06 16:48:39 +00:00
|
|
|
}
|