lfrfid-app-scene-save-data.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "lfrfid-app-scene-save-data.h"
  2. static void print_buffer(const uint8_t* buffer) {
  3. for(uint8_t i = 0; i < LFRFID_KEY_SIZE; i++) {
  4. printf("%02X", buffer[i]);
  5. }
  6. }
  7. void LfRfidAppSceneSaveData::on_enter(LfRfidApp* app, bool need_restore) {
  8. auto byte_input = app->view_controller.get<ByteInputVM>();
  9. RfidKey& key = app->worker.key;
  10. printf("k: ");
  11. print_buffer(key.get_data());
  12. printf(" o: ");
  13. print_buffer(old_key_data);
  14. printf(" n: ");
  15. print_buffer(new_key_data);
  16. printf("\r\n");
  17. if(need_restore) printf("restored\r\n");
  18. if(need_restore) {
  19. key.set_data(old_key_data, key.get_type_data_count());
  20. } else {
  21. memcpy(old_key_data, key.get_data(), key.get_type_data_count());
  22. }
  23. memcpy(new_key_data, key.get_data(), key.get_type_data_count());
  24. byte_input->set_header_text("Enter the data in hex");
  25. byte_input->set_result_callback(
  26. save_callback, NULL, app, new_key_data, app->worker.key.get_type_data_count());
  27. app->view_controller.switch_to<ByteInputVM>();
  28. }
  29. bool LfRfidAppSceneSaveData::on_event(LfRfidApp* app, LfRfidApp::Event* event) {
  30. bool consumed = false;
  31. RfidKey& key = app->worker.key;
  32. if(event->type == LfRfidApp::EventType::Next) {
  33. key.set_data(new_key_data, key.get_type_data_count());
  34. app->scene_controller.switch_to_next_scene(LfRfidApp::SceneType::SaveName);
  35. }
  36. return consumed;
  37. }
  38. void LfRfidAppSceneSaveData::on_exit(LfRfidApp* app) {
  39. app->view_controller.get<ByteInputVM>()->clean();
  40. }
  41. void LfRfidAppSceneSaveData::save_callback(void* context) {
  42. LfRfidApp* app = static_cast<LfRfidApp*>(context);
  43. LfRfidApp::Event event;
  44. event.type = LfRfidApp::EventType::Next;
  45. app->view_controller.send_event(&event);
  46. }