ibutton_scene_emulate.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "../ibutton_i.h"
  2. #include <core/log.h>
  3. #include <toolbox/path.h>
  4. #define EMULATE_TIMEOUT_TICKS 10
  5. static void ibutton_scene_emulate_callback(void* context, bool emulated) {
  6. iButton* ibutton = context;
  7. if(emulated) {
  8. view_dispatcher_send_custom_event(
  9. ibutton->view_dispatcher, iButtonCustomEventWorkerEmulated);
  10. }
  11. }
  12. void ibutton_scene_emulate_on_enter(void* context) {
  13. iButton* ibutton = context;
  14. iButtonKey* key = ibutton->key;
  15. Widget* widget = ibutton->widget;
  16. FuriString* tmp = furi_string_alloc();
  17. widget_add_icon_element(widget, 3, 10, &I_iButtonKey_49x44);
  18. furi_string_printf(
  19. tmp,
  20. "%s\n[%s]",
  21. furi_string_empty(ibutton->file_path) ? "Unsaved Key" : ibutton->key_name,
  22. ibutton_protocols_get_name(ibutton->protocols, ibutton_key_get_protocol_id(key)));
  23. widget_add_text_box_element(
  24. widget, 52, 38, 75, 26, AlignCenter, AlignCenter, furi_string_get_cstr(tmp), true);
  25. widget_add_string_multiline_element(
  26. widget, 88, 10, AlignCenter, AlignTop, FontPrimary, "iButton\nemulating");
  27. ibutton_worker_emulate_set_callback(ibutton->worker, ibutton_scene_emulate_callback, ibutton);
  28. ibutton_worker_emulate_start(ibutton->worker, key);
  29. ibutton_notification_message(ibutton, iButtonNotificationMessageEmulateStart);
  30. view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewWidget);
  31. furi_string_free(tmp);
  32. }
  33. bool ibutton_scene_emulate_on_event(void* context, SceneManagerEvent event) {
  34. iButton* ibutton = context;
  35. bool consumed = false;
  36. if(event.type == SceneManagerEventTypeTick) {
  37. uint32_t cnt = scene_manager_get_scene_state(ibutton->scene_manager, iButtonSceneEmulate);
  38. if(cnt > 0) {
  39. if(--cnt == 0) {
  40. ibutton_notification_message(ibutton, iButtonNotificationMessageEmulateBlink);
  41. }
  42. scene_manager_set_scene_state(ibutton->scene_manager, iButtonSceneEmulate, cnt);
  43. }
  44. consumed = true;
  45. } else if(event.type == SceneManagerEventTypeCustom) {
  46. consumed = true;
  47. if(event.event == iButtonCustomEventWorkerEmulated) {
  48. if(scene_manager_get_scene_state(ibutton->scene_manager, iButtonSceneEmulate) == 0) {
  49. ibutton_notification_message(ibutton, iButtonNotificationMessageYellowBlink);
  50. }
  51. scene_manager_set_scene_state(
  52. ibutton->scene_manager, iButtonSceneEmulate, EMULATE_TIMEOUT_TICKS);
  53. }
  54. }
  55. return consumed;
  56. }
  57. void ibutton_scene_emulate_on_exit(void* context) {
  58. iButton* ibutton = context;
  59. ibutton_worker_stop(ibutton->worker);
  60. widget_reset(ibutton->widget);
  61. ibutton_notification_message(ibutton, iButtonNotificationMessageBlinkStop);
  62. }