lfrfid_app_scene_write.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "lfrfid_app_scene_write.h"
  2. static void lfrfid_write_callback(LFRFIDWorkerWriteResult result, void* ctx) {
  3. LfRfidApp* app = static_cast<LfRfidApp*>(ctx);
  4. LfRfidApp::Event event;
  5. switch(result) {
  6. case LFRFIDWorkerWriteOK:
  7. event.type = LfRfidApp::EventType::WriteEventOK;
  8. break;
  9. case LFRFIDWorkerWriteProtocolCannotBeWritten:
  10. event.type = LfRfidApp::EventType::WriteEventProtocolCannotBeWritten;
  11. break;
  12. case LFRFIDWorkerWriteFobCannotBeWritten:
  13. event.type = LfRfidApp::EventType::WriteEventFobCannotBeWritten;
  14. break;
  15. case LFRFIDWorkerWriteTooLongToWrite:
  16. event.type = LfRfidApp::EventType::WriteEventTooLongToWrite;
  17. break;
  18. }
  19. app->view_controller.send_event(&event);
  20. }
  21. void LfRfidAppSceneWrite::on_enter(LfRfidApp* app, bool /* need_restore */) {
  22. auto popup = app->view_controller.get<PopupVM>();
  23. popup->set_header("Writing", 89, 30, AlignCenter, AlignTop);
  24. if(string_size(app->file_name)) {
  25. popup->set_text(string_get_cstr(app->file_name), 89, 43, AlignCenter, AlignTop);
  26. } else {
  27. popup->set_text(
  28. protocol_dict_get_name(app->dict, app->protocol_id), 89, 43, AlignCenter, AlignTop);
  29. }
  30. popup->set_icon(0, 3, &I_RFIDDolphinSend_97x61);
  31. app->view_controller.switch_to<PopupVM>();
  32. lfrfid_worker_start_thread(app->lfworker);
  33. lfrfid_worker_write_start(
  34. app->lfworker, (LFRFIDProtocol)app->protocol_id, lfrfid_write_callback, app);
  35. notification_message(app->notification, &sequence_blink_start_magenta);
  36. }
  37. bool LfRfidAppSceneWrite::on_event(LfRfidApp* app, LfRfidApp::Event* event) {
  38. bool consumed = true;
  39. auto popup = app->view_controller.get<PopupVM>();
  40. switch(event->type) {
  41. case LfRfidApp::EventType::WriteEventOK:
  42. notification_message(app->notification, &sequence_success);
  43. app->scene_controller.switch_to_next_scene(LfRfidApp::SceneType::WriteSuccess);
  44. break;
  45. case LfRfidApp::EventType::WriteEventProtocolCannotBeWritten:
  46. popup->set_icon(72, 17, &I_DolphinCommon_56x48);
  47. popup->set_header("Error", 64, 3, AlignCenter, AlignTop);
  48. popup->set_text("This protocol\ncannot be written", 3, 17, AlignLeft, AlignTop);
  49. notification_message(app->notification, &sequence_blink_start_red);
  50. break;
  51. case LfRfidApp::EventType::WriteEventFobCannotBeWritten:
  52. case LfRfidApp::EventType::WriteEventTooLongToWrite:
  53. popup->set_icon(72, 17, &I_DolphinCommon_56x48);
  54. popup->set_header("Still trying to write...", 64, 3, AlignCenter, AlignTop);
  55. popup->set_text(
  56. "Make sure this\ncard is writable\nand not\nprotected.", 3, 17, AlignLeft, AlignTop);
  57. notification_message(app->notification, &sequence_blink_start_yellow);
  58. break;
  59. default:
  60. consumed = false;
  61. }
  62. return consumed;
  63. }
  64. void LfRfidAppSceneWrite::on_exit(LfRfidApp* app) {
  65. notification_message(app->notification, &sequence_blink_stop);
  66. app->view_controller.get<PopupVM>()->clean();
  67. lfrfid_worker_stop(app->lfworker);
  68. lfrfid_worker_stop_thread(app->lfworker);
  69. }